This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add 'corpus' to the heuristic for demo or test modules
[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';
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
JV
71 } else {
72 warn "About to fetch 02packages from ftp.funet.fi. This may take a few minutes\n";
9168ed13
LB
73 my $gzipped_content = fetch_url('http://ftp.funet.fi/pub/CPAN/modules/02packages.details.txt.gz');
74 unless ($gzipped_content) {
dc2f75c0
JV
75 die "Unable to read 02packages.details.txt from either your CPAN mirror or ftp.funet.fi";
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 {
99 /(\.pm|_pm\.PL)$/ or return;
100 /PPPort\.pm$/ and return;
101 my $module = $File::Find::name;
9e179539 102 $module =~ /\b(demo|t|private|corpus)\b/ and return; # demo or test modules
dc2f75c0
JV
103 my $version = MM->parse_version($_);
104 defined $version or $version = 'undef';
105 $version =~ /\d/ and $version = "'$version'";
106
107 # some heuristics to figure out the module name from the file name
b79036d2 108 $module =~ s{^(lib|cpan|dist|(?:symbian/)?ext|os2/OS2)/}{}
3eae08df 109 and $1 ne 'lib'
dc2f75c0
JV
110 and (
111 $module =~ s{\b(\w+)/\1\b}{$1},
112 $module =~ s{^B/O}{O},
113 $module =~ s{^Devel-PPPort}{Devel},
3eae08df 114 $module =~ s{^libnet/}{},
ffe2b5e6 115 $module =~ s{^PathTools/}{},
b79036d2 116 $module =~ s{REXX/DLL}{DLL},
dc2f75c0
JV
117 $module =~ s{^Encode/encoding}{encoding},
118 $module =~ s{^IPC-SysV/}{IPC/},
119 $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
d9c6e45d
CBW
120 $module =~ s{^(?:DynaLoader|Errno|Opcode|XSLoader)/}{},
121 $module =~ s{^Sys-Syslog/win32}{Sys-Syslog},
122 $module =~ s{^Time-Piece/Seconds}{Time/Seconds},
dc2f75c0 123 );
71c80a8f 124 $module =~ s{^lib/}{}g;
dc2f75c0
JV
125 $module =~ s{/}{::}g;
126 $module =~ s{-}{::}g;
71c80a8f 127 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
dc2f75c0
JV
128 $module =~ s/(\.pm|_pm\.PL)$//;
129 $lines{$module} = $version;
130 $module_to_file{$module} = $File::Find::name;
131 },
b79036d2 132 'os2/OS2',
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";
ce0b42da 157foreach my $key (sort keys $delta_data->{changed}->%*) {
a272bf38
DL
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";
ce0b42da 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";
ce0b42da 263 foreach my $key (sort keys $delta_data->{changed}->%*) {
1e955fab
RS
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";
ce0b42da 270 for my $key (sort keys %{ $delta_data->{removed} || {} }) {
1e955fab
RS
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
d990e30d
SH
303open( my $utils_fh, '<', $utils_file );
304my $utils = join( '', <$utils_fh> );
305close $utils_fh;
306
1b3a25b0 307my %utils = map { ( $_ => 1 ) } parse_utils_lst();
d990e30d 308
1b3a25b0 309my $delta_utils = make_coreutils_delta($perl_vnum, \%utils);
d990e30d 310
513922a6
SH
311my $utilities_in_release = " " . $perl_vnum . " => {\n";
312$utilities_in_release .= " delta_from => $delta_utils->{delta_from},\n";
313$utilities_in_release .= " changed => {\n";
ce0b42da 314foreach my $key (sort keys $delta_utils->{changed}->%*) {
513922a6
SH
315 $utilities_in_release .= sprintf " %-24s=> %s,\n", "'$key'",
316 defined $delta_utils->{changed}{$key} ? "'"
317 . $delta_utils->{changed}{$key} . "'" : "undef";
318}
319$utilities_in_release .= " },\n";
320$utilities_in_release .= " removed => {\n";
ce0b42da 321for my $key (sort keys %{ $delta_utils->{removed} || {} }) {
513922a6
SH
322 $utilities_in_release .= sprintf " %-24s=> %s,\n", "'$key'", 1;
323}
324$utilities_in_release .= " }\n";
325$utilities_in_release .= " },\n";
326
327$utils =~ s/^(my %delta\s*=\s*.*?)^\s*$perl_vnum\s*=>\s*{.*?},\s*(^\);)$/$1$2/ism;
328$utils =~ s/^(my %delta\s*=\s*.*?)(^\);)$/$1$utilities_in_release$2/ism;
329
d990e30d 330write_corelist($utils,$utils_file);
dc2f75c0 331
1b3a25b0 332warn "All done. Please check over the following files carefully before committing.\nThanks!\n";
64eeb2c0 333warn "$corelist_file\n$utils_file\n";
1b3a25b0 334
dc2f75c0
JV
335sub write_corelist {
336 my $content = shift;
dd4d388c
CBW
337 my $filename = shift;
338 open (my $clfh, ">", $filename);
c89c9a06 339 binmode $clfh;
54233949 340 print $clfh $content;
dc2f75c0
JV
341 close($clfh);
342}
343
344sub fetch_url {
345 my $url = shift;
b87bfaf3
LB
346 my $http = HTTP::Tiny->new;
347 my $response = $http->get($url);
348 if ($response->{success}) {
349 return $response->{content};
350 } else {
351 warn "Error fetching $url: $response->{status} $response->{reason}\n";
352 return;
dc2f75c0 353 }
fb237dfd 354}
a272bf38
DL
355
356sub make_corelist_delta {
1e955fab 357 my($version, $lines, $existing) = @_;
a272bf38
DL
358 # Trust core perl, if someone does use a weird version number the worst that
359 # can happen is an extra delta entry for a module.
360 my %versions = map { $_ => eval $lines->{$_} } keys %$lines;
361
362 # Ensure we have the corelist data loaded from this perl checkout, not the system one.
363 require $corelist_file;
364
365 my %deltas;
366 # Search for the release with the least amount of changes (this avoids having
367 # to ask for where this perl was branched from).
c58bf3ba 368 for my $previous (reverse sort { $a <=> $b } keys %$existing) {
a272bf38 369 # Shouldn't happen, but ensure we don't load weird data...
c58bf3ba 370 next if $previous > $version || $previous == $version;
a272bf38
DL
371 my $delta = $deltas{$previous} = {};
372 ($delta->{changed}, $delta->{removed}) = calculate_delta(
1e955fab 373 $existing->{$previous}, \%versions);
a272bf38
DL
374 }
375
376 my $smallest = (sort {
ce0b42da
AC
377 ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
378 (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
f15874f1 379 $b <=> $a
a272bf38
DL
380 } keys %deltas)[0];
381
382 return {
383 delta_from => $smallest,
384 changed => $deltas{$smallest}{changed},
385 removed => $deltas{$smallest}{removed},
386 }
387}
388
758842b8
SH
389sub make_coreutils_delta {
390 my($version, $lines) = @_;
391 # Trust core perl, if someone does use a weird version number the worst that
392 # can happen is an extra delta entry for a module.
ae864c3a 393 my %utilities = map { $_ => eval $lines->{$_} } keys %$lines;
758842b8
SH
394
395 # Ensure we have the corelist data loaded from this perl checkout, not the system one.
ae864c3a 396 require $utils_file;
758842b8
SH
397
398 my %deltas;
399 # Search for the release with the least amount of changes (this avoids having
400 # to ask for where this perl was branched from).
c58bf3ba 401 for my $previous (reverse sort { $a <=> $b } keys %Module::CoreList::Utils::utilities) {
758842b8 402 # Shouldn't happen, but ensure we don't load weird data...
c58bf3ba 403 next if $previous > $version || $previous == $version;
758842b8
SH
404
405 my $delta = $deltas{$previous} = {};
406 ($delta->{changed}, $delta->{removed}) = calculate_delta(
ae864c3a 407 $Module::CoreList::Utils::utilities{$previous}, \%utilities);
758842b8
SH
408 }
409
410 my $smallest = (sort {
ce0b42da
AC
411 ((keys($deltas{$a}->{changed}->%*) + keys($deltas{$a}->{removed}->%*)) <=>
412 (keys($deltas{$b}->{changed}->%*) + keys($deltas{$b}->{removed}->%*))) ||
f15874f1 413 $b <=> $a
758842b8
SH
414 } keys %deltas)[0];
415
416 return {
417 delta_from => $smallest,
418 changed => $deltas{$smallest}{changed},
419 removed => $deltas{$smallest}{removed},
420 }
421}
422
a272bf38
DL
423# Calculate (changed, removed) modules between two versions.
424sub calculate_delta {
425 my($from, $to) = @_;
426 my(%changed, %removed);
427
ce0b42da 428 for my $package(keys %$from) {
a272bf38
DL
429 if(not exists $to->{$package}) {
430 $removed{$package} = 1;
431 }
432 }
433
ce0b42da 434 for my $package(keys %$to) {
a272bf38
DL
435 if(!exists $from->{$package}
436 || (defined $from->{$package} && !defined $to->{$package})
437 || (!defined $from->{$package} && defined $to->{$package})
438 || (defined $from->{$package} && defined $to->{$package}
439 && $from->{$package} ne $to->{$package})) {
440 $changed{$package} = $to->{$package};
441 }
442 }
443
444 return \%changed, \%removed;
445}
a681e3bc
FR
446
447sub quote {
448 my ($str) = @_;
449 # There's gotta be something already doing this properly that we could just
450 # reuse, but I can't quite thing of where to look for it, so I'm gonna do
0af431d3 451 # the simplest possible thing that'll allow me to release 5.17.7. --rafl
a681e3bc
FR
452 $str =~ s/'/\\'/g;
453 "'${str}'";
454}
d990e30d
SH
455
456sub parse_utils_lst {
457 require File::Spec::Unix;
458 my @scripts;
459 open my $fh, '<', 'utils.lst' or die "$!\n";
460 while (<$fh>) {
461 chomp;
462 my ($file,$extra) = split m!#!;
463 $file =~ s!\s+!!g;
464 push @scripts, $file;
465 $extra =~ s!\s+!!g if $extra;
466 if ( $extra and my ($link) = $extra =~ m!^link=(.+?)$! ) {
467 push @scripts, $link;
468 }
469 }
470 return map { +( File::Spec::Unix->splitpath( $_ ) )[-1] } @scripts;
471}