forked from timbunce/Dist-Surveyor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MakeCpan.pm
363 lines (283 loc) · 11 KB
/
MakeCpan.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package Dist::Surveyor::MakeCpan;
use strict;
use warnings;
use Carp; # core
use Data::Dumper; # core
use File::Path; # core
use CPAN::DistnameInfo;
use File::Basename qw{dirname}; # core
use LWP::Simple qw{is_error mirror};
use LWP::UserAgent;
use Dist::Surveyor::Inquiry;
use List::Util qw(max); # core
our $verbose;
*verbose = \$::VERBOSE;
sub new {
my ($class, $cpan_dir, $progname, $irregularities) = @_;
require Compress::Zlib;
mkpath("$cpan_dir/modules");
# --- write extra data files that may be useful XXX may change
# XXX these don't all (yet?) merge with existing data
my $survey_datadump_dir = "$cpan_dir/$progname";
mkpath($survey_datadump_dir);
# Write list of releases, like default stdout
open my $rel_fh, ">", "$survey_datadump_dir/releases.txt";
# dump the primary result data for additional info and debugging
my $gzwrite = Compress::Zlib::gzopen("$survey_datadump_dir/_data_dump.perl.gz", 'wb')
or croak "Cannot open $survey_datadump_dir/_data_dump.perl.gz for writing: " . $Compress::Zlib::gzerrno;
$gzwrite->gzwrite("[\n");
my $self = {
errors => 0,
cpan_dir => $cpan_dir,
irregularities => $irregularities,
pkg_ver_rel => {}, # for 02packages
progname => $progname,
rel_fh => $rel_fh,
gzwrite => $gzwrite,
};
return bless $self, $class;
}
sub close {
my $self = shift;
# --- write 02packages file
my $pkg_lines = _readpkgs($self->{cpan_dir});
my %packages;
for my $line (@$pkg_lines, map { $_->{line} } values %{ $self->{pkg_ver_rel} }) {
my ($pkg) = split(/\s+/, $line, 2);
if ($packages{$pkg} and $packages{$pkg} ne $line) {
warn "Old $packages{$pkg}\nNew $line\n" if $verbose;
}
$packages{$pkg} = $line;
};
_writepkgs($self->{cpan_dir}, [ sort values %packages ] );
# Write list of token packages - each should match only one release.
# This makes it _much_ faster to do installs via cpanm because it
# can skip the modules it knows are installed (whereas using a list of
# distros it has to reinstall _all_ of them every time).
# XXX maybe add as a separate option: "--mainpkgs mainpkgs.lst"
my %dist_packages;
while ( my ($pkg, $line) = each %packages) {
my $distpath = (split /\s+/, $line)[2];
$dist_packages{$distpath}{$pkg}++;
}
my %token_package;
my %token_package_pri = ( # alter install order for some modules
'Module::Build' => 100, # should be near first
Moose => 50,
# install distros that use Module::Install late so their dependencies
# have already been resolved (else they try to fetch them directly,
# bypassing our cpanm --mirror-only goal)
'Olson::Abbreviations' => -90,
# distros with special needs
'Term::ReadKey' => -100, # tests hang if run in background
);
for my $distpath (sort keys %dist_packages) {
my $dp = $dist_packages{$distpath};
my $di = CPAN::DistnameInfo->new($distpath);
#warn Dumper([ $distpath, $di->dist, $di]);
(my $token_pkg = $di->dist) =~ s/-/::/g;
if (!$dp->{$token_pkg}) {
if (my $keypkg = $self->{irregularities}->{$di->dist}) {
$token_pkg = $keypkg;
}
else {
# XXX not good - may pick a dummy test package
$token_pkg = (grep { $_ } keys %$dp)[0] || $token_pkg;
warn "Picked $token_pkg as token package for ".$di->distvname."\n";
}
}
$token_package{$token_pkg} = $token_package_pri{$token_pkg} || 0;
}
my @main_pkgs = sort { $token_package{$b} <=> $token_package{$a} or $a cmp $b } keys %token_package;
open my $key_pkg_fh, ">", join('/', $self->{cpan_dir}, $self->{progname}, "token_packages.txt");
print $key_pkg_fh "$_\n" for @main_pkgs;
close $key_pkg_fh;
close $self->{rel_fh};
$self->{gzwrite}->gzwrite("]\n");
$self->{gzwrite}->gzclose;
warn $self->{cpan_dir}." updated.\n";
return $self->{errors};
}
sub add_release {
my ($self, $ri) = @_;
# --- get the file
my $main_url = $ri->{download_url};
my $di = distname_info_from_url($main_url);
my $pathfile = "authors/id/".$di->pathname;
my $destfile = $self->{cpan_dir}."/$pathfile";
mkpath(dirname($destfile));
my @urls = ($main_url);
for my $mirror ('http://backpan.perl.org') {
push @urls, "$mirror/$pathfile";
}
my $mirror_status;
for my $url (@urls) {
$mirror_status = eval { mirror($url, $destfile) };
last if not is_error($mirror_status||500);
}
if ($@ || is_error($mirror_status)) {
my $err = ($@ and chomp $@) ? $@ : $mirror_status;
my $msg = "Error $err mirroring $main_url";
if (-f $destfile) {
warn "$msg - using existing file\n";
}
else {
# better to keep going and add the packages to the index
# than abort at this stage due to network/mirror problems
# the user can drop the files in later
warn "$msg - continuing, ADD FILE MANUALLY!\n";
$self->{errors}++;
}
}
else {
warn "$mirror_status $main_url\n" if $verbose;
}
my $mods_in_rel = get_module_versions_in_release($ri->{author}, $ri->{name});
if (!keys %$mods_in_rel) { # XXX hack for common::sense
(my $dist_as_pkg = $ri->{distribution}) =~ s/-/::/g;
warn "$ri->{author}/$ri->{name} has no modules! Adding fake module $dist_as_pkg ".$di->version."\n";
$mods_in_rel->{$dist_as_pkg} = {
name => $dist_as_pkg,
version => $di->version,
version_obj => version->parse($di->version),
};
}
# --- accumulate package info for 02packages file
for my $pkg (sort keys %$mods_in_rel ) {
# pi => { name=>, version=>, version_obj=> }
my $pi = $mods_in_rel->{$pkg};
# for selecting which dist a package belongs to
# XXX should factor in authorization status
my $p_r_match_score = p_r_match_score($pkg, $ri);
if (my $pvr = $self->{pkg_ver_rel}->{$pkg}) {
# already seen same package name in different distribution
if ($p_r_match_score < $pvr->{p_r_match_score}) {
warn "$pkg seen in $pvr->{ri}{name} so ignoring one in $ri->{name}\n";
next;
}
warn "$pkg seen in $pvr->{ri}{name} - now overridden by $ri->{name}\n";
}
my $line = _fmtmodule($pkg, $di->pathname, $pi->{version});
$self->{pkg_ver_rel}->{$pkg} = { line => $line, pi => $pi, ri => $ri, p_r_match_score => $p_r_match_score };
}
printf { $self->{rel_fh} } "%s\n", ( exists $ri->{url} ? $ri->{url} : "?url" );
$self->{gzwrite}->gzwrite(Dumper($ri));
$self->{gzwrite}->gzwrite(",");
}
sub p_r_match_score {
my ($pkg_name, $ri) = @_;
my @p = split /\W/, $pkg_name;
my @r = split /\W/, $ri->{name};
for my $i (0..max(scalar @p, scalar @r)) {
return $i if not defined $p[$i]
or not defined $r[$i]
or $p[$i] ne $r[$i]
}
die; # unreached
}
# copied from CPAN::Mini::Inject and hacked
sub _readpkgs {
my ($cpandir) = @_;
my $packages_file = $cpandir.'/modules/02packages.details.txt.gz';
return [] if not -f $packages_file;
my $gzread = Compress::Zlib::gzopen($packages_file, 'rb')
or croak "Cannot open $packages_file: " . $Compress::Zlib::gzerrno . "\n";
my $inheader = 1;
my @packages;
my $package;
while ( $gzread->gzreadline( $package ) ) {
if ( $inheader ) {
$inheader = 0 unless $package =~ /\S/;
next;
}
chomp $package;
push @packages, $package;
}
$gzread->gzclose;
return \@packages;
}
sub _writepkgs {
my ($cpandir, $pkgs) = @_;
my $packages_file = $cpandir.'/modules/02packages.details.txt.gz';
my $gzwrite = Compress::Zlib::gzopen($packages_file, 'wb')
or croak "Cannot open $packages_file for writing: " . $Compress::Zlib::gzerrno;
$gzwrite->gzwrite( "File: 02packages.details.txt\n" );
$gzwrite->gzwrite(
"URL: http://www.perl.com/CPAN/modules/02packages.details.txt\n"
);
$gzwrite->gzwrite(
'Description: Package names found in directory $CPAN/authors/id/'
. "\n" );
$gzwrite->gzwrite( "Columns: package name, version, path\n" );
$gzwrite->gzwrite(
"Intended-For: Automated fetch routines, namespace documentation.\n"
);
$gzwrite->gzwrite( "Written-By: $0 0.001\n" ); # XXX TODO
$gzwrite->gzwrite( "Line-Count: " . scalar( @$pkgs ) . "\n" );
# Last-Updated: Sat, 19 Mar 2005 19:49:10 GMT
my @date = split( /\s+/, scalar( gmtime ) );
$gzwrite->gzwrite( "Last-Updated: $date[0], $date[2] $date[1] $date[4] $date[3] GMT\n\n" );
$gzwrite->gzwrite( "$_\n" ) for ( @$pkgs );
$gzwrite->gzclose;
}
sub distname_info_from_url {
my ($url) = @_;
$url =~ s{.* \b authors/id/ }{}x
or warn "No authors/ in '$url'\n";
my $di = CPAN::DistnameInfo->new($url);
return $di;
}
sub _fmtmodule {
my ( $module, $file, $version ) = @_;
$version = "undef" if not defined $version;
my $fw = 38 - length $version;
$fw = length $module if $fw < length $module;
return sprintf "%-${fw}s %s %s", $module, $version, $file;
}
sub errors {
my $self = shift;
return $self->{errors};
}
1;
=head1 NAME
Dist::Surveyor::MakeCpan - Create a Mini-CPAN for the surveyed modules
=head1 SYNOPSIS
use Dist::Surveyor::MakeCpan;
my $cpan = Dist::Surveyor::MakeCpan->new(
$cpan_dir, $progname, $irregularities);
foreach my $rel (@releases) {
$cpan->add_release($rel);
}
$cpan->close();
say "There where ", $cpan->errors(), " errors";
=head1 DESCRIPTION
Create a mini-CPAN for the surveyed modules, so you will be able to re-install
the same setup in a new computer.
=head1 CONSTRUCTOR
my $cpan = Dist::Surveyor::MakeCpan->new(
$cpan_dir, $progname, $irregularities, $verbose);
=over
=item $cpan_dir
The directory where the mini-cpan will be created
=item $progname
The name of the running program - will be used to create a subdirectory
inside $cpan_dir, that will contain debug information.
=item $irregularities
A hashref with a list of irregular named releases. i.e. 'libwww-perl' => 'LWP'.
=back
=head1 METHODS
=head2 $cpan->add_release($rel)
Add one release to the mini-cpan. the $rel should be a hashref,
and contain the following fields:
$rel = {
download_url => 'http://cpan.metacpan.org/authors/id/S/SE/SEMUELF/Dist-Surveyor-0.009.tar.gz',
url => 'authors/id/S/SE/SEMUELF/Dist-Surveyor-0.009.tar.gz',
author => 'SEMUELF',
name => 'Dist-Surveyor-0.009',
distribution => 'Dist-Surveyor',
}
=head2 $cpan->close()
Close the mini-CPAN, and close all the debug data dump files.
=head1 License, Copyright
Please see L<Dist::Surveyor> for details
=cut