API/Sync.pm

filter: Statementstmt 53.1% 76 / 143 Branchbran 23.0% 12 / 52 Conditioncond 11.1% 2 / 18 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 75.0% 21 / 28 Podpod 100% 8 / 8 totaltotal 45.9% 120 / 261 CC 34 SCAR 38.4
CC
34
Cov
42%
update_index
182.0
unzip
21.2
regex_for_uri_template
20.0
CRAP 46.4
↑ Summary API/Indexer.pm »
1 package PGXN::API::Sync;
2  
3 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use PGXN::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use PGXN::API::Indexer;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Digest::SHA1;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Spec::Functions qw(catfile path rel2abs tmpdir catdir);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Archive::Zip qw(:ERROR_CODES);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use constant WIN32 => $^O eq 'MSWin32';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 our $VERSION = v0.22.1;
18  
19 2 ▶subtype Executable => as 'Str', where {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 my $exe = $_;
21 2 ▶ first { -f $_ && -x _ } $exe, map { catfile $_, $exe } path;
Subroutine __ANON__: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
-f $_ and -x _ 2 0 2
22 2 };
23  
24 2 has rsync_path => (is => 'rw', isa => 'Executable', default => 'rsync', required => 1);
25 2 has source => (is => 'rw', isa => 'Str', required => 1);
26 2 has base_url => (is => 'rw', isa => 'Str', default => 'https://api.pgxn.org', required => 1);
27 2 has verbose => (is => 'rw', isa => 'Int', default => 0);
28 2 ▶has log_file => (is => 'rw', isa => 'Str', required => 1, default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 catfile tmpdir, "pgxn-api-sync-$$.txt"
30 2 });
31  
32 0 ▶has mirror_uri_templates => (is => 'ro', isa => 'HashRef', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
33 0 my $self = shift;
34 0 my $api = PGXN::API->instance;
35 0 my $tmpl = $api->read_json_from(catfile $api->mirror_root, 'index.json');
36 0 return { map { $_ => URI::Template->new($tmpl->{$_}) } keys %{ $tmpl } };
37 2 });
38  
39 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
40 0 my $self = shift;
41 0 $self->run_rsync;
42 0 $self->update_index;
43 }
44  
45 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
46 2 my $self = shift;
47 2 unlink $self->log_file;
48 2 $self->SUPER::DESTROY;
49 }
50  
51 1 ▶sub run_rsync {
Subroutine run_rsync: called CC=3 SCAR=11.7
Pod documented
52 1 my $self = shift;
53  
54 # Sync the mirror.
55 1 ▶ say "Updating the mirror from ", $self->source if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 1 1
56 1 ▶ system (
Branch: 1 branch
BranchTrueFalseTotal
system($self->rsync_path, ("--archive", "--compress", "--delete", "--quiet"), "--log-file-format", "%i %n", "--log-file", $self->log_file, $self->source, "PGXN::API"->instance->mirror_root) == 0 or die 0 1 1
57 $self->rsync_path,
58 qw(--archive --compress --delete --quiet),
59 '--log-file-format' => '%i %n',
60 '--log-file' => $self->log_file,
61 $self->source,
62 PGXN::API->instance->mirror_root,
63 ) == 0 or die;
64 }
65  
66 0 ▶sub update_index {
Subroutine update_index: not called CC=13 SCAR=52.0
Pod documented
67 0 my $self = shift;
68 0 my $indexer = PGXN::API::Indexer->new(verbose => $self->verbose);
69  
70 0 my $meta_re = $self->regex_for_uri_template('meta');
71 0 my $mirr_re = $self->regex_for_uri_template('mirrors');
72 0 my $spec_re = $self->regex_for_uri_template('spec');
73 0 my $stat_re = $self->regex_for_uri_template('stats');
74 0 my $user_re = $self->regex_for_uri_template('user');
75 0 my $log = $self->log_file;
76  
77 0 ▶ say 'Parsing the rsync log file' if $self->verbose > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 1 0 0 0
78 0 ▶ open my $fh, '<:encoding(UTF-8)', $log or die "Cannot open $log: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $log or die "Cannot open ${log}: $!\n" 0 0 0
79 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
80 0 ▶ if ($line =~ $meta_re) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /$meta_re/u) { } 0 0 0
elsif ($line =~ /$stat_re/u or $line =~ /$mirr_re/u) { } 0 0 0
elsif ($line =~ /$spec_re/u) { } 0 0 0
elsif ($line =~ /\s>f(?:[+]+|(?:c|.s|..t)[^ ]+)\sindex[.]json$/u) { } 0 0 0
elsif ($line =~ /$user_re/u) { } 0 0 0
81 0 ▶ if (my $params = $self->validate_distribution($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $params = $self->validate_distribution($1)) 0 0 0
82 0 $indexer->add_distribution($params);
83 }
84 }
85 ▶ elsif ($line =~ $stat_re || $line =~ $mirr_re) {
Condition0%0/3
$line =~ /$stat_re/u or $line =~ /$mirr_re/u
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /$stat_re/u or $line =~ /$mirr_re/u
$line =~ /$stat_re/u $line =~ /$mirr_re/u
Truth table0%0/3
A or B
A= $line =~ /$stat_re/uB= $line =~ /$mirr_re/u
ABresult
1X1
011
000
86 0 $indexer->copy_from_mirror($1);
87 }
88 elsif ($line =~ $spec_re) {
89 0 my $path = $1;
90 0 $indexer->copy_from_mirror($path);
91 0 $indexer->parse_from_mirror($path, 'Markdown');
92 }
93 elsif ($line =~ /\s>f(?:[+]+|(?:c|.s|..t)[^ ]+)\sindex[.]json$/) {
94 # Always update the index JSON if it's mentioned.
95 0 $indexer->update_root_json;
96 }
97 elsif ($line =~ $user_re) {
98 0 $indexer->merge_user($2);
99 }
100 }
101 0 ▶ close $fh or die "Cannot close $log: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Cannot close ${log}: $!\n" 0 0 0
102 0 $indexer->finalize;
103 0 ▶ say 'Sync complete' if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
104 0 return $self;
105 }
106  
107 0 ▶sub regex_for_uri_template {
Subroutine regex_for_uri_template: not called CC=4 SCAR=30.0
Pod documented
108 0 my ($self, $name) = @_;
109  
110 # Get the URI for the template.
111 my $uri = $self->mirror_uri_templates->{$name}->process(
112 0 map { $_ => "{$_}" } qw(dist version user extension tag stats format)
113 );
114  
115 0 my %regex_for = (
116 '{dist}' => qr{[^/]+?},
117 # https://regex101.com/r/vkijKf/
118 '{version}' => qr{(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?},
119 '{user}' => qr{([a-z]([-a-z0-9]{0,61}[a-z0-9])?)}i,
120 '{extension}' => qr{[^/]+?},
121 '{tag}' => qr{[^/]+?},
122 '{stats}' => qr{(?:dist|tag|user|extension|summary)},
123 '{format}' => qr{(?:txt|html|atom|xml)},
124 );
125  
126 # Assemble the regex corresponding to the template.
127 my $regex = join '', map {
128 0 ▶ $regex_for{$_} || quotemeta $_
Condition0%0/3
$regex_for{$_} || quotemeta $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$regex_for{$_} || quotemeta $_
$regex_for{$_} quotemeta $_
Truth table0%0/3
A || B
A= $regex_for{$_}B= quotemeta $_
ABresult
1X1
011
000
129 0 ▶ } grep { defined && length } map {
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
130 0 split /(\{.+?\})/
131 0 ▶ } catfile grep { defined && length } $uri->path_segments;
Condition0%0/3
defined $_ && length $_
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table0%0/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
132  
133 # Return the regex to match new or updated in rsync output lines.
134 0 return qr{\s>f(?:[+]+|(?:c|.s|..t)[^ ]+)\s($regex)$};
135  
136 # The rsync %i output format:
137 # YXcstpogz # Snow Leopard
138 # YXcstpoguax # Debian
139 # c: checkum has changed, file will be updated
140 # s: file size has changed, file will be updated
141 # t: modtime has changed, file will be updated
142 # +++++++: New item
143 }
144  
145 0 ▶sub validate_distribution {
Subroutine validate_distribution: not called CC=4 SCAR=30.0
Pod documented
146 0 my ($self, $fn) = shift->_rel_to_mirror(@_);
147 0 my $api = PGXN::API->instance;
148 0 my $meta = $api->read_json_from($fn);
149 0 my $zip_path = $self->download_for($meta);
150  
151 # Validate it against the SHA1 checksum.
152 0 ▶ say ' Checksumming ', $zip_path if $self->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose 0 0 0
153 0 ▶ if ($self->digest_for($zip_path) ne $meta->{sha1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->digest_for($zip_path) ne $meta->{"sha1"}) 0 0 0
154 0 warn "Checksum verification failed for $fn\n";
155 0 return;
156 }
157  
158 # Unpack the distribution.
159 0 ▶ my $zip = $self->unzip($zip_path, $meta) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $zip = $self->unzip($zip_path, $meta) or return 0 0 0
160 return {
161 meta => $meta,
162 zip => $zip,
163 src_url => $self->{base_url} . $api->uri_templates->{source}->process(
164 dist => lc $meta->{name},
165 version => lc $meta->{version},
166 0 ),
167 };
168 }
169  
170 0 ▶sub download_for {
Subroutine download_for: not called CC=1 SCAR=6.9
Pod documented
171 0 my ($self, $meta) = @_;
172 my $zip_uri = $self->mirror_uri_templates->{download}->process(
173 dist => lc $meta->{name},
174 version => lc $meta->{version},
175 0 );
176  
177 0 my (undef, @segments) = $zip_uri->path_segments;
178 0 return catfile @segments;
179 }
180  
181 0 ▶sub digest_for {
Subroutine digest_for: not called CC=2 SCAR=17.9
Pod documented
182 0 my ($self, $fn) = shift->_rel_to_mirror(@_);
183 0 ▶ open my $fh, '<:raw', $fn or die "Cannot open $fn: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $fn or die "Cannot open ${fn}: $!\n" 0 0 0
184 0 my $sha1 = Digest::SHA1->new;
185 0 $sha1->addfile($fh);
186 0 return $sha1->hexdigest;
187 }
188  
189 2 ▶sub unzip {
Subroutine unzip: called CC=12 SCAR=30.5
Pod documented
190 2 ▶ say ' Extracting ', $_[1] if $_[0]->verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->verbose 0 2 2
191 2 my ($self, $zip_path, $meta) = shift->_rel_to_mirror(@_);
192  
193 2 my $zip = Archive::Zip->new;
194 2 ▶ if ($zip->read(rel2abs $zip_path) != AZ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zip->read(rel2abs($zip_path)) != 0) 0 2 2
195 0 warn "Error reading $zip_path\n";
196 0 return;
197 }
198  
199 my $dist_dir = catdir(
200 PGXN::API->instance->source_dir,
201 lc $meta->{name}
202 2 );
203 2 ▶ make_path $dist_dir unless -e $dist_dir && -d _;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $dist_dir and -d _ 1 1 2
Condition66%2/3
-e $dist_dir and -d _
!ll&&!rl&&r
101
MC/DC50%1/2
-e $dist_dir and -d _
-e $dist_dir -d _
Truth table66%2/3
A and B
A= -e $dist_dirB= -d _
ABresult
0X0
100
111
204  
205 2 foreach my $member ($zip->members) {
206 29 my $fn = catfile $dist_dir, split m{/} => $member->fileName;
207 29 ▶ say " $fn\n" if $self->verbose > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose > 2 0 29 29
208  
209 29 ▶ if ($member->isSymbolicLink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member->isSymbolicLink) { } 0 29 29
210 # Delete exsting so Archive::Zip won't fail to create it.
211 0 ▶ warn "Cannot unlink $fn: $!\n" if -e $fn && !unlink $fn;
Branch: 1 branch
BranchTrueFalseTotal
if -e $fn and not unlink($fn) 0 0 0
Condition0%0/3
-e $fn and not unlink($fn)
!ll&&!rl&&r
000
MC/DC0%0/2
-e $fn and not unlink($fn)
-e $fn not unlink($fn)
Truth table0%0/3
A and not(B)
A= -e $fnB= not unlink($fn)
ABresult
0X0
100
111
212 } else {
213 # Make sure the member is readable by everyone.
214 29 ▶ $member->unixFileAttributes( $member->isDirectory ? 0755 : 0644 );
Branch: 1 branch
BranchTrueFalseTotal
$member->isDirectory ? : 12 17 29
215 }
216  
217 29 ▶ if ($member->extractToFileNamed($fn) != AZ_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($member->extractToFileNamed($fn) != 0) 0 29 29
218 0 warn "Error extracting $fn from $zip_path\n";
219 0 next;
220 }
221 }
222  
223 2 return $zip;
224 }
225  
226 2 ▶sub _rel_to_mirror {
Subroutine _rel_to_mirror: called CC=1 SCAR=0.0
227 2 return shift, catfile(+PGXN::API->instance->mirror_root, shift), @_;
228 }
229  
230 2 __PACKAGE__->meta->make_immutable(inline_destructor => 0);
231  
232 2 1;
233  
234 __END__
↑ Summary API/Indexer.pm »