Commit | Line | Data |
4a656c5e |
1 | #!perl |
2 | # Generates info for Module::CoreList from this perl tree |
dc2f75c0 |
3 | # run this from the root of a perl tree |
e1018a69 |
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 |
10 | use strict; |
11 | use warnings; |
12 | use File::Find; |
13 | use ExtUtils::MM_Unix; |
dc2f75c0 |
14 | use version; |
fb237dfd |
15 | use lib "Porting"; |
16 | use Maintainers qw(%Modules files_to_modules); |
17 | use File::Spec; |
dc2f75c0 |
18 | use Parse::CPAN::Meta; |
4a656c5e |
19 | |
2ce7d676 |
20 | my $corelist_file = 'ext/Module-CoreList/lib/Module/CoreList.pm'; |
e1018a69 |
21 | |
59189dd7 |
22 | my %lines; |
fb237dfd |
23 | my %module_to_file; |
24 | my %modlist; |
e1018a69 |
25 | |
dc2f75c0 |
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//; |
fb237dfd |
33 | |
dc2f75c0 |
34 | if ( !-f 'MANIFEST' ) { |
35 | die "Must be run from the root of a clean perl tree\n"; |
e1018a69 |
36 | } |
37 | |
dc2f75c0 |
38 | open( my $corelist_fh, '<', $corelist_file ) || die "Could not open $corelist_file: $!"; |
39 | my $corelist = join( '', <$corelist_fh> ); |
40 | |
fb237dfd |
41 | if ($cpan) { |
dc2f75c0 |
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> ); |
fb237dfd |
63 | } |
64 | |
dc2f75c0 |
65 | die "Incompatible modlist format" |
66 | unless $content =~ /^Columns: +package name, version, path/m; |
67 | |
fb237dfd |
68 | # Converting the file to a hash is about 5 times faster than a regexp flat |
69 | # lookup. |
dc2f75c0 |
70 | for ( split( qr/\n/, $content ) ) { |
71 | next unless /^([A-Za-z_:0-9]+) +[-0-9.undefHASHVERSIONvsetwhenloadingbogus]+ +(\S+)/; |
72 | $modlist{$1} = $2; |
fb237dfd |
73 | } |
74 | } |
75 | |
dc2f75c0 |
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 | ); |
59189dd7 |
110 | |
0fdd9e5c |
111 | -e 'configpm' and $lines{Config} = 'undef'; |
cc8432b2 |
112 | |
dc2f75c0 |
113 | if ( open my $ucdv, "<", "lib/unicore/version" ) { |
114 | chomp( my $ucd = <$ucdv> ); |
0fdd9e5c |
115 | $lines{Unicode} = "'$ucd'"; |
59189dd7 |
116 | close $ucdv; |
dc2f75c0 |
117 | } |
fb237dfd |
118 | |
dc2f75c0 |
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}; |
0fdd9e5c |
122 | } |
dc2f75c0 |
123 | $versions_in_release .= " },\n"; |
fb237dfd |
124 | |
dc2f75c0 |
125 | $corelist =~ s/^(%version\s*=\s*.*?)(^\);)$/$1$versions_in_release$2/xism; |
fb237dfd |
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. |
dc2f75c0 |
131 | my $file_to_M = files_to_modules( values %module_to_file ); |
fb237dfd |
132 | |
133 | my %module_to_upstream; |
134 | my %module_to_dist; |
135 | my %dist_to_meta_YAML; |
dc2f75c0 |
136 | while ( my ( $module, $file ) = each %module_to_file ) { |
fb237dfd |
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}; |
dc2f75c0 |
141 | next |
142 | if defined $module_to_upstream{$module} |
143 | && $module_to_upstream{$module} =~ /^(?:blead|first-come)$/; |
fb237dfd |
144 | my $dist = $modlist{$module}; |
145 | unless ($dist) { |
dc2f75c0 |
146 | warn "Can't find a distribution for $module\n"; |
147 | next; |
fb237dfd |
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. |
dc2f75c0 |
156 | my $meta_YAML_path = "authors/id/$dist"; |
fb237dfd |
157 | $meta_YAML_path =~ s/(?:tar\.gz|zip)$/meta/ or die "$meta_YAML_path"; |
dc2f75c0 |
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; |
fb237dfd |
179 | } |
fb237dfd |
180 | } |
181 | |
dc2f75c0 |
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; |
fb237dfd |
186 | } |
dc2f75c0 |
187 | $upstream_stanza .= ");"; |
188 | |
189 | $corelist =~ s/^%upstream .*? ;$/$upstream_stanza/ismx; |
fb237dfd |
190 | |
dc2f75c0 |
191 | my $tracker = "%bug_tracker = (\n"; |
192 | foreach my $module ( sort keys %module_to_upstream ) { |
fb237dfd |
193 | my $upstream = defined $module_to_upstream{$module}; |
dc2f75c0 |
194 | next |
195 | if defined $upstream |
196 | and $upstream eq 'blead' || $upstream eq 'first-come'; |
fb237dfd |
197 | |
198 | my $bug_tracker; |
199 | |
200 | my $dist = $module_to_dist{$module}; |
201 | $bug_tracker = $dist_to_meta_YAML{$dist}->{resources}{bugtracker} |
dc2f75c0 |
202 | if $dist; |
fb237dfd |
203 | |
204 | $bug_tracker = defined $bug_tracker ? "'$bug_tracker'" : 'undef'; |
205 | next if $bug_tracker eq "'http://rt.perl.org/perlbug/'"; |
dc2f75c0 |
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 | |
2ce7d676 |
231 | warn "All done. Please check over $corelist_file carefully before committing. Thanks!\n"; |
dc2f75c0 |
232 | |
233 | |
234 | sub write_corelist { |
235 | my $content = shift; |
2ce7d676 |
236 | open (my $clfh, ">", $corelist_file) || die "Failed to open $corelist_file for writing: $!"; |
dc2f75c0 |
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 | } |
fb237dfd |
251 | } |