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