| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | if (ref $extra->{identifiers} eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 128 | $info{identifiers} = | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | [ grep { defined && !/$JINJA_SENTINEL/ } | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
defined $_ && !/$JINJA_SENTINEL/
MC/DC0%0/2
defined $_ && !/$JINJA_SENTINEL/
defined $_
!/$JINJA_SENTINEL/
Truth table33%1/3
A && not(B)
A= defined $_B= !/$JINJA_SENTINEL/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | for my $out (@{ $tree->{outputs} }) { | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | next unless ref $out eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 0 | my $oname = _clean_scalar($out->{name}); | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | next unless defined $oname && length $oname | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
defined $oname and length $oname and not $oname =~ /$JINJA_SENTINEL/
defined $oname and length $oname
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/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $info{"name"} and length $info{"name"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $info{"name"} and length $info{"name"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 158 | push @warnings, 'unresolved jinja in name' | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 8 | ▶ | if defined $info{name} && $info{name} =~ /$JINJA_SENTINEL|\{\{/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $info{"name"} and $info{"name"} =~ /$JINJA_SENTINEL|\{\{/
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|\{\{/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 160 | push @warnings, 'missing version' | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | if (!defined $info{version} || !length $info{version}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 8 | ▶ | && !@output_names; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $info{"version"} and length $info{"version"} or @output_names
defined $info{"version"} and length $info{"version"}
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | push @warnings, 'unresolved jinja in version' | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 8 | ▶ | if defined $info{version} && $info{version} =~ /$JINJA_SENTINEL|\{\{/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $info{"version"} and $info{"version"} =~ /$JINJA_SENTINEL|\{\{/
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|\{\{/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 10 | ▶ | return $1 if $atom =~ /^'([^']*)'$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | # Number literal. | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 9 | ▶ | return $atom if $atom =~ /^-?\d+(?:\.\d+)?$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 6 | my ($name, $filters) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||
| 215 | 6 | ▶ | return $JINJA_SENTINEL unless exists $vars->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 6 | my $val = $vars->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||
| 217 | 6 | ▶ | return $JINJA_SENTINEL if $val eq $JINJA_SENTINEL; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 5 | ▶ | while ($filters =~ /\|\s*(\w+)(?:\(([^)]*)\))?/g) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 2 | my ($f, $args) = ($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||
| 220 | 2 | ▶ | if ($f eq 'lower') { $val = lc $val } | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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*["']([^"']*)["']/
$f eq "replace" and defined $args
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*["']([^"']*)["']/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 67 | ▶ | next if $raw =~ /^\s*#/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | 67 | ▶ | next if $raw =~ /^---\s*$/ || $raw =~ /^\.\.\.\s*$/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$raw =~ /^---\s*$/ or $raw =~ /^\.\.\.\s*$/
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*$/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 33 | my $indent = $toks[$pos][0]; | |||||||||||||||||||||||||||||||||||||||||||||
| 277 | 33 | ▶ | return undef if $indent < $min_indent; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | # Sequence? | ||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 33 | ▶ | if ($toks[$pos][1] =~ /^-\s|^-$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 8 | my @list; | |||||||||||||||||||||||||||||||||||||||||||||
| 282 | 8 | ▶ | while ($pos <= $#toks | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$pos <= $#toks and $toks[$pos][0] == $indent and $toks[$pos][1] =~ /^-(\s+|$)/
$pos <= $#toks and $toks[$pos][0] == $indent
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+|$)/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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[://]
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[://]
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | $map->{$k} = ($v eq '') ? undef : $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$pos <= $#toks and $toks[$pos][0] >= $child_indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks and $toks[$pos][0] >= $child_indent
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] =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$pos <= $#toks and $toks[$pos][0] > $indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks and $toks[$pos][0] > $indent
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] =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$pos <= $#toks and $toks[$pos][0] == $indent and not $toks[$pos][1] =~ /^-/
$pos <= $#toks and $toks[$pos][0] == $indent
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] =~ /^-/
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $val and $val ne ""
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 ""
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | my @body; | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | 1 | ▶ | while ($pos <= $#toks && $toks[$pos][0] > $indent) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$pos <= $#toks and $toks[$pos][0] > $indent
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$pos <= $#toks and $toks[$pos][0] > $indent and _has_unclosed_quote($full)
$pos <= $#toks and $toks[$pos][0] > $indent
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$pos <= $#toks and $toks[$pos][0] > $indent
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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) // {}
MC/DC0%0/1
&$build(0) // {}
&$build(0)
Truth table50%1/2
A
A= &$build(0)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 377 | 8 | ▶ | return ref $root eq 'HASH' ? $root : { _root => $root }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 4 | ▶ | return $s =~ /^'(?:[^']|'')*'\s*$/ ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 23 | ▶ | if ($s =~ /^"/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 0 | ▶ | return $s =~ /^"(?:[^"\\]|\\.)*"\s*$/ ? 0 : 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 399 | 26 | (my $k = $1) =~ s/\s+$//; | |||||||||||||||||||||||||||||||||||||||||||||
| 400 | 26 | return ($k, $2); | |||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 402 | 25 | ▶ | if ($line =~ /^([^:][^:]*?)\s*:\s*$/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 416 | 27 | ▶ | return undef if ref $v; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 417 | 27 | $v =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||
| 418 | # Strip matching surrounding quotes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 419 | 27 | ▶ | $v =~ s/^"(.*)"$/$1/ or $v =~ s/^'(.*)'$/$1/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 420 | 27 | ▶ | return undef if $v eq ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 421 | 27 | ▶ | return undef if $v =~ /$JINJA_SENTINEL/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 6 | my @deps; | |||||||||||||||||||||||||||||||||||||||||||||
| 432 | 6 | for my $raw (@$list) { | |||||||||||||||||||||||||||||||||||||||||||||
| 433 | 9 | ▶ | next if ref $raw; # skip nested structures | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 9 | ▶ | next unless defined $raw; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 9 | my $s = $raw; | |||||||||||||||||||||||||||||||||||||||||||||
| 436 | 9 | $s =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||||||||||||||||||||
| 437 | 9 | ▶ | next if $s eq ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 438 | 9 | ▶ | next if $s =~ /$JINJA_SENTINEL/; # {{ compiler('c') }} etc. | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 439 | 8 | ▶ | next if $s =~ /\{\{/; # any leftover jinja | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 440 | 8 | my ($name, $constraint) = split /\s+/, $s, 2; | |||||||||||||||||||||||||||||||||||||||||||||
| 441 | 8 | ▶ | next unless defined $name && length $name; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $name and length $name
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 442 | 8 | ▶ | push @deps, { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $constraint and length $constraint
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||||||