lib/Suck/Huggingface.pm

filter: Statementstmt 14.4% 41 / 284 Branchbran 1.4% 2 / 138 Conditioncond 9.3% 8 / 86 MC/DCmcdc 1.9% 1 / 51 Subroutinesub 28.5% 10 / 35 Podpod 0.0% 0 / 23 totaltotal 10.0% 62 / 617 CC 112 SCAR 44.9
CC
112
Cov
10%
_download_files
342.0
_scan_for_external_downloads
134.8
_dissect_repo_url
110.0
CRAP 89.3
↑ Summary
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
BranchTrueFalseTotal
if $k0 eq $k1 3 0 3
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() // "."
l!l
10
MC/DC0%0/1
find_home() // "."
find_home()
Truth table50%1/2
A
A= find_home()
Aresult
11
00
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"
l!l
10
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"}
Aresult
11
00
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"};})
l!l&&r!l&&!r
001
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"};})
ABresult
1X1
011
000
45 1 ▶ die "cannot find wget executable! PATH=$path_env" unless (defined($self->{wget_bin}));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"wget_bin"} 1 0 1
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
BranchTrueFalseTotal
if $repo_url =~ /(.+?)\.git$/ 0 0 0
53 0 ▶ $repo_url = $1 if ($repo_url =~ /(.+?)\/$/);
Branch: 1 branch
BranchTrueFalseTotal
if $repo_url =~ m[(.+?)/$] 0 0 0
54  
55 0 ▶ return $self->err("skipping docs repo, unsupported", $repo_url) if ($repo_url =~ /^https:\/\/[^\/]+\/docs\//);
Branch: 1 branch
BranchTrueFalseTotal
if $repo_url =~ m[^https://[^/]+/docs/] 0 0 0
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
BranchTrueFalseTotal
if ($repo_url =~ m[^(https://[^/]+/datasets/[^/]+/[^/]+)]) { } 0 0 0
elsif ($repo_url =~ m[^(https://[^/]+/datasets/[^/]+)]) { } 0 0 0
elsif ($repo_url =~ m[^(https://[^/]+/[^/]+/[^/]+)]) { } 0 0 0
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
BranchTrueFalseTotal
if $repo_url =~ m[^(https:.+?)/(blob|tree|discussions)$] 0 0 0
77 0 ▶ $repo_url = $1 if ($repo_url =~ /^(https:.+?)\/(blob|tree|discussions)\//);
Branch: 1 branch
BranchTrueFalseTotal
if $repo_url =~ m[^(https:.+?)/(blob|tree|discussions)/] 0 0 0
78  
79 0 ▶ return $self->err("skipping unparseable repo_url", $repo_url) unless ($repo_url =~ /\/([^\/]+)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $repo_url =~ m[/([^/]+)$] 0 0 0
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
BranchTrueFalseTotal
if (-e $repo_dir) { } 0 0 0
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
BranchTrueFalseTotal
unless $self->opt("v") 0 0 0
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
BranchTrueFalseTotal
unless $self->{"testing"} 0 0 0
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
BranchTrueFalseTotal
unless -e $repo_dir 0 0 0
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
BranchTrueFalseTotal
if $exclude_csv 0 0 0
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
BranchTrueFalseTotal
unless $gf =~ m[^$repo_dir/(.+)] 0 0 0
113 0 my $f = $1;
114 0 $self->dbg("reduced file", $f, 7);
115 0 ▶ next if ($f =~ /^\./);
Branch: 1 branch
BranchTrueFalseTotal
if $f =~ /^\./ 0 0 0
116 0 $self->dbg("not a dot-file", $f, 7);
117 0 ▶ next if (-d $gf);
Branch: 1 branch
BranchTrueFalseTotal
if -d $gf 0 0 0
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>"
l!l
00
MC/DC0%0/1
$file_size // "<undef>"
$file_size
Truth table0%0/2
A
A= $file_size
Aresult
11
00
121 0 ▶ next if (!$file_size || $file_size > $sz_threshold);
Branch: 1 branch
BranchTrueFalseTotal
if not $file_size or $file_size > $sz_threshold 0 0 0
Condition0%0/3
not $file_size or $file_size > $sz_threshold
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $excl 0 0 0
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
BranchTrueFalseTotal
unless (defined $x) 0 0 0
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
BranchTrueFalseTotal
unless $x =~ /^\s*version\s+([^\s]+)\s*[\r\n]+\s*oid\s+(\w+):([^\s]+)\s*[\r\n]+\s*size\s+(\d+)/ 0 0 0
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
BranchTrueFalseTotal
if (index($f, $x) != -1) 0 0 0
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")
l!l&&r!l&&!r
000
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")
ABresult
1X1
011
000
156 0 ▶ my $un = $self->opt('user') // $self->opt('username');
Condition0%0/3
$self->opt("user") // $self->opt("username")
l!l&&r!l&&!r
000
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")
ABresult
1X1
011
000
157 0 ▶ my $pw = $self->opt('pw') // $self->opt('password');
Condition0%0/3
$self->opt("pw") // $self->opt("password")
l!l&&r!l&&!r
000
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")
ABresult
1X1
011
000
158 0 ▶ $wget_opts .= "-q " unless ($self->opt('v', 0));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->opt("v", 0) 0 0 0
159 0 ▶ $wget_opts .= "--limit-rate=$rl " if ($rl && $rl =~ /^[\d\.]+\w?$/); # eg: "2.5m" or "500k"
Branch: 1 branch
BranchTrueFalseTotal
if $rl and $rl =~ /^[\d\.]+\w?$/ 0 0 0
Condition0%0/3
$rl and $rl =~ /^[\d\.]+\w?$/
!ll&&!rl&&r
000
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?$/
ABresult
0X0
100
111
160 0 ▶ $wget_opts .= "--user=$un " if (defined $un);
Branch: 1 branch
BranchTrueFalseTotal
if defined $un 0 0 0
161 0 ▶ $wget_opts .= "--password=$pw " if (defined $pw);
Branch: 1 branch
BranchTrueFalseTotal
if defined $pw 0 0 0
162 0 my $n_retries = $self->opt('retries', 1000);
163 0 ▶ $n_retries = 1000 if ($n_retries < 1);
Branch: 1 branch
BranchTrueFalseTotal
if $n_retries < 1 0 0 0
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
BranchTrueFalseTotal
if ($f =~ /(.+?)\.orig$/) 0 0 0
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
BranchTrueFalseTotal
if (-e "$repo_dir/$tf") 0 0 0
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
BranchTrueFalseTotal
if $mk_orig 0 0 0
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"
l!l
00
MC/DC0%0/1
$self->{"wget_bin"} // "wget"
$self->{"wget_bin"}
Truth table0%0/2
A
A= $self->{"wget_bin"}
Aresult
11
00
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
BranchTrueFalseTotal
unless $self->{"testing"} 0 0 0
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
l!l
00
MC/DC0%0/1
$f_hr->{"sz"} // 0
$f_hr->{"sz"}
Truth table0%0/2
A
A= $f_hr->{"sz"}
Aresult
11
00
190 0 ▶ $kbps = $1 if ($kbps + 0.0005 =~ /^(\d+\.\d{3})/);
Branch: 1 branch
BranchTrueFalseTotal
if $kbps + "0.0005" =~ /^(\d+\.\d{3})/ 0 0 0
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
BranchTrueFalseTotal
unless is_ok($ok) 0 0 0
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
BranchTrueFalseTotal
unless is_ok($ok) 0 0 0
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
BranchTrueFalseTotal
unless is_ok($ok) 0 0 0
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
BranchTrueFalseTotal
unless ($n_dl) 0 0 0
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
BranchTrueFalseTotal
unless is_ok($ok) 0 0 0
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
BranchTrueFalseTotal
unless defined $js 0 0 0
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
BranchTrueFalseTotal
unless defined $r 0 0 0
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
BranchTrueFalseTotal
unless $score >= $best_score 0 0 0
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
BranchTrueFalseTotal
if $self->opt("best_match_and_score", 0, $opt_hr) 0 0 0
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 //= {}
l!l
02
MC/DC0%0/1
$alt_hr //= {}
$alt_hr
Truth table50%1/2
A
A= $alt_hr
Aresult
11
00
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
l!l&&r!l&&!r
011
($self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}) // $alt_hr->{$name}
l!l&&r!l&&!r
002
$self->{"opt_hr"}{$name} // $self->{"conf_hr"}{$name}
l!l&&r!l&&!r
002
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
ABCDresult
1XXX1
01XX1
001X1
00011
00000
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
BranchTrueFalseTotal
unless @_ and defined $_[0] 0 0 0
Condition0%0/3
@_ and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and defined $_[0]
@_ defined $_[0]
Truth table0%0/3
A and B
A= @_B= defined $_[0]
ABresult
0X0
100
111
265 0 ▶ if (ref($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 0 0 0
266 0 ▶ foreach my $x (@{$_[0]}) { return 1 if defined($x) && $x eq $v; }
Branch: 1 branch
BranchTrueFalseTotal
if defined $x and $x eq $v 0 0 0
Condition0%0/3
defined $x and $x eq $v
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
267 } else {
268 0 ▶ foreach my $x (@_) { return 1 if defined($x) && $x eq $v; }
Branch: 1 branch
BranchTrueFalseTotal
if defined $x and $x eq $v 0 0 0
Condition0%0/3
defined $x and $x eq $v
!ll&&!rl&&r
000
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
ABresult
0X0
100
111
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
l!l
00
MC/DC0%0/1
$level //= 3
$level
Truth table0%0/2
A
A= $level
Aresult
11
00
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
BranchTrueFalseTotal
if defined $sub and $sub =~ /^main::(.+)/ 0 0 0
Condition0%0/3
defined $sub and $sub =~ /^main::(.+)/
!ll&&!rl&&r
000
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::(.+)/
ABresult
0X0
100
111
281 0 ▶ while(defined $package) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $package) 0 0 0
282 0 ▶ push @st, [$file, $line, $sub] unless ($sub =~ /^(dbg|info|warn|err|crit)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /^(dbg|info|warn|err|crit)$/ 0 0 0
283 0 ($package, $file, $line, $sub) = caller($i);
284 0 ▶ $sub = $1 if (defined $sub && $sub =~ /^main::(.+)/);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sub and $sub =~ /^main::(.+)/ 0 0 0
Condition0%0/3
defined $sub and $sub =~ /^main::(.+)/
!ll&&!rl&&r
000
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::(.+)/
ABresult
0X0
100
111
285 0 ▶ last if ($i > $level);
Branch: 1 branch
BranchTrueFalseTotal
if $i > $level 0 0 0
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
BranchTrueFalseTotal
if $self->opt("no_log") or $self->opt("log", 1) == 0 0 0 0
Condition0%0/3
$self->opt("no_log") or $self->opt("log", 1) == 0
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
294 0 ▶ return ($mode, @stuff) if ($self->opt('log_level', 3) < $lvl);
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("log_level", 3) < $lvl 0 0 0
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"}]
l!l
00
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)
Aresult
11
00
301 0 ▶ print STDERR $log_rec,"\n" if ($self->opt('show_log'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("show_log") 0 0 0
302 0 ▶ print STDOUT $log_rec,"\n" if ($self->opt('show_log_to_stdout'));
Branch: 1 branch
BranchTrueFalseTotal
if $self->opt("show_log_to_stdout") 0 0 0
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
BranchTrueFalseTotal
unless $self->opt("no_logfile") 0 0 0
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))
l!l
00
MC/DC0%0/1
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
$trace_id
Truth table0%0/2
A
A= $trace_id
Aresult
11
00
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
BranchTrueFalseTotal
unless $cur_level > 1 0 0 0
324 0 ▶ $to_level //= $cur_level - 1; # default to popping most recent trace
Condition0%0/2
$to_level //= $cur_level - 1
l!l
00
MC/DC0%0/1
$to_level //= $cur_level - 1
$to_level
Truth table0%0/2
A
A= $to_level
Aresult
11
00
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
BranchTrueFalseTotal
if (scalar @{$self->{"trace_ar"};} > $to_level) 0 0 0
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
l!l&&r!l&&!r
000
MC/DC0%0/2
$to_level //= $cur_level
$to_level $cur_level
Truth table0%0/3
A //= B
A= $to_levelB= $cur_level
ABresult
1X1
011
000
340 0 ▶ return unless ($to_level > 1); # don't let user overwrite trace[0]
Branch: 1 branch
BranchTrueFalseTotal
unless $to_level > 1 0 0 0
341 0 ▶ $trace_id //= sprintf('%08X', int(rand() * 0xFFFFFFFF));
Condition0%0/2
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
l!l
00
MC/DC0%0/1
$trace_id //= sprintf("%08X", int((rand()) * 4294967295))
$trace_id
Truth table0%0/2
A
A= $trace_id
Aresult
11
00
342 0 ▶ $self->trace_pop($to_level) if ($to_level < $cur_level);
Branch: 1 branch
BranchTrueFalseTotal
if $to_level < $cur_level 0 0 0
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
BranchTrueFalseTotal
if ($_[-1] =~ /^[0-9]$/) 0 0 0
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
BranchTrueFalseTotal
if $mode eq "OK" 0 0 0
365 0 ▶ return 1 if ($mode eq 'WARNING');
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "WARNING" 0 0 0
366 0 ▶ return 1 if ($mode eq 'INFO');
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "INFO" 0 0 0
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
BranchTrueFalseTotal
unless defined $x and $x =~ /(\-?\d+)/ 0 0 0
Condition0%0/3
defined $x and $x =~ /(\-?\d+)/
!ll&&!rl&&r
000
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+)/
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $x and $x =~ /(\-?\d*\.?\d*)/ 0 0 0
Condition0%0/3
defined $x and $x =~ /(\-?\d*\.?\d*)/
!ll&&!rl&&r
000
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*)/
ABresult
0X0
100
111
379 0 my $v = $1;
380 0 ▶ return 0 unless ($v =~ /\d/);
Branch: 1 branch
BranchTrueFalseTotal
unless $v =~ /\d/ 0 0 0
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"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$name //= $self->{"project_name"}
$name $self->{"project_name"}
Truth table0%0/3
A //= B
A= $nameB= $self->{"project_name"}
ABresult
1X1
011
000
392 0 ▶ my $home = $ENV{HOME} // '';
Condition0%0/2
$ENV{"HOME"} // ""
l!l
00
MC/DC0%0/1
$ENV{"HOME"} // ""
$ENV{"HOME"}
Truth table0%0/2
A
A= $ENV{"HOME"}
Aresult
11
00
393 0 ▶ my $home_config = ($home && !$self->opt('no_home_config')) ? "$home/.$name.conf" : "";
Branch: 1 branch
BranchTrueFalseTotal
$home && !$self->opt('no_home_config') ? : 0 0 0
Condition0%0/3
$home and not $self->opt("no_home_config")
!ll&&!rl&&r
000
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")
ABresult
0X0
100
111
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) // "{}"
l!l
00
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)
Aresult
11
00
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;
↑ Summary