-
Notifications
You must be signed in to change notification settings - Fork 13
/
reporter.pm
489 lines (394 loc) · 13 KB
/
reporter.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
package App::cpanminus::reporter;
use warnings;
use strict;
our $VERSION = '0.13';
use Carp ();
use File::Spec 3.19;
use File::HomeDir 0.58 ();
use Test::Reporter 1.54;
use CPAN::Testers::Common::Client 0.10;
use CPAN::Testers::Common::Client::Config;
use Parse::CPAN::Meta;
use CPAN::Meta::Converter;
use Try::Tiny;
use URI;
use Metabase::Resource;
use Capture::Tiny qw(capture);
use IO::Prompt::Tiny ();
sub new {
my ($class, %params) = @_;
my $self = bless {}, $class;
$self->config(
CPAN::Testers::Common::Client::Config->new(
prompt => sub { local %ENV; IO::Prompt::Tiny::prompt(@_) },
)
);
if ($params{cpanm}) {
my $cpanm = $self->_cpanm( $params{cpanm} );
$params{only} =~ s/-\d+(\.\d+)*$//; # strip version from cpanm's "only" data
# FIXME: cpanm doesn't provide an accessor here, so
# we break encapsulation in order to make sure we
# always have the right paths.
$params{build_dir} = $cpanm->{home};
$params{build_logfile} = $cpanm->{log};
}
$self->build_dir(
$params{build_dir}
|| File::Spec->catdir( File::HomeDir->my_home, '.cpanm' )
);
$self->build_logfile(
$params{build_logfile}
|| File::Spec->catfile( $self->build_dir, 'build.log' )
);
foreach my $option ( qw(quiet verbose force exclude only dry-run skip-history) ) {
my $method = $option;
$method =~ s/\-/_/g;
$self->$method( $params{$option} ) if exists $params{$option};
}
return $self;
}
sub setup { shift->config->setup }
## basic accessors ##
sub author {
my ($self, $author) = @_;
$self->{_author} = $author if $author;
return $self->{_author};
}
sub distfile {
my ($self, $distfile) = @_;
$self->{_distfile} = $distfile if $distfile;
return $self->{_distfile};
}
sub config {
my ($self, $config) = @_;
$self->{_config} = $config if $config;
return $self->{_config};
}
sub verbose {
my ($self, $verbose) = @_;
$self->{_verbose} = $verbose if $verbose;
return $self->{_verbose};
}
sub force {
my ($self, $force) = @_;
$self->{_force} = $force if $force;
return $self->{_force};
}
sub quiet {
my ($self, $quiet) = @_;
if ($quiet) {
$self->verbose(0);
$self->{_quiet} = 1;
}
return $self->{_quiet};
}
sub dry_run {
my ($self, $dry_run) = @_;
$self->{_dry_run} = $dry_run if $dry_run;
$self->{_dry_run};
}
sub skip_history {
my ($self, $skip) = @_;
$self->{_skip_history} = $skip if $skip;
$self->{_skip_history};
}
sub only {
my ($self, $only) = @_;
if ($only) {
$only =~ s/::/-/g;
my @modules = split /\s*,\s*/, $only;
foreach (@modules) { $_ =~ s/(\S+)-[\d.]+$/$1/ };
$self->{_only} = { map { $_ => 0 } @modules };
}
return $self->{_only};
}
sub exclude {
my ($self, $exclude) = @_;
if ($exclude) {
$exclude =~ s/::/-/g;
my @modules = split /\s*,\s*/, $exclude;
foreach (@modules) { $_ =~ s/(\S+)-[\d.]+$/$1/ };
$self->{_exclude} = { map { $_ => 0 } @modules };
}
return $self->{_exclude};
}
sub build_dir {
my ($self, $dir) = @_;
$self->{_build_dir} = $dir if $dir;
return $self->{_build_dir};
}
sub build_logfile {
my ($self, $file) = @_;
$self->{_build_logfile} = $file if $file;
return $self->{_build_logfile};
}
sub _cpanm {
my ($self, $cpanm) = @_;
$self->{_cpanm_object} = $cpanm if $cpanm;
return $self->{_cpanm_object};
}
sub _check_cpantesters_config_data {
my $self = shift;
my $config = $self->config;
my $filename = $config->get_config_filename;
if (-e $filename) {
if (!$config->read) {
print "Error reading CPAN Testers configuration file '$filename'. Aborting.";
return;
}
}
else {
my $answer = IO::Prompt::Tiny::prompt("CPAN Testers configuration file '$filename' not found. Would you like to set it up now? (y/n)", 'y');
if ( $answer =~ /^y/i ) {
$config->setup;
}
else {
print "The CPAN Testers configuration file is required. Aborting.\n";
return;
}
}
return 1;
}
sub _check_build_log {
my $self = shift;
# as a safety mechanism, we only let people parse build.log files
# if they were generated up to 30 minutes (1800 seconds) ago,
# unless the user asks us to --force it.
my $mtime = (stat $self->build_logfile)[9];
if ( !$self->force && $mtime && time - $mtime > 1800 ) {
print <<'EOMESSAGE';
Fatal: build.log was created longer than 30 minutes ago.
As a standalone tool, it is important that you run cpanm-reporter
as soon as you finish cpanm, otherwise your system data may have
changed, from new libraries to a completely different perl binary.
Because of that, this app will *NOT* parse build.log files last modified
longer than 30 minutes before the moment it runs.
You can override this behaviour by touching the file or passing
a --force flag to cpanm-reporter, but please take good care to avoid
sending bogus reports.
EOMESSAGE
return;
}
return 1;
}
sub run {
my $self = shift;
return unless ($self->_check_cpantesters_config_data and $self->_check_build_log);
my $logfile = $self->build_logfile;
open my $fh, '<', $logfile
or Carp::croak "error opening build log file '$logfile' for reading: $!";
my $header = <$fh>;
if ($header =~ /^cpanm \(App::cpanminus\) (\d+\.\d+) on perl (\d+\.\d+)/) {
$self->{_cpanminus_version} = $1;
$self->{_perl_version} = $2;
}
my $found = 0;
my $parser;
# we could go over 100 levels deep on the dependency track
no warnings 'recursion';
$parser = sub {
my ($dist, $resource) = @_;
(my $dist_vstring = $dist) =~ s/\-(\d+(?:\.\d)+)$/-v$1/ if $dist;
my @test_output = ();
my $recording = 0;
my $str = '';
my $fetched;
while (<$fh>) {
if ( /^Fetching (\S+)/ ) {
next if /CHECKSUMS$/;
$fetched = $1;
$resource = $fetched unless $resource;
}
elsif ( /^Entering (\S+)/ ) {
my $dep = $1;
$found = 1;
Carp::croak 'Parsing error. This should not happen. Please send us a report!' if $recording;
Carp::croak "Parsing error. Found '$dep' without fetching first." unless $resource;
print "entering $dep, $fetched\n" if $self->verbose;
$parser->($dep, $fetched);
print "left $dep, $fetched\n" if $self->verbose;
next;
}
elsif ( $dist and /^Building and testing (?:$dist|$dist_vstring)/) {
print "recording $dist\n" if $self->verbose;
$recording = 1;
}
push @test_output, $_ if $recording;
if ( $recording and ( /^Result: (PASS|NA|FAIL|UNKNOWN)/ or /^-> (FAIL|OK)/ ) ) {
my $result = $1;
$result = 'PASS' if $result eq 'OK';
my $dist_without_version = $dist;
$dist_without_version =~ s/(\S+)-[\d.]+$/$1/;
if (@test_output <= 2) {
print "No test output found for '$dist'. Skipping...\n"
. "To send test reports, please make sure *NOT* to pass '-v' to cpanm or your build.log will contain no output to send.\n";
}
elsif ( defined $self->exclude && exists $self->exclude->{$dist_without_version} ) {
print "Skipping $dist as it's in the 'exclude' list...\n" if $self->verbose;
}
elsif ( defined $self->only && !exists $self->only->{$dist_without_version} ) {
print "Skipping $dist as it isn't in the 'only' list...\n" if $self->verbose;
}
else {
my $report = $self->make_report($resource, $dist, $result, @test_output);
}
return;
}
}
};
print "Parsing $logfile...\n" if $self->verbose;
$parser->();
print "No reports found.\n" if !$found and $self->verbose;
print "Finished.\n" if $self->verbose;
close $fh;
return;
}
sub get_author {
my ($self, $path ) = @_;
my $metadata;
try {
$metadata = Metabase::Resource->new( q[cpan:///distfile/] . $path )->metadata;
}
catch {
print "DEBUG: $_" if $self->verbose;
};
return unless $metadata;
return $metadata->{cpan_id};
}
# returns false in case of error (so, skip!)
sub parse_uri {
my ($self, $resource) = @_;
my $uri = URI->new( $resource );
my $scheme = lc $uri->scheme;
if ( $scheme ne 'http'
and $scheme ne 'https'
and $scheme ne 'ftp'
and $scheme ne 'cpan'
) {
print "invalid scheme '$scheme' for resource '$resource'. Skipping...\n"
unless $self->quiet;
return;
}
my $author = $self->get_author( $uri->path );
unless ($author) {
print "error fetching author for resource '$resource'. Skipping...\n"
unless $self->quiet;
return;
}
# the 'LOCAL' user is reserved and should never send reports.
if ($author eq 'LOCAL') {
print "'LOCAL' user is reserved. Skipping resource '$resource'\n"
unless $self->quiet;
return;
}
$self->author($author);
$self->distfile(substr("$uri", index("$uri", $author)));
return 1;
}
sub make_report {
my ($self, $resource, $dist, $result, @test_output) = @_;
if ( index($dist, 'Local-') == 0 ) {
print "'Local::' namespace is reserved. Skipping resource '$resource'\n"
unless $self->quiet;
return;
}
return unless $self->parse_uri($resource);
my $author = $self->author;
my $cpanm_version = $self->{_cpanminus_version} || 'unknown cpanm version';
my $meta = $self->get_meta_for( $dist );
my $client = CPAN::Testers::Common::Client->new(
author => $self->author,
distname => $dist,
grade => $result,
via => "App::cpanminus::reporter $VERSION ($cpanm_version)",
test_output => join( '', @test_output ),
prereqs => ($meta && ref $meta) ? $meta->{prereqs} : undef,
);
if (!$self->skip_history && $client->is_duplicate) {
print "($resource, $author, $dist, $result) was already sent. Skipping...\n"
if $self->verbose;
return;
}
else {
print "sending: ($resource, $author, $dist, $result)\n" unless $self->quiet;
}
my $reporter = Test::Reporter->new(
transport => $self->config->transport_name,
transport_args => $self->config->transport_args,
grade => $client->grade,
distribution => $dist,
distfile => $self->distfile,
from => $self->config->email_from,
comments => $client->email,
via => $client->via,
);
if ($self->dry_run) {
print "not sending (drun run)\n" unless $self->quiet;
return;
}
try {
$reporter->send() || die $reporter->errstr();
}
catch {
print "Error while sending this report, continuing with the next one...\n" unless $self->quiet;
print "DEBUG: @_" if $self->verbose;
} finally{
$client->record_history unless $self->skip_history;
};
return;
}
sub get_meta_for {
my ($self, $dist) = @_;
my $distdir = File::Spec->catdir( $self->build_dir, $dist );
foreach my $meta_file ( qw( META.json META.yml META.yaml ) ) {
my $meta_path = File::Spec->catfile( $distdir, $meta_file );
if (-e $meta_path) {
my $meta = eval { Parse::CPAN::Meta->load_file( $meta_path ) };
next if $@;
if (!$meta->{'meta-spec'} or $meta->{'meta-spec'}{version} < 2) {
$meta = CPAN::Meta::Converter->new( $meta )->convert( version => 2 );
}
return $meta;
}
}
return;
}
42;
__END__
=head1 NAME
App::cpanminus::reporter - send cpanm output to CPAN Testers
=head1 SYNOPSIS
This is just the backend module, you are probably looking for L<cpanm-reporter>'s
documentation instead. Please look there for a B<much more> comprehensive documentation.
=head1 STILL HERE?
use App::cpanminus::reporter;
my $tester = App::cpanminus::reporter->new( %options );
$tester->run;
=head1 DESCRIPTION
See L<cpanm-reporter>.
=head1 AUTHOR
Breno G. de Oliveira C<< <garu@cpan.org> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2012-2015, Breno G. de Oliveira C<< <garu@cpan.org> >>. All rights reserved.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.
=head1 DISCLAIMER OF WARRANTY
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.