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