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