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