This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Enable autodie in Porting/corelist.pl
[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';
4a656c5e 21
1f809cd9 22my $corelist_file = 'dist/Module-CoreList/lib/Module/CoreList.pm';
e1018a69 23
59189dd7 24my %lines;
fb237dfd
NC
25my %module_to_file;
26my %modlist;
e1018a69 27
dc2f75c0
JV
28die "usage: $0 [ cpan-mirror/ ] [ 5.x.y] \n" unless @ARGV <= 2;
29my $cpan = shift;
30my $raw_version = shift || $];
31my $perl_version = version->parse("$raw_version");
32my $perl_vnum = $perl_version->numify;
33my $perl_vstring = $perl_version->normal; # how do we get version.pm to not give us leading v?
34$perl_vstring =~ s/^v//;
fb237dfd 35
dc2f75c0
JV
36if ( !-f 'MANIFEST' ) {
37 die "Must be run from the root of a clean perl tree\n";
e1018a69
DM
38}
39
54233949 40open( my $corelist_fh, '<', $corelist_file );
dc2f75c0
JV
41my $corelist = join( '', <$corelist_fh> );
42
fb237dfd 43if ($cpan) {
dc2f75c0
JV
44 my $modlistfile = File::Spec->catfile( $cpan, 'modules', '02packages.details.txt' );
45 my $content;
46
47 my $fh;
48 if ( -e $modlistfile ) {
49 warn "Reading the module list from $modlistfile";
54233949 50 open $fh, '<', $modlistfile;
dc2f75c0 51 } elsif ( -e $modlistfile . ".gz" ) {
a762e054 52 my $zcat = can_run('gzcat') || can_run('zcat') or die "Can't find gzcat or zcat";
dc2f75c0 53 warn "Reading the module list from $modlistfile.gz";
54233949 54 open $fh, '-|', "$zcat $modlistfile.gz";
dc2f75c0
JV
55 } else {
56 warn "About to fetch 02packages from ftp.funet.fi. This may take a few minutes\n";
57 $content = fetch_url('http://ftp.funet.fi/pub/CPAN/modules/02packages.details.txt');
58 unless ($content) {
59 die "Unable to read 02packages.details.txt from either your CPAN mirror or ftp.funet.fi";
60 }
61 }
62
63 if ( $fh and !$content ) {
64 local $/ = "\n";
65 $content = join( '', <$fh> );
fb237dfd
NC
66 }
67
dc2f75c0
JV
68 die "Incompatible modlist format"
69 unless $content =~ /^Columns: +package name, version, path/m;
70
fb237dfd
NC
71 # Converting the file to a hash is about 5 times faster than a regexp flat
72 # lookup.
dc2f75c0
JV
73 for ( split( qr/\n/, $content ) ) {
74 next unless /^([A-Za-z_:0-9]+) +[-0-9.undefHASHVERSIONvsetwhenloadingbogus]+ +(\S+)/;
75 $modlist{$1} = $2;
fb237dfd
NC
76 }
77}
78
dc2f75c0
JV
79find(
80 sub {
81 /(\.pm|_pm\.PL)$/ or return;
82 /PPPort\.pm$/ and return;
83 my $module = $File::Find::name;
84 $module =~ /\b(demo|t|private)\b/ and return; # demo or test modules
85 my $version = MM->parse_version($_);
86 defined $version or $version = 'undef';
87 $version =~ /\d/ and $version = "'$version'";
88
89 # some heuristics to figure out the module name from the file name
f4ccb67a 90 $module =~ s{^(lib|cpan|dist|(?:symbian/)?ext)/}{}
3eae08df 91 and $1 ne 'lib'
dc2f75c0
JV
92 and (
93 $module =~ s{\b(\w+)/\1\b}{$1},
94 $module =~ s{^B/O}{O},
95 $module =~ s{^Devel-PPPort}{Devel},
3eae08df 96 $module =~ s{^libnet/}{},
dc2f75c0
JV
97 $module =~ s{^Encode/encoding}{encoding},
98 $module =~ s{^IPC-SysV/}{IPC/},
99 $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
d9c6e45d
CBW
100 $module =~ s{^(?:DynaLoader|Errno|Opcode|XSLoader)/}{},
101 $module =~ s{^Sys-Syslog/win32}{Sys-Syslog},
102 $module =~ s{^Time-Piece/Seconds}{Time/Seconds},
dc2f75c0 103 );
f4ccb67a 104 $module =~ s{^vms/ext}{VMS};
71c80a8f 105 $module =~ s{^lib/}{}g;
dc2f75c0
JV
106 $module =~ s{/}{::}g;
107 $module =~ s{-}{::}g;
71c80a8f 108 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
dc2f75c0
JV
109 $module =~ s/(\.pm|_pm\.PL)$//;
110 $lines{$module} = $version;
111 $module_to_file{$module} = $File::Find::name;
112 },
71c80a8f
JV
113 'vms/ext',
114 'symbian/ext',
dc2f75c0
JV
115 'lib',
116 'ext',
3eae08df 117 'cpan',
71c80a8f 118 'dist'
dc2f75c0 119);
59189dd7 120
0fdd9e5c 121-e 'configpm' and $lines{Config} = 'undef';
cc8432b2 122
dc2f75c0
JV
123if ( open my $ucdv, "<", "lib/unicore/version" ) {
124 chomp( my $ucd = <$ucdv> );
0fdd9e5c 125 $lines{Unicode} = "'$ucd'";
59189dd7 126 close $ucdv;
dc2f75c0 127}
fb237dfd 128
dc2f75c0
JV
129my $versions_in_release = " " . $perl_vnum . " => {\n";
130foreach my $key ( sort keys %lines ) {
131 $versions_in_release .= sprintf "\t%-24s=> %s,\n", "'$key'", $lines{$key};
0fdd9e5c 132}
dc2f75c0 133$versions_in_release .= " },\n";
fb237dfd 134
dc2f75c0 135$corelist =~ s/^(%version\s*=\s*.*?)(^\);)$/$1$versions_in_release$2/xism;
fb237dfd
NC
136
137exit unless %modlist;
138
139# We have to go through this two stage lookup, given how Maintainers.pl keys its
140# data by "Module", which is really a dist.
dc2f75c0 141my $file_to_M = files_to_modules( values %module_to_file );
fb237dfd
NC
142
143my %module_to_upstream;
144my %module_to_dist;
145my %dist_to_meta_YAML;
a762e054 146my %module_to_deprecated;
dc2f75c0 147while ( my ( $module, $file ) = each %module_to_file ) {
fb237dfd
NC
148 my $M = $file_to_M->{$file};
149 next unless $M;
150 next if $Modules{$M}{MAINTAINER} eq 'p5p';
151 $module_to_upstream{$module} = $Modules{$M}{UPSTREAM};
a762e054 152 $module_to_deprecated{$module} = 1 if $Modules{$M}{DEPRECATED};
dc2f75c0
JV
153 next
154 if defined $module_to_upstream{$module}
155 && $module_to_upstream{$module} =~ /^(?:blead|first-come)$/;
fb237dfd
NC
156 my $dist = $modlist{$module};
157 unless ($dist) {
dc2f75c0
JV
158 warn "Can't find a distribution for $module\n";
159 next;
fb237dfd
NC
160 }
161 $module_to_dist{$module} = $dist;
162
163 next if exists $dist_to_meta_YAML{$dist};
164
165 $dist_to_meta_YAML{$dist} = undef;
166
167 # Like it or lump it, this has to be Unix format.
dc2f75c0 168 my $meta_YAML_path = "authors/id/$dist";
fd48f21c 169 $meta_YAML_path =~ s/(?:tar\.gz|tar\.bz2|zip)$/meta/ or die "$meta_YAML_path";
dc2f75c0
JV
170 my $meta_YAML_url = 'http://ftp.funet.fi/pub/CPAN/' . $meta_YAML_path;
171
172 if ( -e "$cpan/$meta_YAML_path" ) {
173 $dist_to_meta_YAML{$dist} = Parse::CPAN::Meta::LoadFile( $cpan . "/" . $meta_YAML_path );
174 } elsif ( my $content = fetch_url($meta_YAML_url) ) {
175 unless ($content) {
176 warn "Failed to fetch $meta_YAML_url\n";
177 next;
178 }
179 eval { $dist_to_meta_YAML{$dist} = Parse::CPAN::Meta::Load($content); };
180 if ( my $err = $@ ) {
181 warn "$meta_YAML_path: ".$err;
182 next;
183 }
184 } else {
185 warn "$meta_YAML_path does not exist for $module\n";
186
187 # I tried code to open the tarballs with Archive::Tar to find and
188 # extract META.yml, but only Text-Tabs+Wrap-2006.1117.tar.gz had one,
189 # so it's not worth including.
190 next;
fb237dfd 191 }
fb237dfd
NC
192}
193
dc2f75c0
JV
194my $upstream_stanza = "%upstream = (\n";
195foreach my $module ( sort keys %module_to_upstream ) {
196 my $upstream = defined $module_to_upstream{$module} ? "'$module_to_upstream{$module}'" : 'undef';
197 $upstream_stanza .= sprintf " %-24s=> %s,\n", "'$module'", $upstream;
fb237dfd 198}
dc2f75c0
JV
199$upstream_stanza .= ");";
200
201$corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx;
fb237dfd 202
a762e054
DG
203# Deprecation generation
204my $deprecated_stanza = " " . $perl_vnum . " => {\n";
205foreach my $module ( sort keys %module_to_deprecated ) {
206 my $deprecated = defined $module_to_deprecated{$module} ? "'$module_to_deprecated{$module}'" : 'undef';
207 $deprecated_stanza .= sprintf "\t%-24s=> %s,\n", "'$module'", $deprecated;
208}
209$deprecated_stanza .= " },\n";
210$corelist =~ s/^(%deprecated\s*=\s*.*?)(^\);)$/$1$deprecated_stanza$2/xism;
211
dc2f75c0
JV
212my $tracker = "%bug_tracker = (\n";
213foreach my $module ( sort keys %module_to_upstream ) {
fb237dfd 214 my $upstream = defined $module_to_upstream{$module};
dc2f75c0
JV
215 next
216 if defined $upstream
217 and $upstream eq 'blead' || $upstream eq 'first-come';
fb237dfd
NC
218
219 my $bug_tracker;
220
221 my $dist = $module_to_dist{$module};
222 $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker}
dc2f75c0 223 if $dist;
fb237dfd
NC
224
225 $bug_tracker = defined $bug_tracker ? "'$bug_tracker'" : 'undef';
71c80a8f 226 next if $bug_tracker eq "'http://rt.perl.org/perlbug/'";
dc2f75c0
JV
227 $tracker .= sprintf " %-24s=> %s,\n", "'$module'", $bug_tracker;
228}
229$tracker .= ");";
230
231$corelist =~ s/^%bug_tracker .*? ;/$tracker/eismx;
232
233unless ( $corelist =~ /and $perl_vstring releases of perl/ ) {
234 warn "Adding $perl_vstring to the list of perl versions covered by Module::CoreList\n";
c99cfaa7 235 $corelist =~ s/(currently covers (?:.*?))\s*and (.*?) releases of perl/$1, $2 and $perl_vstring releases of perl/ism;
dc2f75c0
JV
236}
237
238unless (
c99cfaa7
FR
239 $corelist =~ /^%released \s* = \s* \(
240 .*?
241 $perl_vnum => .*?
dc2f75c0
JV
242 \);/ismx
243 )
244{
245 warn "Adding $perl_vnum to the list of released perl versions. Please consider adding a release date.\n";
246 $corelist =~ s/^(%released \s* = \s* .*?) ( \) )
fd48f21c 247 /$1 $perl_vnum => '????-??-??',\n $2/ismx;
dc2f75c0
JV
248}
249
250write_corelist($corelist);
251
2ce7d676 252warn "All done. Please check over $corelist_file carefully before committing. Thanks!\n";
dc2f75c0
JV
253
254
255sub write_corelist {
256 my $content = shift;
54233949
LB
257 open (my $clfh, ">", $corelist_file);
258 print $clfh $content;
dc2f75c0
JV
259 close($clfh);
260}
261
262sub fetch_url {
263 my $url = shift;
264 eval { require LWP::Simple };
265 if ( LWP::Simple->can('get') ) {
1af4ba53 266 return LWP::Simple::get($url);
dc2f75c0
JV
267 } elsif (`which curl`) {
268 return `curl -s $url`;
269 } elsif (`which wget`) {
270 return `wget -q -O - $url`;
271 }
fb237dfd 272}