blib/lib/PGXN/API/Sync.pm

filter: Statementstmt 53.1% 76 / 143 Branchbran 20.6% 12 / 58 Conditioncond 22.2% 2 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 75.0% 21 / 28 Podpod 100% 8 / 8 totaltotal 47.6% 120 / 252 CC 34 SCAR 38.4
CC
34
Cov
43%
update_index
182.0
unzip
21.2
regex_for_uri_template
20.0
CRAP 46.4
« _build/lib/PGXN/Build.pm ↑ Summary blib/lib/PGXN/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.0;
18  
19 subtype Executable => as 'Str', where {
20 2 ▶ my $exe = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
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 has log_file => (is => 'rw', isa => 'Str', required => 1, default => sub {
29 2 ▶ catfile tmpdir, "pgxn-api-sync-$$.txt"
Subroutine __ANON__: called CC=1 SCAR=0.0
30 2 });
31  
32 has mirror_uri_templates => (is => 'ro', isa => 'HashRef', lazy => 1, default => sub {
33 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 sub run {
40 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
41 0 $self->run_rsync;
42 0 $self->update_index;
43 }
44  
45 sub DESTROY {
46 1 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
47 1 unlink $self->log_file;
48 1 $self->SUPER::DESTROY;
49 }
50  
51 sub run_rsync {
52 1 ▶ my $self = shift;
Subroutine run_rsync: called CC=3 SCAR=11.7
Pod documented
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 1 0 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 sub update_index {
67 0 ▶ my $self = shift;
Subroutine update_index: not called CC=13 SCAR=52.0
Pod documented
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 sub regex_for_uri_template {
108 0 ▶ my ($self, $name) = @_;
Subroutine regex_for_uri_template: not called CC=4 SCAR=30.0
Pod documented
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 $_
Branch: 1 branch
BranchTrueFalseTotal
$regex_for{$_} or quotemeta $_ 0 0 0
129 0 ▶ } grep { defined && length } map {
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 0 0 0
130 0 split /(\{.+?\})/
131 0 ▶ } catfile grep { defined && length } $uri->path_segments;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and length $_ 0 0 0
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 sub validate_distribution {
146 0 ▶ my ($self, $fn) = shift->_rel_to_mirror(@_);
Subroutine validate_distribution: not called CC=4 SCAR=30.0
Pod documented
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 sub download_for {
171 0 ▶ my ($self, $meta) = @_;
Subroutine download_for: not called CC=1 SCAR=6.9
Pod documented
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 sub digest_for {
182 0 ▶ my ($self, $fn) = shift->_rel_to_mirror(@_);
Subroutine digest_for: not called CC=2 SCAR=17.9
Pod documented
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 sub unzip {
190 2 ▶ say ' Extracting ', $_[1] if $_[0]->verbose;
Subroutine unzip: called CC=12 SCAR=30.5
Pod documented
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 sub _rel_to_mirror {
227 2 ▶ return shift, catfile(+PGXN::API->instance->mirror_root, shift), @_;
Subroutine _rel_to_mirror: called CC=1 SCAR=0.0
228 }
229  
230 2 __PACKAGE__->meta->make_immutable(inline_destructor => 0);
231  
232 2 1;
233  
234 __END__
235  
236 =head1 Name
237  
238 PGXN::API::Sync - Sync from a PGXN mirror and update the index
239  
240 =head1 Synopsis
241  
242 use PGXN::API::Sync;
243 PGXN::API::Sync->new(
244 source => $source,
245 rsync_path => $rsync_path,
246 verbose => $verbose,
247 )->run;
248  
249 =head1 Description
250  
251 This module provides the implementation for L<pgxn_api_sync>, the command-line
252 utility for syncing to a PGXN mirror and creating the API. It syncs to the
253 specified PGXN rsync source URL, which should be a PGXN mirror server, and
254 then verifies and unpacks newly-uploaded distributions and hands them off to
255 L<PGXN::API::Indexer> to index.
256  
257 =head1 Class Interface
258  
259 =head2 Constructor
260  
261 =head3 C<new>
262  
263 my $sync = PGXN::API::Sync->new(%params);
264  
265 Creates and returns a new PGXN::API::Sync object. The supported parameters
266 are:
267  
268 =over
269  
270 =item C<rsync_path>
271  
272 Path to the rsync executable. Defaults to C<rsync>, which should work find if
273 there is an executable with that name in your path.
274  
275 =item C<source>
276  
277 An C<rsync> URL specifying the source from which to sync. The source should be
278 an C<rsync> server serving up a PGXN mirror source as created or mirrored from
279 a PGXN Manager server.
280  
281 =item C<verbose>
282  
283 An incremental integer specifying the level of verbosity to use during a sync.
284 By default, PGXN::API::Sync runs in quiet mode, where only errors are emitted
285 to C<STDERR>.
286  
287 =back
288  
289 =head1 Instance Interface
290  
291 =head2 Instance Methods
292  
293 =head3 C<run>
294  
295 $sync->run;
296  
297 Runs the sync, C<rsync>ing from the source mirror server, verifying and
298 unpacking distributions, and handing them off to the indexer for indexing.
299 This is the main method called by L<pgxn_api_sync> to just do the job.
300  
301 =head3 C<run_rsync>
302  
303 $sync->run_rsync;
304  
305 C<rsync>s from the source mirror server. Called by C<run>.
306  
307 =head3 C<update_index>
308  
309 $sync->update_index;
310  
311 Parses the log generated by the execution of C<run_rsync()> for new
312 distribution F<META.json> files and passes any found off to
313 C<validate_distribution()> and L<PGXN::API::Indexer/add_distribution> for
314 validation, unpacking, and indexing. Called internally by C<run()>.
315  
316 =head3 C<regex_for_uri_template>
317  
318 my $regex = $sync->regex_for_uri_template('download');
319  
320 Returns a regular expression that will match the path to a file in the rsync
321 logs. The regular expression is created from a named URI template as loaded
322 from the F</index.json> file synced from the mirror server. Used internally to
323 parse the paths to distribution files from the rsync logs so that they can be
324 validated, unpacked, and indexed.
325  
326 =head3 C<download_for>
327  
328 my $download = $sync->download_for($meta);
329  
330 Given the metadata loaded from a mirror server F<META.json> file, returns the
331 path to the download file for the distribution. Used internally by
332 C<validate_distribution()> to find the file to validate.
333  
334 =head3 C<validate_distribution>
335  
336 my $params = $sync->validate_distribution($path_to_dist_meta);
337  
338 Given the path to a distribution F<META.json> file, this method validates the
339 digest for the download file and unpacks it. Returns parameters suitable for
340 passing to L<PGXN::Indexer/add_distribution> for indexing.
341  
342 =head3 C<digest_for>
343  
344 my $digest = $sync->digest_for($zipfile);
345  
346 Returns the SHA-1 hex digest for a distribution file (or any file, really).
347 Called by C<validate_distribution()>.
348  
349 =head3 C<unzip>
350  
351 $sync->unzip($download, $meta);)
352  
353 Given a download file for a distribution, and the metadata loaded from the
354 C<META.json> describing the download, this method unpacks the download under
355 the F<src/> directory under the document root. Each file will be readable by
356 all users. This provides the browsable file interface for the API server to
357 serve. Called internally by C<validate_distribution()>.
358  
359 =head2 Instance Accessors
360  
361 =head3 C<rsync_path>
362  
363 my $rsync_path = $sync->rsync_path;
364 $sync->rsync_path($rsync_path);
365  
366 Get or set the path to the C<rsync> executable.
367  
368 =head3 C<source>
369  
370 my $source = $sync->source;
371 $sync->source($source);
372  
373 Get or set the source C<rsync> URL from which to sync a PGXN mirror.
374  
375 =head3 C<verbose>
376  
377 my $verbose = $sync->verbose;
378 $sync->verbose($verbose);
379  
380 Get or set an incremental verbosity. The higher the integer specified, the
381 more verbose the sync.
382  
383 =head3 C<log_file>
384  
385 my $log_file = $sync->log_file;
386 $sync->log_file($log_file);
387  
388 Get or set the path to use for the C<rsync> log file. This file will then be
389 parsed by C<update_index> for new distributions to index.
390  
391 =head3 C<mirror_uri_templates>
392  
393 my $templates = $pgxn->mirror_uri_templates;
394  
395 Returns a hash reference of the URI templates loaded from the F<index.json>
396 file in the mirror root. The keys are the names of the templates, and the
397 values are L<URI::Template> objects.
398  
399 =cut
400  
401 =head1 Author
402  
403 David E. Wheeler <david.wheeler@pgexperts.com>
404  
405 =head1 Copyright and License
406  
407 Copyright (c) 2011-2026 David E. Wheeler.
408  
409 This module is free software; you can redistribute it and/or modify it under
410 the L<PostgreSQL License|http://www.opensource.org/licenses/postgresql>.
411  
412 Permission to use, copy, modify, and distribute this software and its
413 documentation for any purpose, without fee, and without a written agreement is
414 hereby granted, provided that the above copyright notice and this paragraph
415 and the following two paragraphs appear in all copies.
416  
417 In no event shall David E. Wheeler be liable to any party for direct,
418 indirect, special, incidental, or consequential damages, including lost
419 profits, arising out of the use of this software and its documentation, even
420 if David E. Wheeler has been advised of the possibility of such damage.
421  
422 David E. Wheeler specifically disclaims any warranties, including, but not
423 limited to, the implied warranties of merchantability and fitness for a
424 particular purpose. The software provided hereunder is on an "as is" basis,
425 and David E. Wheeler has no obligations to provide maintenance, support,
426 updates, enhancements, or modifications.
427  
428 =cut
« _build/lib/PGXN/Build.pm ↑ Summary blib/lib/PGXN/API/Indexer.pm »