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