This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add a builtin:: namespace, with true/false/isbool
[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
b5b2770a
S
24my $corelist_file = './dist/Module-CoreList/lib/Module/CoreList.pm';
25my $utils_file = './dist/Module-CoreList/lib/Module/CoreList/Utils.pm';
e1018a69 26
59189dd7 27my %lines;
fb237dfd
NC
28my %module_to_file;
29my %modlist;
e1018a69 30
dc2f75c0
JV
31die "usage: $0 [ cpan-mirror/ ] [ 5.x.y] \n" unless @ARGV <= 2;
32my $cpan = shift;
33my $raw_version = shift || $];
34my $perl_version = version->parse("$raw_version");
35my $perl_vnum = $perl_version->numify;
36my $perl_vstring = $perl_version->normal; # how do we get version.pm to not give us leading v?
37$perl_vstring =~ s/^v//;
fb237dfd 38
dc2f75c0
JV
39if ( !-f 'MANIFEST' ) {
40 die "Must be run from the root of a clean perl tree\n";
e1018a69
DM
41}
42
54233949 43open( my $corelist_fh, '<', $corelist_file );
dc2f75c0 44my $corelist = join( '', <$corelist_fh> );
7497b91f
SH
45close $corelist_fh;
46
47unless (
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}
dc2f75c0 58
fb237dfd 59if ($cpan) {
dc2f75c0
JV
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";
54233949 66 open $fh, '<', $modlistfile;
dc2f75c0 67 } elsif ( -e $modlistfile . ".gz" ) {
a762e054 68 my $zcat = can_run('gzcat') || can_run('zcat') or die "Can't find gzcat or zcat";
dc2f75c0 69 warn "Reading the module list from $modlistfile.gz";
54233949 70 open $fh, '-|', "$zcat $modlistfile.gz";
dc2f75c0 71 } else {
ee64eb7d
RS
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');
9168ed13 74 unless ($gzipped_content) {
ee64eb7d 75 die "Unable to read 02packages.details.txt from either your CPAN mirror or www.cpan.org";
dc2f75c0 76 }
9168ed13
LB
77 IO::Uncompress::Gunzip::gunzip(\$gzipped_content, \$content, Transparent => 0)
78 or die "Can't gunzip content: $IO::Uncompress::Gunzip::GunzipError";
dc2f75c0
JV
79 }
80
81 if ( $fh and !$content ) {
82 local $/ = "\n";
83 $content = join( '', <$fh> );
fb237dfd
NC
84 }
85
dc2f75c0
JV
86 die "Incompatible modlist format"
87 unless $content =~ /^Columns: +package name, version, path/m;
88
fb237dfd
NC
89 # Converting the file to a hash is about 5 times faster than a regexp flat
90 # lookup.
dc2f75c0
JV
91 for ( split( qr/\n/, $content ) ) {
92 next unless /^([A-Za-z_:0-9]+) +[-0-9.undefHASHVERSIONvsetwhenloadingbogus]+ +(\S+)/;
93 $modlist{$1} = $2;
fb237dfd
NC
94 }
95}
96
dc2f75c0
JV
97find(
98 sub {
d4f7ec97
KE
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
dc2f75c0
JV
105 /(\.pm|_pm\.PL)$/ or return;
106 /PPPort\.pm$/ and return;
107 my $module = $File::Find::name;
9e179539 108 $module =~ /\b(demo|t|private|corpus)\b/ and return; # demo or test modules
dc2f75c0
JV
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
822c8b4d 114 $module =~ s{^(lib|cpan|dist|ext|os2/OS2)/}{}
3eae08df 115 and $1 ne 'lib'
dc2f75c0
JV
116 and (
117 $module =~ s{\b(\w+)/\1\b}{$1},
118 $module =~ s{^B/O}{O},
119 $module =~ s{^Devel-PPPort}{Devel},
3eae08df 120 $module =~ s{^libnet/}{},
ffe2b5e6 121 $module =~ s{^PathTools/}{},
b79036d2 122 $module =~ s{REXX/DLL}{DLL},
dc2f75c0
JV
123 $module =~ s{^Encode/encoding}{encoding},
124 $module =~ s{^IPC-SysV/}{IPC/},
125 $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
d9c6e45d
CBW
126 $module =~ s{^(?:DynaLoader|Errno|Opcode|XSLoader)/}{},
127 $module =~ s{^Sys-Syslog/win32}{Sys-Syslog},
128 $module =~ s{^Time-Piece/Seconds}{Time/Seconds},
dc2f75c0 129 );
71c80a8f 130 $module =~ s{^lib/}{}g;
dc2f75c0
JV
131 $module =~ s{/}{::}g;
132 $module =~ s{-}{::}g;
71c80a8f 133 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
dc2f75c0
JV
134 $module =~ s/(\.pm|_pm\.PL)$//;
135 $lines{$module} = $version;
136 $module_to_file{$module} = $File::Find::name;
137 },
b79036d2 138 'os2/OS2',
dc2f75c0
JV
139 'lib',
140 'ext',
3eae08df 141 'cpan',
71c80a8f 142 'dist'
dc2f75c0 143);
59189dd7 144
e1b61e88 145-e 'configpm' and $lines{Config} = "$]";
cc8432b2 146
dc2f75c0
JV
147if ( open my $ucdv, "<", "lib/unicore/version" ) {
148 chomp( my $ucd = <$ucdv> );
0fdd9e5c 149 $lines{Unicode} = "'$ucd'";
59189dd7 150 close $ucdv;
dc2f75c0 151}
fb237dfd 152
1e955fab
RS
153my $delta_data = make_corelist_delta(
154 $perl_vnum,
155 \%lines,
156 \%Module::CoreList::version
157);
158
dc2f75c0 159my $versions_in_release = " " . $perl_vnum . " => {\n";
a272bf38
DL
160$versions_in_release .= " delta_from => $delta_data->{delta_from},\n";
161$versions_in_release .= " changed => {\n";
ce0b42da 162foreach my $key (sort keys $delta_data->{changed}->%*) {
a272bf38
DL
163 $versions_in_release .= sprintf " %-24s=> %s,\n", "'$key'",
164 defined $delta_data->{changed}{$key} ? "'"
165 . $delta_data->{changed}{$key} . "'" : "undef";
0fdd9e5c 166}
a272bf38
DL
167$versions_in_release .= " },\n";
168$versions_in_release .= " removed => {\n";
ce0b42da 169for my $key (sort keys %{ $delta_data->{removed} || {} }) {
621de13f 170 $versions_in_release .= sprintf " %-24s=> %s,\n", "'$key'", 1;
a272bf38
DL
171}
172$versions_in_release .= " }\n";
dc2f75c0 173$versions_in_release .= " },\n";
fb237dfd 174
75e840ca
SH
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;
fb237dfd
NC
177
178exit 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.
dc2f75c0 182my $file_to_M = files_to_modules( values %module_to_file );
fb237dfd 183
febeafff
Z
184sub slurp_utf8($) {
185 open my $fh, "<:utf8", "$_[0]"
186 or die "can't open $_[0] for reading: $!";
187 return do { local $/; <$fh> };
188}
189
190sub parse_cpan_meta($) {
191 return Parse::CPAN::Meta->${
192 $_[0] =~ /\A\x7b/ ? \"load_json_string" : \"load_yaml_string"
193 }($_[0]);
194}
195
fb237dfd
NC
196my %module_to_upstream;
197my %module_to_dist;
198my %dist_to_meta_YAML;
a762e054 199my %module_to_deprecated;
dc2f75c0 200while ( my ( $module, $file ) = each %module_to_file ) {
fb237dfd
NC
201 my $M = $file_to_M->{$file};
202 next unless $M;
872818ae 203 next if $Modules{$M}{MAINTAINER} && $Modules{$M}{MAINTAINER} eq 'P5P';
fb237dfd 204 $module_to_upstream{$module} = $Modules{$M}{UPSTREAM};
a762e054 205 $module_to_deprecated{$module} = 1 if $Modules{$M}{DEPRECATED};
dc2f75c0
JV
206 next
207 if defined $module_to_upstream{$module}
0582d0ad 208 && $module_to_upstream{$module} eq 'blead';
fb237dfd
NC
209 my $dist = $modlist{$module};
210 unless ($dist) {
dc2f75c0
JV
211 warn "Can't find a distribution for $module\n";
212 next;
fb237dfd
NC
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.
dc2f75c0 221 my $meta_YAML_path = "authors/id/$dist";
d9450ee2
DM
222 $meta_YAML_path =~ s/(?:tar\.gz|tar\.bz2|zip|tgz)$/meta/
223 or die "ERROR: bad meta YAML path: '$meta_YAML_path'";
ee64eb7d 224 my $meta_YAML_url = 'http://www.cpan.org/' . $meta_YAML_path;
dc2f75c0
JV
225
226 if ( -e "$cpan/$meta_YAML_path" ) {
febeafff 227 $dist_to_meta_YAML{$dist} = parse_cpan_meta(slurp_utf8( $cpan . "/" . $meta_YAML_path ));
dc2f75c0
JV
228 } elsif ( my $content = fetch_url($meta_YAML_url) ) {
229 unless ($content) {
230 warn "Failed to fetch $meta_YAML_url\n";
231 next;
232 }
febeafff 233 eval { $dist_to_meta_YAML{$dist} = parse_cpan_meta($content); };
dc2f75c0
JV
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;
fb237dfd 245 }
fb237dfd
NC
246}
247
dc2f75c0
JV
248my $upstream_stanza = "%upstream = (\n";
249foreach 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;
fb237dfd 252}
dc2f75c0
JV
253$upstream_stanza .= ");";
254
255$corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx;
fb237dfd 256
a762e054 257# Deprecation generation
1e955fab
RS
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";
ce0b42da 268 foreach my $key (sort keys $delta_data->{changed}->%*) {
1e955fab
RS
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";
ce0b42da 275 for my $key (sort keys %{ $delta_data->{removed} || {} }) {
1e955fab
RS
276 $deprecated_stanza .= sprintf " %-24s=> %s,\n", "'$key'", 1;
277 }
278 $deprecated_stanza .= " }\n";
279 $deprecated_stanza .= " },\n";
280
dc852d37 281 $corelist =~ s/^(%deprecated\s*=\s*.*?)^\s*$perl_vnum\s*=>\s*{.*?},\s*(^\);)$/$1$2/ism;
1e955fab 282 $corelist =~ s/^(%deprecated\s*=\s*.*?)(^\);)$/$1$deprecated_stanza$2/xism;
a762e054 283}
a762e054 284
dc2f75c0
JV
285my $tracker = "%bug_tracker = (\n";
286foreach my $module ( sort keys %module_to_upstream ) {
fb237dfd 287 my $upstream = defined $module_to_upstream{$module};
dc2f75c0 288 next
0582d0ad 289 if defined $upstream and $upstream eq 'blead';
fb237dfd
NC
290
291 my $bug_tracker;
292
293 my $dist = $module_to_dist{$module};
294 $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker}
dc2f75c0 295 if $dist;
d141a985 296 $bug_tracker = $bug_tracker->{web} if ref($bug_tracker) eq "HASH";
fb237dfd 297
a681e3bc 298 $bug_tracker = defined $bug_tracker ? quote($bug_tracker) : 'undef';
47ef154c 299 next if $bug_tracker eq "'https://github.com/Perl/perl5/issues'";
71c80a8f 300 next if $bug_tracker eq "'http://rt.perl.org/perlbug/'";
94a9f254 301 next if $bug_tracker eq "'https://rt.perl.org/perlbug/'";
dc2f75c0
JV
302 $tracker .= sprintf " %-24s=> %s,\n", "'$module'", $bug_tracker;
303}
304$tracker .= ");";
305
306$corelist =~ s/^%bug_tracker .*? ;/$tracker/eismx;
307
dd4d388c
CBW
308write_corelist($corelist,$corelist_file);
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";
ce0b42da 321foreach my $key (sort keys $delta_utils->{changed}->%*) {
513922a6
SH
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";
ce0b42da 328for my $key (sort keys %{ $delta_utils->{removed} || {} }) {
513922a6
SH
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 339warn "All done. Please check over the following files carefully before committing.\nThanks!\n";
64eeb2c0 340warn "$corelist_file\n$utils_file\n";
1b3a25b0 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).
c58bf3ba 375 for my $previous (reverse sort { $a <=> $b } keys %$existing) {
a272bf38 376 # Shouldn't happen, but ensure we don't load weird data...
c58bf3ba 377 next if $previous > $version || $previous == $version;
a272bf38
DL
378 my $delta = $deltas{$previous} = {};
379 ($delta->{changed}, $delta->{removed}) = calculate_delta(
1e955fab 380 $existing->{$previous}, \%versions);
a272bf38
DL
381 }
382
383 my $smallest = (sort {
ce0b42da
AC
384 ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
385 (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
f15874f1 386 $b <=> $a
a272bf38
DL
387 } keys %deltas)[0];
388
389 return {
390 delta_from => $smallest,
391 changed => $deltas{$smallest}{changed},
392 removed => $deltas{$smallest}{removed},
393 }
394}
395
758842b8
SH
396sub 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.
ae864c3a 400 my %utilities = map { $_ => eval $lines->{$_} } keys %$lines;
758842b8
SH
401
402 # Ensure we have the corelist data loaded from this perl checkout, not the system one.
ae864c3a 403 require $utils_file;
758842b8
SH
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).
c58bf3ba 408 for my $previous (reverse sort { $a <=> $b } keys %Module::CoreList::Utils::utilities) {
758842b8 409 # Shouldn't happen, but ensure we don't load weird data...
c58bf3ba 410 next if $previous > $version || $previous == $version;
758842b8
SH
411
412 my $delta = $deltas{$previous} = {};
413 ($delta->{changed}, $delta->{removed}) = calculate_delta(
ae864c3a 414 $Module::CoreList::Utils::utilities{$previous}, \%utilities);
758842b8
SH
415 }
416
417 my $smallest = (sort {
ce0b42da
AC
418 ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
419 (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
f15874f1 420 $b <=> $a
758842b8
SH
421 } keys %deltas)[0];
422
423 return {
424 delta_from => $smallest,
425 changed => $deltas{$smallest}{changed},
426 removed => $deltas{$smallest}{removed},
427 }
428}
429
a272bf38
DL
430# Calculate (changed, removed) modules between two versions.
431sub calculate_delta {
432 my($from, $to) = @_;
433 my(%changed, %removed);
434
ce0b42da 435 for my $package(keys %$from) {
a272bf38
DL
436 if(not exists $to->{$package}) {
437 $removed{$package} = 1;
438 }
439 }
440
ce0b42da 441 for my $package(keys %$to) {
a272bf38
DL
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}
a681e3bc
FR
453
454sub 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
0af431d3 458 # the simplest possible thing that'll allow me to release 5.17.7. --rafl
a681e3bc
FR
459 $str =~ s/'/\\'/g;
460 "'${str}'";
461}
d990e30d
SH
462
463sub 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}