| 1 | # Copyright © 2013 Guillem Jover <guillem@debian.org> | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 4 | # it under the terms of the GNU General Public License as published by | ||||||||||||||||||||
| 5 | # the Free Software Foundation; either version 2 of the License, or | ||||||||||||||||||||
| 6 | # (at your option) any later version. | ||||||||||||||||||||
| 7 | # | ||||||||||||||||||||
| 8 | # This program is distributed in the hope that it will be useful, | ||||||||||||||||||||
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||||||||
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||||||||
| 11 | # GNU General Public License for more details. | ||||||||||||||||||||
| 12 | # | ||||||||||||||||||||
| 13 | # You should have received a copy of the GNU General Public License | ||||||||||||||||||||
| 14 | # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | =encoding utf8 | ||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | =head1 NAME | ||||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | Dpkg::BuildProfiles - handle build profiles | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | The Dpkg::BuildProfiles module provides functions to handle the build | ||||||||||||||||||||
| 25 | profiles. | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | package Dpkg::BuildProfiles 1.01; | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 5 | ▶ | use v5.36; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 5 | our @EXPORT_OK = qw( | |||||||||||||||||||
| 34 | get_build_profiles | ||||||||||||||||||||
| 35 | set_build_profiles | ||||||||||||||||||||
| 36 | build_profile_is_invalid | ||||||||||||||||||||
| 37 | parse_build_profiles | ||||||||||||||||||||
| 38 | evaluate_restriction_formula | ||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 5 | ▶ | use Exporter qw(import); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 42 | 5 | ▶ | use List::Util qw(any); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 5 | ▶ | use Dpkg::Gettext; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | 5 | ▶ | use Dpkg::ErrorHandling; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 46 | 5 | ▶ | use Dpkg::BuildEnv; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 5 | my $cache_profiles; | |||||||||||||||||||
| 49 | my @build_profiles; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 5 | my $profile_name_regex = qr{ | |||||||||||||||||||
| 52 | # Be lenient for now. Accept operators for extensibility, uppercase, | ||||||||||||||||||||
| 53 | # and package name characters. | ||||||||||||||||||||
| 54 | [ | ||||||||||||||||||||
| 55 | ?/;:=@%*~_ | ||||||||||||||||||||
| 56 | A-Z | ||||||||||||||||||||
| 57 | a-z0-9+.\- | ||||||||||||||||||||
| 58 | ]+ | ||||||||||||||||||||
| 59 | }x; | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 5 | my $restriction_list_regex = qr{ | |||||||||||||||||||
| 62 | < | ||||||||||||||||||||
| 63 | \s* | ||||||||||||||||||||
| 64 | ( | ||||||||||||||||||||
| 65 | !? $profile_name_regex | ||||||||||||||||||||
| 66 | (?: | ||||||||||||||||||||
| 67 | \s+ | ||||||||||||||||||||
| 68 | !? $profile_name_regex | ||||||||||||||||||||
| 69 | )* | ||||||||||||||||||||
| 70 | ) | ||||||||||||||||||||
| 71 | \s* | ||||||||||||||||||||
| 72 | > | ||||||||||||||||||||
| 73 | }x; | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 5 | my $restriction_formula_regex = qr{ | |||||||||||||||||||
| 76 | ^ | ||||||||||||||||||||
| 77 | (?: | ||||||||||||||||||||
| 78 | \s* | ||||||||||||||||||||
| 79 | $restriction_list_regex | ||||||||||||||||||||
| 80 | )* | ||||||||||||||||||||
| 81 | \s* | ||||||||||||||||||||
| 82 | $ | ||||||||||||||||||||
| 83 | }x; | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | =head1 FUNCTIONS | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | =over 4 | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =item @profiles = get_build_profiles() | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Get an array with the currently active build profiles, taken from | ||||||||||||||||||||
| 92 | the environment variable B<DEB_BUILD_PROFILES>. | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 9 | ▶ | sub get_build_profiles { | ||||||||||||||||||
|
Subroutine
get_build_profiles: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 97 | 9 | ▶ | return @build_profiles if $cache_profiles; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 2 | ▶ | if (Dpkg::BuildEnv::has('DEB_BUILD_PROFILES')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | @build_profiles = map { | ||||||||||||||||||||
| 101 | 1 | ▶ | if (m{^$profile_name_regex$}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 102 | 3 | $_ | |||||||||||||||||||
| 103 | } else { | ||||||||||||||||||||
| 104 | 0 | warning(g_('invalid profile name %s ' . | |||||||||||||||||||
| 105 | 'in environment variable %s; ignoring it for now'), | ||||||||||||||||||||
| 106 | $_, 'DEB_BUILD_PROFILES'); | ||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | } split ' ', Dpkg::BuildEnv::get('DEB_BUILD_PROFILES'); | ||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | 2 | $cache_profiles = 1; | |||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 2 | return @build_profiles; | |||||||||||||||||||
| 113 | } | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =item set_build_profiles(@profiles) | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | Set C<@profiles> as the current active build profiles, by setting | ||||||||||||||||||||
| 118 | the environment variable B<DEB_BUILD_PROFILES>. | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | 1 | ▶ | sub set_build_profiles { | ||||||||||||||||||
|
Subroutine
set_build_profiles: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||
| 123 | 1 | my (@profiles) = @_; | |||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 1 | $cache_profiles = 1; | |||||||||||||||||||
| 126 | @build_profiles = map { | ||||||||||||||||||||
| 127 | 1 | ▶ | if (m{^$profile_name_regex$}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 2 | $_ | |||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||
| 130 | 0 | warning(g_('invalid profile name %s; ignoring it for now'), $_); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | } @profiles; | ||||||||||||||||||||
| 133 | 1 | Dpkg::BuildEnv::set('DEB_BUILD_PROFILES', join ' ', @profiles); | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | =item $bool = build_profile_is_invalid($string) | ||||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | Validate a build profile formula. | ||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | =cut | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 155 | ▶ | sub build_profile_is_invalid($string) | ||||||||||||||||||
|
Subroutine
build_profile_is_invalid: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 143 | { | ||||||||||||||||||||
| 144 | 155 | return $string !~ $restriction_formula_regex; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =item @profiles = parse_build_profiles($string) | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | Parses a build profiles specification, into an array of array references. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | It will die on invalid syntax. | ||||||||||||||||||||
| 152 | |||||||||||||||||||||
| 153 | =cut | ||||||||||||||||||||
| 154 | |||||||||||||||||||||
| 155 | 146 | ▶ | sub parse_build_profiles($string) | ||||||||||||||||||
|
Subroutine
parse_build_profiles: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 156 | { | ||||||||||||||||||||
| 157 | 146 | ▶ | if (build_profile_is_invalid($string)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | 6 | error(g_("'%s' is not a valid build profile restriction formula"), | |||||||||||||||||||
| 159 | $string); | ||||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 140 | my @restrictions = $string =~ m{$restriction_list_regex}g; | |||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 140 | return map { [ split ' ' ] } @restrictions; | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | =item $bool = evaluate_restriction_formula(\@formula, \@profiles) | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | Evaluate whether a restriction formula of the form "<foo bar> <baz>", given as | ||||||||||||||||||||
| 170 | a nested array, is true or false, given the array of enabled build profiles. | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | =cut | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 124 | ▶ | sub evaluate_restriction_formula { | ||||||||||||||||||
|
Subroutine
evaluate_restriction_formula: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||
| 175 | 124 | my ($formula, $profiles) = @_; | |||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # Restriction formulas are in disjunctive normal form: | ||||||||||||||||||||
| 178 | # (foo AND bar) OR (blub AND bla) | ||||||||||||||||||||
| 179 | 124 | foreach my $restrlist (@{$formula}) { | |||||||||||||||||||
| 180 | 150 | my $seen_profile = 1; | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 150 | foreach my $restriction (@$restrlist) { | |||||||||||||||||||
| 183 | 168 | ▶ | next if $restriction !~ m/^(!)?(.+)/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | ## no critic (RegularExpressions::ProhibitCaptureWithoutTest) | ||||||||||||||||||||
| 186 | 168 | ▶ | my $negated = defined $1 && $1 eq '!'; | ||||||||||||||||||
|
Condition66%2/3
defined $1 && $1 eq '!'
MC/DC50%1/2
defined $1 && $1 eq '!'
defined $1
$1 eq '!'
Truth table66%2/3
A && B
A= defined $1B= $1 eq '!'
| |||||||||||||||||||||
| 187 | 168 | my $profile = $2; | |||||||||||||||||||
| 188 | 168 | ▶ | my $found = any { $_ eq $profile } @{$profiles}; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | # If a negative set profile is encountered, stop processing. | ||||||||||||||||||||
| 191 | # If a positive unset profile is encountered, stop processing. | ||||||||||||||||||||
| 192 | 168 | ▶ | if ($found == $negated) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 193 | 84 | $seen_profile = 0; | |||||||||||||||||||
| 194 | 84 | last; | |||||||||||||||||||
| 195 | } | ||||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | # This conjunction evaluated to true so we do not have to evaluate | ||||||||||||||||||||
| 199 | # the others. | ||||||||||||||||||||
| 200 | 150 | ▶ | return 1 if $seen_profile; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 201 | } | ||||||||||||||||||||
| 202 | 58 | return 0; | |||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||
| 206 | |||||||||||||||||||||
| 207 | =head1 CHANGES | ||||||||||||||||||||
| 208 | |||||||||||||||||||||
| 209 | =head2 Version 1.01 (dpkg 1.23.0) | ||||||||||||||||||||
| 210 | |||||||||||||||||||||
| 211 | New functions: build_profile_is_invalid(). | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =head2 Version 1.00 (dpkg 1.17.17) | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | Mark the module as public. | ||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | 5 | 1; | |||||||||||||||||||