blib/lib/Bioconda/Packages/Updater.pm

filter: Statementstmt 71.9% 149 / 207 Branchbran 53.0% 53 / 100 Conditioncond 60.5% 46 / 76 MC/DCmcdc 32.5% 14 / 43 Subroutinesub 90.0% 18 / 20 Podpod 66.6% 4 / 6 totaltotal 62.8% 284 / 452 CC 88 SCAR 41.8
CC
88
Cov
65%
_enrich
272.0
build
51.8
pull_repo
42.0
CRAP 65.5
↑ Summary blib/lib/Bioconda/Packages/RecipeParser.pm »
1 package Bioconda::Packages::Updater;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Bioconda::Packages::DB ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Bioconda::Packages::RecipeParser ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 our $VERSION = '0.01';
13  
14 =head1 NAME
15  
16 Bioconda::Packages::Updater - Maintainer tool: (re)build the JSON database from a
17 bioconda-recipes clone.
18  
19 =head1 SYNOPSIS
20  
21 use Bioconda::Packages::Updater;
22  
23 my $up = Bioconda::Packages::Updater->new(
24 repo => 'bioconda-recipes',
25 db => 'share/bioconda-packages.json',
26 verbose => 1,
27 );
28  
29 $up->pull_repo; # optional: git pull --ff-only
30 my $report = $up->build; # parse recipes, merge, enrich, write
31  
32 =head1 DESCRIPTION
33  
34 This is developer-facing: end users only ever read the shipped database via
35 L<Bioconda::Packages>. The intended workflow is
36  
37 1. pull the bioconda-recipes clone (pull_repo / git pull)
38 2. rebuild the database (build)
39 3. commit the regenerated JSON (git)
40  
41 Removed-package handling: any package present in the previous database but no
42 longer found in the repo is B<kept> and flagged C<is_removed>, with C<removed_at>
43 stamped once. A package that reappears is un-flagged.
44  
45 Dependencies: core Perl only. L<HTTP::Tiny> (core since 5.14) is used for the
46 optional anaconda.org enrichment.
47  
48 =head1 METHODS
49  
50 =head2 new
51  
52 my $up = Bioconda::Packages::Updater->new(%opts);
53  
54 Options: C<repo> (path to the bioconda-recipes clone, required for L</build>),
55 C<db> (output JSON path; defaults to the located bundled db), C<api> (enable
56 anaconda.org enrichment), C<api_sleep> (seconds between API calls, default 0),
57 C<limit> (process at most N recipes, for testing), C<force> (bump the version
58 and rewrite even when the content is unchanged), C<verbose>, and C<log> (a code
59 ref called with log lines; defaults to printing to STDERR when verbose).
60  
61 The version only advances, and the database file is only rewritten, when the
62 semantic package content actually changed (see L</build>). Volatile fields
63 (timestamps, download counts) are excluded from that comparison.
64  
65 =cut
66  
67 7 ▶sub new {
Subroutine new: called CC=5 SCAR=17.1
Pod documented
68 7 my ($class, %opt) = @_;
69 my $self = {
70 repo => $opt{repo},
71 db => $opt{db},
72 api => $opt{api} || 0,
73 api_sleep => $opt{api_sleep} || 0,
74 limit => $opt{limit},
75 force => $opt{force} || 0,
76 verbose => $opt{verbose} || 0,
77 log => $opt{log},
78 7 ▶ };
Condition62%5/8
$opt{'api'} || 0
l!l
07
$opt{'api_sleep'} || 0
l!l
07
$opt{'force'} || 0
l!l
16
$opt{'verbose'} || 0
l!l
07
MC/DC0%0/1
$opt{'api'} || 0
$opt{'api'}
MC/DC0%0/1
$opt{'api_sleep'} || 0
$opt{'api_sleep'}
MC/DC100%1/1
$opt{'force'} || 0
$opt{'force'}
MC/DC0%0/1
$opt{'verbose'} || 0
$opt{'verbose'}
Truth table50%1/2
A
A= $opt{'api'}
Aresult
11
00
Truth table50%1/2
A
A= $opt{'api_sleep'}
Aresult
11
00
Truth table100%2/2
A
A= $opt{'force'}
Aresult
11
00
Truth table50%1/2
A
A= $opt{'verbose'}
Aresult
11
00
79 7 return bless $self, $class;
80 }
81  
82 6 ▶sub _log {
Subroutine _log: called CC=3 SCAR=14.2
83 6 my ($self, @msg) = @_;
84 0 ▶ if ($self->{log}) { $self->{log}->(@msg) }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'log'}) { } 0 6 6
elsif ($self->{'verbose'}) { } 0 6 6
85 0 elsif ($self->{verbose}) { print STDERR @msg, "\n" }
86 }
87  
88 =head2 pull_repo
89  
90 my $sha = $up->pull_repo;
91  
92 Runs C<git -C $repo pull --ff-only> and returns the new HEAD sha. Dies if the
93 repo is not a git checkout or the pull fails.
94  
95 =cut
96  
97 0 ▶sub pull_repo {
Subroutine pull_repo: not called CC=6 SCAR=37.4
Pod documented
98 0 my ($self) = @_;
99 0 ▶ my $repo = $self->{repo} or croak "No repo path configured";
Branch: 1 branch
BranchTrueFalseTotal
my $repo = $self->{"repo"} or croak("No repo path configured") 0 0 0
100 0 ▶ croak "Repo '$repo' is not a directory" unless -d $repo;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $repo 0 0 0
101 0 ▶ croak "Repo '$repo' is not a git checkout"
Branch: 1 branch
BranchTrueFalseTotal
unless -d "File::Spec"->catdir($repo, ".git") 0 0 0
102 unless -d File::Spec->catdir($repo, '.git');
103  
104 0 $self->_log("Pulling $repo ...");
105 0 my $out = qx(git -C "$repo" pull --ff-only 2>&1);
106 0 ▶ croak "git pull failed:\n$out" if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
107 0 ▶ $self->_log($out) if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
108 0 return $self->repo_commit;
109 }
110  
111 =head2 repo_commit
112  
113 Returns the repo's current HEAD sha (or undef if unavailable).
114  
115 =cut
116  
117 6 ▶sub repo_commit {
Subroutine repo_commit: called CC=5 SCAR=25.2
Pod documented
118 6 my ($self) = @_;
119 6 ▶ my $repo = $self->{repo} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $repo = $self->{"repo"} or return undef 6 0 6
120 6 ▶ return undef unless -d File::Spec->catdir($repo, '.git');
Branch: 1 branch
BranchTrueFalseTotal
unless -d "File::Spec"->catdir($repo, ".git") 6 0 6
121 0 my $sha = qx(git -C "$repo" rev-parse HEAD 2>/dev/null);
122 0 ▶ return undef if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 0 0
123 0 chomp $sha;
124 0 ▶ return $sha || undef;
Condition0%0/2
$sha || undef
l!l
00
MC/DC0%0/1
$sha || undef
$sha
Truth table0%0/2
A
A= $sha
Aresult
11
00
125 }
126  
127 =head2 build
128  
129 my $report = $up->build;
130  
131 Parses every recipe, merges with the existing database (preserving removed
132 state), optionally enriches via anaconda.org, and writes the database atomically.
133  
134 Returns a report hashref: C<added>, C<updated>, C<removed>, C<reappeared>,
135 C<parse_warnings>, C<api_ok>, C<api_fail>, C<total>, C<db_path>.
136  
137 =cut
138  
139 7 ▶sub build {
Subroutine build: called CC=42 SCAR=39.5
Pod documented
140 7 my ($self) = @_;
141 7 ▶ my $repo = $self->{repo} or croak "No repo path configured";
Branch: 1 branch
BranchTrueFalseTotal
my $repo = $self->{"repo"} or croak("No repo path configured") 7 0 7
142 7 my $recipes_dir = File::Spec->catdir($repo, 'recipes');
143 7 ▶ croak "No recipes directory at '$recipes_dir'" unless -d $recipes_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $recipes_dir 1 6 7
144  
145 6 my $db_path = Bioconda::Packages::DB->locate_db($self->{db});
146  
147 # Load previous db (if any) for removed-flag continuity and to advance the
148 # progressive database version.
149 6 my $old = {};
150 6 my ($old_version, $old_sig);
151 6 ▶ if (-f $db_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $db_path) 4 2 6
152 4 my $prev = eval { Bioconda::Packages::DB->load($db_path) };
153 4 ▶ if ($prev && ref $prev->{packages} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev and ref $prev->{"packages"} eq "HASH") 4 0 4
Condition33%1/3
$prev and ref $prev->{"packages"} eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
$prev and ref $prev->{"packages"} eq "HASH"
$prev ref $prev->{"packages"} eq "HASH"
Truth table33%1/3
A and B
A= $prevB= ref $prev->{"packages"} eq "HASH"
ABresult
0X0
100
111
154 4 $old = $prev->{packages};
155 4 $old_version = $prev->{meta}{db_version};
156 4 $old_sig = $prev->{meta}{content_sha};
157 }
158 }
159  
160 6 my $now = _now_iso();
161 6 my %new; # name => record
162 my %recipe_warnings;
163 6 my ($n_recipes, $n_warn) = (0, 0);
164  
165 6 ▶ opendir my $dh, $recipes_dir or croak "Cannot read '$recipes_dir': $!";
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $recipes_dir or croak("Cannot read '${recipes_dir}': $!") 6 0 6
166 6 ▶ my @dirs = sort grep { !/^\./ && -d File::Spec->catdir($recipes_dir, $_) }
Condition66%2/3
!/^\./ && -d 'File::Spec'->catdir($recipes_dir, $_)
!ll&&!rl&&r
12024
MC/DC50%1/2
!/^\./ && -d 'File::Spec'->catdir($recipes_dir, $_)
!/^\./ -d 'File::Spec'->catdir($recipes_dir, $_)
Truth table66%2/3
not(A) && B
A= !/^\./B= -d 'File::Spec'->catdir($recipes_dir, $_)
ABresult
0X0
100
111
167 readdir $dh;
168 6 closedir $dh;
169  
170 6 for my $rname (@dirs) {
171 22 ▶ last if defined $self->{limit} && $n_recipes >= $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"limit"} and $n_recipes >= $self->{"limit"} 1 21 22
Condition100%3/3
defined $self->{"limit"} and $n_recipes >= $self->{"limit"}
!ll&&!rl&&r
2011
MC/DC100%2/2
defined $self->{"limit"} and $n_recipes >= $self->{"limit"}
defined $self->{"limit"} $n_recipes >= $self->{"limit"}
Truth table100%3/3
A and B
A= defined $self->{"limit"}B= $n_recipes >= $self->{"limit"}
ABresult
0X0
100
111
172 21 my $rdir = File::Spec->catdir($recipes_dir, $rname);
173 21 my ($meta, $recipe_versions) = _find_meta($rdir);
174 21 ▶ next unless $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta 1 20 21
175 20 $n_recipes++;
176  
177 20 my $info = eval { Bioconda::Packages::RecipeParser::parse_file($meta) };
178 20 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 20 20
179 0 $self->_log("parse failed for $rname: $@");
180 0 next;
181 }
182 20 ▶ if (@{ $info->{warnings} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$info->{"warnings"};}) 0 20 20
183 0 $n_warn++;
184 0 $recipe_warnings{$rname} = $info->{warnings};
185 }
186  
187 # Build one record per installable package name this recipe provides.
188 20 my @names = @{ $info->{package_names} };
189 20 ▶ @names = ($rname) unless @names; # fallback to dir name
Branch: 1 branch
BranchTrueFalseTotal
unless @names 0 20 20
190 20 ▶ my $primary = $info->{name} // $names[0];
Condition33%1/3
$info->{"name"} // $names[0]
l!l&&r!l&&!r
2000
MC/DC0%0/2
$info->{"name"} // $names[0]
$info->{"name"} $names[0]
Truth table33%1/3
A // B
A= $info->{"name"}B= $names[0]
ABresult
1X1
011
000
191  
192 20 for my $pname (@names) {
193 26 ▶ my $is_primary = defined $primary && $pname eq $primary;
Condition66%2/3
defined $primary && $pname eq $primary
!ll&&!rl&&r
0620
MC/DC50%1/2
defined $primary && $pname eq $primary
defined $primary $pname eq $primary
Truth table66%2/3
A && B
A= defined $primaryB= $pname eq $primary
ABresult
0X0
100
111
194 26 my $old_rec = $old->{ lc $pname };
195 my $rec = {
196 name => $pname,
197 current_version => $info->{version},
198 recipe_versions => $recipe_versions,
199 published_versions =>
200 ($old_rec && $old_rec->{published_versions}) || [],
201 downloads => ($old_rec && $old_rec->{downloads}),
202 build_number => $info->{build_number},
203 noarch => $info->{noarch},
204 summary => $info->{summary},
205 license => $info->{license},
206 license_family => $info->{license_family},
207 home => $info->{home},
208 dev_url => $info->{dev_url},
209 doc_url => $info->{doc_url},
210 identifiers => $info->{identifiers},
211 outputs => $info->{outputs},
212 depends => $info->{depends},
213 recipe_path => "recipes/$rname",
214 is_removed => JSON_false(),
215 removed_at => undef,
216 26 ▶ first_seen => ($old_rec && $old_rec->{first_seen}) || $now,
Branch: 1 branch
BranchTrueFalseTotal
$is_primary ? : 20 6 26
Condition71%10/14
$old_rec && $old_rec->{'published_versions'} || []
l!l
197
$old_rec and $old_rec->{"published_versions"}
!ll&&!rl&&r
7019
$old_rec && $old_rec->{'downloads'}
!ll&&!rl&&r
7190
$old_rec && $old_rec->{'first_seen'} || $now
l!l&&r!l&&!r
1970
$old_rec and $old_rec->{"first_seen"}
!ll&&!rl&&r
7019
MC/DC50%1/2
$old_rec && $old_rec->{'published_versions'} || []
$old_rec $old_rec->{"published_versions"}
MC/DC0%0/2
$old_rec && $old_rec->{'downloads'}
$old_rec $old_rec->{'downloads'}
MC/DC0%0/3
$old_rec && $old_rec->{'first_seen'} || $now
$old_rec $old_rec->{"first_seen"} $now
Truth table66%2/3
A and B
A= $old_recB= $old_rec->{"published_versions"}
ABresult
111
0X0
100
Truth table66%2/3
A && B
A= $old_recB= $old_rec->{'downloads'}
ABresult
0X0
100
111
Truth table40%2/5
A and B || C
A= $old_recB= $old_rec->{"first_seen"}C= $now
ABCresult
11X1
0X11
1011
0X00
1000
217 last_seen => $now,
218 is_subpackage => $is_primary ? JSON_false() : JSON_true(),
219 };
220 # If an output record duplicates nothing useful, still keep it so the
221 # name is discoverable; prefer the primary record on name clashes.
222 26 ▶ if (!exists $new{ lc $pname } || $is_primary) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $new{lc $pname} or $is_primary) 26 0 26
Condition33%1/3
not exists $new{lc $pname} or $is_primary
l!l&&r!l&&!r
2600
MC/DC0%0/2
not exists $new{lc $pname} or $is_primary
not exists $new{lc $pname} $is_primary
Truth table33%1/3
not(A) or B
A= not exists $new{lc $pname}B= $is_primary
ABresult
1X1
011
000
223 26 $new{ lc $pname } = $rec;
224 }
225 }
226 }
227  
228 # Merge: carry over removed packages, detect reappearances.
229 6 my ($added, $updated, $removed, $reappeared) = (0, 0, 0, 0);
230  
231 6 for my $key (keys %new) {
232 26 ▶ if (exists $old->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $old->{$key}) { } 19 7 26
233 19 ▶ if ($old->{$key}{is_removed}) { $reappeared++ }
Branch: 1 branch
BranchTrueFalseTotal
if ($old->{$key}{'is_removed'}) { } 1 18 19
234 18 else { $updated++ }
235 }
236 7 else { $added++ }
237 }
238  
239 6 for my $key (keys %$old) {
240 20 ▶ next if exists $new{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $new{$key} 19 1 20
241 # Package vanished from the repo: retain but flag removed.
242 1 my $rec = { %{ $old->{$key} } };
243 1 $rec->{is_removed} = JSON_true();
244 1 ▶ $rec->{removed_at} ||= $now;
Condition33%1/3
$rec->{"removed_at"} ||= $now
l!l&&r!l&&!r
010
MC/DC0%0/2
$rec->{"removed_at"} ||= $now
$rec->{"removed_at"} $now
Truth table33%1/3
A ||= B
A= $rec->{"removed_at"}B= $now
ABresult
1X1
011
000
245 1 $new{$key} = $rec;
246 1 $removed++;
247 }
248  
249 # Optional enrichment from anaconda.org.
250 6 my ($api_ok, $api_fail) = (0, 0);
251 6 ▶ if ($self->{api}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"api"}) 0 6 6
252 0 ($api_ok, $api_fail) = $self->_enrich(\%new);
253 }
254  
255 6 my $removed_total = grep { $new{$_}{is_removed} } keys %new;
256  
257 # Bump the version only when the semantic content actually changed. The
258 # signature deliberately ignores volatile fields (timestamps, download
259 # counts) so a no-op rebuild neither bumps the version nor rewrites the file.
260 6 my $sig = _content_signature(\%new);
261 my $unchanged = (!$self->{force}
262 6 ▶ && defined $old_sig && $old_sig eq $sig && -f $db_path)
Branch: 1 branch
BranchTrueFalseTotal
!$self->{'force'} && defined $old_sig && $old_sig eq $sig && -f $db_path ? : 1 5 6
Condition88%8/9
not $self->{"force"} and defined $old_sig and $old_sig eq $sig and -f $db_path
!ll&&!rl&&r
501
not $self->{"force"} and defined $old_sig and $old_sig eq $sig
!ll&&!rl&&r
321
not $self->{"force"} and defined $old_sig
!ll&&!rl&&r
123
MC/DC75%3/4
not $self->{"force"} and defined $old_sig and $old_sig eq $sig and -f $db_path
not $self->{"force"} defined $old_sig $old_sig eq $sig -f $db_path
Truth table80%4/5
not(A) and B and C and D
A= not $self->{"force"}B= defined $old_sigC= $old_sig eq $sigD= -f $db_path
ABCDresult
0XXX0
10XX0
110X0
11100
11111
263 ? 1 : 0;
264 6 ▶ my $db_version = $unchanged ? $old_version : _next_db_version($old_version);
Branch: 1 branch
BranchTrueFalseTotal
$unchanged ? : 1 5 6
265  
266 my $data = {
267 meta => {
268 db_version => $db_version,
269 content_sha => $sig,
270 generated_at => $now,
271 generator => "Bioconda::Packages::Updater $VERSION",
272 recipes_commit => $self->repo_commit,
273 source => 'bioconda-recipes',
274 package_count => scalar(keys %new),
275 removed_count => $removed_total,
276 6 ▶ api_enriched => $self->{api} ? JSON_true() : JSON_false(),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'api'} ? : 0 6 6
277 },
278 packages => \%new,
279 };
280  
281 # Skip rewriting an unchanged database so the file (and its git state) stays
282 # byte-identical.
283 6 ▶ Bioconda::Packages::DB->save($db_path, $data) unless $unchanged;
Branch: 1 branch
BranchTrueFalseTotal
unless $unchanged 5 1 6
284  
285 6 ▶ my $report = {
Branch: 1 branch
BranchTrueFalseTotal
$unchanged ? : 1 5 6
286 added => $added,
287 updated => $updated,
288 removed => $removed,
289 reappeared => $reappeared,
290 parse_warnings => $n_warn,
291 warnings_by_recipe => \%recipe_warnings,
292 api_ok => $api_ok,
293 api_fail => $api_fail,
294 total => scalar(keys %new),
295 recipes_seen => $n_recipes,
296 db_path => $db_path,
297 db_version => $db_version,
298 changed => $unchanged ? 0 : 1,
299 };
300 6 ▶ if ($unchanged) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unchanged) { } 1 5 6
301 1 $self->_log(sprintf
302 "No content changes; database stays at version %s (not rewritten).",
303 $db_version);
304 }
305 else {
306 $self->_log(sprintf
307 "Built %d packages as db version %s (%d added, %d updated, %d newly-removed, %d reappeared, %d parse warnings) -> %s",
308 5 $report->{total}, $db_version, $added, $updated, $removed, $reappeared, $n_warn, $db_path);
309 }
310 6 return $report;
311 }
312  
313 #-----------------------------------------------------------------------------
314 # anaconda.org enrichment
315 #-----------------------------------------------------------------------------
316  
317 0 ▶sub _enrich {
Subroutine _enrich: not called CC=16 SCAR=56.1
318 0 my ($self, $pkgs) = @_;
319 0 require HTTP::Tiny;
320 0 require JSON::PP;
321 0 my $http = HTTP::Tiny->new(
322 timeout => 30,
323 agent => "Bioconda-Packages/$VERSION ",
324 );
325 0 my ($ok, $fail) = (0, 0);
326 0 my @names = sort keys %$pkgs;
327 0 my $i = 0;
328 0 for my $key (@names) {
329 0 ▶ next if $pkgs->{$key}{is_removed}; # don't hammer the API for gone pkgs
Branch: 1 branch
BranchTrueFalseTotal
if $pkgs->{$key}{"is_removed"} 0 0 0
330 0 $i++;
331 0 my $name = $pkgs->{$key}{name};
332 0 my $url = "https://api.anaconda.org/package/bioconda/$name";
333 0 my $resp = $http->get($url);
334 0 ▶ if (!$resp->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
335 0 $fail++;
336 $self->_log("API $name: $resp->{status} $resp->{reason}")
337 0 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
338 0 next;
339 }
340 0 my $json = eval { JSON::PP->new->utf8->decode($resp->{content}) };
341 0 ▶ if ($@ || ref $json ne 'HASH') { $fail++; next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $json ne "HASH") 0 0 0
Condition0%0/3
$@ or ref $json ne "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or ref $json ne "HASH"
$@ ref $json ne "HASH"
Truth table0%0/3
A or B
A= $@B= ref $json ne "HASH"
ABresult
1X1
011
000
342  
343 0 my %vers;
344 0 my $downloads = 0;
345 0 ▶ if (ref $json->{files} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json->{"files"} eq "ARRAY") 0 0 0
346 0 for my $f (@{ $json->{files} }) {
347 0 ▶ $vers{ $f->{version} } = 1 if defined $f->{version};
Branch: 1 branch
BranchTrueFalseTotal
if defined $f->{"version"} 0 0 0
348 0 ▶ $downloads += $f->{ndownloads} if $f->{ndownloads};
Branch: 1 branch
BranchTrueFalseTotal
if $f->{"ndownloads"} 0 0 0
349 }
350 }
351 0 ▶ if (ref $json->{versions} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $json->{"versions"} eq "ARRAY") 0 0 0
352 0 $vers{$_} = 1 for @{ $json->{versions} };
353 }
354 0 $pkgs->{$key}{published_versions} = [ _sort_versions(keys %vers) ];
355 0 $pkgs->{$key}{downloads} = $downloads;
356 0 $ok++;
357 0 ▶ select undef, undef, undef, $self->{api_sleep} if $self->{api_sleep};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"api_sleep"} 0 0 0
358 $self->_log("API $i/".scalar(@names)." $name ok")
359 0 ▶ if $self->{verbose} && $i % 100 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} and $i % 100 == 0 0 0 0
Condition0%0/3
$self->{"verbose"} and $i % 100 == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"verbose"} and $i % 100 == 0
$self->{"verbose"} $i % 100 == 0
Truth table0%0/3
A and B
A= $self->{"verbose"}B= $i % 100 == 0
ABresult
0X0
100
111
360 }
361 0 return ($ok, $fail);
362 }
363  
364 #-----------------------------------------------------------------------------
365 # Helpers
366 #-----------------------------------------------------------------------------
367  
368 # Locate the recipe's meta.yaml and collect any pinned version subdirectories.
369 # Returns ($meta_path, \@recipe_versions).
370 21 ▶sub _find_meta {
Subroutine _find_meta: called CC=6 SCAR=17.9
371 21 my ($rdir) = @_;
372 21 my $top = File::Spec->catfile($rdir, 'meta.yaml');
373  
374 21 ▶ opendir my $dh, $rdir or return (undef, []);
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, $rdir or return undef, [] 21 0 21
375 21 ▶ my @subs = grep { !/^\./ && -d File::Spec->catdir($rdir, $_) } readdir $dh;
Condition100%3/3
!/^\./ && -d 'File::Spec'->catdir($rdir, $_)
!ll&&!rl&&r
421515
MC/DC100%2/2
!/^\./ && -d 'File::Spec'->catdir($rdir, $_)
!/^\./ -d 'File::Spec'->catdir($rdir, $_)
Truth table100%3/3
not(A) && B
A= !/^\./B= -d 'File::Spec'->catdir($rdir, $_)
ABresult
0X0
100
111
376 21 closedir $dh;
377 21 my @versions = grep { /^[0-9]/ } @subs; # version subdirs start with a digit
378  
379 21 ▶ if (-f $top) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $top) 15 6 21
380 15 return ($top, [ _sort_versions(@versions) ]);
381 }
382 # No top-level meta.yaml: use the highest version subdir, if any.
383 6 ▶ if (@versions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@versions) 5 1 6
384 5 my ($highest) = reverse _sort_versions(@versions);
385 5 my $meta = File::Spec->catfile($rdir, $highest, 'meta.yaml');
386 5 ▶ return (-f $meta ? $meta : undef, [ _sort_versions(@versions) ]);
Branch: 1 branch
BranchTrueFalseTotal
-f $meta ? : 5 0 5
387 }
388 1 return (undef, []);
389 }
390  
391 # Rough version sort: numeric-aware, good enough for display ordering.
392 34 ▶sub _sort_versions {
Subroutine _sort_versions: called CC=8 SCAR=21.0
393 34 my @v = @_;
394 return sort {
395 34 my @a = split /[._-]/, $a;
396 58 my @b = split /[._-]/, $b;
397 58 for my $k (0 .. $#a) {
398 116 ▶ my ($x, $y) = ($a[$k], $b[$k] // 0);
Condition50%1/2
$b[$k] // 0
l!l
1160
MC/DC0%0/1
$b[$k] // 0
$b[$k]
Truth table50%1/2
A
A= $b[$k]
Aresult
11
00
399 116 ▶ my $cmp = ($x =~ /^\d+$/ && (defined $y && $y =~ /^\d+$/))
Branch: 1 branch
BranchTrueFalseTotal
$x =~ /^\d+$/ && (defined $y && $y =~ /^\d+$/) ? : 114 2 116
Condition75%6/8
$x =~ /^\d+$/ and (defined $y and $y =~ /^\d+$/)
!ll&&!rl&&r
11114
defined $y and $y =~ /^\d+$/
!ll&&!rl&&r
01114
$y // ""
l!l
20
MC/DC66%2/3
$x =~ /^\d+$/ and (defined $y and $y =~ /^\d+$/)
$x =~ /^\d+$/ defined $y $y =~ /^\d+$/
MC/DC0%0/1
$y // ""
$y
Truth table75%3/4
A and B and C
A= $x =~ /^\d+$/B= defined $yC= $y =~ /^\d+$/
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $y
Aresult
11
00
400 ? $x <=> $y : ($x cmp ($y // ''));
401 116 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 57 59 116
402 }
403 1 return @a <=> @b;
404 } @v;
405 }
406  
407 6 ▶sub _now_iso { strftime('%Y-%m-%dT%H:%M:%SZ', gmtime) }
Subroutine _now_iso: called CC=1 SCAR=0.0
408  
409 # A stable hash of the semantically meaningful package data, used to decide
410 # whether a rebuild is a real change. Volatile fields (timestamps, download
411 # counts) are excluded so a no-op rebuild produces the same signature.
412 11 ▶sub _content_signature {
Subroutine _content_signature: called CC=2 SCAR=6.9
413 11 my ($pkgs) = @_;
414 11 require Digest::SHA;
415 11 my @volatile = qw(first_seen last_seen removed_at downloads);
416 11 my %reduced;
417 11 for my $key (keys %$pkgs) {
418 33 my %rec = %{ $pkgs->{$key} }; # shallow copy; nested refs are read-only
419 33 delete @rec{@volatile};
420 33 $reduced{$key} = \%rec;
421 }
422 11 my $json = JSON::PP->new->utf8->canonical->encode(\%reduced);
423 11 return Digest::SHA::sha256_hex($json);
424 }
425  
426 # CalVer-style database version: YYYY.M.N (a valid 3-part SemVer, so it sorts
427 # under SemVer/numeric rules). N is a progressive counter that resets each month
428 # and increments on every rebuild within the same year+month. Uses UTC to match
429 # generated_at.
430 9 ▶sub _next_db_version {
Subroutine _next_db_version: called CC=4 SCAR=13.9
431 9 my ($prev) = @_;
432 9 my @t = gmtime;
433 9 my $year = $t[5] + 1900;
434 9 my $month = $t[4] + 1;
435 9 my $patch = 1;
436 9 ▶ if (defined $prev && $prev =~ /^(\d+)\.(\d+)\.(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev and $prev =~ /^(\d+)\.(\d+)\.(\d+)$/) 6 3 9
Condition66%2/3
defined $prev and $prev =~ /^(\d+)\.(\d+)\.(\d+)$/
!ll&&!rl&&r
306
MC/DC50%1/2
defined $prev and $prev =~ /^(\d+)\.(\d+)\.(\d+)$/
defined $prev $prev =~ /^(\d+)\.(\d+)\.(\d+)$/
Truth table66%2/3
A and B
A= defined $prevB= $prev =~ /^(\d+)\.(\d+)\.(\d+)$/
ABresult
0X0
100
111
437 6 ▶ $patch = $3 + 1 if "$1.$2" eq "$year.$month";
Branch: 1 branch
BranchTrueFalseTotal
if "$1.$2" eq "$year.$month" 5 1 6
438 }
439 9 return "$year.$month.$patch";
440 }
441  
442 # JSON booleans so they round-trip as true/false, not 1/0/"".
443 7 ▶sub JSON_true { JSON::PP::true() }
Subroutine JSON_true: called CC=1 SCAR=0.0
Pod undocumented
444 52 ▶sub JSON_false { JSON::PP::false() }
Subroutine JSON_false: called CC=1 SCAR=0.0
Pod undocumented
445  
446 4 require JSON::PP;
447  
448 4 1;
↑ Summary blib/lib/Bioconda/Packages/RecipeParser.pm »