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