File Coverage

blib/lib/Dpkg/BuildProfiles.pm
Criterion Covered Total %
statement 44 44 100.0
branch 8 10 80.0
condition 2 3 66.6
subroutine 10 10 100.0
pod 4 4 100.0
total 68 71 95.7


line stmt bran cond sub pod time code
1             # Copyright © 2013 Guillem Jover
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 .
15              
16             package Dpkg::BuildProfiles;
17              
18 2     2   71812 use strict;
  2         14  
  2         63  
19 2     2   11 use warnings;
  2         2  
  2         131  
20              
21             our $VERSION = '1.00';
22             our @EXPORT_OK = qw(
23             get_build_profiles
24             set_build_profiles
25             parse_build_profiles
26             evaluate_restriction_formula
27             );
28              
29 2     2   12 use Exporter qw(import);
  2         4  
  2         96  
30 2     2   13 use List::Util qw(any);
  2         3  
  2         185  
31              
32 2     2   451 use Dpkg::Build::Env;
  2         4  
  2         953  
33              
34             my $cache_profiles;
35             my @build_profiles;
36              
37             =encoding utf8
38              
39             =head1 NAME
40              
41             Dpkg::BuildProfiles - handle build profiles
42              
43             =head1 DESCRIPTION
44              
45             The Dpkg::BuildProfiles module provides functions to handle the build
46             profiles.
47              
48             =head1 FUNCTIONS
49              
50             =over 4
51              
52             =item @profiles = get_build_profiles()
53              
54             Get an array with the currently active build profiles, taken from
55             the environment variable B.
56              
57             =cut
58              
59             sub get_build_profiles {
60 2 100   2 1 13 return @build_profiles if $cache_profiles;
61              
62 1 50       5 if (Dpkg::Build::Env::has('DEB_BUILD_PROFILES')) {
63 1         4 @build_profiles = split ' ', Dpkg::Build::Env::get('DEB_BUILD_PROFILES');
64             }
65 1         2 $cache_profiles = 1;
66              
67 1         7 return @build_profiles;
68             }
69              
70             =item set_build_profiles(@profiles)
71              
72             Set C<@profiles> as the current active build profiles, by setting
73             the environment variable B.
74              
75             =cut
76              
77             sub set_build_profiles {
78 1     1 1 569 my (@profiles) = @_;
79              
80 1         2 $cache_profiles = 1;
81 1         4 @build_profiles = @profiles;
82 1         6 Dpkg::Build::Env::set('DEB_BUILD_PROFILES', join ' ', @profiles);
83             }
84              
85             =item @profiles = parse_build_profiles($string)
86              
87             Parses a build profiles specification, into an array of array references.
88              
89             =cut
90              
91             sub parse_build_profiles {
92 138     138 1 324 my $string = shift;
93              
94 138         649 $string =~ s/^\s*<\s*(.*)\s*>\s*$/$1/;
95              
96 138         479 return map { [ split ' ' ] } split /\s*>\s+<\s*/, $string;
  192         984  
97             }
98              
99             =item evaluate_restriction_formula(\@formula, \@profiles)
100              
101             Evaluate whether a restriction formula of the form " ", given as
102             a nested array, is true or false, given the array of enabled build profiles.
103              
104             =cut
105              
106             sub evaluate_restriction_formula {
107 124     124 1 186 my ($formula, $profiles) = @_;
108              
109             # Restriction formulas are in disjunctive normal form:
110             # (foo AND bar) OR (blub AND bla)
111 124         167 foreach my $restrlist (@{$formula}) {
  124         210  
112 150         185 my $seen_profile = 1;
113              
114 150         221 foreach my $restriction (@$restrlist) {
115 168 50       557 next if $restriction !~ m/^(!)?(.+)/;
116              
117 168   66     520 my $negated = defined $1 && $1 eq '!';
118 168         258 my $profile = $2;
119 168     130   463 my $found = any { $_ eq $profile } @{$profiles};
  130         200  
  168         364  
120              
121             # If a negative set profile is encountered, stop processing.
122             # If a positive unset profile is encountered, stop processing.
123 168 100       500 if ($found == $negated) {
124 84         116 $seen_profile = 0;
125 84         135 last;
126             }
127             }
128              
129             # This conjunction evaluated to true so we don't have to evaluate
130             # the others.
131 150 100       388 return 1 if $seen_profile;
132             }
133 58         147 return 0;
134             }
135              
136             =back
137              
138             =head1 CHANGES
139              
140             =head2 Version 1.00 (dpkg 1.17.17)
141              
142             Mark the module as public.
143              
144             =cut
145              
146             1;