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