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