Debian Codegraph @

script/cpan-outdated

raw
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use ExtUtils::Installed;
5 use Getopt::Long;
6 use Config;
7 use version;
8 use IO::Zlib;
9 use CPAN::DistnameInfo;
10 use Module::Metadata;
11 use URI;
12
13 our $VERSION = "0.32";
14
15 my $mirror = 'http://www.cpan.org/';
16 my $local_lib;
17 my $self_contained = 0;
18 my $index_file;
19 my $help;
20 Getopt::Long::Configure("bundling");
21 Getopt::Long::GetOptions(
22 'h|help' => \$help,
23 'verbose' => \my $verbose,
24 'm|mirror=s' => \$mirror,
25 'index=s' => \$index_file,
26 'p|print-package' => \my $print_package,
27 'I=s' => sub { die "this option was deprecated" },
28 'l|local-lib=s' => \$local_lib,
29 'L|local-lib-contained=s' =>
30 sub { $local_lib = $_[1]; $self_contained = 1; },
31 'compare-changes' => sub {
32 die "--compare-changes option was deprecated.\n"
33 . "You can use 'cpan-listchanges `cpan-outdated -p`' instead.\n"
34 . "cpanm cpan-listchanges # install from CPAN\n"
35 },
36 'exclude-core' => \my $exclude_core,
37 ) or $help++;
38 if ($help) {
39 require Pod::Usage;
40 Pod::Usage::pod2usage();
41 }
42
43 $mirror =~ s:/$::;
44 my $index_url = "${mirror}/modules/02packages.details.txt.gz";
45 $index_url = URI->new($index_url);
46 if ($index_url->isa('URI::file')) {
47 die '--index is incompatible with a file:// mirror' if defined $index_file;
48 $index_file = $index_url->file
49 }
50
51 my $core_modules;
52 if ($exclude_core) {
53 require Module::CoreList;
54 no warnings 'once';
55 $core_modules = $Module::CoreList::version{$]};
56 }
57
58 unless ($ENV{HARNESS_ACTIVE}) {
59 &main;
60 exit;
61 }
62
63 sub modules_to_check {
64 my @inc = @_;
65 my @modules =
66 ExtUtils::Installed->new(skip_cwd => 1, inc_override => \@inc)->modules;
67 # As core modules may not have been listed by EUI because they lack
68 # .packlist, we add them from Module::CoreList
69 if (!$exclude_core || ($local_lib && !$self_contained)) {
70 require Module::CoreList;
71 # This adds duplicates, but they are removed by the caller
72 push @modules, keys %{ $Module::CoreList::version{$]} };
73 }
74 (@modules)
75 }
76
77 sub installed_version_for {
78 my($pkg, $inc) = @_;
79
80 local $SIG{__WARN__} = sub {};
81 my $meta = Module::Metadata->new_from_module($pkg, inc => $inc);
82 $meta ? $meta->version($pkg) : undef;
83 }
84
85 sub main {
86 my @inc = make_inc($local_lib, $self_contained);
87
88 if ( !defined($index_file)
89 || ! -e $index_file || -z $index_file
90 || !$index_url->isa('URI::file')) {
91
92 $index_file = get_index($index_url, $index_file)
93 }
94
95 my %installed = map { $_ => 1 } modules_to_check(@inc);
96
97 my $fh = zopen($index_file) or die "cannot open $index_file";
98 # skip header part
99 while (my $line = <$fh>) {
100 last if $line eq "\n";
101 }
102 # body part
103 my %seen;
104 my %dist_latest_version;
105 LINES: while (my $line = <$fh>) {
106 my ($pkg, $version, $dist) = split /\s+/, $line;
107 next unless $installed{$pkg};
108 next if $version eq 'undef';
109
110 # The note below about the latest version heuristics applies here too
111 next if $seen{$dist};
112
113 # $Mail::SpamAssassin::Conf::VERSION is 'bogus'
114 # https://rt.cpan.org/Public/Bug/Display.html?id=73465
115 next unless $version =~ /[0-9]/;
116
117 # if excluding core modules
118 next if $exclude_core && exists $core_modules->{$pkg};
119
120 next if $dist =~ m{/perl-[0-9._]+\.tar\.(gz|bz2)$};
121
122 my $inst_version = installed_version_for($pkg, \@inc)
123 or next;
124
125 if (compare_version($inst_version, $version)) {
126 $seen{$dist}++;
127 if ($verbose) {
128 printf "%-30s %-7s %-7s %s\n", $pkg, $inst_version, $version, $dist;
129 } elsif ($print_package) {
130 print "$pkg\n";
131 } else {
132 print "$dist\n";
133 }
134 }
135 }
136 }
137
138
139 # return true if $inst_version is less than $version
140 sub compare_version {
141 my ($inst_version, $version) = @_;
142 return 0 if $inst_version eq $version;
143
144 my $inst_version_obj = eval { version->new($inst_version) } || version->new(permissive_filter($inst_version));
145 my $version_obj = eval { version->new($version) } || version->new(permissive_filter($version));
146
147 return $inst_version_obj < $version_obj ? 1 : 0;
148 }
149
150 # for broken packages.
151 sub permissive_filter {
152 local $_ = $_[0];
153 s/^[Vv](\d)/$1/; # Bioinf V2.0
154 s/^(\d+)_(\d+)$/$1.$2/; # VMS-IndexedFile 0_02
155 s/-[a-zA-Z]+$//; # Math-Polygon-Tree 0.035-withoutworldwriteables
156 s/([a-j])/ord($1)-ord('a')/gie; # DBD-Solid 0.20a
157 s/[_h-z-]/./gi; # makepp 1.50.2vs.070506
158 s/\.{2,}/./g;
159 $_;
160 }
161
162
163 # Return the $fname (a generated File::Temp object if not provided)
164 sub get_index {
165 my ($url, $fname) = @_;
166 require HTTP::Tiny;
167 my $ua = HTTP::Tiny->new;
168 my $response;
169 if (defined $fname) {
170 # If the file is not empty, use it as a local cached copy
171 if (-s $fname) {
172 $response = $ua->mirror($url, $fname);
173 } else {
174 # If the file is empty we do not trust its timestamp
175 # so set a custom If-Modified-Since (Perl 5.0 release)
176 $response = $ua->mirror($url, $fname,
177 {
178 headers => {
179 'if-modified-since' => 'Wed, 19 Oct 1994 17:18:57 GMT',
180 },
181 });
182 }
183 } else {
184 require File::Temp;
185 $fname = File::Temp->new(UNLINK => 1, SUFFIX => '.gz');
186 binmode $fname;
187 $response = $ua->request(
188 'GET' => $url,
189 {
190 data_callback => sub { print {$fname} $_[0] },
191 }
192 );
193 close $fname;
194 }
195 if ($response->{status} == 599) {
196 die "Cannot get_index $url to $fname: $response->{content}";
197 # 304 = "Not Modified" is still a success since we are mirroring
198 } elsif (! $response->{success}) {
199 die "Cannot get_index $url to $fname: $response->{status} $response->{reason}";
200 }
201 #print "$fname $response->{status} $response->{reason}\n";
202 # Return the filename (which might be a File::Temp object)
203 $fname
204 }
205
206 sub zopen {
207 # Explicitely stringify the filename as it may be a File::Temp object
208 IO::Zlib->new("$_[0]", "rb");
209 }
210
211 sub make_inc {
212 my ($base, $self_contained) = @_;
213
214 if ($base) {
215 require local::lib;
216 my @modified_inc = (
217 local::lib->install_base_perl_path($base),
218 local::lib->install_base_arch_path($base),
219 );
220 if ($self_contained) {
221 push @modified_inc, @Config{qw(privlibexp archlibexp)};
222 } else {
223 push @modified_inc, @INC;
224 }
225 return @modified_inc;
226 } else {
227 return @INC;
228 }
229 }
230
231 __END__
232
233 =head1 NAME
234
235 cpan-outdated - detect outdated CPAN modules in your environment
236
237 =head1 SYNOPSIS
238
239 # print a list of distributions that contain outdated modules
240 % cpan-outdated
241
242 # print a list of outdated modules in packages
243 % cpan-outdated -p
244
245 # verbose
246 % cpan-outdated --verbose
247 
248 # ignore core modules (do not update dual life modules)
249 % cpan-outdated --exclude-core
250
251 # alternate mirrors
252 % cpan-outdated --mirror file:///home/user/minicpan/
253
254 # additional module path(same as cpanminus)
255 % cpan-outdated -l extlib/
256 % cpan-outdated -L extlib/
257
258 # install with cpan
259 % cpan-outdated | xargs cpan -i
260
261 # install with cpanm
262 % cpan-outdated | cpanm
263 % cpan-outdated -p | cpanm
264
265 =head1 DESCRIPTION
266
267 This script prints a list of outdated CPAN modules on your machine.
268
269 This is the same feature as 'CPAN::Shell->r', but C<cpan-outdated> is much faster and uses less memory.
270
271 This script can be integrated with the L<cpanm> command.
272
273 =head1 PRINTING PACKAGES VS DISTRIBUTIONS
274
275 This script by default prints the outdated distribution as in the CPAN
276 distro format, i.e: C<A/AU/AUTHOR/Distribution-Name-0.10.tar.gz> so
277 you can pipe it into CPAN installers, but with the C<-p> option it can be
278 tweaked to print the module's package names.
279
280 If you wish to manage a set of modules separately from your system
281 perl installation and not install newer versions of "dual life modules"
282 that are distributed with perl, the C<--exclude-core> option will make
283 cpan-outdated ignore changes to core modules. Used with tools like
284 cpanm and its C<-L --local-lib-contained> and C<--self-contained> options,
285 this facilitates maintaining updates on standalone sets of modules.
286
287 For some tools, such as L<cpanm>, installing from packages could be a
288 bit more useful since you can track to see the old version number
289 which you upgrade from.
290
291 =head1 AUTHOR
292
293 Tokuhiro Matsuno
294
295 =head1 LICENSE
296
297 Copyright (C) 2009 Tokuhiro Matsuno.
298
299 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
300
301 =head1 SEE ALSO
302
303 L<CPAN>
304
305 L<App::cpanminus>
306
307 If you want to see what's changed for modules that require upgrades, use L<cpan-listchanges>
308
309 =cut