| 1 | package OS::CheckUpdates::AUR; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.16; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 1 | ▶ | use if $ENV{CHECKUPDATES_DEBUG}, 'Smart::Comments'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use WWW::AUR::URI qw(rpc_uri); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use WWW::AUR::UserAgent; | ||
| 11 | use IO::Pipe; | ||
| 12 | use JSON; | ||
| 13 | |||
| 14 | =head1 NAME | ||
| 15 | |||
| 16 | OS::CheckUpdates::AUR - checkupdates for aur | ||
| 17 | |||
| 18 | =head1 VERSION | ||
| 19 | |||
| 20 | Version 0.04 | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | our $VERSION = '0.04'; | ||
| 25 | |||
| 26 | |||
| 27 | =head1 SYNOPSIS | ||
| 28 | |||
| 29 | checkupdates for aur | ||
| 30 | |||
| 31 | Example of code: | ||
| 32 | |||
| 33 | use OS::CheckUpdates::AUR; | ||
| 34 | |||
| 35 | my $foo = OS::CheckUpdates::AUR->new(); | ||
| 36 | |||
| 37 | # Print available updates: | ||
| 38 | |||
| 39 | $foo->print(); | ||
| 40 | # or | ||
| 41 | printf("%s %s -> %s\n", @{$_}[0..2]) foreach (@{$foo->get()}); | ||
| 42 | |||
| 43 | =head1 SUBROUTINES/METHODS | ||
| 44 | |||
| 45 | =head2 new() | ||
| 46 | |||
| 47 | New... | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | sub new { | ||
| 52 | ### OS-CheckUpdates-AUR created here | ||
| 53 | return bless({}, shift); | ||
| 54 | } | ||
| 55 | |||
| 56 | =head2 get() | ||
| 57 | |||
| 58 | Get array with checkupdates: [name, local_ver, aur_ver] | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | sub get { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | ### get() run refresh() if updates db is not created | ||
| 66 | |||
| 67 | exists $self->{'updates'} | ||
| 68 | or $self->refresh(); | ||
| 69 | |||
| 70 | ### get() return updates | ||
| 71 | |||
| 72 | return $self->{'updates'}; | ||
| 73 | } | ||
| 74 | |||
| 75 | =head2 print | ||
| 76 | |||
| 77 | Print checkupdates into stdout in chekupdates format. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub print { | ||
| 82 | my $self = shift; | ||
| 83 | |||
| 84 | printf("%s %s -> %s\n", @{$_}[0..2]) foreach (@{$self->get()}); | ||
| 85 | |||
| 86 | return 1; | ||
| 87 | } | ||
| 88 | |||
| 89 | =head2 refresh() | ||
| 90 | |||
| 91 | Create/retrive/parse/refresh data about packages. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | sub refresh { | ||
| 96 | my $self = shift; | ||
| 97 | my $local; | ||
| 98 | |||
| 99 | $self->{'updates'} = []; | ||
| 100 | |||
| 101 | ### refresh() reading 'pacman -Qm' output | ||
| 102 | |||
| 103 | my $pipe = IO::Pipe->new(); | ||
| 104 | $pipe->reader(qw[pacman -Qm]); | ||
| 105 | |||
| 106 | while(<$pipe>) { | ||
| 107 | my ($name, $version) = split(" "); | ||
| 108 | $local->{$name} = $version; | ||
| 109 | }; | ||
| 110 | |||
| 111 | if ($#{[keys %$local]} < 0) { | ||
| 112 | ### found 0 packages, nothing to do... | ||
| 113 | return $self; | ||
| 114 | } | ||
| 115 | |||
| 116 | ### refresh() getting multiinfo() | ||
| 117 | |||
| 118 | my @multiinfo_results = @{$self->multiinfo(sort keys %$local)->{'results'}}; | ||
| 119 | |||
| 120 | ### refresh() comparing versions | ||
| 121 | |||
| 122 | my %seen; | ||
| 123 | foreach (@multiinfo_results) { | ||
| 124 | my $name = $_->{'Name'}; | ||
| 125 | my $vloc = $local->{$name} or next; | ||
| 126 | my $vaur = $_->{'Version'}; | ||
| 127 | |||
| 128 | !$seen{$name}++ | ||
| 129 | and ($vaur ne $vloc) | ||
| 130 | and ($self->vercmp($vloc, $vaur) eq "-1") | ||
| 131 | and push @{$self->{'updates'}}, [$name, $vloc, $vaur]; | ||
| 132 | } | ||
| 133 | |||
| 134 | ### Locally installed: $#{[keys %$local]} + 1 | ||
| 135 | ### Found on AUR: $#multiinfo_results + 1 | ||
| 136 | ### Updates: $#{$self->{'updates'}} + 1 | ||
| 137 | |||
| 138 | return $self | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | =head2 vercmp($$) | ||
| 143 | |||
| 144 | Compare two versions in pacman way. Frontend for vercmp command. | ||
| 145 | |||
| 146 | =cut | ||
| 147 | |||
| 148 | sub vercmp { | ||
| 149 | my ($self, $a, $b) = @_; | ||
| 150 | |||
| 151 | if (defined $a and defined $b) { | ||
| 152 | my $pipe = IO::Pipe->new(); | ||
| 153 | $pipe->reader('vercmp', $a, $b); | ||
| 154 | |||
| 155 | while(<$pipe>) { | ||
| 156 | chomp; | ||
| 157 | |||
| 158 | /^(-1|0|1)$/ | ||
| 159 | and return scalar $_ | ||
| 160 | or last; | ||
| 161 | }; | ||
| 162 | |||
| 163 | $!=1; die(__PACKAGE__ . '->varcmp(): command not generated proper output'); | ||
| 164 | }; | ||
| 165 | |||
| 166 | $!=1; die(__PACKAGE__ . '->varcmp(): one or more versions are empty'); | ||
| 167 | } | ||
| 168 | |||
| 169 | =head2 multiinfo(@) | ||
| 170 | |||
| 171 | Fast method to get info about multiple packages. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | sub multiinfo { | ||
| 176 | my $self = shift; | ||
| 177 | my $lwp = WWW::AUR::UserAgent->new( | ||
| 178 | 'timeout' => 10, | ||
| 179 | 'agent' => sprintf( | ||
| 180 | 'WWW::AUR/v%s (OS::CheckUpdates::AUR/v%s)', | ||
| 181 | $WWW::AUR::VERSION, | ||
| 182 | $VERSION, | ||
| 183 | ), | ||
| 184 | 'protocols_allowed' => ['https'], | ||
| 185 | ); | ||
| 186 | |||
| 187 | my $response = $lwp->get(rpc_uri('multiinfo', @_)); | ||
| 188 | |||
| 189 | $response->is_success | ||
| 190 | and return decode_json($response->decoded_content); | ||
| 191 | |||
| 192 | ### LWP decoded: $response->decoded_content | ||
| 193 | |||
| 194 | $!=1; die(__PACKAGE__ . '->multiinfo(): LWP status error: ' . $response->status_line) | ||
| 195 | } | ||
| 196 | |||
| 197 | =head1 AUTHOR | ||
| 198 | |||
| 199 | 3ED, C<< <krzysztof1987 at gmail.com> >> | ||
| 200 | |||
| 201 | =head1 BUGS | ||
| 202 | |||
| 203 | Please report any bugs or feature requests to C<bug-checkupdates-aur at rt.cpan.org>, or through | ||
| 204 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=OS-CheckUpdates-AUR>. I will be notified, and then you'll | ||
| 205 | automatically be notified of progress on your bug as I make changes. | ||
| 206 | |||
| 207 | |||
| 208 | |||
| 209 | |||
| 210 | =head1 SUPPORT | ||
| 211 | |||
| 212 | You can find documentation for this module with the perldoc command. | ||
| 213 | |||
| 214 | perldoc OS::CheckUpdates::AUR | ||
| 215 | |||
| 216 | |||
| 217 | You can also look for information at: | ||
| 218 | |||
| 219 | =over 4 | ||
| 220 | |||
| 221 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 222 | |||
| 223 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=OS-CheckUpdates-AUR> | ||
| 224 | |||
| 225 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 226 | |||
| 227 | L<http://annocpan.org/dist/OS-CheckUpdates-AUR> | ||
| 228 | |||
| 229 | =item * CPAN Ratings | ||
| 230 | |||
| 231 | L<http://cpanratings.perl.org/d/OS-CheckUpdates-AUR> | ||
| 232 | |||
| 233 | =item * Search CPAN | ||
| 234 | |||
| 235 | L<http://search.cpan.org/dist/OS-CheckUpdates-AUR/> | ||
| 236 | |||
| 237 | =back | ||
| 238 | |||
| 239 | |||
| 240 | =head1 ACKNOWLEDGEMENTS | ||
| 241 | |||
| 242 | |||
| 243 | =head1 LICENSE AND COPYRIGHT | ||
| 244 | |||
| 245 | Copyright 2016 3ED. | ||
| 246 | |||
| 247 | This program is free software; you can redistribute it and/or modify it | ||
| 248 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 249 | copy of the full license at: | ||
| 250 | |||
| 251 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 252 | |||
| 253 | Any use, modification, and distribution of the Standard or Modified | ||
| 254 | Versions is governed by this Artistic License. By using, modifying or | ||
| 255 | distributing the Package, you accept this license. Do not use, modify, | ||
| 256 | or distribute the Package, if you do not accept this license. | ||
| 257 | |||
| 258 | If your Modified Version has been derived from a Modified Version made | ||
| 259 | by someone other than you, you are nevertheless required to ensure that | ||
| 260 | your Modified Version complies with the requirements of this license. | ||
| 261 | |||
| 262 | This license does not grant you the right to use any trademark, service | ||
| 263 | mark, tradename, or logo of the Copyright Holder. | ||
| 264 | |||
| 265 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 266 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 267 | otherwise transfer the Package with respect to any patent claims | ||
| 268 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 269 | Package. If you institute patent litigation (including a cross-claim or | ||
| 270 | counterclaim) against any party alleging that the Package constitutes | ||
| 271 | direct or contributory patent infringement, then this Artistic License | ||
| 272 | to you shall terminate on the date that such litigation is filed. | ||
| 273 | |||
| 274 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 275 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 276 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 277 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 278 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 279 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 280 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 281 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 282 | |||
| 283 | |||
| 284 | =cut | ||
| 285 | |||
| 286 | 1; # End of OS::CheckUpdates::AUR | ||