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