| 1 | package Suck::Huggingface; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | ## ABSTRACT: Clone huggingface repos and then download their models | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use v5.10.0; # provides "say" and "state" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use File::Valet; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Time::TAI::Simple; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=21.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my ($class, %opt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | my $self = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | opt_hr => \%opt_hr, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | conf_hr => {}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ok => 'OK', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | ex => undef, # stash caught exceptions here | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | n_err => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | n_warn => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | err => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | err_ar => [], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | project_name => 'suck-huggingface', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | trace_ar => [sprintf('SKH:%08X', int(rand() * 0xFFFFFFFF))], | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | js_or => JSON::MaybeXS->new(ascii => 1, allow_nonref => 1, space_after => 1) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | bless ($self, $class); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # convert keys of form "some-parameter" to "some_parameter": | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | foreach my $k0 (keys %{$self->{opt_hr}}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 3 | my $k1 = join('_', split(/-/, $k0)); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | ▶ | next if ($k0 eq $k1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $self->{opt_hr}->{$k1} = $self->{opt_hr}->{$k0}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | delete $self->{opt_hr}->{$k0}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | my $home_dir = find_home() // '.'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
find_home() // "."
MC/DC0%0/1
find_home() // "."
find_home()
Truth table50%1/2
A
A= find_home()
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 1 | ▶ | my $path_env = $self->opt('path_env', $ENV{PATH} // '$home_dir/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{"PATH"} // "\$home_dir/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin"
MC/DC0%0/1
$ENV{"PATH"} // "\$home_dir/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/sbin"
$ENV{"PATH"}
Truth table50%1/2
A
A= $ENV{"PATH"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | $self->{path_ar} = [split(/:/, $path_env)]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | $self->{wget_bin} = $self->opt('wget') // find_bin('wget', @{$self->{path_ar}}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->opt("wget") // find_bin("wget", @{$$self{"path_ar"};})
MC/DC0%0/2
$self->opt("wget") // find_bin("wget", @{$$self{"path_ar"};})
$self->opt("wget")
find_bin("wget", @{$$self{"path_ar"};})
Truth table33%1/3
A // B
A= $self->opt("wget")B= find_bin("wget", @{$$self{"path_ar"};})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | die "cannot find wget executable! PATH=$path_env" unless (defined($self->{wget_bin})); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | sub _dissect_repo_url { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_dissect_repo_url: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ($self, $repo_url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | $repo_url = $1 if ($repo_url =~ /(.+?)\.git$/); # will use this much later for $dl_url | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | $repo_url = $1 if ($repo_url =~ /(.+?)\/$/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | return $self->err("skipping docs repo, unsupported", $repo_url) if ($repo_url =~ /^https:\/\/[^\/]+\/docs\//); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | # problem: datasets URLs are ambiguous! | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Valid repo: https://../datasets/username/reponame | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # Also valid: https://../datasets/reponame | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # .. but not always. Sometimes the username is *required!* | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | # Will match the longer of the two cases, and trim it off if the last term | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | # matches something unlikely to be a repo name (blob, tree, discussions). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if ($repo_url =~ /^(https:\/\/[^\/]+\/datasets\/[^\/]+\/[^\/]+)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # longer case of dataset | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $repo_url = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } elsif ($repo_url =~ /^(https:\/\/[^\/]+\/datasets\/[^\/]+)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # shorter case of dataset | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $repo_url = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } elsif ($repo_url =~ /^(https:\/\/[^\/]+\/[^\/]+\/[^\/]+)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | # expected case: model repo | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $repo_url = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | # above logic was optimistic; trim off non-repo parts: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | $repo_url = $1 if ($repo_url =~ /^(https:.+?)\/(blob|tree|discussions)$/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | $repo_url = $1 if ($repo_url =~ /^(https:.+?)\/(blob|tree|discussions)\//); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return $self->err("skipping unparseable repo_url", $repo_url) unless ($repo_url =~ /\/([^\/]+)$/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | my $repo_dir = "$1.git"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $git_url = "$repo_url.git"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | return $self->ok($repo_url, $git_url, $repo_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub _clone_repo { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone_repo: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | my ($self, $git_url, $repo_dir) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if (-e $repo_dir) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $self->dbg("already got repo, will not clone", $repo_dir, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $git_options = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | $git_options .= "-q " unless($self->opt('v')); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $self->dbg("cloning repo", $git_url, $git_options, $repo_dir, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | ▶ | system("git clone $git_options '$git_url' '$repo_dir'") unless ($self->{testing}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | $self->dbg("cloned repo", $git_url, $repo_dir, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | return $self->err("unable to clone repo, skipping", $git_url, $repo_dir) unless (-e $repo_dir); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | return $self->ok(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub _scan_for_external_downloads { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scan_for_external_downloads: not called CC=12 SCAR=49.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my ($self, $git_url, $repo_dir) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | my $total_sz = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my @to_dl; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $self->info("scanning for external downloads", $repo_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | my $exclude_csv = $self->opt('exclude', ''); # comma-delimited list of filename substrings for files to exclude | |||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my @excludes; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | @excludes = split(/,/, $exclude_csv) if ($exclude_csv); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $sz_threshold = $self->opt('too_big', 300); # external downloads are small, usually fewer than 200 bytes | |||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | for my $gf (glob("$repo_dir/*")) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $self->dbg("found file", $gf, 7); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | next unless ($gf =~ /^$repo_dir\/(.+)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $f = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | $self->dbg("reduced file", $f, 7); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | next if ($f =~ /^\./); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $self->dbg("not a dot-file", $f, 7); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | next if (-d $gf); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $self->dbg("not a directory", $f, 7); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | my $file_size = -s $gf; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | $self->dbg("file size", $f, $file_size // '<undef>', 7); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$file_size // "<undef>"
MC/DC0%0/1
$file_size // "<undef>"
$file_size
Truth table0%0/2
A
A= $file_size
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | next if (!$file_size || $file_size > $sz_threshold); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $file_size or $file_size > $sz_threshold
MC/DC0%0/2
not $file_size or $file_size > $sz_threshold
not $file_size
$file_size > $sz_threshold
Truth table0%0/3
not(A) or B
A= not $file_sizeB= $file_size > $sz_threshold
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | my ($excl, $match) = $self->exclude_file($f, \@excludes); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $self->dbg("checked exclusions for match", $f, $excl, $match, 5); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | next if ($excl); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $self->dbg("squinting at small file", $repo_dir, $f, 5); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | my $x = File::Valet::rd_f($gf); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | unless (defined $x) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $self->err("failed to open small file, skipping", $f, $gf, $File::Valet::ERROR); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | next unless ($x =~ /^\s*version\s+([^\s]+)\s*[\r\n]+\s*oid\s+(\w+):([^\s]+)\s*[\r\n]+\s*size\s+(\d+)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my ($ver, $oid_alg, $oid_digest, $sz) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | $self->dbg("found external download", $repo_dir, $f, {ver => $ver, oid_alg => $oid_alg, oid_dig => $oid_digest, sz => $sz}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | $total_sz += $sz; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | push @to_dl, {f => $f, oid_alg => $oid_alg, oid_dig => $oid_digest, sz => $sz}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | return $self->ok($total_sz, \@to_dl); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | # ($excl, $match) = exclude_file($f, \@excludes); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub exclude_file { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
exclude_file: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my ($self, $f, $ex_ar) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | for my $x (@$ex_ar) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | ▶ | return (1, $x) if (index($f, $x) != -1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return (0, ''); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | ▶ | sub _download_files { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_download_files: not called CC=18 SCAR=58.3
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | my ($self, $repo_url, $git_url, $repo_dir, $to_dl_ar) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | my $dl_url = "$repo_url/resolve/main"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my $wget_opts = ""; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my $rl = $self->opt('limit_rate') // $self->opt('rate_limit'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->opt("limit_rate") // $self->opt("rate_limit")
MC/DC0%0/2
$self->opt("limit_rate") // $self->opt("rate_limit")
$self->opt("limit_rate")
$self->opt("rate_limit")
Truth table0%0/3
A // B
A= $self->opt("limit_rate")B= $self->opt("rate_limit")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | my $un = $self->opt('user') // $self->opt('username'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->opt("user") // $self->opt("username")
MC/DC0%0/2
$self->opt("user") // $self->opt("username")
$self->opt("user")
$self->opt("username")
Truth table0%0/3
A // B
A= $self->opt("user")B= $self->opt("username")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my $pw = $self->opt('pw') // $self->opt('password'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->opt("pw") // $self->opt("password")
MC/DC0%0/2
$self->opt("pw") // $self->opt("password")
$self->opt("pw")
$self->opt("password")
Truth table0%0/3
A // B
A= $self->opt("pw")B= $self->opt("password")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | ▶ | $wget_opts .= "-q " unless ($self->opt('v', 0)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | $wget_opts .= "--limit-rate=$rl " if ($rl && $rl =~ /^[\d\.]+\w?$/); # eg: "2.5m" or "500k" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$rl and $rl =~ /^[\d\.]+\w?$/
MC/DC0%0/2
$rl and $rl =~ /^[\d\.]+\w?$/
$rl
$rl =~ /^[\d\.]+\w?$/
Truth table0%0/3
A and B
A= $rlB= $rl =~ /^[\d\.]+\w?$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | $wget_opts .= "--user=$un " if (defined $un); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | $wget_opts .= "--password=$pw " if (defined $pw); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $n_retries = $self->opt('retries', 1000); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | $n_retries = 1000 if ($n_retries < 1); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | $wget_opts .= "--tries=$n_retries "; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my $mk_orig = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | for my $f_hr (@$to_dl_ar) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | my $f = $f_hr->{f}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | if ($f =~ /(.+?)\.orig$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | # left over from a previous attempt to download from this repo. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # if the target file already exists, skip. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # otherwise, download it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | my $tf = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (-e "$repo_dir/$tf") { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | $self->info("file already local, skipping", {tf => $tf, f_hr => $f_hr}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | $f = $tf; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | ▶ | rename("$repo_dir/$f", "$repo_dir/$f.orig") if ($mk_orig); # chicken | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | $self->info("downloading file", $f_hr); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 0 | my $url = "$dl_url/$f"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | my $wget_bin = $self->{wget_bin} // "wget"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"wget_bin"} // "wget"
MC/DC0%0/1
$self->{"wget_bin"} // "wget"
$self->{"wget_bin"}
Truth table0%0/2
A
A= $self->{"wget_bin"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my $cmd = "cd $repo_dir && $wget_bin $wget_opts '$url'"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | $self->dbg("wget cmd", $cmd, 6); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | my $tm0 = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | ▶ | system($cmd) unless ($self->{testing}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | my $dur = Time::HiRes::time() - $tm0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | ▶ | my $kbps = int(($f_hr->{sz} // 0) / $dur + 0.5) / 1024; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$f_hr->{"sz"} // 0
MC/DC0%0/1
$f_hr->{"sz"} // 0
$f_hr->{"sz"}
Truth table0%0/2
A
A= $f_hr->{"sz"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | ▶ | $kbps = $1 if ($kbps + 0.0005 =~ /^(\d+\.\d{3})/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 0 | $self->dbg("wget done", {duration => $dur, kbps => $kbps}, 6);; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | # zzapp -- TO-DO: check against digest and re-download on mismatch | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | return $self->ok(); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub suck { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
suck: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 0 | my ($self, $repo_url_p, $opt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | my($ok, @errs) = $self->_dissect_repo_url($repo_url_p); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 0 | ▶ | return ($ok, @errs) unless (is_ok($ok)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | my ($repo_url, $git_url, $repo_dir) = @errs; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 0 | $self->info("contemplating repo", $git_url, $repo_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ($ok, @errs) = $self->_clone_repo($git_url, $repo_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | ▶ | return ($ok, @errs) unless (is_ok($ok)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ($ok, @errs) = $self->_scan_for_external_downloads($git_url, $repo_dir); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 0 | ▶ | return ($ok, @errs) unless (is_ok($ok)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 0 | my ($total_sz, $to_dl_ar) = @errs; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my $n_dl = @$to_dl_ar; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | ▶ | if (!$n_dl) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | $self->dbg("found nothing to download", $repo_dir, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | return $self->ok(0, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $self->dbg("found stuff to download", {n_dl => $n_dl, total_sz => $total_sz}, 4); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | ($ok, @errs) = $self->_download_files($repo_url, $git_url, $repo_dir, $to_dl_ar); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | return ($ok, @errs) unless (is_ok($ok)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | $self->info("done downloading files for this repo", {n_downloaded => $n_dl, total_size_bytes => $total_sz}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | return $self->ok($n_dl, $total_sz); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 0 | ▶ | sub safely_to_json { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
safely_to_json: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | my ($self, $r) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | my $js = eval { $self->{js_or}->encode($r); }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | ▶ | $self->{ex} = $@ unless(defined($js)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | return $js; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | ▶ | sub safely_from_json { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
safely_from_json: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 0 | my ($self, $js) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | my $r = eval { $self->{js_or}->decode($js); }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | $self->{ex} = $@ unless(defined($r)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | return $r; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | sub most_similar { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
most_similar: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 0 | my ($self, $thing, $ar, $opt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | my $best_match = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 0 | my $best_score = $self->opt('min_score', 0, $opt_hr); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 0 | for my $x (@$ar) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 0 | my $score = similarity($thing, $x); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | next unless($score >= $best_score); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | $best_score = $score; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | $best_match = $x; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | return ($best_match, $best_score) if ($self->opt('best_match_and_score', 0, $opt_hr)); # for testing, mostly | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | return $best_match; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 2 | ▶ | sub opt { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
opt: called CC=5 SCAR=19.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 2 | my ($self, $name, $default_value, $alt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 2 | ▶ | $alt_hr //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$alt_hr //= {}
MC/DC0%0/1
$alt_hr //= {}
$alt_hr
Truth table50%1/2
A
A= $alt_hr
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 2 | ▶ | return $self->{opt_hr}->{$name} // $self->{conf_hr}->{$name} // $alt_hr->{$name} // $default_value; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition44%4/9
(($self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}) // $alt_hr->{$name}) // $default_value
($self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}) // $alt_hr->{$name}
$self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}
MC/DC25%1/4
(($self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}) // $alt_hr->{$name}) // $default_value
$self->{"opt_hr"}{$name}
$self->{"conf_hr"}{$name}
$alt_hr->{$name}
$default_value
Truth table40%2/5
A // B // C // D
A= $self->{"opt_hr"}{$name}B= $self->{"conf_hr"}{$name}C= $alt_hr->{$name}D= $default_value
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | # approximates python's "in" operator, because ~~ is unsane: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 0 | ▶ | sub in { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in: not called CC=10 SCAR=47.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | my $v = shift @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | ▶ | return 0 unless (@_ && defined($_[0])); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@_ and defined $_[0]
MC/DC0%0/2
@_ and defined $_[0]
@_
defined $_[0]
Truth table0%0/3
A and B
A= @_B= defined $_[0]
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | if (ref($_[0]) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | foreach my $x (@{$_[0]}) { return 1 if defined($x) && $x eq $v; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x and $x eq $v
MC/DC0%0/2
defined $x and $x eq $v
defined $x
$x eq $v
Truth table0%0/3
A and B
A= defined $xB= $x eq $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | ▶ | foreach my $x (@_) { return 1 if defined($x) && $x eq $v; } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x and $x eq $v
MC/DC0%0/2
defined $x and $x eq $v
defined $x
$x eq $v
Truth table0%0/3
A and B
A= defined $xB= $x eq $v
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | ▶ | sub stack_trace { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stack_trace: not called CC=9 SCAR=45.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 0 | my $level = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | $level //= 3; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$level //= 3
MC/DC0%0/1
$level //= 3
$level
Truth table0%0/2
A
A= $level
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | my @st; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | my $i=1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | my ($package, $file, $line, $sub) = caller($i++); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 0 | ▶ | $sub = $1 if (defined $sub && $sub =~ /^main::(.+)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $sub and $sub =~ /^main::(.+)/
MC/DC0%0/2
defined $sub and $sub =~ /^main::(.+)/
defined $sub
$sub =~ /^main::(.+)/
Truth table0%0/3
A and B
A= defined $subB= $sub =~ /^main::(.+)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | while(defined $package) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 0 | ▶ | push @st, [$file, $line, $sub] unless ($sub =~ /^(dbg|info|warn|err|crit)$/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ($package, $file, $line, $sub) = caller($i); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 0 | ▶ | $sub = $1 if (defined $sub && $sub =~ /^main::(.+)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $sub and $sub =~ /^main::(.+)/
MC/DC0%0/2
defined $sub and $sub =~ /^main::(.+)/
defined $sub
$sub =~ /^main::(.+)/
Truth table0%0/3
A and B
A= defined $subB= $sub =~ /^main::(.+)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | 0 | ▶ | last if ($i > $level); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 0 | $i++; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | return \@st; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 0 | ▶ | sub logger { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
logger: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | my ($self, $mode, $lvl, @stuff) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | ▶ | return ($mode, @stuff) if ($self->opt('no_log') || $self->opt('log',1) == 0); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->opt("no_log") or $self->opt("log", 1) == 0
MC/DC0%0/2
$self->opt("no_log") or $self->opt("log", 1) == 0
$self->opt("no_log")
$self->opt("log", 1) == 0
Truth table0%0/3
A or B
A= $self->opt("no_log")B= $self->opt("log", 1) == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 0 | ▶ | return ($mode, @stuff) if ($self->opt('log_level', 3) < $lvl); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | my $st_ar = $self->stack_trace(5); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | shift @$st_ar; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 0 | my $tm = tai(); # will not reflect leapseconds | |||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | my $lt = localtime(); # will reflect leapseconds and other artificial time skew | |||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | my $log_ar = [$tm, $lt, $$, $mode, $lvl, $self->{trace_ar}, $st_ar, @stuff]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | my $log_rec = $self->safely_to_json($log_ar) // "ERROR: $self->{ex}"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->safely_to_json($log_ar) // qq[ERROR: $self->{"ex"}]
MC/DC0%0/1
$self->safely_to_json($log_ar) // qq[ERROR: $self->{"ex"}]
$self->safely_to_json($log_ar)
Truth table0%0/2
A
A= $self->safely_to_json($log_ar)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | print STDERR $log_rec,"\n" if ($self->opt('show_log')); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | ▶ | print STDOUT $log_rec,"\n" if ($self->opt('show_log_to_stdout')); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | my $log_dir = $self->opt('log_dir', "/var/tmp"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 0 | my $log_file = $self->opt('logfile', "$log_dir/$self->{project_name}.log"); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | ▶ | File::Valet::ap_f($log_file, $log_rec."\n") unless($self->opt('no_logfile')); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 0 | return ($mode, @stuff); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # Add a log trace id to the trace list. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Returns the new trace list length, suitable for passing to trace_pop() or trace_set(). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 0 | ▶ | sub trace_push { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trace_push: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 0 | my ($self, $trace_id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | $trace_id //= sprintf('%08X', int(rand() * 0xFFFFFFFF)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
MC/DC0%0/1
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
$trace_id
Truth table0%0/2
A
A= $trace_id
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | push @{$self->{trace_ar}}, $trace_id; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | return scalar @{$self->{trace_ar}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | # Truncate the log trace list to a specific length, defaulting to one less the current length. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | # Returns the tuple of traces removed, if any. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | ▶ | sub trace_pop { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trace_pop: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 0 | my ($self, $to_level) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 0 | my $cur_level = scalar(@{$self->{trace_ar}}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | return () unless ($cur_level > 1); # don't let user empty trace list | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 0 | ▶ | $to_level //= $cur_level - 1; # default to popping most recent trace | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$to_level //= $cur_level - 1
MC/DC0%0/1
$to_level //= $cur_level - 1
$to_level
Truth table0%0/2
A
A= $to_level
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | my @traces; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | # could just splice(), but this is more understandable: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | while (scalar(@{$self->{trace_ar}}) > $to_level) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | push @traces, pop @{$self->{trace_ar}}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 0 | return @traces; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | # Set the topmost log trace id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | # optionally truncating first to a given length. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | # Returns nothing. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 0 | ▶ | sub trace_set { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
trace_set: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | my ($self, $trace_id, $to_level) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | 0 | my $cur_level = scalar(@{$self->{trace_ar}}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | $to_level //= $cur_level; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$to_level //= $cur_level
MC/DC0%0/2
$to_level //= $cur_level
$to_level
$cur_level
Truth table0%0/3
A //= B
A= $to_levelB= $cur_level
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 0 | ▶ | return unless ($to_level > 1); # don't let user overwrite trace[0] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | ▶ | $trace_id //= sprintf('%08X', int(rand() * 0xFFFFFFFF)); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
MC/DC0%0/1
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
$trace_id
Truth table0%0/2
A
A= $trace_id
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | ▶ | $self->trace_pop($to_level) if ($to_level < $cur_level); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | $self->{trace_ar}->[$to_level-1] = $trace_id; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 0 | ▶ | sub dbg { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
dbg: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | my $lvl = 4; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 0 | ▶ | if ($_[-1] =~ /^[0-9]$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | $lvl = pop @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | 0 | return $self->logger("DEBUG", $lvl, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | 0 | ▶ | sub crit { my $self = shift; return $self->logger("CRITICAL", 0, @_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
crit: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 0 | ▶ | sub err { my $self = shift; return $self->logger("ERROR", 1, @_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
err: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 0 | ▶ | sub warn { my $self = shift; return $self->logger("WARNING", 2, @_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
warn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 0 | ▶ | sub info { my $self = shift; return $self->logger("INFO", 3, @_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
info: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | ▶ | sub ok { my $self = shift; return ("OK", @_); } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ok: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 0 | ▶ | sub is_ok { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_ok: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | my ($mode) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 0 | ▶ | return 1 if ($mode eq 'OK'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 0 | ▶ | return 1 if ($mode eq 'WARNING'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | ▶ | return 1 if ($mode eq 'INFO'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | 0 | ▶ | sub xint { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xint: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 0 | ▶ | return 0 unless (defined $x && $x =~ /(\-?\d+)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x and $x =~ /(\-?\d+)/
MC/DC0%0/2
defined $x and $x =~ /(\-?\d+)/
defined $x
$x =~ /(\-?\d+)/
Truth table0%0/3
A and B
A= defined $xB= $x =~ /(\-?\d+)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 0 | return int($1); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 0 | ▶ | sub xfloat { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xfloat: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 0 | my ($x) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | ▶ | return 0 unless (defined $x && $x =~ /(\-?\d*\.?\d*)/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $x and $x =~ /(\-?\d*\.?\d*)/
MC/DC0%0/2
defined $x and $x =~ /(\-?\d*\.?\d*)/
defined $x
$x =~ /(\-?\d*\.?\d*)/
Truth table0%0/3
A and B
A= defined $xB= $x =~ /(\-?\d*\.?\d*)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 0 | my $v = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 0 | ▶ | return 0 unless ($v =~ /\d/); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | 0 | return $v + 0.0; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | # Loads any configuration files found in /etc, $HOME, or --conf=<FILENAME>, in that order. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # Thus parameters found in the $HOME config overrides parameters from the system config, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | # and parameters found in --conf override both. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | # Use --no-home-config to suppress looking in $HOME entirely. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | ▶ | sub load_config { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load_config: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 0 | my ($self, $name, $opt_hr) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | 0 | $self->{conf_hr} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | 0 | ▶ | $name //= $self->{project_name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$name //= $self->{"project_name"}
MC/DC0%0/2
$name //= $self->{"project_name"}
$name
$self->{"project_name"}
Truth table0%0/3
A //= B
A= $nameB= $self->{"project_name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | 0 | ▶ | my $home = $ENV{HOME} // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$ENV{"HOME"} // ""
MC/DC0%0/1
$ENV{"HOME"} // ""
$ENV{"HOME"}
Truth table0%0/2
A
A= $ENV{"HOME"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | my $home_config = ($home && !$self->opt('no_home_config')) ? "$home/.$name.conf" : ""; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$home and not $self->opt("no_home_config")
MC/DC0%0/2
$home and not $self->opt("no_home_config")
$home
not $self->opt("no_home_config")
Truth table0%0/3
A and not(B)
A= $homeB= not $self->opt("no_home_config")
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my $user_config = $self->opt('config', '', $opt_hr); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 0 | for my $f (grep {-e $_} ("/etc/$name.conf", $home_config, $user_config)) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | ▶ | my $hr = $self->safely_from_json(File::Valet::rd_f($f) // '{}'); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
File::Valet::rd_f($f) // "{}"
MC/DC0%0/1
File::Valet::rd_f($f) // "{}"
File::Valet::rd_f($f)
Truth table0%0/2
A
A= File::Valet::rd_f($f)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 0 | for my $k (keys %$hr) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | 0 | $self->{conf_hr}->{$k} = $hr->{$k}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||