Fix Pod errors spotted by podcheck.t
[p5sagit/p5-mst-13.2.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 = 'dist/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|cpan|dist|(?: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{^libnet/}{},
94             $module =~ s{^Encode/encoding}{encoding},
95             $module =~ s{^IPC-SysV/}{IPC/},
96             $module =~ s{^MIME-Base64/QuotedPrint}{MIME/QuotedPrint},
97             $module =~ s{^(?:DynaLoader|Errno|Opcode)/}{},
98             );
99                 $module =~ s{^lib/}{}g;
100         $module =~ s{/}{::}g;
101         $module =~ s{-}{::}g;
102                 $module =~ s{^.*::lib::}{}; # turns Foo/lib/Foo.pm into Foo.pm
103         $module =~ s/(\.pm|_pm\.PL)$//;
104         $lines{$module}          = $version;
105         $module_to_file{$module} = $File::Find::name;
106     },
107     'vms/ext',
108     'symbian/ext',
109     'lib',
110     'ext',
111         'cpan',
112         'dist'
113 );
114
115 -e 'configpm' and $lines{Config} = 'undef';
116
117 if ( open my $ucdv, "<", "lib/unicore/version" ) {
118     chomp( my $ucd = <$ucdv> );
119     $lines{Unicode} = "'$ucd'";
120     close $ucdv;
121 }
122
123 my $versions_in_release = "    " . $perl_vnum . " => {\n";
124 foreach my $key ( sort keys %lines ) {
125     $versions_in_release .= sprintf "\t%-24s=> %s,\n", "'$key'", $lines{$key};
126 }
127 $versions_in_release .= "    },\n";
128
129 $corelist =~ s/^(%version\s*=\s*.*?)(^\);)$/$1$versions_in_release$2/xism;
130
131 exit unless %modlist;
132
133 # We have to go through this two stage lookup, given how Maintainers.pl keys its
134 # data by "Module", which is really a dist.
135 my $file_to_M = files_to_modules( values %module_to_file );
136
137 my %module_to_upstream;
138 my %module_to_dist;
139 my %dist_to_meta_YAML;
140 while ( my ( $module, $file ) = each %module_to_file ) {
141     my $M = $file_to_M->{$file};
142     next unless $M;
143     next if $Modules{$M}{MAINTAINER} eq 'p5p';
144     $module_to_upstream{$module} = $Modules{$M}{UPSTREAM};
145     next
146         if defined $module_to_upstream{$module}
147             && $module_to_upstream{$module} =~ /^(?:blead|first-come)$/;
148     my $dist = $modlist{$module};
149     unless ($dist) {
150         warn "Can't find a distribution for $module\n";
151         next;
152     }
153     $module_to_dist{$module} = $dist;
154
155     next if exists $dist_to_meta_YAML{$dist};
156
157     $dist_to_meta_YAML{$dist} = undef;
158
159     # Like it or lump it, this has to be Unix format.
160     my $meta_YAML_path = "authors/id/$dist";
161     $meta_YAML_path =~ s/(?:tar\.gz|zip)$/meta/ or die "$meta_YAML_path";
162     my $meta_YAML_url = 'http://ftp.funet.fi/pub/CPAN/' . $meta_YAML_path;
163
164     if ( -e "$cpan/$meta_YAML_path" ) {
165         $dist_to_meta_YAML{$dist} = Parse::CPAN::Meta::LoadFile( $cpan . "/" . $meta_YAML_path );
166     } elsif ( my $content = fetch_url($meta_YAML_url) ) {
167         unless ($content) {
168             warn "Failed to fetch $meta_YAML_url\n";
169             next;
170         }
171         eval { $dist_to_meta_YAML{$dist} = Parse::CPAN::Meta::Load($content); };
172         if ( my $err = $@ ) {
173             warn "$meta_YAML_path: ".$err;
174             next;
175         }
176     } else {
177         warn "$meta_YAML_path does not exist for $module\n";
178
179         # I tried code to open the tarballs with Archive::Tar to find and
180         # extract META.yml, but only Text-Tabs+Wrap-2006.1117.tar.gz had one,
181         # so it's not worth including.
182         next;
183     }
184 }
185
186 my $upstream_stanza = "%upstream = (\n";
187 foreach my $module ( sort keys %module_to_upstream ) {
188     my $upstream = defined $module_to_upstream{$module} ? "'$module_to_upstream{$module}'" : 'undef';
189     $upstream_stanza .= sprintf "    %-24s=> %s,\n", "'$module'", $upstream;
190 }
191 $upstream_stanza .= ");";
192
193 $corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx;
194
195 my $tracker = "%bug_tracker = (\n";
196 foreach my $module ( sort keys %module_to_upstream ) {
197     my $upstream = defined $module_to_upstream{$module};
198     next
199         if defined $upstream
200             and $upstream eq 'blead' || $upstream eq 'first-come';
201
202     my $bug_tracker;
203
204     my $dist = $module_to_dist{$module};
205     $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker}
206         if $dist;
207
208     $bug_tracker = defined $bug_tracker ? "'$bug_tracker'" : 'undef';
209         next if $bug_tracker eq "'http://rt.perl.org/perlbug/'";
210     $tracker .= sprintf "    %-24s=> %s,\n", "'$module'", $bug_tracker;
211 }
212 $tracker .= ");";
213
214 $corelist =~ s/^%bug_tracker .*? ;/$tracker/eismx;
215
216 unless ( $corelist =~ /and $perl_vstring releases of perl/ ) {
217     warn "Adding $perl_vstring to the list of perl versions covered by Module::CoreList\n";
218     $corelist =~ s/\s*and (.*?) releases of perl/, $1 and $perl_vstring releases of perl/ism;
219 }
220
221 unless (
222     $corelist =~ /^%released \s* = \s* \( 
223         .*? 
224         $perl_vnum => .*? 
225         \);/ismx
226     )
227 {
228     warn "Adding $perl_vnum to the list of released perl versions. Please consider adding a release date.\n";
229     $corelist =~ s/^(%released \s* = \s* .*?) ( \) )
230                 /$1 $perl_vnum => '????-??-??',\n  $2/ismx;
231 }
232
233 write_corelist($corelist);
234
235 warn "All done. Please check over $corelist_file carefully before committing. Thanks!\n";
236
237
238 sub write_corelist {
239     my $content = shift;
240     open (my $clfh, ">", $corelist_file) || die "Failed to open $corelist_file for writing: $!";
241     print $clfh $content || die "Failed to write the new CoreList.pm: $!";
242     close($clfh);
243 }
244
245 sub fetch_url {
246     my $url = shift;
247     eval { require LWP::Simple };
248     if ( LWP::Simple->can('get') ) {
249         return LWP::Simple->get($url);
250     } elsif (`which curl`) {
251         return `curl -s $url`;
252     } elsif (`which wget`) {
253         return `wget -q -O - $url`;
254     }
255 }