Packages/RecipeParser.pm

filter: Statementstmt 82.5% 180 / 218 Branchbran 64.7% 92 / 142 Conditioncond 43.0% 37 / 86 MC/DCmcdc 18.3% 9 / 49 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 2 totaltotal 64.8% 332 / 512 CC 108 SCAR 40.6
CC
108
Cov
69%
__ANON__
143.5
parse_string
56.0
_parse_yaml_subset
14.6
CRAP 58.1
« Packages/Updater.pm ↑ Summary Packages/DB.pm »
1 package Bioconda::Packages::RecipeParser;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.01';
8 3 our @EXPORT_OK = qw(parse_file parse_string);
9  
10 =head1 NAME
11  
12 Bioconda::Packages::RecipeParser - Extract structured metadata from a Bioconda
13 C<meta.yaml> recipe file.
14  
15 =head1 SYNOPSIS
16  
17 use Bioconda::Packages::RecipeParser qw(parse_file);
18  
19 my $info = parse_file('bioconda-recipes/recipes/samtools/meta.yaml');
20 print $info->{name}; # samtools
21 print $info->{version}; # 1.25
22 print "@{ $info->{depends}{run} }";
23  
24 =head1 DESCRIPTION
25  
26 Bioconda recipes are Jinja2-templated YAML, which is not valid YAML on its own.
27 This module does just enough Jinja rendering and a deliberately small YAML-subset
28 walk to pull out the handful of fields we care about. It is intentionally
29 dependency-free (core Perl only) and forgiving: anything it cannot resolve is
30 flagged rather than fatal.
31  
32 Extracted fields (hashref):
33  
34 name version package_names build_number noarch
35 summary license license_family home dev_url doc_url
36 identifiers outputs depends {build,host,run}
37 warnings (arrayref of parse notes; empty on a clean parse)
38  
39 Each dependency is C<< { name => 'python', constraint => '>=3.9', platform => undef } >>.
40  
41 =cut
42  
43 # Sentinel used for unresolved Jinja function calls like {{ compiler('c') }}.
44 # Dependency entries containing it are dropped (they are build-system pins, not
45 # real package dependencies).
46 3 my $JINJA_SENTINEL = "\0JINJA\0";
47  
48 #-----------------------------------------------------------------------------
49 # Public
50 #-----------------------------------------------------------------------------
51  
52 0 ▶sub parse_file {
Subroutine parse_file: not called CC=2 SCAR=17.9
Pod undocumented
53 0 my ($path) = @_;
54 0 ▶ open my $fh, '<:encoding(UTF-8)', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:encoding(UTF-8)", $path or die "Cannot read recipe '${path}': $!\n" 0 0 0
55 or die "Cannot read recipe '$path': $!\n";
56 0 local $/;
57 0 my $text = <$fh>;
58 0 close $fh;
59 0 return parse_string($text);
60 }
61  
62 8 ▶sub parse_string {
Subroutine parse_string: called CC=29 SCAR=40.3
Pod undocumented
63 8 my ($text) = @_;
64  
65 8 my @warnings;
66 8 my $vars = _collect_jinja_vars($text);
67  
68 # Render each line: drop {% ... %} control lines, substitute {{ ... }}.
69 8 my @lines;
70 8 for my $line (split /\n/, $text, -1) {
71 88 ▶ next if $line =~ /^\s*\{%/; # {% set ... %}, {% if ... %}, etc.
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*\{%/ 6 82 88
72 82 push @lines, _render_jinja($line, $vars);
73 }
74 8 my $rendered = join "\n", @lines;
75  
76 8 my $tree = _parse_yaml_subset($rendered);
77  
78 8 my %info = (
79 name => undef,
80 version => undef,
81 package_names => [],
82 build_number => undef,
83 noarch => undef,
84 summary => undef,
85 license => undef,
86 license_family=> undef,
87 home => undef,
88 dev_url => undef,
89 doc_url => undef,
90 identifiers => [],
91 outputs => [],
92 depends => { build => [], host => [], run => [] },
93 warnings => \@warnings,
94 );
95  
96 # package:
97 8 ▶ if (my $pkg = $tree->{package}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pkg = $tree->{"package"}) 8 0 8
98 8 $info{name} = _clean_scalar($pkg->{name});
99 8 $info{version} = _clean_scalar($pkg->{version});
100 }
101  
102 # build:
103 8 ▶ if (my $build = $tree->{build}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $build = $tree->{"build"}) 1 7 8
104 1 $info{build_number} = _clean_scalar($build->{number});
105 1 $info{noarch} = _clean_scalar($build->{noarch});
106 }
107  
108 # requirements:
109 8 ▶ if (my $req = $tree->{requirements}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $req = $tree->{"requirements"}) 4 4 8
110 4 for my $kind (qw(build host run)) {
111 12 $info{depends}{$kind} = _deps_from_list($req->{$kind});
112 }
113 }
114  
115 # about:
116 8 ▶ if (my $about = $tree->{about}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $about = $tree->{"about"}) 3 5 8
117 3 $info{summary} = _clean_scalar($about->{summary});
118 3 $info{license} = _clean_scalar($about->{license});
119 3 $info{license_family} = _clean_scalar($about->{license_family});
120 3 $info{home} = _clean_scalar($about->{home});
121 3 $info{dev_url} = _clean_scalar($about->{dev_url});
122 3 $info{doc_url} = _clean_scalar($about->{doc_url});
123 }
124  
125 # extra.identifiers:
126 8 ▶ if (my $extra = $tree->{extra}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $extra = $tree->{"extra"}) 1 7 8
127 1 ▶ if (ref $extra->{identifiers} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $extra->{"identifiers"} eq "ARRAY") 1 0 1
128 $info{identifiers} =
129 2 ▶ [ grep { defined && !/$JINJA_SENTINEL/ }
Condition33%1/3
defined $_ && !/$JINJA_SENTINEL/
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_ && !/$JINJA_SENTINEL/
defined $_ !/$JINJA_SENTINEL/
Truth table33%1/3
A && not(B)
A= defined $_B= !/$JINJA_SENTINEL/
ABresult
0X0
100
111
130 1 map { _clean_scalar($_) } @{ $extra->{identifiers} } ];
131 }
132 }
133  
134 # outputs: (multi-package recipes). Capture each output's package name and,
135 # when present, merge its run requirements into the run dependency set so the
136 # names are discoverable.
137 8 my @output_names;
138 8 ▶ if (ref $tree->{outputs} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tree->{"outputs"} eq "ARRAY") 0 8 8
139 0 for my $out (@{ $tree->{outputs} }) {
140 0 ▶ next unless ref $out eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out eq "HASH" 0 0 0
141 0 my $oname = _clean_scalar($out->{name});
142 0 ▶ next unless defined $oname && length $oname
Branch: 1 branch
BranchTrueFalseTotal
unless defined $oname and length $oname and not $oname =~ /$JINJA_SENTINEL/ 0 0 0
Condition0%0/6
defined $oname and length $oname and not $oname =~ /$JINJA_SENTINEL/
!ll&&!rl&&r
000
defined $oname and length $oname
!ll&&!rl&&r
000
MC/DC0%0/3
defined $oname and length $oname and not $oname =~ /$JINJA_SENTINEL/
defined $oname length $oname not $oname =~ /$JINJA_SENTINEL/
Truth table0%0/4
A and B and not(C)
A= defined $onameB= length $onameC= not $oname =~ /$JINJA_SENTINEL/
ABCresult
0XX0
10X0
1100
1111
143 && $oname !~ /$JINJA_SENTINEL/;
144 0 push @output_names, $oname;
145 }
146 }
147 8 $info{outputs} = \@output_names;
148  
149 # The set of installable package names this recipe provides.
150 8 my %names;
151 8 ▶ $names{ $info{name} } = 1 if defined $info{name} && length $info{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{"name"} and length $info{"name"} 8 0 8
Condition33%1/3
defined $info{"name"} and length $info{"name"}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $info{"name"} and length $info{"name"}
defined $info{"name"} length $info{"name"}
Truth table33%1/3
A and B
A= defined $info{"name"}B= length $info{"name"}
ABresult
0X0
100
111
152 8 $names{$_} = 1 for @output_names;
153 8 $info{package_names} = [ sort keys %names ];
154  
155 # Sanity flags.
156 push @warnings, 'missing package name'
157 8 ▶ if !defined $info{name} || !length $info{name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info{"name"} and length $info{"name"} 0 8 8
Condition33%1/3
defined $info{"name"} and length $info{"name"}
!ll&&!rl&&r
008
MC/DC0%0/2
defined $info{"name"} and length $info{"name"}
defined $info{"name"} length $info{"name"}
Truth table33%1/3
A and B
A= defined $info{"name"}B= length $info{"name"}
ABresult
0X0
100
111
158 push @warnings, 'unresolved jinja in name'
159 8 ▶ if defined $info{name} && $info{name} =~ /$JINJA_SENTINEL|\{\{/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{"name"} and $info{"name"} =~ /$JINJA_SENTINEL|\{\{/ 0 8 8
Condition33%1/3
defined $info{"name"} and $info{"name"} =~ /$JINJA_SENTINEL|\{\{/
!ll&&!rl&&r
080
MC/DC0%0/2
defined $info{"name"} and $info{"name"} =~ /$JINJA_SENTINEL|\{\{/
defined $info{"name"} $info{"name"} =~ /$JINJA_SENTINEL|\{\{/
Truth table33%1/3
A and B
A= defined $info{"name"}B= $info{"name"} =~ /$JINJA_SENTINEL|\{\{/
ABresult
0X0
100
111
160 push @warnings, 'missing version'
161 if (!defined $info{version} || !length $info{version})
162 8 ▶ && !@output_names;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info{"version"} and length $info{"version"} or @output_names 1 7 8
Condition66%4/6
defined $info{"version"} and length $info{"version"} or @output_names
l!l&&r!l&&!r
701
defined $info{"version"} and length $info{"version"}
!ll&&!rl&&r
107
MC/DC33%1/3
defined $info{"version"} and length $info{"version"} or @output_names
defined $info{"version"} length $info{"version"} @output_names
Truth table40%2/5
A and B or C
A= defined $info{"version"}B= length $info{"version"}C= @output_names
ABCresult
11X1
0X11
1011
0X00
1000
163 push @warnings, 'unresolved jinja in version'
164 8 ▶ if defined $info{version} && $info{version} =~ /$JINJA_SENTINEL|\{\{/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $info{"version"} and $info{"version"} =~ /$JINJA_SENTINEL|\{\{/ 0 8 8
Condition66%2/3
defined $info{"version"} and $info{"version"} =~ /$JINJA_SENTINEL|\{\{/
!ll&&!rl&&r
170
MC/DC0%0/2
defined $info{"version"} and $info{"version"} =~ /$JINJA_SENTINEL|\{\{/
defined $info{"version"} $info{"version"} =~ /$JINJA_SENTINEL|\{\{/
Truth table66%2/3
A and B
A= defined $info{"version"}B= $info{"version"} =~ /$JINJA_SENTINEL|\{\{/
ABresult
0X0
100
111
165  
166 8 return \%info;
167 }
168  
169 #-----------------------------------------------------------------------------
170 # Jinja handling
171 #-----------------------------------------------------------------------------
172  
173 # Collect {% set key = expr %} declarations into a hash, resolving references to
174 # previously-set vars and simple string concatenation with '~'.
175 8 ▶sub _collect_jinja_vars {
Subroutine _collect_jinja_vars: called CC=2 SCAR=6.9
176 8 my ($text) = @_;
177 8 my %vars;
178 8 ▶ while ($text =~ /\{%\s*set\s+(\w+)\s*=\s*(.+?)\s*%\}/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\{%\s*set\s+(\w+)\s*=\s*(.+?)\s*%\}/g) 6 8 14
179 6 my ($key, $expr) = ($1, $2);
180 6 $vars{$key} = _eval_jinja_expr($expr, \%vars);
181 }
182 8 return \%vars;
183 }
184  
185 # Evaluate a very small subset of Jinja expressions: quoted strings, numbers,
186 # bare variable references (optionally with |lower / |upper), and '~'
187 # concatenation of those. Returns the sentinel for anything else.
188 14 ▶sub _eval_jinja_expr {
Subroutine _eval_jinja_expr: called CC=3 SCAR=11.0
189 14 my ($expr, $vars) = @_;
190 14 $expr =~ s/^\s+|\s+$//g;
191  
192 14 my @parts = split /\s*~\s*/, $expr;
193 14 my @out;
194 14 for my $p (@parts) {
195 14 push @out, _eval_jinja_atom($p, $vars);
196 }
197 14 ▶ return $JINJA_SENTINEL if grep { $_ eq $JINJA_SENTINEL } @out;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $JINJA_SENTINEL;} @out 4 10 14
198 10 return join '', @out;
199 }
200  
201 14 ▶sub _eval_jinja_atom {
Subroutine _eval_jinja_atom: called CC=13 SCAR=26.6
202 14 my ($atom, $vars) = @_;
203 14 $atom =~ s/^\s+|\s+$//g;
204  
205 # String literal.
206 14 ▶ return $1 if $atom =~ /^"([^"]*)"$/;
Branch: 1 branch
BranchTrueFalseTotal
if $atom =~ /^"([^"]*)"$/ 4 10 14
207 10 ▶ return $1 if $atom =~ /^'([^']*)'$/;
Branch: 1 branch
BranchTrueFalseTotal
if $atom =~ /^'([^']*)'$/ 1 9 10
208 # Number literal.
209 9 ▶ return $atom if $atom =~ /^-?\d+(?:\.\d+)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $atom =~ /^-?\d+(?:\.\d+)?$/ 0 9 9
210  
211 # Variable with an optional chain of filters, e.g.
212 # name|lower version|replace("-", "_") x|lower|replace('a','b')
213 9 ▶ if ($atom =~ /^(\w+)((?:\s*\|\s*\w+(?:\([^)]*\))?)*)\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($atom =~ /^(\w+)((?:\s*\|\s*\w+(?:\([^)]*\))?)*)\s*$/) 6 3 9
214 6 my ($name, $filters) = ($1, $2);
215 6 ▶ return $JINJA_SENTINEL unless exists $vars->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $vars->{$name} 0 6 6
216 6 my $val = $vars->{$name};
217 6 ▶ return $JINJA_SENTINEL if $val eq $JINJA_SENTINEL;
Branch: 1 branch
BranchTrueFalseTotal
if $val eq $JINJA_SENTINEL 1 5 6
218 5 ▶ while ($filters =~ /\|\s*(\w+)(?:\(([^)]*)\))?/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filters =~ /\|\s*(\w+)(?:\(([^)]*)\))?/g) 2 5 7
219 2 my ($f, $args) = ($1, $2);
220 2 ▶ if ($f eq 'lower') { $val = lc $val }
Branch: 3 branches
BranchTrueFalseTotal
if ($f eq 'lower') { } 1 1 2
elsif ($f eq 'upper') { } 0 1 1
elsif ($f eq 'replace' and defined $args and $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/) { } 1 0 1
221 0 elsif ($f eq 'upper') { $val = uc $val }
222 ▶ elsif ($f eq 'replace' && defined $args
Condition33%2/6
$f eq "replace" and defined $args and $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/
!ll&&!rl&&r
001
$f eq "replace" and defined $args
!ll&&!rl&&r
001
MC/DC0%0/3
$f eq "replace" and defined $args and $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/
$f eq "replace" defined $args $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/
Truth table25%1/4
A and B and C
A= $f eq "replace"B= defined $argsC= $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/
ABCresult
0XX0
10X0
1100
1111
223 && $args =~ /["']([^"']*)["']\s*,\s*["']([^"']*)["']/) {
224 1 my ($from, $to) = ($1, $2);
225 1 my $q = quotemeta $from;
226 1 $val =~ s/$q/$to/g;
227 }
228 # Unknown filters are ignored (keep the value as-is).
229 }
230 5 return $val;
231 }
232  
233 3 return $JINJA_SENTINEL; # function calls and anything unsupported
234 }
235  
236 # Substitute {{ ... }} expressions within a single line.
237 82 ▶sub _render_jinja {
Subroutine _render_jinja: called CC=1 SCAR=0.0
238 82 my ($line, $vars) = @_;
239 82 $line =~ s/\{\{\s*(.+?)\s*\}\}/ _eval_jinja_expr($1, $vars) /ge;
240 82 return $line;
241 }
242  
243 #-----------------------------------------------------------------------------
244 # Minimal YAML-subset parser
245 #
246 # Handles mappings, block sequences, nested structures and scalar values -
247 # enough for the regular shape of Bioconda recipes. It is not a general YAML
248 # implementation and does not try to be.
249 #-----------------------------------------------------------------------------
250  
251 8 ▶sub _parse_yaml_subset {
Subroutine _parse_yaml_subset: called CC=9 SCAR=26.8
252 8 my ($text) = @_;
253  
254 # Pre-tokenise into (indent, content) pairs, dropping blanks, full-line
255 # comments and YAML document markers. Inline comments (" #...") are stripped
256 # YAML-style: only a '#' preceded by whitespace starts a comment, so URLs
257 # containing '#' are safe.
258 8 my @toks;
259 8 for my $raw (split /\n/, $text) {
260 74 ▶ next if $raw =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /^\s*$/ 7 67 74
261 67 ▶ next if $raw =~ /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /^\s*#/ 0 67 67
262 67 ▶ next if $raw =~ /^---\s*$/ || $raw =~ /^\.\.\.\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $raw =~ /^---\s*$/ or $raw =~ /^\.\.\.\s*$/ 0 67 67
Condition33%1/3
$raw =~ /^---\s*$/ or $raw =~ /^\.\.\.\s*$/
l!l&&r!l&&!r
0067
MC/DC0%0/2
$raw =~ /^---\s*$/ or $raw =~ /^\.\.\.\s*$/
$raw =~ /^---\s*$/ $raw =~ /^\.\.\.\s*$/
Truth table33%1/3
A or B
A= $raw =~ /^---\s*$/B= $raw =~ /^\.\.\.\s*$/
ABresult
1X1
011
000
263 67 (my $noindent = $raw) =~ s/^(\s*)//;
264 67 my $indent = length $1;
265 67 $noindent =~ s/\s+#.*$//; # strip inline comment / selector
266 67 $noindent =~ s/\s+$//;
267 67 ▶ next if $noindent eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $noindent eq "" 0 67 67
268 67 push @toks, [ $indent, $noindent ];
269 }
270  
271 8 my $pos = 0;
272 8 my $build;
273 33 ▶ $build = sub {
Subroutine __ANON__: called CC=34 SCAR=49.7
274 33 my ($min_indent) = @_;
275 33 ▶ return undef if $pos > $#toks;
Branch: 1 branch
BranchTrueFalseTotal
if $pos > $#toks 0 33 33
276 33 my $indent = $toks[$pos][0];
277 33 ▶ return undef if $indent < $min_indent;
Branch: 1 branch
BranchTrueFalseTotal
if $indent < $min_indent 0 33 33
278  
279 # Sequence?
280 33 ▶ if ($toks[$pos][1] =~ /^-\s|^-$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($toks[$pos][1] =~ /^-\s|^-$/) 8 25 33
281 8 my @list;
282 8 ▶ while ($pos <= $#toks
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] == $indent and $toks[$pos][1] =~ /^-(\s+|$)/) 12 8 20
Condition83%5/6
$pos <= $#toks and $toks[$pos][0] == $indent and $toks[$pos][1] =~ /^-(\s+|$)/
!ll&&!rl&&r
8012
$pos <= $#toks and $toks[$pos][0] == $indent
!ll&&!rl&&r
2612
MC/DC66%2/3
$pos <= $#toks and $toks[$pos][0] == $indent and $toks[$pos][1] =~ /^-(\s+|$)/
$pos <= $#toks $toks[$pos][0] == $indent $toks[$pos][1] =~ /^-(\s+|$)/
Truth table75%3/4
A and B and C
A= $pos <= $#toksB= $toks[$pos][0] == $indentC= $toks[$pos][1] =~ /^-(\s+|$)/
ABCresult
0XX0
10X0
1100
1111
283 && $toks[$pos][0] == $indent
284 && $toks[$pos][1] =~ /^-(\s+|$)/) {
285 12 my $item = $toks[$pos][1];
286 12 $item =~ s/^-\s*//;
287 12 ▶ if ($item eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($item eq '') { } 0 12 12
elsif ($item =~ /^(\S[^:]*?):(\s.*)?$/ and not $item =~ m[://]) { } 0 12 12
288 # "-" alone: nested block follows on deeper indent.
289 0 $pos++;
290 0 push @list, $build->($indent + 1);
291 }
292 ▶ elsif ($item =~ /^(\S[^:]*?):(\s.*)?$/
Condition33%1/3
$item =~ /^(\S[^:]*?):(\s.*)?$/ and not $item =~ m[://]
!ll&&!rl&&r
1200
MC/DC0%0/2
$item =~ /^(\S[^:]*?):(\s.*)?$/ and not $item =~ m[://]
$item =~ /^(\S[^:]*?):(\s.*)?$/ not $item =~ m[://]
Truth table33%1/3
A and not(B)
A= $item =~ /^(\S[^:]*?):(\s.*)?$/B= not $item =~ m[://]
ABresult
0X0
100
111
293 && $item !~ m{://}) {
294 # "- key: value" begins an inline mapping within the item.
295 # A mapping requires whitespace after the colon, so scalars
296 # such as "biotools:samtools" are NOT treated as mappings.
297 0 my $map = {};
298 0 my ($k, $v) = ($1, $2);
299 0 $k =~ s/\s+$//;
300 0 ▶ $v = defined $v ? do { (my $t = $v) =~ s/^\s+//; $t } : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
301 0 ▶ $map->{$k} = ($v eq '') ? undef : $v;
Branch: 1 branch
BranchTrueFalseTotal
$v eq '' ? : 0 0 0
302 0 $pos++;
303 # Continuation keys of this mapping live at indent+2.
304 0 my $child_indent = $indent + 2;
305 0 ▶ while ($pos <= $#toks
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] >= $child_indent and not $toks[$pos][1] =~ /^-/) 0 0 0
Condition0%0/6
$pos <= $#toks and $toks[$pos][0] >= $child_indent and not $toks[$pos][1] =~ /^-/
!ll&&!rl&&r
000
$pos <= $#toks and $toks[$pos][0] >= $child_indent
!ll&&!rl&&r
000
MC/DC0%0/3
$pos <= $#toks and $toks[$pos][0] >= $child_indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks $toks[$pos][0] >= $child_indent not $toks[$pos][1] =~ /^-/
Truth table0%0/4
A and B and not(C)
A= $pos <= $#toksB= $toks[$pos][0] >= $child_indentC= not $toks[$pos][1] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
306 && $toks[$pos][0] >= $child_indent
307 && $toks[$pos][1] !~ /^-/) {
308 0 my $sub = $build->($child_indent);
309 0 ▶ if (ref $sub eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub eq "HASH") 0 0 0
310 0 %$map = (%$map, %$sub);
311 }
312 0 last;
313 }
314 # Handle a nested value (e.g. "- name: x" then " run:" list)
315 0 ▶ while ($pos <= $#toks && $toks[$pos][0] > $indent
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] > $indent and not $toks[$pos][1] =~ /^-/) 0 0 0
Condition0%0/6
$pos <= $#toks and $toks[$pos][0] > $indent and not $toks[$pos][1] =~ /^-/
!ll&&!rl&&r
000
$pos <= $#toks and $toks[$pos][0] > $indent
!ll&&!rl&&r
000
MC/DC0%0/3
$pos <= $#toks and $toks[$pos][0] > $indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks $toks[$pos][0] > $indent not $toks[$pos][1] =~ /^-/
Truth table0%0/4
A and B and not(C)
A= $pos <= $#toksB= $toks[$pos][0] > $indentC= not $toks[$pos][1] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
316 && $toks[$pos][1] !~ /^-/) {
317 0 my $sub = $build->($toks[$pos][0]);
318 0 ▶ if (ref $sub eq 'HASH') { %$map = (%$map, %$sub) }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub eq 'HASH') { } 0 0 0
319 0 else { last }
320 }
321 0 push @list, $map;
322 }
323 else {
324 12 push @list, $item;
325 12 $pos++;
326 }
327 }
328 8 return \@list;
329 }
330  
331 # Mapping.
332 25 my %map;
333 25 ▶ while ($pos <= $#toks && $toks[$pos][0] == $indent
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] == $indent and not $toks[$pos][1] =~ /^-/) 51 25 76
Condition83%5/6
$pos <= $#toks and $toks[$pos][0] == $indent and not $toks[$pos][1] =~ /^-/
!ll&&!rl&&r
25051
$pos <= $#toks and $toks[$pos][0] == $indent
!ll&&!rl&&r
16951
MC/DC66%2/3
$pos <= $#toks and $toks[$pos][0] == $indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks $toks[$pos][0] == $indent not $toks[$pos][1] =~ /^-/
Truth table75%3/4
A and B and not(C)
A= $pos <= $#toksB= $toks[$pos][0] == $indentC= not $toks[$pos][1] =~ /^-/
ABCresult
0XX0
10X0
1100
1111
334 && $toks[$pos][1] !~ /^-/) {
335 51 my ($key, $val) = _split_kv($toks[$pos][1]);
336 51 $pos++;
337 51 ▶ if (defined $val && $val ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and $val ne '') { } 26 25 51
Condition66%2/3
defined $val and $val ne ""
!ll&&!rl&&r
02526
MC/DC50%1/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table66%2/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
338 # Block scalar ( "key: |" / "key: >" ): absorb the more-indented
339 # body so it cannot leak into following keys.
340 26 ▶ if ($val =~ /^[|>][-+]?\d*\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($val =~ /^[|>][-+]?\d*\s*$/) { } 1 25 26
elsif (_has_unclosed_quote($val)) { } 1 24 25
341 1 my @body;
342 1 ▶ while ($pos <= $#toks && $toks[$pos][0] > $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] > $indent) 2 1 3
Condition66%2/3
$pos <= $#toks and $toks[$pos][0] > $indent
!ll&&!rl&&r
012
MC/DC50%1/2
$pos <= $#toks and $toks[$pos][0] > $indent
$pos <= $#toks $toks[$pos][0] > $indent
Truth table66%2/3
A and B
A= $pos <= $#toksB= $toks[$pos][0] > $indent
ABresult
0X0
100
111
343 2 push @body, $toks[$pos][1];
344 2 $pos++;
345 }
346 1 $map{$key} = join ' ', @body;
347 }
348 # Multi-line quoted scalar: the value opens a quote that is not
349 # closed on this line, so keep absorbing lines until it closes.
350 elsif (_has_unclosed_quote($val)) {
351 1 my $full = $val;
352 1 ▶ while ($pos <= $#toks && $toks[$pos][0] > $indent
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] > $indent and _has_unclosed_quote($full)) 2 1 3
Condition66%4/6
$pos <= $#toks and $toks[$pos][0] > $indent and _has_unclosed_quote($full)
!ll&&!rl&&r
102
$pos <= $#toks and $toks[$pos][0] > $indent
!ll&&!rl&&r
012
MC/DC33%1/3
$pos <= $#toks and $toks[$pos][0] > $indent and _has_unclosed_quote($full)
$pos <= $#toks $toks[$pos][0] > $indent _has_unclosed_quote($full)
Truth table50%2/4
A and B and C
A= $pos <= $#toksB= $toks[$pos][0] > $indentC= _has_unclosed_quote($full)
ABCresult
0XX0
10X0
1100
1111
353 && _has_unclosed_quote($full)) {
354 2 $full .= ' ' . $toks[$pos][1];
355 2 $pos++;
356 }
357 1 $map{$key} = $full;
358 }
359 else {
360 24 $map{$key} = $val;
361 }
362 }
363 else {
364 # Value is on deeper-indented following lines (block or map).
365 25 ▶ if ($pos <= $#toks && $toks[$pos][0] > $indent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos <= $#toks and $toks[$pos][0] > $indent) { } 25 0 25
Condition33%1/3
$pos <= $#toks and $toks[$pos][0] > $indent
!ll&&!rl&&r
0025
MC/DC0%0/2
$pos <= $#toks and $toks[$pos][0] > $indent
$pos <= $#toks $toks[$pos][0] > $indent
Truth table33%1/3
A and B
A= $pos <= $#toksB= $toks[$pos][0] > $indent
ABresult
0X0
100
111
366 25 $map{$key} = $build->($indent + 1);
367 }
368 else {
369 0 $map{$key} = undef;
370 }
371 }
372 }
373 25 return \%map;
374 8 };
375  
376 8 ▶ my $root = $build->(0) // {};
Condition50%1/2
&$build(0) // {}
l!l
80
MC/DC0%0/1
&$build(0) // {}
&$build(0)
Truth table50%1/2
A
A= &$build(0)
Aresult
11
00
377 8 ▶ return ref $root eq 'HASH' ? $root : { _root => $root };
Branch: 1 branch
BranchTrueFalseTotal
ref $root eq 'HASH' ? : 8 0 8
378 }
379  
380 # True if the string opens a quote that is not closed on the same line.
381 27 ▶sub _has_unclosed_quote {
Subroutine _has_unclosed_quote: called CC=5 SCAR=17.2
382 27 my ($s) = @_;
383 27 ▶ if ($s =~ /^'/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^'/) 4 23 27
384 4 ▶ return $s =~ /^'(?:[^']|'')*'\s*$/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^'(?:[^']|'')*'\s*$/ ? : 1 3 4
385 }
386 23 ▶ if ($s =~ /^"/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /^"/) 0 23 23
387 0 ▶ return $s =~ /^"(?:[^"\\]|\\.)*"\s*$/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^"(?:[^"\\]|\\.)*"\s*$/ ? : 0 0 0
388 }
389 23 return 0;
390 }
391  
392 # Split "key: value", being careful not to split on ':' inside a URL or value.
393 51 ▶sub _split_kv {
Subroutine _split_kv: called CC=3 SCAR=11.1
394 51 my ($line) = @_;
395 # A mapping key requires whitespace after the colon (or nothing after it).
396 # Note some recipes write "name : value" with a space before the colon, so
397 # the captured key is right-trimmed.
398 51 ▶ if ($line =~ /^([^:][^:]*?)\s*:\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:][^:]*?)\s*:\s+(.+)$/) 26 25 51
399 26 (my $k = $1) =~ s/\s+$//;
400 26 return ($k, $2);
401 }
402 25 ▶ if ($line =~ /^([^:][^:]*?)\s*:\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^:][^:]*?)\s*:\s*$/) 25 0 25
403 25 (my $k = $1) =~ s/\s+$//;
404 25 return ($k, '');
405 }
406 0 return ($line, undef);
407 }
408  
409 #-----------------------------------------------------------------------------
410 # Value helpers
411 #-----------------------------------------------------------------------------
412  
413 38 ▶sub _clean_scalar {
Subroutine _clean_scalar: called CC=6 SCAR=18.2
414 38 my ($v) = @_;
415 38 ▶ return undef unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 11 27 38
416 27 ▶ return undef if ref $v;
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 27 27
417 27 $v =~ s/^\s+|\s+$//g;
418 # Strip matching surrounding quotes.
419 27 ▶ $v =~ s/^"(.*)"$/$1/ or $v =~ s/^'(.*)'$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$v =~ s/^"(.*)"$/$1/ or $v =~ s/^'(.*)'$/$1/ 0 27 27
420 27 ▶ return undef if $v eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "" 0 27 27
421 27 ▶ return undef if $v =~ /$JINJA_SENTINEL/;
Branch: 1 branch
BranchTrueFalseTotal
if $v =~ /$JINJA_SENTINEL/ 1 26 27
422 # Normalise YAML booleans/nulls we care about.
423 26 return $v;
424 }
425  
426 # Turn a YAML list of dependency strings into structured entries, dropping
427 # pure-Jinja (build-system) entries.
428 12 ▶sub _deps_from_list {
Subroutine _deps_from_list: called CC=12 SCAR=26.0
429 12 my ($list) = @_;
430 12 ▶ return [] unless ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $list eq "ARRAY" 6 6 12
431 6 my @deps;
432 6 for my $raw (@$list) {
433 9 ▶ next if ref $raw; # skip nested structures
Branch: 1 branch
BranchTrueFalseTotal
if ref $raw 0 9 9
434 9 ▶ next unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 0 9 9
435 9 my $s = $raw;
436 9 $s =~ s/^\s+|\s+$//g;
437 9 ▶ next if $s eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "" 0 9 9
438 9 ▶ next if $s =~ /$JINJA_SENTINEL/; # {{ compiler('c') }} etc.
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /$JINJA_SENTINEL/ 1 8 9
439 8 ▶ next if $s =~ /\{\{/; # any leftover jinja
Branch: 1 branch
BranchTrueFalseTotal
if $s =~ /\{\{/ 0 8 8
440 8 my ($name, $constraint) = split /\s+/, $s, 2;
441 8 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 8 8
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
008
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
442 8 ▶ push @deps, {
Branch: 1 branch
BranchTrueFalseTotal
defined $constraint && length $constraint ? : 1 7 8
Condition66%2/3
defined $constraint and length $constraint
!ll&&!rl&&r
701
MC/DC50%1/2
defined $constraint and length $constraint
defined $constraint length $constraint
Truth table66%2/3
A and B
A= defined $constraintB= length $constraint
ABresult
0X0
100
111
443 name => $name,
444 constraint => (defined $constraint && length $constraint)
445 ? $constraint : undef,
446 platform => undef,
447 };
448 }
449 6 return \@deps;
450 }
451  
452 3 1;
« Packages/Updater.pm ↑ Summary Packages/DB.pm »