This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Correction recommended by Brad Gilbert++.
[perl5.git] / Porting / corelist.pl
1 #!perl
2 # Generates info for Module::CoreList from this perl tree
3 # run this from the root of a perl tree
4 #
5 # Data is on STDOUT.
6 #
7 # With an optional arg specifying the root of a CPAN mirror, outputs the
8 # %upstream and %bug_tracker hashes too.
9
10 use autodie;
11 use strict;
12 use warnings;
13 use File::Find;
14 use ExtUtils::MM_Unix;
15 use version;
16 use lib "Porting";
17 use Maintainers qw(%Modules files_to_modules);
18 use File::Spec;
19 use Parse::CPAN::Meta;
20 use IPC::Cmd 'can_run';
21 use HTTP::Tiny;
22 use IO::Uncompress::Gunzip;
23
24 my $corelist_file = 'dist/Module-CoreList/lib/Module/CoreList.pm';
25 my $pod_file = 'dist/Module-CoreList/lib/Module/CoreList.pod';
26
27 my %lines;
28 my %module_to_file;
29 my %modlist;
30
31 die "usage: $0 [ cpan-mirror/ ] [ 5.x.y] \n" unless @ARGV <= 2;
32 my $cpan         = shift;
33 my $raw_version  = shift || $];
34 my $perl_version = version->parse("$raw_version");
35 my $perl_vnum    = $perl_version->numify;
36 my $perl_vstring = $perl_version->normal; # how do we get version.pm to not give us leading v?
37 $perl_vstring =~ s/^v//;
38
39 if ( !-f 'MANIFEST' ) {
40     die "Must be run from the root of a clean perl tree\n";
41 }
42
43 open( my $corelist_fh, '<', $corelist_file );
44 my $corelist = join( '', <$corelist_fh> );
45
46 if ($cpan) {
47     my $modlistfile = File::Spec->catfile( $cpan, 'modules', '02packages.details.txt' );
48     my $content;
49
50     my $fh;
51     if ( -e $modlistfile ) {
52         warn "Reading the module list from $modlistfile";
53         open $fh, '<', $modlistfile;
54     } elsif ( -e $modlistfile . ".gz" ) {
55         my $zcat = can_run('gzcat') || can_run('zcat') or die "Can't find gzcat or zcat";
56         warn "Reading the module list from $modlistfile.gz";
57         open $fh, '-|', "$zcat $modlistfile.gz";
58     } else {
59         warn "About to fetch 02packages from ftp.funet.fi. This may take a few minutes\n";
60         my $gzipped_content = fetch_url('http://ftp.funet.fi/pub/CPAN/modules/02packages.details.txt.gz');
61         unless ($gzipped_content) {
62             die "Unable to read 02packages.details.txt from either your CPAN mirror or ftp.funet.fi";
63         }
64         IO::Uncompress::Gunzip::gunzip(\$gzipped_content, \$content, Transparent => 0)
65             or die "Can't gunzip content: $IO::Uncompress::Gunzip::GunzipError";
66     }
67
68     if ( $fh and !$content ) {
69         local $/ = "\n";
70         $content = join( '', <$fh> );
71     }
72
73     die "Incompatible modlist format"
74         unless $content =~ /^Columns: +package name, version, path/m;
75
76     # Converting the file to a hash is about 5 times faster than a regexp flat
77     # lookup.
78     for ( split( qr/\n/, $content ) ) {
79         next unless /^([A-Za-z_:0-9]+) +[-0-9.undefHASHVERSIONvsetwhenloadingbogus]+ +(\S+)/;
80         $modlist{$1} = $2;
81     }
82 }
83
84 find(
85     sub {
86         /(\.pm|_pm\.PL)$/ or return;
87         /PPPort\.pm$/ and return;
88         my $module = $File::Find::name;
89         $module =~ /\b(demo|t|private)\b/ and return;    # demo or test modules
90         my $version = MM->parse_version($_);
91         defined $version or $version = 'undef';
92         $version =~ /\d/ and $version = "'$version'";
93
94         # some heuristics to figure out the module name from the file name
95         $module =~ s{^(lib|cpan|dist|(?:symbian/)?ext)/}{}
96                         and $1 ne 'lib'
97             and (
98             $module =~ s{\b(\w+)/\1\b}{$1},
99             $module =~ s{^B/O}{O},
100             $module =~ s{^Devel-PPPort}{Devel},
101             $module =~ s{^libnet/}{},
102             $module =~ s{^Encode/encoding}{encoding},
103             $module =~ s{^IPC-SysV/}{IPC/},
104             $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
105             $module =~ s{^(?:DynaLoader|Errno|Opcode|XSLoader)/}{},
106             $module =~ s{^Sys-Syslog/win32}{Sys-Syslog},
107             $module =~ s{^Time-Piece/Seconds}{Time/Seconds},
108             );
109         $module =~ s{^vms/ext}{VMS};
110                 $module =~ s{^lib/}{}g;
111         $module =~ s{/}{::}g;
112         $module =~ s{-}{::}g;
113                 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
114         $module =~ s/(\.pm|_pm\.PL)$//;
115         $lines{$module}          = $version;
116         $module_to_file{$module} = $File::Find::name;
117     },
118     'vms/ext',
119     'symbian/ext',
120     'lib',
121     'ext',
122         'cpan',
123         'dist'
124 );
125
126 -e 'configpm' and $lines{Config} = 'undef';
127
128 if ( open my $ucdv, "<", "lib/unicore/version" ) {
129     chomp( my $ucd = <$ucdv> );
130     $lines{Unicode} = "'$ucd'";
131     close $ucdv;
132 }
133
134 my $delta_data = make_corelist_delta($perl_vnum, \%lines);
135 my $versions_in_release = "    " . $perl_vnum . " => {\n";
136 $versions_in_release .= "        delta_from => $delta_data->{delta_from},\n";
137 $versions_in_release .= "        changed => {\n";
138 foreach my $key (sort keys $delta_data->{changed}) {
139   $versions_in_release .= sprintf "            %-24s=> %s,\n", "'$key'",
140       defined $delta_data->{changed}{$key} ? "'"
141         . $delta_data->{changed}{$key} . "'" : "undef";
142 }
143 $versions_in_release .= "        },\n";
144 $versions_in_release .= "        removed => {\n";
145 for my $key (sort keys($delta_data->{removed} || {})) {
146   $versions_in_release .= sprintf "           %-24s=> %s,\n", "'$key'", 1;
147 }
148 $versions_in_release .= "        }\n";
149 $versions_in_release .= "    },\n";
150
151 $corelist =~ s/^(my %delta\s*=\s*.*?)(^\);)$/$1$versions_in_release$2/ism;
152
153 exit unless %modlist;
154
155 # We have to go through this two stage lookup, given how Maintainers.pl keys its
156 # data by "Module", which is really a dist.
157 my $file_to_M = files_to_modules( values %module_to_file );
158
159 sub slurp_utf8($) {
160     open my $fh, "<:utf8", "$_[0]"
161         or die "can't open $_[0] for reading: $!";
162     return do { local $/; <$fh> };
163 }
164
165 sub parse_cpan_meta($) {
166     return Parse::CPAN::Meta->${
167         $_[0] =~ /\A\x7b/ ? \"load_json_string" : \"load_yaml_string"
168     }($_[0]);
169 }
170
171 my %module_to_upstream;
172 my %module_to_dist;
173 my %dist_to_meta_YAML;
174 my %module_to_deprecated;
175 while ( my ( $module, $file ) = each %module_to_file ) {
176     my $M = $file_to_M->{$file};
177     next unless $M;
178     next if $Modules{$M}{MAINTAINER} && $Modules{$M}{MAINTAINER} eq 'p5p';
179     $module_to_upstream{$module} = $Modules{$M}{UPSTREAM};
180     $module_to_deprecated{$module} = 1 if $Modules{$M}{DEPRECATED};
181     next
182         if defined $module_to_upstream{$module}
183             && $module_to_upstream{$module} =~ /^(?:blead|first-come)$/;
184     my $dist = $modlist{$module};
185     unless ($dist) {
186         warn "Can't find a distribution for $module\n";
187         next;
188     }
189     $module_to_dist{$module} = $dist;
190
191     next if exists $dist_to_meta_YAML{$dist};
192
193     $dist_to_meta_YAML{$dist} = undef;
194
195     # Like it or lump it, this has to be Unix format.
196     my $meta_YAML_path = "authors/id/$dist";
197     $meta_YAML_path =~ s/(?:tar\.gz|tar\.bz2|zip|tgz)$/meta/
198         or die "ERROR: bad meta YAML path: '$meta_YAML_path'";
199     my $meta_YAML_url = 'http://ftp.funet.fi/pub/CPAN/' . $meta_YAML_path;
200
201     if ( -e "$cpan/$meta_YAML_path" ) {
202         $dist_to_meta_YAML{$dist} = parse_cpan_meta(slurp_utf8( $cpan . "/" . $meta_YAML_path ));
203     } elsif ( my $content = fetch_url($meta_YAML_url) ) {
204         unless ($content) {
205             warn "Failed to fetch $meta_YAML_url\n";
206             next;
207         }
208         eval { $dist_to_meta_YAML{$dist} = parse_cpan_meta($content); };
209         if ( my $err = $@ ) {
210             warn "$meta_YAML_path: ".$err;
211             next;
212         }
213     } else {
214         warn "$meta_YAML_path does not exist for $module\n";
215
216         # I tried code to open the tarballs with Archive::Tar to find and
217         # extract META.yml, but only Text-Tabs+Wrap-2006.1117.tar.gz had one,
218         # so it's not worth including.
219         next;
220     }
221 }
222
223 my $upstream_stanza = "%upstream = (\n";
224 foreach my $module ( sort keys %module_to_upstream ) {
225     my $upstream = defined $module_to_upstream{$module} ? "'$module_to_upstream{$module}'" : 'undef';
226     $upstream_stanza .= sprintf "    %-24s=> %s,\n", "'$module'", $upstream;
227 }
228 $upstream_stanza .= ");";
229
230 $corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx;
231
232 # Deprecation generation
233 my $deprecated_stanza = "    " . $perl_vnum . " => {\n";
234 foreach my $module ( sort keys %module_to_deprecated ) {
235     my $deprecated = defined $module_to_deprecated{$module} ? "'$module_to_deprecated{$module}'" : 'undef';
236     $deprecated_stanza .= sprintf "\t%-24s=> %s,\n", "'$module'", $deprecated;
237 }
238 $deprecated_stanza .= "    },\n";
239 $corelist =~ s/^(%deprecated\s*=\s*.*?)(^\);)$/$1$deprecated_stanza$2/xism;
240
241 my $tracker = "%bug_tracker = (\n";
242 foreach my $module ( sort keys %module_to_upstream ) {
243     my $upstream = defined $module_to_upstream{$module};
244     next
245         if defined $upstream
246             and $upstream eq 'blead' || $upstream eq 'first-come';
247
248     my $bug_tracker;
249
250     my $dist = $module_to_dist{$module};
251     $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker}
252         if $dist;
253     $bug_tracker = $bug_tracker->{web} if ref($bug_tracker) eq "HASH";
254
255     $bug_tracker = defined $bug_tracker ? quote($bug_tracker) : 'undef';
256         next if $bug_tracker eq "'http://rt.perl.org/perlbug/'";
257     $tracker .= sprintf "    %-24s=> %s,\n", "'$module'", $bug_tracker;
258 }
259 $tracker .= ");";
260
261 $corelist =~ s/^%bug_tracker .*? ;/$tracker/eismx;
262
263 unless (
264     $corelist =~ /^%released \s* = \s* \(
265         .*?
266         $perl_vnum => .*?
267         \);/ismx
268     )
269 {
270     warn "Adding $perl_vnum to the list of released perl versions. Please consider adding a release date.\n";
271     $corelist =~ s/^(%released \s* = \s* .*?) ( \) )
272                 /$1  $perl_vnum => '????-??-??',\n  $2/ismx;
273 }
274
275 write_corelist($corelist,$corelist_file);
276
277 open( my $pod_fh, '<', $pod_file );
278 my $pod = join( '', <$pod_fh> );
279
280 unless ( $pod =~ /and $perl_vstring releases of perl/ ) {
281     warn "Adding $perl_vstring to the list of perl versions covered by Module::CoreList\n";
282     $pod =~ s/(currently\s+covers\s+(?:.*?))\s*and\s+(.*?)\s+releases\s+of\s+perl/$1, $2 and $perl_vstring releases of perl/ism;
283 }
284
285 write_corelist($pod,$pod_file);
286
287 warn "All done. Please check over $corelist_file and $pod_file carefully before committing. Thanks!\n";
288
289
290 sub write_corelist {
291     my $content = shift;
292     my $filename = shift;
293     open (my $clfh, ">", $filename);
294     binmode $clfh;
295     print $clfh $content;
296     close($clfh);
297 }
298
299 sub fetch_url {
300     my $url = shift;
301     my $http = HTTP::Tiny->new;
302     my $response = $http->get($url);
303     if ($response->{success}) {
304         return $response->{content};
305     } else {
306         warn "Error fetching $url: $response->{status} $response->{reason}\n";
307         return;
308     }
309 }
310
311 sub make_corelist_delta {
312   my($version, $lines) = @_;
313   # Trust core perl, if someone does use a weird version number the worst that
314   # can happen is an extra delta entry for a module.
315   my %versions = map { $_ => eval $lines->{$_} } keys %$lines;
316
317   # Ensure we have the corelist data loaded from this perl checkout, not the system one.
318   require $corelist_file;
319
320   my %deltas;
321   # Search for the release with the least amount of changes (this avoids having
322   # to ask for where this perl was branched from).
323   for my $previous(reverse sort keys %Module::CoreList::version) {
324     # Shouldn't happen, but ensure we don't load weird data...
325     next if $previous > $version || $previous == $version && $previous eq $version;
326
327     my $delta = $deltas{$previous} = {};
328     ($delta->{changed}, $delta->{removed}) = calculate_delta(
329       $Module::CoreList::version{$previous}, \%versions);
330   }
331
332   my $smallest = (sort {
333       (keys($deltas{$a}->{changed}) + keys($deltas{$a}->{removed})) <=>
334       (keys($deltas{$b}->{changed})+ keys($deltas{$b}->{removed}))
335     } keys %deltas)[0];
336
337   return {
338     delta_from => $smallest,
339     changed => $deltas{$smallest}{changed},
340     removed => $deltas{$smallest}{removed},
341   }
342 }
343
344 # Calculate (changed, removed) modules between two versions.
345 sub calculate_delta {
346   my($from, $to) = @_;
347   my(%changed, %removed);
348
349   for my $package(keys $from) {
350     if(not exists $to->{$package}) {
351       $removed{$package} = 1;
352     }
353   }
354
355   for my $package(keys $to) {
356     if(!exists $from->{$package}
357         || (defined $from->{$package} && !defined $to->{$package})
358         || (!defined $from->{$package} && defined $to->{$package})
359         || (defined $from->{$package} && defined $to->{$package}
360             && $from->{$package} ne $to->{$package})) {
361       $changed{$package} = $to->{$package};
362     }
363   }
364
365   return \%changed, \%removed;
366 }
367
368 sub quote {
369     my ($str) = @_;
370     # There's gotta be something already doing this properly that we could just
371     # reuse, but I can't quite thing of where to look for it, so I'm gonna do
372     # the simplest possible thing that'll allow me to release 5.17.7.  --rafl
373     $str =~ s/'/\\'/g;
374     "'${str}'";
375 }