| 1 | # Copyright 2014-2026, Paul Johnson (paul@pjcj.net) | ||
| 2 | |||
| 3 | # This software is free. It is licensed under the same terms as Perl itself. | ||
| 4 | |||
| 5 | # The latest version of this software should be available from my homepage: | ||
| 6 | # https://pjcj.net | ||
| 7 | |||
| 8 | package Devel::Cover::Collection; | ||
| 9 | |||
| 10 | use 5.42.0; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | our $VERSION = '1.52'; # VERSION | ||
| 14 | |||
| 15 | use Devel::Cover::DB (); | ||
| 16 | use Devel::Cover::DB::IO::JSON (); | ||
| 17 | use Devel::Cover::Dumper qw( Dumper ); | ||
| 18 | use Devel::Cover::Web qw( write_file ); | ||
| 19 | |||
| 20 | use JSON::MaybeXS (); | ||
| 21 | use Parallel::Iterator qw( iterate_as_array ); | ||
| 22 | use POSIX qw( setsid ); | ||
| 23 | use Template (); | ||
| 24 | use Time::HiRes qw( alarm time ); | ||
| 25 | |||
| 26 | use feature "class"; | ||
| 27 | |||
| 28 | no warnings "experimental::class"; | ||
| 29 | |||
| 30 | my $Dist_ext_re = qr/\.(?:zip|tgz|tar\.(?:gz|bz2|xz))/; | ||
| 31 | |||
| 32 | class Devel::Cover::Collection { | ||
| 33 | # ro attributes | ||
| 34 | field $bin_dir :param :reader = undef; | ||
| 35 | field $cpancover_dir :param :reader = undef; | ||
| 36 | field $cpan_dir :param :reader = undef; | ||
| 37 | field $results_dir :param :reader = undef; | ||
| 38 | field $dryrun :param :reader = undef; | ||
| 39 | field $env :param :reader = undef; | ||
| 40 | field $force :param :reader = undef; | ||
| 41 | field $output_file :param :reader = undef; | ||
| 42 | field $report :param :reader = undef; | ||
| 43 | field $timeout :param :reader = undef; | ||
| 44 | field $verbose :param :reader = undef; | ||
| 45 | field $workers :param :reader = undef; | ||
| 46 | field $docker :param :reader = undef; | ||
| 47 | field $local :param :reader = undef; | ||
| 48 | |||
| 49 | # rwp attributes (reader + private setter) | ||
| 50 | field $build_dirs :param :reader = undef; | ||
| 51 | field $modules :param :reader = undef; | ||
| 52 | field $module_file :param :reader = undef; | ||
| 53 | |||
| 54 | # rw attributes (custom accessors for Moo compatibility) | ||
| 55 | field $dir :param = undef; | ||
| 56 | field $file :param = undef; | ||
| 57 | |||
| 58 | ADJUST { | ||
| 59 | # Apply defaults (equivalent to BUILDARGS) | ||
| 60 | $build_dirs //= []; | ||
| 61 | $cpan_dir //= [ grep -d, glob "~/.cpan ~/.local/share/.cpan" ]; | ||
| 62 | $docker //= "docker"; | ||
| 63 | $dryrun //= 0; | ||
| 64 | $env //= "prod"; | ||
| 65 | $force //= 0; | ||
| 66 | $local //= 0; | ||
| 67 | $modules //= []; | ||
| 68 | $output_file //= "index.html"; | ||
| 69 | $report //= "html_basic"; | ||
| 70 | $timeout //= $ENV{CPANCOVER_TIMEOUT} // 30 * 60; # half an hour | ||
| 71 | $verbose //= 0; | ||
| 72 | $workers //= 0; | ||
| 73 | $ENV{CPANCOVER_TIMEOUT} = $timeout; | ||
| 74 | } | ||
| 75 | |||
| 76 | # rwp private setters | ||
| 77 | method _set_build_dirs ($val) { $build_dirs = $val } | ||
| 78 | method _set_modules ($val) { $modules = $val } | ||
| 79 | method _set_module_file ($val) { $module_file = $val } | ||
| 80 | |||
| 81 | # rw accessors (Moo-compatible: reader acts as writer when called with arg) | ||
| 82 | method dir ($new = undef) { $dir = $new if defined $new; $dir } | ||
| 83 | method file ($new = undef) { $file = $new if defined $new; $file } | ||
| 84 | |||
| 85 | # display $non_buffered characters, then buffer | ||
| 86 | method _sys ($non_buffered, @command) { | ||
| 87 | # system @command; return "."; | ||
| 88 | my ($output1, $output2) = ("", ""); | ||
| 89 | $output1 = "dc -> @command\n" if $verbose; | ||
| 90 | my $max = 4e4; | ||
| 91 | # say "Setting alarm for $timeout seconds"; | ||
| 92 | my $ok = 0; | ||
| 93 | my $pid; | ||
| 94 | eval { | ||
| 95 | open STDIN, "<", "/dev/null" or die "Can't read /dev/null: $!"; | ||
| 96 | $pid = open my $fh, "-|" // die "Can't fork: $!"; | ||
| 97 | if ($pid) { | ||
| 98 | my $printed = 0; | ||
| 99 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 100 | alarm $timeout; | ||
| 101 | while (<$fh>) { | ||
| 102 | # print "got: $_"; | ||
| 103 | # say "printed $printed of $non_buffered"; | ||
| 104 | if ($printed < $non_buffered) { | ||
| 105 | print; | ||
| 106 | say "Devel::Cover: buffering ..." | ||
| 107 | if ($printed += length) >= $non_buffered; | ||
| 108 | } elsif (length $output2) { | ||
| 109 | $output2 = substr $output2 . $_, $max * -.1, $max * .1; | ||
| 110 | } else { | ||
| 111 | $output1 .= $_; | ||
| 112 | if (length $output1 > $max * .9) { | ||
| 113 | $output1 = substr $output1, 0, $max * .9; | ||
| 114 | $output2 = "\n"; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | alarm 0; | ||
| 119 | if (close $fh) { | ||
| 120 | $ok = 1; | ||
| 121 | } else { | ||
| 122 | warn "Error running @command\n"; | ||
| 123 | } | ||
| 124 | } else { | ||
| 125 | setsid() != -1 or die "Can't start a new session: $!"; | ||
| 126 | open STDERR, ">&", STDOUT or die "Can't dup stdout: $!"; | ||
| 127 | exec @command or die "Can't exec @command: $!"; | ||
| 128 | } | ||
| 129 | }; | ||
| 130 | if ($@) { | ||
| 131 | $ok = 0; | ||
| 132 | die "$@" unless $@ eq "alarm\n"; # propagate unexpected errs | ||
| 133 | warn "Timed out after $timeout seconds!\n"; | ||
| 134 | my $pgrp = getpgrp $pid; | ||
| 135 | my $n = kill "-KILL", $pgrp; | ||
| 136 | warn "killed $n processes"; | ||
| 137 | } | ||
| 138 | $ok ? length $output2 ? "$output1\n...\n$output2" : $output1 : undef | ||
| 139 | } | ||
| 140 | |||
| 141 | method sys (@a) { $self->_sys(4e4, @a) // "" } | ||
| 142 | method bsys (@a) { $self->_sys(0, @a) // "" } | ||
| 143 | method fsys (@a) { $self->_sys(4e4, @a) // die "Can't run @a" } | ||
| 144 | method fbsys (@a) { $self->_sys(0, @a) // die "Can't run @a" } | ||
| 145 | |||
| 146 | method add_modules (@o) { push $modules->@*, @o } | ||
| 147 | method set_modules (@o) { $modules->@* = @o } | ||
| 148 | method set_module_file ($f) { $self->_set_module_file($f) } | ||
| 149 | |||
| 150 | method process_module_file { | ||
| 151 | my $f = $module_file; | ||
| 152 | return unless defined $f && length $f; | ||
| 153 | open my $fh, "<", $f or die "Can't open $f: $!"; | ||
| 154 | my $m = do { local $/; <$fh> }; | ||
| 155 | close $fh or die "Can't close $f: $!"; | ||
| 156 | my @m = grep /\S/, grep !/^ *#/, split /\n/, $m; | ||
| 157 | $self->add_modules(@m); | ||
| 158 | } | ||
| 159 | |||
| 160 | method build_modules { | ||
| 161 | my @command = qw( cpan -Ti ); | ||
| 162 | push @command, "-f" if $force; | ||
| 163 | my %m; | ||
| 164 | for my $module (sort grep !$m{$_}++, $modules->@*) { | ||
| 165 | say "Building $module"; | ||
| 166 | my $output = $self->fsys(@command, $module); | ||
| 167 | say $output; | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | method add_build_dirs { | ||
| 172 | my $exists = sub { | ||
| 173 | my $d = "/remote_staging/" . (s|.*/||r =~ s/-\d+$/*/r); | ||
| 174 | my @files = glob $d; | ||
| 175 | @files | ||
| 176 | }; | ||
| 177 | push $build_dirs->@*, grep { !$exists->() } grep -d, | ||
| 178 | map glob("$_/build/*"), $cpan_dir->@*; | ||
| 179 | } | ||
| 180 | |||
| 181 | method made_res_dir ($sub_dir = undef) { | ||
| 182 | my $d = $results_dir // die "No results dir"; | ||
| 183 | $d .= "/$sub_dir" if defined $sub_dir; | ||
| 184 | my $output = $self->fsys("mkdir", "-p", $d); | ||
| 185 | $d, $output | ||
| 186 | } | ||
| 187 | |||
| 188 | method run ($build_dir) { | ||
| 189 | chdir $build_dir or die "Can't chdir $build_dir: $!\n"; | ||
| 190 | my ($module) = $build_dir =~ m|.*/([^/]+?)(?:-\d+)$| or return; | ||
| 191 | say "Checking coverage of $module"; | ||
| 192 | |||
| 193 | my $db = "$build_dir/cover_db"; | ||
| 194 | my $line = "=" x 80; | ||
| 195 | my ($res_dir, $out) = $self->made_res_dir; | ||
| 196 | my $rdir = "$res_dir/$module"; | ||
| 197 | my $output = "**** Checking coverage of $module ****\n$out"; | ||
| 198 | |||
| 199 | if (-d $db || -d "$build_dir/structure" || -d $rdir) { | ||
| 200 | $output .= "Already analysed\n"; | ||
| 201 | unless ($force) { | ||
| 202 | say "\n$line\n$output$line\n"; | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | $output .= "Testing $module in $build_dir\n"; | ||
| 208 | |||
| 209 | my @cmd; | ||
| 210 | if ($local) { | ||
| 211 | $ENV{DEVEL_COVER_OPTIONS} = "-ignore,/usr/local/lib/perl5"; | ||
| 212 | $ENV{DEVEL_COVER_TEST_OPTS} = "-Mblib=" . $bin_dir . "/.."; | ||
| 213 | @cmd = ($^X, $ENV{DEVEL_COVER_TEST_OPTS}, $bin_dir . "/cover"); | ||
| 214 | } else { | ||
| 215 | @cmd = ($^X, $bin_dir . "/cover"); | ||
| 216 | } | ||
| 217 | $output .= $self->fbsys( | ||
| 218 | @cmd, "--test", "--report", $report, "--outputfile", $output_file | ||
| 219 | ); | ||
| 220 | $output .= $self->fsys(@cmd, "-report", "json", "-nosummary"); | ||
| 221 | |||
| 222 | # TODO - option to merge DB with existing one | ||
| 223 | # TODO - portability | ||
| 224 | $output .= $self->fsys("rm", "-rf", $rdir); | ||
| 225 | $output .= `rm -f $db/structure/*.lock`; | ||
| 226 | $output .= $self->fsys("mv", $db, $rdir); | ||
| 227 | $output .= $self->fsys("rm", "-rf", $db); | ||
| 228 | |||
| 229 | say "\n$line\n$output$line\n"; | ||
| 230 | } | ||
| 231 | |||
| 232 | method run_all { | ||
| 233 | my @res = iterate_as_array( | ||
| 234 | { workers => $workers }, | ||
| 235 | sub { | ||
| 236 | my (undef, $d) = @_; | ||
| 237 | eval { $self->run($d) }; | ||
| 238 | warn "\n\n\n[$d]: $@\n\n\n" if $@; | ||
| 239 | }, | ||
| 240 | $build_dirs | ||
| 241 | ); | ||
| 242 | } | ||
| 243 | |||
| 244 | method write_json ($vars) { | ||
| 245 | # print Dumper $vars; | ||
| 246 | my $results = {}; | ||
| 247 | for my $module (keys $vars->{vals}->%*) { | ||
| 248 | my $m = $vars->{vals}{$module}; | ||
| 249 | my $mod = $m->{module}; | ||
| 250 | my ($name, $version) = ($mod->{module} // $module) =~ /(.+)-(\d+\.\d+)$/; | ||
| 251 | $name = $mod->{name} if defined $mod->{name}; | ||
| 252 | $version = $mod->{version} if defined $mod->{version}; | ||
| 253 | if (defined $name && defined $version) { | ||
| 254 | $results->{$name}{$version}{coverage}{total} = { | ||
| 255 | map { $_ => $m->{$_}{pc} } grep $m->{$_}{pc} ne "n/a", | ||
| 256 | grep !/link|log|module/, | ||
| 257 | keys %$m, | ||
| 258 | }; | ||
| 259 | } else { | ||
| 260 | print "Cannot process $module: ", Dumper $m if $verbose; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | # print Dumper $vars, $results; | ||
| 264 | |||
| 265 | my $io = Devel::Cover::DB::IO::JSON->new(options => "pretty"); | ||
| 266 | my ($rdir) = $self->made_res_dir; | ||
| 267 | my $f = "$rdir/cpancover.json"; | ||
| 268 | $io->write($results, $f); | ||
| 269 | say "Wrote json output to $f"; | ||
| 270 | } | ||
| 271 | |||
| 272 | method coverage_class ($pc) { | ||
| 273 | $pc eq "n/a" ? "na" | ||
| 274 | : $pc < 75 ? "c0" | ||
| 275 | : $pc < 90 ? "c1" | ||
| 276 | : $pc < 100 ? "c2" | ||
| 277 | : "c3" | ||
| 278 | } | ||
| 279 | |||
| 280 | method write_summary ($vars) { | ||
| 281 | my $d = $self->dir; | ||
| 282 | my $f = $self->file; | ||
| 283 | |||
| 284 | write_file(($self->made_res_dir)[0], "collection.css"); | ||
| 285 | my $template = Template->new({ | ||
| 286 | LOAD_TEMPLATES => | ||
| 287 | [ Devel::Cover::Collection::Template::Provider->new({}) ], | ||
| 288 | }); | ||
| 289 | $template->process("summary", $vars, $f) or die $template->error; | ||
| 290 | for my $start (sort keys $vars->{modules}->%*) { | ||
| 291 | $vars->{module_start} = $start; | ||
| 292 | my $dist = "$d/dist/$start.html"; | ||
| 293 | $template->process("module_by_start", $vars, $dist) | ||
| 294 | or die $template->error; | ||
| 295 | } | ||
| 296 | |||
| 297 | my $about_f = "$d/about.html"; | ||
| 298 | say "\nWriting about page to $about_f ..."; | ||
| 299 | |||
| 300 | $template->process("about", { subdir => "latest/" }, $about_f) | ||
| 301 | or die $template->error; | ||
| 302 | |||
| 303 | # print Dumper $vars; | ||
| 304 | $self->write_json($vars); | ||
| 305 | |||
| 306 | say "Wrote collection output to $f"; | ||
| 307 | } | ||
| 308 | |||
| 309 | method resolve_log_links ($d, $mods, $vars) { | ||
| 310 | # Match top-level .out.gz log files to modules | ||
| 311 | my $n = 0; | ||
| 312 | my $re = qr/^\w-\w\w-\w+-(.*)/; | ||
| 313 | my $ext = qr/($Dist_ext_re)/; | ||
| 314 | my $ts = qr/--\d{10,11}\.\d{6}\.out\.gz$/; | ||
| 315 | for my $f ($mods->@*) { | ||
| 316 | my ($module) = $f =~ /${re}${ext}${ts}/ or next; | ||
| 317 | next if $vars->{vals}{$module}{log}; | ||
| 318 | $vars->{vals}{$module}{log} = $f; | ||
| 319 | print "-" if !($n++ % 1000) && !$verbose; | ||
| 320 | } | ||
| 321 | |||
| 322 | # Fallback: .log_ref for modules built as dependencies | ||
| 323 | $n = 0; | ||
| 324 | for my $module (keys $vars->{vals}->%*) { | ||
| 325 | next if $vars->{vals}{$module}{log}; | ||
| 326 | my $ref = "$d/$module/.log_ref"; | ||
| 327 | open my $fh, "<", $ref or next; | ||
| 328 | chomp(my $log = <$fh>); | ||
| 329 | close $fh or warn "Can't close $ref: $!"; | ||
| 330 | $vars->{vals}{$module}{log} = $log if length $log; | ||
| 331 | print "-" if !($n++ % 1000) && !$verbose; | ||
| 332 | } | ||
| 333 | say ""; | ||
| 334 | } | ||
| 335 | |||
| 336 | method generate_html { | ||
| 337 | my ($d) = $self->made_res_dir; | ||
| 338 | chdir $d or die "Can't chdir $d: $!\n"; | ||
| 339 | $self->dir($d); | ||
| 340 | |||
| 341 | my $f = "$d/index.html"; | ||
| 342 | $self->file($f); | ||
| 343 | say "\n\nWriting collection output to $f ..."; | ||
| 344 | |||
| 345 | my $vars = { | ||
| 346 | title => "Coverage report", | ||
| 347 | modules => {}, | ||
| 348 | vals => {}, | ||
| 349 | subdir => "latest/", | ||
| 350 | headers => | ||
| 351 | [ grep !/path|time/, @Devel::Cover::DB::Criteria_short, "total" ], | ||
| 352 | criteria => [ grep !/path|time/, @Devel::Cover::DB::Criteria, "total" ], | ||
| 353 | }; | ||
| 354 | |||
| 355 | opendir my $dh, $d or die "Can't opendir $d: $!"; | ||
| 356 | my @mods = sort grep !/^\./, readdir $dh; | ||
| 357 | closedir $dh or die "Can't closedir $d: $!"; | ||
| 358 | |||
| 359 | my $n = 0; | ||
| 360 | for my $module (@mods) { | ||
| 361 | my $cover = "$d/$module/cover.json"; | ||
| 362 | next unless -e $cover; | ||
| 363 | say "Adding $module" if $verbose; | ||
| 364 | |||
| 365 | my $io = Devel::Cover::DB::IO::JSON->new; | ||
| 366 | my $json = $io->read($cover); | ||
| 367 | |||
| 368 | my $mod = { | ||
| 369 | module => $module, | ||
| 370 | map { $_ => $json->{runs}[0]{$_} } qw( name version dir ), | ||
| 371 | }; | ||
| 372 | unless (defined $mod->{name} && defined $mod->{version}) { | ||
| 373 | my ($name, $version) | ||
| 374 | = ($mod->{module} // $module) =~ /(.+)-(\d+\.\d+)$/; | ||
| 375 | $mod->{name} //= $name; | ||
| 376 | $mod->{version} //= $version; | ||
| 377 | } | ||
| 378 | my $start = uc substr $module, 0, 1; | ||
| 379 | push $vars->{modules}{$start}->@*, $mod; | ||
| 380 | |||
| 381 | my $m = $vars->{vals}{$module} = {}; | ||
| 382 | $m->{module} = $mod; | ||
| 383 | $m->{link} = "/$module/index.html" | ||
| 384 | if $json->{summary}{Total}{total}{total}; | ||
| 385 | |||
| 386 | for my $criterion ($vars->{criteria}->@*) { | ||
| 387 | my $summary = $json->{summary}{Total}{$criterion}; | ||
| 388 | # print "summary:", Dumper $summary; | ||
| 389 | my $pc = $summary->{percentage}; | ||
| 390 | $pc = defined $pc ? sprintf "%.2f", $pc : "n/a"; | ||
| 391 | $m->{$criterion}{pc} = $pc; | ||
| 392 | $m->{$criterion}{class} = $self->coverage_class($pc); | ||
| 393 | $m->{$criterion}{details} | ||
| 394 | = ($summary->{covered} || 0) . " / " . ($summary->{total} || 0); | ||
| 395 | } | ||
| 396 | |||
| 397 | print "." if !($n++ % 1000) && !$verbose; | ||
| 398 | } | ||
| 399 | |||
| 400 | $self->resolve_log_links($d, \@mods, $vars); | ||
| 401 | |||
| 402 | # print "vars ", Dumper $vars; | ||
| 403 | $self->write_summary($vars); | ||
| 404 | } | ||
| 405 | |||
| 406 | method compress_old_versions ($versions) { | ||
| 407 | my ($d) = $self->made_res_dir; | ||
| 408 | opendir my $fh, $d or die "Can't opendir $d: $!"; | ||
| 409 | my @dirs = sort grep -d, map "$d/$_", readdir $fh; | ||
| 410 | closedir $fh or die "Can't closedir $d: $!"; | ||
| 411 | |||
| 412 | my %mods; | ||
| 413 | for my $entry (@dirs) { | ||
| 414 | my $f = "$entry/cover.json"; | ||
| 415 | my $json = JSON::MaybeXS->new(utf8 => 1, allow_blessed => 1); | ||
| 416 | open my $fh, "<", $f or next; | ||
| 417 | # say "file: $f"; | ||
| 418 | my $data | ||
| 419 | = do { local $/; eval { $json->decode(<$fh>) } } | ||
| 420 | or next; | ||
| 421 | next if $@; | ||
| 422 | close $fh or next; | ||
| 423 | my ($name) = $entry =~ /.+\/(.+)/; | ||
| 424 | $name =~ s/-[^-]+$//; | ||
| 425 | my @runs = grep { ($_->{name} // "") eq $name } $data->{runs}->@*; | ||
| 426 | # say "$name " . @runs; | ||
| 427 | my $run = $runs[0] // next; | ||
| 428 | my $version = $run->{version} =~ s/_//gr // next; | ||
| 429 | my $v = eval { no warnings "overflow"; version->parse($version)->numify }; | ||
| 430 | if ($@ || !$v) { | ||
| 431 | $v = $version; | ||
| 432 | $v =~ s/[^0-9.]//g; | ||
| 433 | my @parts = split /\./, $v; | ||
| 434 | if (@parts > 2) { | ||
| 435 | $v = shift(@parts) . "." . join "", @parts; | ||
| 436 | } | ||
| 437 | } | ||
| 438 | $v ||= 0; | ||
| 439 | push $mods{$name}->@*, { dir => $entry, version => $v }; | ||
| 440 | } | ||
| 441 | |||
| 442 | for my $name (sort keys %mods) { | ||
| 443 | # print Dumper $mods{$name}; | ||
| 444 | my @o = sort { $b->{version} <=> $a->{version} } $mods{$name}->@*; | ||
| 445 | shift @o for 1 .. $versions; | ||
| 446 | for my $v (@o) { | ||
| 447 | |||
| 448 | my ($parent, $s) = $v->{dir} =~ /(.+)\/(.+)/; | ||
| 449 | my $archive = "$v->{dir}.tar.xz"; | ||
| 450 | my @cmd1 | ||
| 451 | = ($self->dc_file, "-r", $parent, "cpancover-uncompress-dir", $s); | ||
| 452 | my @cmd2 = ("bash", "-c", "tar cf - -C $parent $s | xz -z > $archive"); | ||
| 453 | my @cmd3 = ("rm", "-rf", $v->{dir}); | ||
| 454 | |||
| 455 | if ($dryrun) { | ||
| 456 | say for "compressing $s", "@cmd1", "@cmd2", "@cmd3"; | ||
| 457 | } else { | ||
| 458 | say "compressing $s"; | ||
| 459 | eval { $self->fsys(@$_) for \@cmd1, \@cmd2, \@cmd3; }; | ||
| 460 | say $@ if $@; | ||
| 461 | } | ||
| 462 | } | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | method local_build { | ||
| 467 | $self->process_module_file; | ||
| 468 | $self->build_modules; | ||
| 469 | $self->add_build_dirs; | ||
| 470 | $self->run_all; | ||
| 471 | } | ||
| 472 | |||
| 473 | method failed_dir { ($self->made_res_dir("__failed__"))[0] } | ||
| 474 | method covered_dir ($d) { $results_dir . "/$d" } | ||
| 475 | method failed_file ($d) { $self->failed_dir . "/$d" } | ||
| 476 | method is_covered ($d) { -d $self->covered_dir($d) } | ||
| 477 | method is_failed ($d) { -e $self->failed_file($d) } | ||
| 478 | method set_covered ($d) { unlink $self->failed_file($d) } | ||
| 479 | |||
| 480 | method set_failed ($d) { | ||
| 481 | my $ff = $self->failed_file($d); | ||
| 482 | open my $fh, ">", $ff or return warn "Can't open $ff: $!"; | ||
| 483 | print $fh scalar localtime; | ||
| 484 | close $fh or warn "Can't close $ff: $!"; | ||
| 485 | } | ||
| 486 | |||
| 487 | method dc_file { | ||
| 488 | my $d = ""; | ||
| 489 | $d = "/dc/" if $local && -d "/dc"; | ||
| 490 | "${d}utils/dc" | ||
| 491 | } | ||
| 492 | |||
| 493 | method cover_modules { | ||
| 494 | $self->process_module_file; | ||
| 495 | # say "modules: ", Dumper $modules; | ||
| 496 | |||
| 497 | my @cmd = ($self->dc_file, "--env", $env); | ||
| 498 | push @cmd, "--verbose" if $verbose; | ||
| 499 | my @command = (@cmd, "cpancover-docker-module"); | ||
| 500 | my @res = iterate_as_array( | ||
| 501 | { workers => $workers }, | ||
| 502 | sub { | ||
| 503 | # say "mod ", Dumper \@_; | ||
| 504 | my (undef, $module) = @_; | ||
| 505 | my $d = $module =~ s|.*/||r =~ s/${Dist_ext_re}$//r; | ||
| 506 | if ($self->is_covered($d)) { | ||
| 507 | $self->set_covered($d); | ||
| 508 | say "$module already covered" if $verbose; | ||
| 509 | return unless $force; | ||
| 510 | } elsif ($self->is_failed($d)) { | ||
| 511 | say "$module already failed" if $verbose; | ||
| 512 | return unless $force; | ||
| 513 | } | ||
| 514 | |||
| 515 | my $to = $timeout; | ||
| 516 | # say "Setting alarm for $to seconds"; | ||
| 517 | my $name = sprintf("%s-%18.6f", $module, time) =~ tr/a-zA-Z0-9_./-/cr; | ||
| 518 | say "$d -> $name"; | ||
| 519 | eval { | ||
| 520 | local $SIG{ALRM} = sub { die "alarm\n" }; | ||
| 521 | alarm $to; | ||
| 522 | say "running: @command $module $name" if $verbose; | ||
| 523 | system @command, $module, $name; | ||
| 524 | alarm 0; | ||
| 525 | }; | ||
| 526 | if ($@) { | ||
| 527 | die "$@" unless $@ eq "alarm\n"; # unexpected errors | ||
| 528 | say "Timed out after $to seconds!"; | ||
| 529 | $self->sys($docker, "kill", $name); | ||
| 530 | say "Killed docker container $name"; | ||
| 531 | } | ||
| 532 | |||
| 533 | if ($self->is_covered($d)) { | ||
| 534 | $self->set_covered($d); | ||
| 535 | say "$d done"; | ||
| 536 | } else { | ||
| 537 | $self->set_failed($d); | ||
| 538 | say "$d failed"; | ||
| 539 | } | ||
| 540 | }, | ||
| 541 | do { my %m; [ sort grep !$m{$_}++, @$modules ] } | ||
| 542 | ); | ||
| 543 | } | ||
| 544 | |||
| 545 | method get_latest { | ||
| 546 | require CPAN::Releases::Latest; | ||
| 547 | |||
| 548 | my $latest = CPAN::Releases::Latest->new(max_age => 0); # no caching | ||
| 549 | my $iterator = $latest->release_iterator; | ||
| 550 | |||
| 551 | while (my $release = $iterator->next_release) { | ||
| 552 | say $release->path; | ||
| 553 | # Debugging code: | ||
| 554 | # printf "%s path=%s time=%d size=%d\n", | ||
| 555 | # $release->distname, | ||
| 556 | # $release->path, | ||
| 557 | # $release->timestamp, | ||
| 558 | # $release->size; | ||
| 559 | } | ||
| 560 | } | ||
| 561 | } | ||
| 562 | |||
| 563 | package Devel::Cover::Collection::Template::Provider; | ||
| 564 | |||
| 565 | use strict; | ||
| 566 | use warnings; | ||
| 567 | |||
| 568 | our $VERSION = '1.52'; # VERSION | ||
| 569 | |||
| 570 | use base "Template::Provider"; | ||
| 571 | |||
| 572 | my %Templates; | ||
| 573 | |||
| 574 | sub fetch ($self, $name, $) { | ||
| 575 | # print "Looking for <$name>\n"; | ||
| 576 | $self->SUPER::fetch(exists $Templates{$name} ? \$Templates{$name} : $name) | ||
| 577 | } | ||
| 578 | |||
| 579 | $Templates{colours} = <<'EOT'; | ||
| 580 | [% | ||
| 581 | colours = { | ||
| 582 | default => "#ffffad", | ||
| 583 | text => "#000000", | ||
| 584 | number => "#ffffc0", | ||
| 585 | error => "#ff0000", | ||
| 586 | ok => "#00ff00", | ||
| 587 | } | ||
| 588 | %] | ||
| 589 | |||
| 590 | [% MACRO bg BLOCK -%] | ||
| 591 | bgcolor="[% colours.$colour %]" | ||
| 592 | [%- END %] | ||
| 593 | EOT | ||
| 594 | |||
| 595 | $Templates{html} = <<'EOT'; | ||
| 596 | <!DOCTYPE html | ||
| 597 | PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
| 598 | "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
| 599 | <html xmlns="https://www.w3.org/1999/xhtml"> | ||
| 600 | <!-- | ||
| 601 | This file was generated by Devel::Cover Version $VERSION | ||
| 602 | Devel::Cover is copyright 2001-2026, Paul Johnson (paul\@pjcj.net) | ||
| 603 | Devel::Cover is free. It is licensed under the same terms as Perl itself. | ||
| 604 | The latest version of Devel::Cover should be available from my homepage: | ||
| 605 | https://pjcj.net | ||
| 606 | --> | ||
| 607 | [% PROCESS colours %] | ||
| 608 | <head> | ||
| 609 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta> | ||
| 610 | <meta http-equiv="Content-Language" content="en-us"></meta> | ||
| 611 | <link rel="stylesheet" type="text/css" | ||
| 612 | href="/[% subdir %]collection.css"></link> | ||
| 613 | <title> [% title %] </title> | ||
| 614 | </head> | ||
| 615 | <body> | ||
| 616 | [% content %] | ||
| 617 | <hr/> | ||
| 618 | <p> | ||
| 619 | Coverage information from <a href="https://metacpan.org/module/Devel::Cover"> | ||
| 620 | Devel::Cover | ||
| 621 | </a> by <a href="https://pjcj.net">Paul Johnson</a>. | ||
| 622 | |||
| 623 | <br/> | ||
| 624 | |||
| 625 | Please report problems with this site to the | ||
| 626 | <a href="https://github.com/pjcj/Devel--Cover/issues">issue tracker</a>.</p> | ||
| 627 | |||
| 628 | <p><a href="http://cpancover.com/latest/about.html">About</a> the project.</p> | ||
| 629 | |||
| 630 | <p>This server generously donated by | ||
| 631 | <a href="https://www.bytemark.co.uk">Bytemark</a>.</p> | ||
| 632 | </body> | ||
| 633 | </html> | ||
| 634 | EOT | ||
| 635 | |||
| 636 | $Templates{summary} = <<'EOT'; | ||
| 637 | [% WRAPPER html %] | ||
| 638 | |||
| 639 | <h1> CPANCover </h1> | ||
| 640 | |||
| 641 | <h2> Distributions </h2> | ||
| 642 | |||
| 643 | <p>Search for distributions by first character:</p> | ||
| 644 | |||
| 645 | [% FOREACH start = modules.keys.sort %] | ||
| 646 | <a href="dist/[%- start -%].html">[% start %]</a> | ||
| 647 | [% END %] | ||
| 648 | |||
| 649 | <h2> Core coverage </h2> | ||
| 650 | |||
| 651 | <a href="http://cpancover.com/blead/latest/coverage.html">Perl core coverage</a> | ||
| 652 | (under development) | ||
| 653 | |||
| 654 | [% END %] | ||
| 655 | EOT | ||
| 656 | |||
| 657 | $Templates{about} = <<'EOT'; | ||
| 658 | [% WRAPPER html %] | ||
| 659 | |||
| 660 | <h1> CPANCover </h1> | ||
| 661 | |||
| 662 | <h2> About </h2> | ||
| 663 | |||
| 664 | <p>CPANCover is a project to provide code coverage information for | ||
| 665 | <a href="https://metacpan.org">CPAN</a> modules. When a new module, or an | ||
| 666 | update to an existing module, is uploaded to CPAN it will automatically be | ||
| 667 | downloaded by | ||
| 668 | CPANCover. CPANCover will run the module's tests and measure the code coverage | ||
| 669 | provided by the tests. This information is then made available as HTML pages | ||
| 670 | and JSON data.</p> | ||
| 671 | |||
| 672 | <p>The coverage data is generated by <a | ||
| 673 | href="https://metacpan.org/release/Devel-Cover">Devel::Cover</a>. | ||
| 674 | |||
| 675 | <p>The source code is available at the | ||
| 676 | <a href="https://github.com/pjcj/Devel--Cover">GitHub repository</a>. | ||
| 677 | Contributions are also accepted for several | ||
| 678 | <a href="https://pjcj.net/devel-cover/projects.html">open projects</a>. | ||
| 679 | </p> | ||
| 680 | |||
| 681 | [% END %] | ||
| 682 | EOT | ||
| 683 | |||
| 684 | $Templates{module_by_start} = <<'EOT'; | ||
| 685 | [% WRAPPER html %] | ||
| 686 | |||
| 687 | <h1> [% title %] - [% module_start %] </h1> | ||
| 688 | |||
| 689 | <table> | ||
| 690 | |||
| 691 | [% IF modules.$module_start %] | ||
| 692 | <tr align="right" valign="middle"> | ||
| 693 | <th class="header" align="left" style='white-space: nowrap;'> Module </th> | ||
| 694 | <th class="header"> Version </th> | ||
| 695 | <th class="header"> Log </th> | ||
| 696 | [% FOREACH header = headers %] | ||
| 697 | <th class="header"> [% header %] </th> | ||
| 698 | [% END %] | ||
| 699 | </tr> | ||
| 700 | [% END %] | ||
| 701 | |||
| 702 | [% FOREACH module = modules.$module_start %] | ||
| 703 | [% m = module.module %] | ||
| 704 | <tr align="right" valign="middle"> | ||
| 705 | <td align="left"> | ||
| 706 | [% IF vals.$m.link %] | ||
| 707 | <a href="/[% subdir %][%- vals.$m.link -%]"> | ||
| 708 | [% module.name || module.module %] | ||
| 709 | </a> | ||
| 710 | [% ELSE %] | ||
| 711 | [% module.name || module.module %] | ||
| 712 | [% END %] | ||
| 713 | </td> | ||
| 714 | <td> [% module.version %] </td> | ||
| 715 | <td> | ||
| 716 | [% IF vals.$m.log %] | ||
| 717 | <a href="/[% subdir %][% vals.$m.log %]"> ¶ </a> | ||
| 718 | [% END %] | ||
| 719 | </td> | ||
| 720 | [% FOREACH criterion = criteria %] | ||
| 721 | <td class="[%- vals.$m.$criterion.class -%]" | ||
| 722 | title="[%- vals.$m.$criterion.details -%]"> | ||
| 723 | [% vals.$m.$criterion.pc %] | ||
| 724 | </td> | ||
| 725 | [% END %] | ||
| 726 | </tr> | ||
| 727 | [% END %] | ||
| 728 | |||
| 729 | </table> | ||
| 730 | |||
| 731 | <br/> | ||
| 732 | |||
| 733 | [% END %] | ||
| 734 | EOT | ||
| 735 | |||
| 736 | " | ||
| 737 | We have normality, I repeat we have normality. | ||
| 738 | Anything you still can't cope with is therefore your own problem. | ||
| 739 | " | ||
| 740 | |||
| 741 | __END__ |