-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathRelease.pm
581 lines (485 loc) · 16 KB
/
Release.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package MetaCPAN::Model::Release;
use Moose;
use v5.10;
use CPAN::DistnameInfo ();
use CPAN::Meta ();
use DateTime ();
use File::Find ();
use File::Spec ();
use Log::Contextual::Easy::Default qw( :log :dlog );
use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Model::Archive ();
use MetaCPAN::Types::TypeTiny qw( AbsPath ArrayRef InstanceOf Str );
use MetaCPAN::Util qw( fix_version true false );
use Module::Metadata 1.000012 (); # Improved package detection.
use MooseX::StrictConstructor;
use Parse::PMFile ();
use Path::Tiny qw( path );
use Try::Tiny qw( catch try );
has archive => (
is => 'ro',
isa => InstanceOf ['MetaCPAN::Model::Archive'],
lazy => 1,
builder => '_build_archive',
);
has dependencies => (
is => 'ro',
isa => ArrayRef,
lazy => 1,
builder => '_build_dependencies',
);
has distinfo => (
is => 'ro',
isa => InstanceOf ['CPAN::DistnameInfo'],
handles => {
maturity => 'maturity',
author => 'cpanid',
name => 'distvname',
distribution => 'dist',
filename => 'filename',
},
lazy => 1,
default => sub {
my $self = shift;
return CPAN::DistnameInfo->new( $self->file );
},
);
has document => (
is => 'ro',
isa => InstanceOf ['MetaCPAN::Document::Release'],
lazy => 1,
builder => '_build_document',
);
has file => (
is => 'ro',
isa => AbsPath,
required => 1,
coerce => 1,
);
has files => (
is => 'ro',
isa => ArrayRef,
init_arg => undef,
lazy => 1,
builder => '_build_files',
);
has date => (
is => 'ro',
isa => InstanceOf ['DateTime'],
lazy => 1,
default => sub {
my $self = shift;
return DateTime->from_epoch( epoch => $self->file->stat->mtime );
},
);
has model => ( is => 'ro' );
has metadata => (
is => 'ro',
isa => InstanceOf ['CPAN::Meta'],
lazy => 1,
builder => '_build_metadata',
);
has modules => (
is => 'ro',
isa => ArrayRef,
lazy => 1,
default => sub {
my $self = shift;
if ( keys %{ $self->metadata->provides } ) {
return $self->_modules_from_meta;
}
else {
return $self->_modules_from_files;
}
},
);
has version => (
is => 'ro',
isa => Str,
lazy => 1,
default => sub {
return fix_version( shift->distinfo->version );
},
);
has status => (
is => 'ro',
isa => Str,
);
has es => ( is => 'ro' );
has bulk => ( is => 'ro' );
=head2 run
Try to fix some ordering issues, which are causing deep recursion. There's
probably a much cleaner way to do this.
=cut
sub run {
my $self = shift;
$self->document;
$self->document->_set_changes_file(
$self->get_changes_file( $self->files ) );
$self->set_main_module( $self->modules, $self->document );
}
sub _build_archive {
my $self = shift;
log_info { 'Processing ', $self->file };
my $archive = MetaCPAN::Model::Archive->new( file => $self->file );
log_error { $self->file, ' is being impolite' } if $archive->is_impolite;
log_error { $self->file, ' is being naughty' } if $archive->is_naughty;
return $archive;
}
sub _build_dependencies {
my $self = shift;
my $meta = $self->metadata;
log_debug {'Gathering dependencies'};
my @dependencies;
if ( my $prereqs = $meta->prereqs ) {
while ( my ( $phase, $data ) = each %$prereqs ) {
while ( my ( $relationship, $v ) = each %$data ) {
while ( my ( $module, $version ) = each %$v ) {
push(
@dependencies,
Dlog_trace {"adding dependency $_"} +{
phase => $phase,
relationship => $relationship,
module => $module,
version => $version,
}
);
}
}
}
}
log_debug { 'Found ', scalar @dependencies, ' dependencies' };
return \@dependencies;
}
sub _build_document {
my $self = shift;
my $st = $self->file->stat;
my $stat = { map { $_ => $st->$_ } qw(mode size mtime) };
my $meta = $self->metadata;
my $dependencies = $self->dependencies;
my $document = DlogS_trace {"adding release $_"} +{
abstract => MetaCPAN::Util::strip_pod( $meta->abstract ),
archive => $self->filename,
author => $self->author,
checksum_md5 => $self->archive->file_digest_md5,
checksum_sha256 => $self->archive->file_digest_sha256,
date => $self->date . q{},
dependency => $dependencies,
distribution => $self->distribution,
# CPAN::Meta->license *must* be called in list context
# (and *may* return multiple strings).
license => [ $meta->license ],
maturity => $self->maturity,
metadata => $meta,
name => $self->name,
provides => [],
stat => $stat,
status => $self->status,
# Call in scalar context to make sure we only get one value (building a hash).
( map { ( $_ => scalar $meta->$_ ) } qw( version resources ) ),
};
delete $document->{abstract}
if ( $document->{abstract} eq 'unknown'
|| $document->{abstract} eq 'null' );
$document
= $self->model->doc('release')->put( $document, { refresh => true } );
# create distribution if doesn't exist
$self->es->update(
es_doc_path('distribution'),
id => $self->distribution,
body => {
doc => {
name => $self->distribution,
},
doc_as_upsert => true,
},
);
return $document;
}
sub set_main_module {
my $self = shift;
my ( $mod, $release ) = @_;
# Only select modules (files) that have modules (packages).
my @modules = grep { scalar @{ $_->module } } @$mod;
return unless @modules;
my $dist2module = $release->distribution;
$dist2module =~ s{-}{::}g;
if ( scalar @modules == 1 ) {
# there is only one module and it will become the main_module
$release->_set_main_module( $modules[0]->module->[0]->name );
return;
}
foreach my $file (@modules) {
# the module has the exact name as the ditribution
if ( $file->module->[0]->name eq $dist2module ) {
$release->_set_main_module( $file->module->[0]->name );
return;
}
}
# the distribution has modules on different levels
# the main_module is the first one with the minimum level
# or if they are on the same level, the one with the shortest name
my @sorted_modules = sort {
$a->level <=> $b->level
|| length $a->module->[0]->name <=> length $b->module->[0]->name
} @modules;
$release->_set_main_module( $sorted_modules[0]->module->[0]->name );
}
my @changes_files = qw(
CHANGELOG
ChangeLog
Changelog
CHANGES
Changes
NEWS
);
my @exclude_dirs = qw(
corpus
fatlib
inc
local
perl5
share
t
xt
);
# this should match the same set of files as MetaCPAN::Query::File->interesting_files
my ($changes_match) = map qr/^(?:$_)$/, join '|',
( map quotemeta, @changes_files, map "$_.md", @changes_files ),
(
"(?:(?!"
. join( '|', map "$_/", @exclude_dirs )
. ").*/)?(?:"
. join(
'|', map quotemeta, map +( "$_.pm", "$_.pod" ), @changes_files
)
. ')'
);
sub get_changes_file {
my $self = shift;
my @files = @{ $_[0] };
if ( $files[0]->distribution eq 'perl' ) {
foreach my $file (@files) {
if ( $file->name eq 'perldelta.pod' ) {
return $file->path;
}
}
}
# prioritize files in the top level but otherwise alphabetical
@files = sort { $a->level <=> $b->level || $a->path cmp $b->path } @files;
foreach my $file (@files) {
return $file->path if $file->path =~ $changes_match;
}
}
sub _build_files {
my $self = shift;
my @files;
log_debug { 'Indexing ', scalar @{ $self->archive->files }, ' files' };
my $file_set = $self->model->doc('file');
my $extract_dir = $self->extract;
File::Find::find(
sub {
my $child = path($File::Find::name);
return if $self->_is_broken_file($File::Find::name);
my $relative = $child->relative($extract_dir);
my $stat = do {
my $s = $child->stat;
+{ map { $_ => $s->$_ } qw(mode size mtime) };
};
return if ( $relative eq q{.} );
( my $fpath = "$relative" ) =~ s/^.*?\///;
my $filename = $fpath;
$child->is_dir
? $filename =~ s/^(.*\/)?(.+?)\/?$/$2/
: $filename =~ s/.*\///;
$fpath = q{} if $relative !~ /\// && !$self->archive->is_impolite;
my $file = $file_set->new_document(
Dlog_trace {"adding file $_"} +{
author => $self->author,
binary => -B $child ? true : false,
content => $child->is_dir ? \""
: \( scalar $child->slurp ),
date => $self->date,
directory => $child->is_dir ? true : false,
distribution => $self->distribution,
indexed => $self->metadata->should_index_file($fpath)
? true
: false,
local_path => $child,
maturity => $self->maturity,
metadata => $self->metadata,
name => $filename,
path => $fpath,
release => $self->name,
download_url => $self->document->download_url,
stat => $stat,
status => $self->status,
version => $self->version,
}
);
$self->bulk->put($file);
push( @files, $file );
},
$extract_dir
);
$self->bulk->commit;
return \@files;
}
my @always_no_index_dirs = (
# Always ignore the same dirs as PAUSE (lib/PAUSE/dist.pm):
## skip "t" - libraries in ./t are test libraries!
## skip "xt" - libraries in ./xt are author test libraries!
## skip "inc" - libraries in ./inc are usually install libraries
## skip "local" - somebody shipped his carton setup!
## skip 'perl5" - somebody shipped her local::lib!
## skip 'fatlib' - somebody shipped their fatpack lib!
qw( t xt inc local perl5 fatlib ),
# and add a few more
qw( example blib examples eg ),
);
sub _build_metadata {
my $self = shift;
my $extract_dir = $self->extract;
return $self->_load_meta_file || CPAN::Meta->new( {
license => 'unknown',
name => $self->distribution,
no_index => { directory => [@always_no_index_dirs] },
version => $self->version || 0,
} );
}
sub _load_meta_file {
my $self = shift;
my $extract_dir = $self->extract;
my @files;
for (qw{*/META.json */META.yml */META.yaml META.json META.yml META.yaml})
{
# scalar context globbing (without exhausting results) produces
# confusing results (which caused existsing */META.json files to
# get skipped). using list context seems more reliable.
my ($path) = <$extract_dir/$_>;
push( @files, $path ) if ( $path && -e $path );
}
return unless (@files);
my $last;
for my $file (@files) {
try {
$last = CPAN::Meta->load_file($file);
}
catch {
log_warn {"META file ($file) could not be loaded: $_"};
};
if ($last) {
last;
}
}
if ($last) {
push( @{ $last->{no_index}->{directory} }, @always_no_index_dirs );
return $last;
}
log_warn {'No META files could be loaded'};
}
sub extract {
my $self = shift;
log_debug {'Extracting archive to filesystem'};
return $self->archive->extract;
}
sub _is_broken_file {
my $self = shift;
my $filename = shift;
return 1 if ( -p $filename || !-e $filename );
if ( -l $filename ) {
my $syml = readlink $filename;
return 1 if ( !-e $filename && !-l $filename );
}
return 0;
}
sub _modules_from_meta {
my $self = shift;
my @modules;
my $provides = $self->metadata->provides;
my $files = $self->files;
foreach my $module ( sort keys %$provides ) {
my $data = $provides->{$module};
my $path = File::Spec->canonpath( $data->{file} );
# Obey no_index and take the shortest path if multiple files match.
my ($file) = sort { length( $a->path ) <=> length( $b->path ) }
grep { $_->indexed && $_->path =~ /\Q$path\E$/ } @$files;
next unless $file;
$file->add_module( {
name => $module,
version => $data->{version},
indexed => true,
} );
push( @modules, $file );
}
return \@modules;
}
sub _modules_from_files {
my $self = shift;
my @modules;
my @perl_files = grep { $_->name =~ m{(?:\.pm|\.pm\.PL)\z} }
grep { $_->indexed } @{ $self->files };
foreach my $file (@perl_files) {
if ( $file->name =~ m{\.PL\z} ) {
my $parser = Parse::PMFile->new( $self->metadata->as_struct );
# FIXME: Should there be a timeout on this
# (like there is below for Module::Metadata)?
my $info = $parser->parse( $file->local_path );
next if !$info;
foreach my $module_name ( keys %{$info} ) {
$file->add_module( {
name => $module_name,
defined $info->{$module_name}->{version}
? ( version => $info->{$module_name}->{version} )
: (),
} );
}
push @modules, $file;
}
else {
eval {
local $SIG{'ALRM'} = sub {
log_error {'Call to Module::Metadata timed out '};
die;
};
alarm(50);
my $info;
{
local $SIG{__WARN__} = sub { };
$info = Module::Metadata->new_from_file(
$file->local_path );
}
# Ignore packages that people cannot claim.
# https://github.com/andk/pause/blob/master/lib/PAUSE/pmfile.pm#L236
#
# Parse::PMFile and PAUSE translate apostrophes to double colons,
# but Module::Metadata does not.
my @packages
= map s{'}{::}gr,
grep { $_ ne 'main' && $_ ne 'DB' }
$info->packages_inside;
for my $pkg (@packages) {
my $version = $info->version($pkg);
$file->add_module( {
name => $pkg,
defined $version
# Stringify if it's a version object, otherwise fall back to stupid stringification
# Changes in Module::Metadata were causing inconsistencies in the return value,
# we are just trying to survive.
? (
version => ref $version eq 'version'
? $version->stringify
: ( $version . q{} )
)
: ()
} );
}
push( @modules, $file );
alarm(0);
};
}
}
return \@modules;
}
__PACKAGE__->meta->make_immutable();
1;