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