Packages/DB.pm

filter: Statementstmt 97.4% 75 / 77 Branchbran 63.0% 29 / 46 Conditioncond 46.6% 7 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 78.2% 126 / 161 CC 31 SCAR 22.1
CC
31
Cov
80%
locate_db
14.4
save
9.5
load
8.8
CRAP 9.1
« Packages/RecipeParser.pm ↑ Summary Packages.pm »
1 package Bioconda::Packages::DB;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use File::Basename qw(dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.01';
11  
12 =head1 NAME
13  
14 Bioconda::Packages::DB - Locate, load and save the bundled JSON package database.
15  
16 =head1 DESCRIPTION
17  
18 A tiny storage layer shared by the read-only API (L<Bioconda::Packages>) and the
19 maintainer tooling (L<Bioconda::Packages::Updater>). The on-disk format is a
20 single JSON file; keeping all I/O here means a different backend (e.g. SQLite)
21 could be slotted in later without touching callers.
22  
23 =head1 DATABASE FORMAT
24  
25 {
26 "meta": {
27 "generated_at": "2026-11-24T10:00:00Z",
28 "generator": "Bioconda::Packages::Updater 0.01",
29 "recipes_commit": "<git sha or null>",
30 "package_count": 11500,
31 "removed_count": 3
32 },
33 "packages": {
34 "samtools": {
35 "name": "samtools",
36 "current_version": "1.25",
37 "recipe_versions": ["0.1.18", "...", "1.25"],
38 "published_versions": [...], # from anaconda.org (optional)
39 "downloads": 123456, # from anaconda.org (optional)
40 "build_number": 0,
41 "noarch": null,
42 "summary": "...",
43 "license": "MIT",
44 "license_family": null,
45 "home": "...",
46 "dev_url": "...",
47 "doc_url": "...",
48 "identifiers": ["biotools:samtools"],
49 "outputs": ["samtools"],
50 "depends": { "build": [...], "host": [...], "run": [...] },
51 "recipe_path": "recipes/samtools",
52 "is_removed": false,
53 "removed_at": null,
54 "first_seen": "2026-11-24T10:00:00Z",
55 "last_seen": "2026-11-24T10:00:00Z"
56 }
57 }
58 }
59  
60 =cut
61  
62 # Default filenames shipped in the distribution's share/ directory. The gzipped
63 # form is preferred (it is ~1.4MB vs ~16MB) and loaded transparently.
64 5 our $DB_BASENAME = 'bioconda-packages.json.gz';
65 5 our @DB_CANDIDATES = ('bioconda-packages.json.gz', 'bioconda-packages.json');
66  
67 =head2 locate_db
68  
69 my $path = Bioconda::Packages::DB->locate_db;
70 my $path = Bioconda::Packages::DB->locate_db($explicit_path);
71  
72 Returns the path to the database, searching in order:
73  
74 =over 4
75  
76 =item 1. an explicit path argument (if given);
77  
78 =item 2. the C<BIOCONDA_PACKAGES_DB> environment variable;
79  
80 =item 3. L<File::ShareDir> (when the dist is installed and that module is present);
81  
82 =item 4. the C<share/> directory relative to this module (running from the source tree).
83  
84 =back
85  
86 Dies if nothing can be found.
87  
88 =cut
89  
90 4 ▶sub locate_db {
Subroutine locate_db: called CC=12 SCAR=26.7
Pod documented
91 4 my ($class, $explicit) = @_;
92  
93 4 ▶ return $explicit if defined $explicit && length $explicit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $explicit and length $explicit 2 2 4
Condition66%2/3
defined $explicit and length $explicit
!ll&&!rl&&r
202
MC/DC50%1/2
defined $explicit and length $explicit
defined $explicit length $explicit
Truth table66%2/3
A and B
A= defined $explicitB= length $explicit
ABresult
0X0
100
111
94 return $ENV{BIOCONDA_PACKAGES_DB}
95 2 ▶ if defined $ENV{BIOCONDA_PACKAGES_DB} && length $ENV{BIOCONDA_PACKAGES_DB};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"BIOCONDA_PACKAGES_DB"} and length $ENV{"BIOCONDA_PACKAGES_DB"} 1 1 2
Condition66%2/3
defined $ENV{"BIOCONDA_PACKAGES_DB"} and length $ENV{"BIOCONDA_PACKAGES_DB"}
!ll&&!rl&&r
101
MC/DC50%1/2
defined $ENV{"BIOCONDA_PACKAGES_DB"} and length $ENV{"BIOCONDA_PACKAGES_DB"}
defined $ENV{"BIOCONDA_PACKAGES_DB"} length $ENV{"BIOCONDA_PACKAGES_DB"}
Truth table66%2/3
A and B
A= defined $ENV{"BIOCONDA_PACKAGES_DB"}B= length $ENV{"BIOCONDA_PACKAGES_DB"}
ABresult
0X0
100
111
96  
97 # Installed location via File::ShareDir, if available.
98 1 for my $base (@DB_CANDIDATES) {
99 2 my $shared = eval {
100 2 require File::ShareDir;
101 0 File::ShareDir::dist_file('Bioconda-Packages', $base);
102 };
103 2 ▶ return $shared if defined $shared && -f $shared;
Branch: 1 branch
BranchTrueFalseTotal
if defined $shared and -f $shared 0 2 2
Condition33%1/3
defined $shared and -f $shared
!ll&&!rl&&r
200
MC/DC0%0/2
defined $shared and -f $shared
defined $shared -f $shared
Truth table33%1/3
A and B
A= defined $sharedB= -f $shared
ABresult
0X0
100
111
104 }
105  
106 # Source-tree fallback: <dist>/share/<basename>, relative to this file.
107 1 my $here = $INC{'Bioconda/Packages/DB.pm'};
108 1 ▶ if (defined $here) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $here) 1 0 1
109 1 my $libdir = dirname(dirname(dirname($here))); # .../lib
110 1 my $distdir = dirname($libdir); # dist root
111 1 for my $base (@DB_CANDIDATES) {
112 1 my $cand = File::Spec->catfile($distdir, 'share', $base);
113 1 ▶ return $cand if -f $cand;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand 0 1 1
114 # Also allow a db sitting next to the modules.
115 1 my $cand2 = File::Spec->catfile(dirname($here), $base);
116 1 ▶ return $cand2 if -f $cand2;
Branch: 1 branch
BranchTrueFalseTotal
if -f $cand2 1 0 1
117 }
118 }
119  
120 0 croak "Could not locate the Bioconda::Packages database. Set the "
121 . "BIOCONDA_PACKAGES_DB environment variable or pass db => \$path.";
122 }
123  
124 =head2 load
125  
126 my $data = Bioconda::Packages::DB->load($path);
127  
128 Reads and decodes the JSON database. Dies on failure.
129  
130 =cut
131  
132 4 ▶sub load {
Subroutine load: called CC=8 SCAR=21.8
Pod documented
133 4 my ($class, $path) = @_;
134 4 ▶ croak "No database path given" unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 4 4
135  
136 4 my $json;
137 4 ▶ if (_is_gzip($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_gzip($path)) { } 2 2 4
138 2 require IO::Uncompress::Gunzip;
139 2 ▶ IO::Uncompress::Gunzip::gunzip($path => \$json)
Branch: 1 branch
BranchTrueFalseTotal
IO::Uncompress::Gunzip::gunzip($path, \$json) or croak("Cannot gunzip database '${path}': " . $IO::Uncompress::Gunzip::GunzipError) 2 0 2
140 or croak "Cannot gunzip database '$path': "
141 . $IO::Uncompress::Gunzip::GunzipError;
142 }
143 else {
144 2 ▶ open my $fh, '<:raw', $path or croak "Cannot read database '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or croak("Cannot read database '${path}': $!") 2 0 2
145 2 local $/;
146 2 $json = <$fh>;
147 2 close $fh;
148 }
149  
150 4 my $data = eval { JSON::PP->new->utf8->decode($json) };
151 4 ▶ croak "Database '$path' is not valid JSON: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 4 4
152 croak "Database '$path' has no 'packages' section"
153 4 ▶ unless ref $data eq 'HASH' && ref $data->{packages} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" and ref $data->{"packages"} eq "HASH" 0 4 4
Condition33%1/3
ref $data eq "HASH" and ref $data->{"packages"} eq "HASH"
!ll&&!rl&&r
004
MC/DC0%0/2
ref $data eq "HASH" and ref $data->{"packages"} eq "HASH"
ref $data eq "HASH" ref $data->{"packages"} eq "HASH"
Truth table33%1/3
A and B
A= ref $data eq "HASH"B= ref $data->{"packages"} eq "HASH"
ABresult
0X0
100
111
154 4 return $data;
155 }
156  
157 =head2 save
158  
159 Bioconda::Packages::DB->save($path, $data);
160  
161 Encodes C<$data> as canonical JSON and writes it atomically (temp file + rename).
162 If C<$path> ends in C<.gz> the output is gzip-compressed and written compact
163 (it is ~1.4MB that way); otherwise it is pretty-printed for human readability.
164  
165 =cut
166  
167 3 ▶sub save {
Subroutine save: called CC=9 SCAR=22.5
Pod documented
168 3 my ($class, $path, $data) = @_;
169 3 ▶ croak "No database path given" unless defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 3 3
170 3 my $gz = $path =~ /\.gz$/;
171  
172 # Compact + canonical for the shipped gzip; pretty for a plain .json.
173 3 my $encoder = JSON::PP->new->utf8->canonical;
174 3 ▶ $encoder->pretty unless $gz;
Branch: 1 branch
BranchTrueFalseTotal
unless $gz 2 1 3
175 3 my $json = $encoder->encode($data);
176  
177 3 my $tmp = "$path.tmp.$$";
178 3 ▶ if ($gz) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gz) { } 1 2 3
179 1 require IO::Compress::Gzip;
180 1 ▶ IO::Compress::Gzip::gzip(\$json => $tmp, Level => 9)
Branch: 1 branch
BranchTrueFalseTotal
IO::Compress::Gzip::gzip(\$json, $tmp, "Level", 9) or croak("Cannot gzip '${tmp}': $IO::Compress::Gzip::GzipError") 1 0 1
181 or croak "Cannot gzip '$tmp': $IO::Compress::Gzip::GzipError";
182 }
183 else {
184 2 ▶ open my $fh, '>:raw', $tmp or croak "Cannot write '$tmp': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:raw", $tmp or croak("Cannot write '${tmp}': $!") 2 0 2
185 2 ▶ print {$fh} $json or croak "Error writing '$tmp': $!";
Branch: 1 branch
BranchTrueFalseTotal
print {$fh;} $json or croak("Error writing '${tmp}': $!") 2 0 2
186 2 ▶ close $fh or croak "Error closing '$tmp': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error closing '${tmp}': $!") 2 0 2
187 }
188 3 ▶ rename $tmp, $path or croak "Cannot rename '$tmp' -> '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
rename $tmp, $path or croak("Cannot rename '${tmp}' -> '${path}': $!") 3 0 3
189 3 return $path;
190 }
191  
192 # Detect a gzip file by extension or magic bytes (1f 8b).
193 4 ▶sub _is_gzip {
Subroutine _is_gzip: called CC=5 SCAR=17.0
194 4 my ($path) = @_;
195 4 ▶ return 1 if $path =~ /\.gz$/;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ /\.gz$/ 2 2 4
196 2 ▶ open my $fh, '<:raw', $path or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or return 0 2 0 2
197 2 read $fh, my $magic, 2;
198 2 close $fh;
199 2 ▶ return (defined $magic && $magic eq "\x1f\x8b") ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $magic && $magic eq "\037\213" ? : 0 2 2
Condition33%1/3
defined $magic and $magic eq "\37\213"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $magic and $magic eq "\37\213"
defined $magic $magic eq "\37\213"
Truth table33%1/3
A and B
A= defined $magicB= $magic eq "\37\213"
ABresult
0X0
100
111
200 }
201  
202 5 1;
« Packages/RecipeParser.pm ↑ Summary Packages.pm »