blib/lib/PDL/ML/Hub.pm

filter: Statementstmt 81.3% 83 / 102 Branchbran 46.4% 26 / 56 Conditioncond 37.3% 25 / 67 MC/DCmcdc 0.0% 0 / 36 Subroutinesub 94.4% 17 / 18 Podpod 0.0% 0 / 7 totaltotal 52.8% 151 / 286 CC 54 SCAR 28.1
CC
54
Cov
60%
cache_path
38.1
parse_uri
20.3
auth_headers
20.3
CRAP 16.6
↑ Summary
1 package PDL::ML::Hub;
2  
3 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Cwd qw(abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use MIME::Base64 qw(encode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.012';
15  
16 2 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod undocumented
17 2 my ($class, %args) = @_;
18 my $cache_dir = $args{cache_dir}
19 || $ENV{PDL_ML_HUB_CACHE}
20 2 ▶ || File::Spec->catdir($ENV{XDG_CACHE_HOME} || File::Spec->catdir($ENV{HOME} || '/tmp', '.cache'), 'pdl-ml-hub');
Condition54%6/11
$args{'cache_dir'} || $ENV{'PDL_ML_HUB_CACHE'} || 'File::Spec'->catdir($ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache'), 'pdl-ml-hub')
l!l&&r!l&&!r
110
$args{"cache_dir"} or $ENV{"PDL_ML_HUB_CACHE"}
l!l&&r!l&&!r
101
$ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache')
l!l&&r!l&&!r
010
$ENV{'HOME'} || '/tmp'
l!l
10
MC/DC0%0/3
$args{'cache_dir'} || $ENV{'PDL_ML_HUB_CACHE'} || 'File::Spec'->catdir($ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache'), 'pdl-ml-hub')
$args{"cache_dir"} $ENV{"PDL_ML_HUB_CACHE"} 'File::Spec'->catdir($ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache'), 'pdl-ml-hub')
MC/DC0%0/2
$ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache')
$ENV{'XDG_CACHE_HOME'} 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache')
MC/DC0%0/1
$ENV{'HOME'} || '/tmp'
$ENV{'HOME'}
Truth table50%2/4
A or B || C
A= $args{"cache_dir"}B= $ENV{"PDL_ML_HUB_CACHE"}C= 'File::Spec'->catdir($ENV{'XDG_CACHE_HOME'} || 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache'), 'pdl-ml-hub')
ABCresult
1XX1
01X1
0011
0000
Truth table33%1/3
A || B
A= $ENV{'XDG_CACHE_HOME'}B= 'File::Spec'->catdir($ENV{'HOME'} || '/tmp', '.cache')
ABresult
1X1
011
000
Truth table50%1/2
A
A= $ENV{'HOME'}
Aresult
11
00
21 return bless {
22 cache_dir => $cache_dir,
23 2 ▶ http => $args{http} || HTTP::Tiny->new(agent => "PDL-ML-Hub/$VERSION"),
Condition66%2/3
$args{'http'} || 'HTTP::Tiny'->new('agent', "PDL-ML-Hub/$PDL::ML::Hub::VERSION")
l!l&&r!l&&!r
110
MC/DC0%0/2
$args{'http'} || 'HTTP::Tiny'->new('agent', "PDL-ML-Hub/$PDL::ML::Hub::VERSION")
$args{'http'} 'HTTP::Tiny'->new('agent', "PDL-ML-Hub/$PDL::ML::Hub::VERSION")
Truth table66%2/3
A || B
A= $args{'http'}B= 'HTTP::Tiny'->new('agent', "PDL-ML-Hub/$PDL::ML::Hub::VERSION")
ABresult
1X1
011
000
24 }, $class;
25 }
26  
27 0 ▶sub cache_dir { $_[0]->{cache_dir} }
Subroutine cache_dir: not called CC=1 SCAR=6.9
Pod undocumented
28  
29 4 ▶sub parse_uri {
Subroutine parse_uri: called CC=10 SCAR=30.1
Pod undocumented
30 4 my ($self, $uri) = @_;
31 4 ▶ croak 'URI required' unless defined $uri && length $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uri and length $uri 0 4 4
Condition33%1/3
defined $uri and length $uri
!ll&&!rl&&r
004
MC/DC0%0/2
defined $uri and length $uri
defined $uri length $uri
Truth table33%1/3
A and B
A= defined $uriB= length $uri
ABresult
0X0
100
111
32  
33 4 ▶ if ($uri =~ m{^kaggle://([^/]+)/([^/]+)/([^/]+)/([^/]+)(?:/(.+))?$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^kaggle://([^/]+)/([^/]+)/([^/]+)/([^/]+)(?:/(.+))?$]u) 1 3 4
34 1 ▶ return { provider => 'kaggle', owner => $1, model => $2, framework => $3, variation => $4, file => $5 || '' };
Condition50%1/2
$5 || ''
l!l
10
MC/DC0%0/1
$5 || ''
$5
Truth table50%1/2
A
A= $5
Aresult
11
00
35 }
36 3 ▶ if ($uri =~ m{^gs://([^/]+)/(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^gs://([^/]+)/(.+)$]u) 2 1 3
37 2 return { provider => 'gcs', bucket => $1, object => $2 };
38 }
39 1 ▶ if ($uri =~ m{^hf://([^/]+/[^/]+?)(?:\@([^/]+))?/(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^hf://([^/]+/[^/]+?)(?:\@([^/]+))?/(.+)$]u) 1 0 1
40 1 ▶ return { provider => 'huggingface', repo => $1, revision => $2 || 'main', file => $3 };
Condition50%1/2
$2 || 'main'
l!l
10
MC/DC0%0/1
$2 || 'main'
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
41 }
42 0 ▶ if ($uri =~ m{^oci://([^/]+)/(.+)$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^oci://([^/]+)/(.+)$]u) 0 0 0
43 0 return { provider => 'oci', registry => $1, ref => $2 };
44 }
45 0 ▶ if ($uri =~ m{^https?://}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^https?://]u) 0 0 0
46 0 return { provider => 'http', url => $uri };
47 }
48 0 croak "Unsupported model hub URI: $uri";
49 }
50  
51 4 ▶sub resolve_url {
Subroutine resolve_url: called CC=7 SCAR=21.8
Pod undocumented
52 4 my ($self, $uri) = @_;
53 4 ▶ my $p = ref($uri) eq 'HASH' ? $uri : $self->parse_uri($uri);
Branch: 1 branch
BranchTrueFalseTotal
ref $uri eq 'HASH' ? : 4 0 4
54 4 ▶ if ($p->{provider} eq 'kaggle') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"provider"} eq "kaggle") 1 3 4
55 1 my $base = "https://www.kaggle.com/api/v1/models/$p->{owner}/$p->{model}/$p->{framework}/$p->{variation}/1/download";
56 1 ▶ return $p->{file} ? "$base/$p->{file}" : $base;
Branch: 1 branch
BranchTrueFalseTotal
$p->{'file'} ? : 1 0 1
57 }
58 3 ▶ if ($p->{provider} eq 'gcs') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"provider"} eq "gcs") 2 1 3
59 2 return "https://storage.googleapis.com/$p->{bucket}/$p->{object}";
60 }
61 1 ▶ if ($p->{provider} eq 'huggingface') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"provider"} eq "huggingface") 1 0 1
62 1 return "https://huggingface.co/$p->{repo}/resolve/$p->{revision}/$p->{file}";
63 }
64 0 ▶ if ($p->{provider} eq 'http') {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"provider"} eq "http") 0 0 0
65 0 return $p->{url};
66 }
67 0 croak "resolve_url not supported directly for provider '$p->{provider}'";
68 }
69  
70 1 ▶sub auth_headers {
Subroutine auth_headers: called CC=8 SCAR=30.1
Pod undocumented
71 1 my ($self, $provider) = @_;
72 1 my %h;
73 1 ▶ if ($provider eq 'kaggle' && $ENV{KAGGLE_USERNAME} && $ENV{KAGGLE_KEY}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($provider eq 'kaggle' and $ENV{'KAGGLE_USERNAME'} and $ENV{'KAGGLE_KEY'}) { } 0 1 1
elsif ($provider eq 'huggingface' and $ENV{'HF_TOKEN'}) { } 0 1 1
elsif ($provider eq 'gcs' and $ENV{'GCS_ACCESS_TOKEN'}) { } 0 1 1
Condition33%2/6
$provider eq "kaggle" and $ENV{"KAGGLE_USERNAME"} and $ENV{"KAGGLE_KEY"}
!ll&&!rl&&r
100
$provider eq "kaggle" and $ENV{"KAGGLE_USERNAME"}
!ll&&!rl&&r
100
MC/DC0%0/3
$provider eq "kaggle" and $ENV{"KAGGLE_USERNAME"} and $ENV{"KAGGLE_KEY"}
$provider eq "kaggle" $ENV{"KAGGLE_USERNAME"} $ENV{"KAGGLE_KEY"}
Truth table25%1/4
A and B and C
A= $provider eq "kaggle"B= $ENV{"KAGGLE_USERNAME"}C= $ENV{"KAGGLE_KEY"}
ABCresult
0XX0
10X0
1100
1111
74 0 my $tok = encode_base64("$ENV{KAGGLE_USERNAME}:$ENV{KAGGLE_KEY}", '');
75 0 $h{Authorization} = "Basic $tok";
76 ▶ } elsif ($provider eq 'huggingface' && $ENV{HF_TOKEN}) {
Condition33%1/3
$provider eq "huggingface" and $ENV{"HF_TOKEN"}
!ll&&!rl&&r
100
MC/DC0%0/2
$provider eq "huggingface" and $ENV{"HF_TOKEN"}
$provider eq "huggingface" $ENV{"HF_TOKEN"}
Truth table33%1/3
A and B
A= $provider eq "huggingface"B= $ENV{"HF_TOKEN"}
ABresult
0X0
100
111
77 0 $h{Authorization} = "Bearer $ENV{HF_TOKEN}";
78 ▶ } elsif ($provider eq 'gcs' && $ENV{GCS_ACCESS_TOKEN}) {
Condition33%1/3
$provider eq "gcs" and $ENV{"GCS_ACCESS_TOKEN"}
!ll&&!rl&&r
010
MC/DC0%0/2
$provider eq "gcs" and $ENV{"GCS_ACCESS_TOKEN"}
$provider eq "gcs" $ENV{"GCS_ACCESS_TOKEN"}
Truth table33%1/3
A and B
A= $provider eq "gcs"B= $ENV{"GCS_ACCESS_TOKEN"}
ABresult
0X0
100
111
79 0 $h{Authorization} = "Bearer $ENV{GCS_ACCESS_TOKEN}";
80 }
81 1 return \%h;
82 }
83  
84 4 ▶sub _validate_segment {
Subroutine _validate_segment: called CC=8 SCAR=27.1
85 4 my ($seg) = @_;
86 4 ▶ croak 'Invalid empty path segment in model hub URI'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seg and length $seg 0 4 4
Condition33%1/3
defined $seg and length $seg
!ll&&!rl&&r
004
MC/DC0%0/2
defined $seg and length $seg
defined $seg length $seg
Truth table33%1/3
A and B
A= defined $segB= length $seg
ABresult
0X0
100
111
87 if !defined $seg || !length $seg;
88 4 ▶ croak 'Invalid path segment containing NUL or backslash'
Branch: 1 branch
BranchTrueFalseTotal
if $seg =~ /[\0\\]/u 0 4 4
89 if $seg =~ /[\0\\]/;
90 4 ▶ croak "Path traversal segment '$seg' not allowed in model hub URI"
Branch: 1 branch
BranchTrueFalseTotal
if $seg eq "." or $seg eq ".." or $seg =~ m[^/]u or $seg =~ /^[A-Za-z]:/u 0 4 4
Condition33%3/9
$seg eq "." or $seg eq ".." or $seg =~ m[^/]u or $seg =~ /^[A-Za-z]:/u
l!l&&r!l&&!r
004
$seg eq "." or $seg eq ".." or $seg =~ m[^/]u
l!l&&r!l&&!r
004
$seg eq "." or $seg eq ".."
l!l&&r!l&&!r
004
MC/DC0%0/4
$seg eq "." or $seg eq ".." or $seg =~ m[^/]u or $seg =~ /^[A-Za-z]:/u
$seg eq "." $seg eq ".." $seg =~ m[^/]u $seg =~ /^[A-Za-z]:/u
Truth table20%1/5
A or B or C or D
A= $seg eq "."B= $seg eq ".."C= $seg =~ m[^/]uD= $seg =~ /^[A-Za-z]:/u
ABCDresult
1XXX1
01XX1
001X1
00011
00000
91 if $seg eq '.' || $seg eq '..' || $seg =~ m{^/} || $seg =~ m{^[A-Za-z]:};
92 4 return $seg;
93 }
94  
95 1 ▶sub cache_path {
Subroutine cache_path: called CC=10 SCAR=36.4
Pod undocumented
96 1 my ($self, $uri) = @_;
97 1 ▶ my $p = ref($uri) eq 'HASH' ? $uri : $self->parse_uri($uri);
Branch: 1 branch
BranchTrueFalseTotal
ref $uri eq 'HASH' ? : 1 0 1
98 1 my @parts;
99 1 ▶ if ($p->{provider} eq 'kaggle') {
Branch: 3 branches
BranchTrueFalseTotal
if ($p->{'provider'} eq 'kaggle') { } 0 1 1
elsif ($p->{'provider'} eq 'gcs') { } 1 0 1
elsif ($p->{'provider'} eq 'huggingface') { } 0 0 0
100 0 ▶ my @file_parts = length($p->{file}) ? split(m{/}, $p->{file}, -1) : ('archive.tar.gz');
Branch: 1 branch
BranchTrueFalseTotal
length $p->{'file'} ? : 0 0 0
101 0 @parts = ('kaggle', $p->{owner}, $p->{model}, $p->{framework}, $p->{variation}, @file_parts);
102 } elsif ($p->{provider} eq 'gcs') {
103 1 @parts = ('gcs', $p->{bucket}, split(m{/}, $p->{object}, -1));
104 } elsif ($p->{provider} eq 'huggingface') {
105 0 @parts = ('hf', split(m{/}, $p->{repo}, -1), $p->{revision}, split(m{/}, $p->{file}, -1));
106 } else {
107 0 ▶ my $raw = ref($uri) eq 'HASH' ? ($p->{url} // '') : $uri;
Branch: 1 branch
BranchTrueFalseTotal
ref $uri eq 'HASH' ? : 0 0 0
Condition0%0/2
$p->{"url"} // ""
l!l
00
MC/DC0%0/1
$p->{"url"} // ""
$p->{"url"}
Truth table0%0/2
A
A= $p->{"url"}
Aresult
11
00
108 0 (my $safe = $raw) =~ s{[^A-Za-z0-9._-]+}{_}g;
109 0 ▶ @parts = ('http', $safe || 'download');
Condition0%0/2
$safe || 'download'
l!l
00
MC/DC0%0/1
$safe || 'download'
$safe
Truth table0%0/2
A
A= $safe
Aresult
11
00
110 }
111 1 _validate_segment($_) for @parts;
112 1 return File::Spec->catfile($self->{cache_dir}, @parts);
113 }
114  
115 1 ▶sub fetch {
Subroutine fetch: called CC=11 SCAR=28.7
Pod undocumented
116 1 my ($self, $uri, %opts) = @_;
117 1 my $parsed = $self->parse_uri($uri);
118 1 ▶ my $dest = $opts{dest} || $self->cache_path($parsed);
Condition33%1/3
$opts{'dest'} || $self->cache_path($parsed)
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts{'dest'} || $self->cache_path($parsed)
$opts{'dest'} $self->cache_path($parsed)
Truth table33%1/3
A || B
A= $opts{'dest'}B= $self->cache_path($parsed)
ABresult
1X1
011
000
119 1 ▶ return $dest if -f $dest && !$opts{force};
Branch: 1 branch
BranchTrueFalseTotal
if -f $dest and not $opts{"force"} 0 1 1
Condition33%1/3
-f $dest and not $opts{"force"}
!ll&&!rl&&r
100
MC/DC0%0/2
-f $dest and not $opts{"force"}
-f $dest not $opts{"force"}
Truth table33%1/3
A and not(B)
A= -f $destB= not $opts{"force"}
ABresult
0X0
100
111
120  
121 1 my $dest_dir = dirname($dest);
122 1 make_path($dest_dir);
123 1 ▶ if (!$opts{dest}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"dest"}) 1 0 1
124 1 make_path($self->{cache_dir});
125 1 my $abs_cache = abs_path($self->{cache_dir});
126 1 my $abs_dir = abs_path($dest_dir);
127 1 ▶ croak "Resolved cache path '$abs_dir' escapes cache_dir '$abs_cache'"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $abs_cache and defined $abs_dir and $abs_dir eq $abs_cache || index($abs_dir, "$abs_cache/") == 0 || index($abs_dir, "$abs_cache\\") == 0 0 1 1
Condition33%4/12
defined $abs_cache and defined $abs_dir and $abs_dir eq $abs_cache || index($abs_dir, "$abs_cache/") == 0 || index($abs_dir, "$abs_cache\\") == 0
!ll&&!rl&&r
001
defined $abs_cache and defined $abs_dir
!ll&&!rl&&r
001
$abs_dir eq $abs_cache or index($abs_dir, "$abs_cache/") == 0 or index($abs_dir, "$abs_cache\\") == 0
l!l&&r!l&&!r
100
$abs_dir eq $abs_cache or index($abs_dir, "$abs_cache/") == 0
l!l&&r!l&&!r
010
MC/DC0%0/5
defined $abs_cache and defined $abs_dir and $abs_dir eq $abs_cache || index($abs_dir, "$abs_cache/") == 0 || index($abs_dir, "$abs_cache\\") == 0
defined $abs_cache defined $abs_dir $abs_dir eq $abs_cache index($abs_dir, "$abs_cache/") == 0 index($abs_dir, "$abs_cache\\") == 0
Truth table16%1/6
A and B and C or D or E
A= defined $abs_cacheB= defined $abs_dirC= $abs_dir eq $abs_cacheD= index($abs_dir, "$abs_cache/") == 0E= index($abs_dir, "$abs_cache\\") == 0
ABCDEresult
0XXXX0
10XXX0
110000
111XX1
1101X1
110011
128 unless defined $abs_cache && defined $abs_dir
129 && ($abs_dir eq $abs_cache || index($abs_dir, "$abs_cache/") == 0 || index($abs_dir, "$abs_cache\\") == 0);
130 }
131 1 my $url = $self->resolve_url($parsed);
132 1 my $headers = $self->auth_headers($parsed->{provider});
133 1 my $res = $self->{http}->mirror($url, $dest, { headers => $headers });
134 croak "Failed to fetch $uri ($url): $res->{status} $res->{reason}"
135 1 ▶ unless $res->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"success"} 0 1 1
136 1 return $dest;
137 }
138  
139 2 1;
140  
141 __END__
↑ Summary