This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Bump version for 5.35.11
[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 $utils_file = './dist/Module-CoreList/lib/Module/CoreList/Utils.pm';
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 close $corelist_fh;
46
47 unless (
48     $corelist =~ /^%released \s* = \s* \(
49         .*?
50         $perl_vnum \s* => \s* .*?
51         \);/ismx
52     )
53 {
54     warn "Adding $perl_vnum to the list of released perl versions. Please consider adding a release date.\n";
55     $corelist =~ s/^(%released \s* = \s* .*?) ( \) )
56                 /$1  $perl_vnum => '????-??-??',\n  $2/ismx;
57 }
58
59 if ($cpan) {
60     my $modlistfile = File::Spec->catfile( $cpan, 'modules', '02packages.details.txt' );
61     my $content;
62
63     my $fh;
64     if ( -e $modlistfile ) {
65         warn "Reading the module list from $modlistfile";
66         open $fh, '<', $modlistfile;
67     } elsif ( -e $modlistfile . ".gz" ) {
68         my $zcat = can_run('gzcat') || can_run('zcat') or die "Can't find gzcat or zcat";
69         warn "Reading the module list from $modlistfile.gz";
70         open $fh, '-|', "$zcat $modlistfile.gz";
71     } else {
72         warn "About to fetch 02packages from www.cpan.org. This may take a few minutes\n";
73         my $gzipped_content = fetch_url('http://www.cpan.org/modules/02packages.details.txt.gz');
74         unless ($gzipped_content) {
75             die "Unable to read 02packages.details.txt from either your CPAN mirror or www.cpan.org";
76         }
77         IO::Uncompress::Gunzip::gunzip(\$gzipped_content, \$content, Transparent => 0)
78             or die "Can't gunzip content: $IO::Uncompress::Gunzip::GunzipError";
79     }
80
81     if ( $fh and !$content ) {
82         local $/ = "\n";
83         $content = join( '', <$fh> );
84     }
85
86     die "Incompatible modlist format"
87         unless $content =~ /^Columns: +package name, version, path/m;
88
89     # Converting the file to a hash is about 5 times faster than a regexp flat
90     # lookup.
91     for ( split( qr/\n/, $content ) ) {
92         next unless /^([A-Za-z_:0-9]+) +[-0-9.undefHASHVERSIONvsetwhenloadingbogus]+ +(\S+)/;
93         $modlist{$1} = $2;
94     }
95 }
96
97 find(
98     sub {
99         if (-d) {
100           my @parts = File::Spec->splitdir($File::Find::name);
101           # be careful not to skip inc::latest
102           return $File::Find::prune = 1 if @parts == 3 and ($parts[-1] eq 'inc' or $parts[-1] eq 't');
103         }
104
105         /(\.pm|_pm\.PL)$/ or return;
106         /PPPort\.pm$/ and return;
107         my $module = $File::Find::name;
108         $module =~ /\b(demo|t|private|corpus)\b/ and return;    # demo or test modules
109         my $version = MM->parse_version($_);
110         defined $version or $version = 'undef';
111         $version =~ /\d/ and $version = "'$version'";
112
113         # some heuristics to figure out the module name from the file name
114         $module =~ s{^(lib|cpan|dist|ext|os2/OS2)/}{}
115                         and $1 ne 'lib'
116             and (
117             $module =~ s{\b(\w+)/\1\b}{$1},
118             $module =~ s{^B/O}{O},
119             $module =~ s{^Devel-PPPort}{Devel},
120             $module =~ s{^libnet/}{},
121             $module =~ s{^PathTools/}{},
122             $module =~ s{REXX/DLL}{DLL},
123             $module =~ s{^Encode/encoding}{encoding},
124             $module =~ s{^IPC-SysV/}{IPC/},
125             $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
126             $module =~ s{^(?:DynaLoader|Errno|Opcode|XSLoader)/}{},
127             $module =~ s{^Sys-Syslog/win32}{Sys-Syslog},
128             $module =~ s{^Time-Piece/Seconds}{Time/Seconds},
129             );
130                 $module =~ s{^lib/}{}g;
131         $module =~ s{/}{::}g;
132         $module =~ s{-}{::}g;
133                 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
134         $module =~ s/(\.pm|_pm\.PL)$//;
135         $lines{$module}          = $version;
136         $module_to_file{$module} = $File::Find::name;
137     },
138     'os2/OS2',
139     'lib',
140     'ext',
141         'cpan',
142         'dist'
143 );
144
145 -e 'configpm' and $lines{Config} = "$]";
146
147 if ( open my $ucdv, "<", "lib/unicore/version" ) {
148     chomp( my $ucd = <$ucdv> );
149     $lines{Unicode} = "'$ucd'";
150     close $ucdv;
151 }
152
153 my $delta_data = make_corelist_delta(
154   $perl_vnum,
155   \%lines,
156   \%Module::CoreList::version
157 );
158
159 my $versions_in_release = "    " . $perl_vnum . " => {\n";
160 $versions_in_release .= "        delta_from => $delta_data->{delta_from},\n";
161 $versions_in_release .= "        changed => {\n";
162 foreach my $key (sort keys $delta_data->{changed}->%*) {
163   $versions_in_release .= sprintf "            %-24s=> %s,\n", "'$key'",
164       defined $delta_data->{changed}{$key} ? "'"
165         . $delta_data->{changed}{$key} . "'" : "undef";
166 }
167 $versions_in_release .= "        },\n";
168 $versions_in_release .= "        removed => {\n";
169 for my $key (sort keys %{ $delta_data->{removed} || {} }) {
170   $versions_in_release .= sprintf "            %-24s=> %s,\n", "'$key'", 1;
171 }
172 $versions_in_release .= "        }\n";
173 $versions_in_release .= "    },\n";
174
175 $corelist =~ s/^(%delta\s*=\s*.*?)^\s*$perl_vnum\s*=>\s*{.*?},\s*(^\);)$/$1$2/ism;
176 $corelist =~ s/^(%delta\s*=\s*.*?)(^\);)$/$1$versions_in_release$2/ism;
177
178 exit unless %modlist;
179
180 # We have to go through this two stage lookup, given how Maintainers.pl keys its
181 # data by "Module", which is really a dist.
182 my $file_to_M = files_to_modules( values %module_to_file );
183
184 sub slurp_utf8($) {
185     open my $fh, "<:utf8", "$_[0]"
186         or die "can't open $_[0] for reading: $!";
187     return do { local $/; <$fh> };
188 }
189
190 sub parse_cpan_meta($) {
191     return Parse::CPAN::Meta->${
192         $_[0] =~ /\A\x7b/ ? \"load_json_string" : \"load_yaml_string"
193     }($_[0]);
194 }
195
196 my %module_to_upstream;
197 my %module_to_dist;
198 my %dist_to_meta_YAML;
199 my %module_to_deprecated;
200 while ( my ( $module, $file ) = each %module_to_file ) {
201     my $M = $file_to_M->{$file};
202     next unless $M;
203     next if $Modules{$M}{MAINTAINER} && $Modules{$M}{MAINTAINER} eq 'P5P';
204     $module_to_upstream{$module} = $Modules{$M}{UPSTREAM};
205     $module_to_deprecated{$module} = 1 if $Modules{$M}{DEPRECATED};
206     next
207         if defined $module_to_upstream{$module}
208             && $module_to_upstream{$module} eq 'blead';
209     my $dist = $modlist{$module};
210     unless ($dist) {
211         warn "Can't find a distribution for $module\n";
212         next;
213     }
214     $module_to_dist{$module} = $dist;
215
216     next if exists $dist_to_meta_YAML{$dist};
217
218     $dist_to_meta_YAML{$dist} = undef;
219
220     # Like it or lump it, this has to be Unix format.
221     my $meta_YAML_path = "authors/id/$dist";
222     $meta_YAML_path =~ s/(?:tar\.gz|tar\.bz2|zip|tgz)$/meta/
223         or die "ERROR: bad meta YAML path: '$meta_YAML_path'";
224     my $meta_YAML_url = 'http://www.cpan.org/' . $meta_YAML_path;
225
226     if ( -e "$cpan/$meta_YAML_path" ) {
227         $dist_to_meta_YAML{$dist} = parse_cpan_meta(slurp_utf8( $cpan . "/" . $meta_YAML_path ));
228     } elsif ( my $content = fetch_url($meta_YAML_url) ) {
229         unless ($content) {
230             warn "Failed to fetch $meta_YAML_url\n";
231             next;
232         }
233         eval { $dist_to_meta_YAML{$dist} = parse_cpan_meta($content); };
234         if ( my $err = $@ ) {
235             warn "$meta_YAML_path: ".$err;
236             next;
237         }
238     } else {
239         warn "$meta_YAML_path does not exist for $module\n";
240
241         # I tried code to open the tarballs with Archive::Tar to find and
242         # extract META.yml, but only Text-Tabs+Wrap-2006.1117.tar.gz had one,
243         # so it's not worth including.
244         next;
245     }
246 }
247
248 my $upstream_stanza = "%upstream = (\n";
249 foreach my $module ( sort keys %module_to_upstream ) {
250     my $upstream = defined $module_to_upstream{$module} ? "'$module_to_upstream{$module}'" : 'undef';
251     $upstream_stanza .= sprintf "    %-24s=> %s,\n", "'$module'", $upstream;
252 }
253 $upstream_stanza .= ");";
254
255 $corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx;
256
257 # Deprecation generation
258 {
259   my $delta_data = make_corelist_delta(
260     $perl_vnum,
261     \%module_to_deprecated,
262     do { no warnings 'once'; \%Module::CoreList::deprecated },
263   );
264
265   my $deprecated_stanza = "    " . $perl_vnum . " => {\n";
266   $deprecated_stanza .= "        delta_from => $delta_data->{delta_from},\n";
267   $deprecated_stanza .= "        changed => {\n";
268   foreach my $key (sort keys $delta_data->{changed}->%*) {
269     $deprecated_stanza .= sprintf "            %-24s=> %s,\n", "'$key'",
270         defined $delta_data->{changed}{$key} ? "'"
271           . $delta_data->{changed}{$key} . "'" : "undef";
272   }
273   $deprecated_stanza .= "        },\n";
274   $deprecated_stanza .= "        removed => {\n";
275   for my $key (sort keys %{ $delta_data->{removed} || {} }) {
276     $deprecated_stanza .= sprintf "           %-24s=> %s,\n", "'$key'", 1;
277   }
278   $deprecated_stanza .= "        }\n";
279   $deprecated_stanza .= "    },\n";
280
281   $corelist =~ s/^(%deprecated\s*=\s*.*?)^\s*$perl_vnum\s*=>\s*{.*?},\s*(^\);)$/$1$2/ism;
282   $corelist =~ s/^(%deprecated\s*=\s*.*?)(^\);)$/$1$deprecated_stanza$2/xism;
283 }
284
285 my $tracker = "%bug_tracker = (\n";
286 foreach my $module ( sort keys %module_to_upstream ) {
287     my $upstream = defined $module_to_upstream{$module};
288     next
289         if defined $upstream and $upstream eq 'blead';
290
291     my $bug_tracker;
292
293     my $dist = $module_to_dist{$module};
294     $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker}
295         if $dist;
296     $bug_tracker = $bug_tracker->{web} if ref($bug_tracker) eq "HASH";
297
298     $bug_tracker = defined $bug_tracker ? quote($bug_tracker) : 'undef';
299     next if $bug_tracker eq "'https://github.com/Perl/perl5/issues'";
300         next if $bug_tracker eq "'http://rt.perl.org/perlbug/'";
301         next if $bug_tracker eq "'https://rt.perl.org/perlbug/'";
302     $tracker .= sprintf "    %-24s=> %s,\n", "'$module'", $bug_tracker;
303 }
304 $tracker .= ");";
305
306 $corelist =~ s/^%bug_tracker .*? ;/$tracker/eismx;
307
308 write_corelist($corelist,$corelist_file);
309
310 open( my $utils_fh, '<', $utils_file );
311 my $utils = join( '', <$utils_fh> );
312 close $utils_fh;
313
314 my %utils = map { ( $_ => 1 ) } parse_utils_lst();
315
316 my $delta_utils = make_coreutils_delta($perl_vnum, \%utils);
317
318 my $utilities_in_release = "    " . $perl_vnum . " => {\n";
319 $utilities_in_release .= "        delta_from => $delta_utils->{delta_from},\n";
320 $utilities_in_release .= "        changed => {\n";
321 foreach my $key (sort keys $delta_utils->{changed}->%*) {
322   $utilities_in_release .= sprintf "            %-24s=> %s,\n", "'$key'",
323       defined $delta_utils->{changed}{$key} ? "'"
324         . $delta_utils->{changed}{$key} . "'" : "undef";
325 }
326 $utilities_in_release .= "        },\n";
327 $utilities_in_release .= "        removed => {\n";
328 for my $key (sort keys %{ $delta_utils->{removed} || {} }) {
329   $utilities_in_release .= sprintf "            %-24s=> %s,\n", "'$key'", 1;
330 }
331 $utilities_in_release .= "        }\n";
332 $utilities_in_release .= "    },\n";
333
334 $utils =~ s/^(my %delta\s*=\s*.*?)^\s*$perl_vnum\s*=>\s*{.*?},\s*(^\);)$/$1$2/ism;
335 $utils =~ s/^(my %delta\s*=\s*.*?)(^\);)$/$1$utilities_in_release$2/ism;
336
337 write_corelist($utils,$utils_file);
338
339 warn "All done. Please check over the following files carefully before committing.\nThanks!\n";
340 warn "$corelist_file\n$utils_file\n";
341
342 sub write_corelist {
343     my $content = shift;
344     my $filename = shift;
345     open (my $clfh, ">", $filename);
346     binmode $clfh;
347     print $clfh $content;
348     close($clfh);
349 }
350
351 sub fetch_url {
352     my $url = shift;
353     my $http = HTTP::Tiny->new;
354     my $response = $http->get($url);
355     if ($response->{success}) {
356         return $response->{content};
357     } else {
358         warn "Error fetching $url: $response->{status} $response->{reason}\n";
359         return;
360     }
361 }
362
363 sub make_corelist_delta {
364   my($version, $lines, $existing) = @_;
365   # Trust core perl, if someone does use a weird version number the worst that
366   # can happen is an extra delta entry for a module.
367   my %versions = map { $_ => eval $lines->{$_} } keys %$lines;
368
369   # Ensure we have the corelist data loaded from this perl checkout, not the system one.
370   require $corelist_file;
371
372   my %deltas;
373   # Search for the release with the least amount of changes (this avoids having
374   # to ask for where this perl was branched from).
375   for my $previous (reverse sort { $a <=> $b } keys %$existing) {
376     # Shouldn't happen, but ensure we don't load weird data...
377     next if $previous > $version || $previous == $version;
378     my $delta = $deltas{$previous} = {};
379     ($delta->{changed}, $delta->{removed}) = calculate_delta(
380       $existing->{$previous}, \%versions);
381   }
382
383   my $smallest = (sort {
384       ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
385        (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
386       $b <=> $a
387     } keys %deltas)[0];
388
389   return {
390     delta_from => $smallest,
391     changed => $deltas{$smallest}{changed},
392     removed => $deltas{$smallest}{removed},
393   }
394 }
395
396 sub make_coreutils_delta {
397   my($version, $lines) = @_;
398   # Trust core perl, if someone does use a weird version number the worst that
399   # can happen is an extra delta entry for a module.
400   my %utilities = map { $_ => eval $lines->{$_} } keys %$lines;
401
402   # Ensure we have the corelist data loaded from this perl checkout, not the system one.
403   require $utils_file;
404
405   my %deltas;
406   # Search for the release with the least amount of changes (this avoids having
407   # to ask for where this perl was branched from).
408   for my $previous (reverse sort { $a <=> $b } keys %Module::CoreList::Utils::utilities) {
409     # Shouldn't happen, but ensure we don't load weird data...
410     next if $previous > $version || $previous == $version;
411
412     my $delta = $deltas{$previous} = {};
413     ($delta->{changed}, $delta->{removed}) = calculate_delta(
414       $Module::CoreList::Utils::utilities{$previous}, \%utilities);
415   }
416
417   my $smallest = (sort {
418       ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
419        (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
420       $b <=> $a
421     } keys %deltas)[0];
422
423   return {
424     delta_from => $smallest,
425     changed => $deltas{$smallest}{changed},
426     removed => $deltas{$smallest}{removed},
427   }
428 }
429
430 # Calculate (changed, removed) modules between two versions.
431 sub calculate_delta {
432   my($from, $to) = @_;
433   my(%changed, %removed);
434
435   for my $package(keys %$from) {
436     if(not exists $to->{$package}) {
437       $removed{$package} = 1;
438     }
439   }
440
441   for my $package(keys %$to) {
442     if(!exists $from->{$package}
443         || (defined $from->{$package} && !defined $to->{$package})
444         || (!defined $from->{$package} && defined $to->{$package})
445         || (defined $from->{$package} && defined $to->{$package}
446             && $from->{$package} ne $to->{$package})) {
447       $changed{$package} = $to->{$package};
448     }
449   }
450
451   return \%changed, \%removed;
452 }
453
454 sub quote {
455     my ($str) = @_;
456     # There's gotta be something already doing this properly that we could just
457     # reuse, but I can't quite thing of where to look for it, so I'm gonna do
458     # the simplest possible thing that'll allow me to release 5.17.7.  --rafl
459     $str =~ s/'/\\'/g;
460     "'${str}'";
461 }
462
463 sub parse_utils_lst {
464   require File::Spec::Unix;
465   my @scripts;
466   open my $fh, '<', 'utils.lst' or die "$!\n";
467   while (<$fh>) {
468     chomp;
469     my ($file,$extra) = split m!#!;
470     $file =~ s!\s+!!g;
471     push @scripts, $file;
472     $extra =~ s!\s+!!g if $extra;
473     if ( $extra and my ($link) = $extra =~ m!^link=(.+?)$! ) {
474       push @scripts, $link;
475     }
476   }
477   return map { +( File::Spec::Unix->splitpath( $_ ) )[-1] } @scripts;
478 }