Dpkg/Conf.pm

filter: Statementstmt 79.5% 70 / 88 Branchbran 57.1% 16 / 28 Conditioncond 70.0% 14 / 20 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 76.4% 13 / 17 Podpod 100% 8 / 8 totaltotal 72.9% 127 / 174 CC 24 SCAR 23.8
CC
24
Cov
74%
load_user_config
42.0
parse
11.0
load_system_config
6.0
CRAP 10.8
« Dpkg/OpenPGP.pm ↑ Summary Dpkg/Version.pm »
1 # Copyright © 2009-2010 Raphaël Hertzog <hertzog@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::Conf - parse dpkg configuration files
21  
22 =head1 DESCRIPTION
23  
24 The Dpkg::Conf object can be used to read options from a configuration
25 file. It can export an array that can then be parsed exactly like @ARGV.
26  
27 =cut
28  
29 package Dpkg::Conf 1.04;
30  
31 1 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 1 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 1 ▶use parent qw(Dpkg::Interface::Storable);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 use overload
41 0 ▶ '@{}' => sub { return [ $_[0]->get_options() ] },
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 1 fallback => 1;
43  
44 =head1 METHODS
45  
46 =over 4
47  
48 =item $conf = Dpkg::Conf->new(%opts)
49  
50 Create a new Dpkg::Conf object.
51  
52 Options:
53  
54 =over
55  
56 =item B<allow_short>
57  
58 If set to true (it defaults to false), then short options are allowed in
59 the configuration file, they should be prepended with a single hyphen.
60  
61 =back
62  
63 =cut
64  
65 5 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
66 5 my ($this, %opts) = @_;
67 5 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
68  
69 5 my $self = {
70 options => [],
71 allow_short => 0,
72 };
73 5 foreach my $opt (keys %opts) {
74 4 $self->{$opt} = $opts{$opt};
75 }
76 5 bless $self, $class;
77  
78 5 return $self;
79 }
80  
81 =item @options = @{$conf}
82  
83 =item @options = $conf->get_options()
84  
85 Returns the list of options that can be parsed like @ARGV.
86  
87 =cut
88  
89 6 ▶sub get_options {
Subroutine get_options: called CC=1 SCAR=0.0
Pod documented
90 6 my $self = shift;
91  
92 6 return @{$self->{options}};
93 }
94  
95 =item $count = $conf->load($file)
96  
97 Read options from a file. Return the number of options parsed.
98  
99 =item $count = $conf->load_system_config($file)
100  
101 Read options from a system configuration file.
102  
103 Return the number of options parsed.
104  
105 =cut
106  
107 0 ▶sub load_system_config {
Subroutine load_system_config: not called CC=2 SCAR=17.9
Pod documented
108 0 my ($self, $file) = @_;
109  
110 0 ▶ return 0 unless -e "$Dpkg::CONFDIR/$file";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$Dpkg::CONFDIR/$file" 0 0 0
111 0 return $self->load("$Dpkg::CONFDIR/$file");
112 }
113  
114 =item $count = $conf->load_user_config($file)
115  
116 Read options from a user configuration file. It will try to use the XDG
117 directory, either $XDG_CONFIG_HOME/dpkg/ or $HOME/.config/dpkg/.
118  
119 Return the number of options parsed.
120  
121 =cut
122  
123 0 ▶sub load_user_config {
Subroutine load_user_config: not called CC=6 SCAR=37.4
Pod documented
124 0 my ($self, $file) = @_;
125  
126 0 my $confdir = $ENV{XDG_CONFIG_HOME};
127 0 ▶ $confdir ||= $ENV{HOME} . '/.config' if length $ENV{HOME};
Branch: 1 branch
BranchTrueFalseTotal
if length $ENV{"HOME"} 0 0 0
Condition0%0/2
$confdir ||= $ENV{"HOME"} . '/.config'
l!l
00
MC/DC0%0/1
$confdir ||= $ENV{"HOME"} . '/.config'
$confdir
Truth table0%0/2
A
A= $confdir
Aresult
11
00
128  
129 0 ▶ return 0 unless length $confdir;
Branch: 1 branch
BranchTrueFalseTotal
unless length $confdir 0 0 0
130 0 ▶ return 0 unless -e "$confdir/dpkg/$file";
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$confdir/dpkg/$file" 0 0 0
131 0 ▶ return $self->load("$confdir/dpkg/$file") if length $confdir;
Branch: 1 branch
BranchTrueFalseTotal
if length $confdir 0 0 0
132 0 return 0;
133 }
134  
135 =item $conf->load_config($file)
136  
137 Read options from system and user configuration files.
138  
139 Return the number of options parsed.
140  
141 =cut
142  
143 0 ▶sub load_config {
Subroutine load_config: not called CC=1 SCAR=6.9
Pod documented
144 0 my ($self, $file) = @_;
145  
146 0 my $nopts = 0;
147  
148 0 $nopts += $self->load_system_config($file);
149 0 $nopts += $self->load_user_config($file);
150  
151 0 return $nopts;
152 }
153  
154 =item $count = $conf->parse($fh)
155  
156 Parse options from a file handle. When called multiple times, the parsed
157 options are accumulated.
158  
159 Return the number of options parsed.
160  
161 =cut
162  
163 5 ▶sub parse {
Subroutine parse: called CC=11 SCAR=24.0
Pod documented
164 5 my ($self, $fh, $desc) = @_;
165 5 my $count = 0;
166 5 local $_;
167  
168 5 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 130 5 135
169 130 chomp;
170 # Strip leading spaces.
171 130 s/^\s+//;
172 # Strip trailing spaces.
173 130 s/\s+$//;
174 # Remove spaces around the first =.
175 130 s/\s+=\s+/=/;
176 # First spaces becomes = if no =.
177 130 ▶ s/\s+/=/ unless m/=/;
Branch: 1 branch
BranchTrueFalseTotal
unless /=/u 70 60 130
178 # Skip empty lines and comments.
179 130 ▶ next if /^#/ or length == 0;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/u or length $_ == 0 55 75 130
Condition100%3/3
/^#/u or length $_ == 0
l!l&&r!l&&!r
253075
MC/DC100%2/2
/^#/u or length $_ == 0
/^#/u length $_ == 0
Truth table100%3/3
A or B
A= /^#/uB= length $_ == 0
ABresult
1X1
011
000
180 75 ▶ if (/^-[^-]/ and not $self->{allow_short}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^-[^-]/u and not $self->{"allow_short"}) 2 73 75
Condition100%3/3
/^-[^-]/u and not $self->{"allow_short"}
!ll&&!rl&&r
6582
MC/DC100%2/2
/^-[^-]/u and not $self->{"allow_short"}
/^-[^-]/u not $self->{"allow_short"}
Truth table100%3/3
A and not(B)
A= /^-[^-]/uB= not $self->{"allow_short"}
ABresult
0X0
100
111
181 2 warning(g_('short option not allowed in %s, line %d'), $desc, $.);
182 2 next;
183 }
184 73 ▶ if (/^([^=]+)(?:=(.*))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^([^=]+)(?:=(.*))?$/u) { } 73 0 73
185 73 my ($name, $value) = ($1, $2);
186 73 ▶ $name = "--$name" unless $name =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^-/u 55 18 73
187 73 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 64 9 73
188 64 ▶ $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/;
Branch: 1 branch
BranchTrueFalseTotal
$value =~ s/^"(.*)"$/$1/u or $value =~ s/^'(.*)'$/$1/u 5 59 64
189 64 push @{$self->{options}}, "$name=$value";
190 } else {
191 9 push @{$self->{options}}, $name;
192 }
193 73 $count++;
194 } else {
195 0 warning(g_('invalid syntax for option in %s, line %d'), $desc, $.);
196 }
197 }
198 5 return $count;
199 }
200  
201 =item $conf->filter(%opts)
202  
203 Filter the list of configuration options.
204  
205 Options:
206  
207 =over
208  
209 =item B<remove>
210  
211 A function returning a boolean,
212 used to decide whether to remove the configuration option,
213 executed as $opts{remove}->($option).
214  
215 =item B<keep>
216  
217 A function returning a boolean,
218 used to decide whether to keep the configuration option,
219 executed as $opts{keep}->($option).
220  
221 =back
222  
223 =cut
224  
225 3 ▶sub filter {
Subroutine filter: called CC=4 SCAR=13.9
Pod documented
226 3 my ($self, %opts) = @_;
227 3 ▶ my $remove = $opts{remove} // sub { 0 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$opts{"remove"} // sub { 0; }
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{"remove"} // sub { 0; }
$opts{"remove"} sub { 0; }
Truth table66%2/3
A // B
A= $opts{"remove"}B= sub { 0; }
ABresult
1X1
011
000
228 3 ▶ my $keep = $opts{keep} // sub { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$opts{"keep"} // sub { 1; }
l!l&&r!l&&!r
210
MC/DC0%0/2
$opts{"keep"} // sub { 1; }
$opts{"keep"} sub { 1; }
Truth table66%2/3
A // B
A= $opts{"keep"}B= sub { 1; }
ABresult
1X1
011
000
229  
230 45 ▶ @{$self->{options}} = grep { not $remove->($_) and $keep->($_) }
Condition100%3/3
!&$remove($_) && &$keep($_)
!ll&&!rl&&r
24156
MC/DC100%2/2
!&$remove($_) && &$keep($_)
!&$remove($_) &$keep($_)
Truth table100%3/3
not(A) && B
A= !&$remove($_)B= &$keep($_)
ABresult
0X0
100
111
231 3 @{$self->{options}};
232 }
233  
234 =item $string = "$conf"
235  
236 Return a string representation of the content.
237  
238 =item $string = $conf->output([$fh])
239  
240 Write the options in the given filehandle (if defined) and return a string
241 representation of the content (that would be) written.
242  
243 =cut
244  
245 4 ▶sub output {
Subroutine output: called CC=3 SCAR=11.1
Pod documented
246 4 my ($self, $fh) = @_;
247 4 my $ret = '';
248 4 foreach my $opt ($self->get_options()) {
249 21 $opt =~ s/^--//;
250 21 $opt =~ s/^([^=]+)=(.*)$/$1 = "$2"/;
251 21 $opt .= "\n";
252 0 ▶ print { $fh } $opt if defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fh 0 21 21
253 21 $ret .= $opt;
254 }
255 4 return $ret;
256 }
257  
258 =item $conf->save($file)
259  
260 Save the options in a file.
261  
262 =back
263  
264 =head1 CHANGES
265  
266 =head2 Version 1.04 (dpkg 1.20.0)
267  
268 Remove croak: For 'format_argv' in $conf->filter().
269  
270 Remove methods: $conf->get(), $conf->set().
271  
272 =head2 Version 1.03 (dpkg 1.18.8)
273  
274 Obsolete option: 'format_argv' in $conf->filter().
275  
276 Obsolete methods: $conf->get(), $conf->set().
277  
278 New methods: $conf->load_system_config(), $conf->load_system_user(),
279 $conf->load_config().
280  
281 =head2 Version 1.02 (dpkg 1.18.5)
282  
283 New option: Accept new option 'format_argv' in $conf->filter().
284  
285 New methods: $conf->get(), $conf->set().
286  
287 =head2 Version 1.01 (dpkg 1.15.8)
288  
289 New method: $conf->filter()
290  
291 =head2 Version 1.00 (dpkg 1.15.6)
292  
293 Mark the module as public.
294  
295 =cut
296  
297 1 1;
« Dpkg/OpenPGP.pm ↑ Summary Dpkg/Version.pm »