blib/lib/RPM/Make/DWIW.pm

filter: Statementstmt 80.8% 135 / 167 Branchbran 50.0% 32 / 64 Conditioncond 77.7% 7 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 82.1% 23 / 28 Podpod 12.0% 3 / 25 totaltotal 67.8% 203 / 299 CC 47 SCAR 16.8
CC
47
Cov
72%
verify_rpm
12.0
get_rpm_path
12.0
xcp_rpm_here
12.0
CRAP 5.4
↑ Summary
1 # $Header: /usr/local/cvsroot/apb/lib/RPM-Make-DWIW/lib/RPM/Make/DWIW.pm,v 1.4 2010-03-08 06:10:49 asher Exp $
2  
3 package RPM::Make::DWIW;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 $VERSION = '0.2';
9  
10 3 my $FINAL_RPM_PATH;
11 my $TOP;
12  
13 ## validation: key => type, mandatory
14  
15 3 my $TOP_VAL = {
16 tags => [ {}, 1 ],
17 description => [ '', 1 ],
18 items => [ [], 1],
19 requirements => [ [], 0],
20 pre => [ '', 0 ],
21 post => [ '', 0 ],
22 preun => [ '', 0 ],
23 postun => [ '', 0 ],
24 cleanup => [ '', 0 ],
25 };
26  
27 3 my $TAGS_VAL = {
28 Summary => [ '', 1 ],
29 Name => [ '', 1 ],
30 Version => [ '', 1 ],
31 Release => [ '', 1 ],
32 License => [ '', 1 ],
33 Group => [ '', 1 ],
34 Source => [ '', 0 ],
35 URL => [ '', 0 ],
36 Distribution => [ '', 0 ],
37 Vendor => [ '', 0 ],
38 Packager => [ '', 0 ],
39 };
40  
41 3 my $ITEM_VAL = {
42 type => [ '', 1 ],
43 dest => [ '', 1 ],
44 src => [ '', 0 ],
45 mode => [ '', 1 ],
46 owner => [ '', 1 ],
47 group => [ '', 1 ],
48 defaults => [ '', 0 ],
49 config_p => [ '', 0 ],
50 };
51  
52 ## example spec
53  
54 3 my $X = {
55 tags => {
56 Summary => 'A CD player app that rocks!',
57 Name => 'cdplayer',
58 Version => '1.2',
59 Release => '3',
60 License => 'GPL',
61 Group => 'Applications/Sound',
62 #Source => 'ftp://ftp.gnomovision.com/pub/cdplayer/cdplayer-1.0.tgz',
63 #URL => 'http://www.gnomovision.com/cdplayer/cdplayer.html',
64 #Distribution => 'WSS Linux',
65 #Vendor => 'White Socks Software, Inc.',
66 #Packager => 'Santa Claus <sclaus@northpole.com>',
67 },
68 description => 'abc def ghi',
69 items => [
70 {
71 defaults => 1,
72 owner => 'root',
73 group => 'admin',
74 mode => '0644',
75 },
76 {
77 src => 'abc.txt',
78 dest => '/usr/bin/abc.txt',
79 mode => '0755',
80 owner => 'cdplayer',
81 group => 'admin',
82 },
83 {
84 src => 'def.txt',
85 dest => '/usr/lib/def.txt',
86 },
87 {
88 src => 'def.txt',
89 dest => '/etc/cdplayer.conf',
90 config_p => 1,
91 },
92 {
93 dest => '/tmp/acme6',
94 type => 'dir',
95 mode => '0777',
96 },
97 ],
98 requirements => [
99 {
100 name => 'libxml2',
101 min_version => '2.6.0',
102 }
103 ],
104 post => '/sbin/ldconfig',
105 cleanup => 0,
106 };
107  
108 ## mkdir or die
109  
110 13 ▶sub xmkdir {
Subroutine xmkdir: called CC=2 SCAR=7.2
Pod undocumented
111 13 my $dir = shift;
112 13 ▶ mkdir($dir) or die "Can't mkdir $dir: $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "Can't mkdir ${dir}: $!" 13 0 13
113 }
114  
115 2 ▶sub mk_dirs {
Subroutine mk_dirs: called CC=1 SCAR=0.0
Pod undocumented
116 2 $TOP = "topdir-$$";
117 2 system("rm -rf $TOP"); # just in case it exists
118 2 xmkdir($TOP);
119 2 xmkdir("$TOP/RPMS"); # where the rpm will end up
120 2 xmkdir("$TOP/BUILD"); # ??
121 2 xmkdir("$TOP/root"); # where rpmbuild will take files from
122 }
123  
124 0 ▶sub rm_dirs {
Subroutine rm_dirs: not called CC=2 SCAR=17.9
Pod undocumented
125 0 ▶ die "top not defined" unless $TOP;
Branch: 1 branch
BranchTrueFalseTotal
unless $TOP 0 0 0
126 0 system("rm -rf $TOP");
127 }
128  
129 ## generate RPM spec file as string
130  
131 2 ▶sub mk_spec {
Subroutine mk_spec: called CC=7 SCAR=19.5
Pod undocumented
132 2 my $x = shift;
133 2 my $t = scalar localtime;
134 2 my $res = "## autogenerated by $0 - $t\n\n";
135 2 my $tags = $x->{ tags };
136 2 foreach my $key(sort keys %$tags) {
137 17 $res .= "$key: $tags->{ $key }\n";
138 }
139  
140 2 $res .= "\n%description\n$x->{ description }\n\n";
141  
142 2 foreach my $dep(@{ $x->{ requirements } }) {
143 3 ▶ my $mv = defined $dep->{ min_ver } ? " >= $dep->{ min_ver }" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $dep->{'min_ver'} ? : 0 3 3
144 3 $res .= "requires: $dep->{ name }$mv\n";
145 }
146  
147 2 $res .= "\n%files\n";
148  
149 2 my $items = get_items($x);
150 2 foreach my $item(@$items) {
151 6 $res .= mk_spec_file_line($item) . "\n";
152 }
153  
154 2 foreach my $section(qw( pre post preun postun )) {
155 8 ▶ $res .= "\n\n%$section\n$x->{ $section }\n\n" if $x->{ $section };
Branch: 1 branch
BranchTrueFalseTotal
if $x->{$section} 6 2 8
156 }
157 2 $res;
158 }
159  
160 ## given file (or dir) hashref, return specfile line
161  
162 6 ▶sub mk_spec_file_line {
Subroutine mk_spec_file_line: called CC=4 SCAR=14.2
Pod undocumented
163 6 my $file = shift;
164 6 foreach my $k(qw( mode owner group dest )) {
165 24 ▶ die "Missing key: $k in item" unless defined $file->{ $k };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file->{$k} 0 24 24
166 }
167 6 my $line = "%attr($file->{ mode } $file->{ owner } $file->{ group }) $file->{ dest }";
168 6 ▶ $line = "%config $line" if $file->{ config_p };
Branch: 1 branch
BranchTrueFalseTotal
if $file->{"config_p"} 0 6 6
169 6 $line;
170 }
171  
172 ## given spec hashref, write specfile
173  
174 2 ▶sub write_spec {
Subroutine write_spec: called CC=1 SCAR=0.0
Pod undocumented
175 2 my $x = shift;
176 2 spew("$TOP/specfile", mk_spec($x));
177 }
178  
179 ## cp src file to dest or die; create dirs as needed
180  
181 4 ▶sub cpx {
Subroutine cpx: called CC=6 SCAR=18.1
Pod undocumented
182 4 my($src, $dest, $mode) = @_;
183 4 ▶ die "Invalid mode '$mode'" unless $mode =~ /^\d{4}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mode =~ /^\d{4}$/ 0 4 4
184 4 ▶ die "Not found: $src" unless -e $src;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $src 0 4 4
185 4 my @parts = split /\//, $dest;
186 4 pop @parts;
187 4 my @p2;
188 4 ▶ while(@parts) {
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 15 4 19
189 15 push @p2, shift @parts;
190 15 my $dir = join('/', @p2);
191 15 ▶ unless(-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $dir) 5 10 15
192 5 xmkdir($dir);
193 }
194 }
195 4 ▶ system('/bin/cp', $src, $dest) && die "Failed to cp '$src' to '$dest'";
Branch: 1 branch
BranchTrueFalseTotal
system "/bin/cp", $src, $dest and die "Failed to cp '${src}' to '${dest}'" 0 4 4
196 #system('/bin/chmod', $mode, $dest) && die "Failed to chmod '$dest'";
197 }
198
199 ## given spec hashref, cp necessary files into tmp tree
200  
201 2 ▶sub cp_files {
Subroutine cp_files: called CC=3 SCAR=11.1
Pod undocumented
202 2 my $x = shift;
203 2 my $files = get_files($x);
204 2 foreach my $file(@$files) {
205 4 ▶ $file->{ dest } =~ m|^/| or die "Dest path must start with /";
Branch: 1 branch
BranchTrueFalseTotal
$file->{"dest"} =~ m[^/] or die "Dest path must start with /" 4 0 4
206 4 cpx($file->{ src }, "$TOP/root$file->{ dest }", $file->{ mode });
207 }
208 }
209  
210 ## mk dirs explicitly requested
211 ## wait, is this any good? rpm copy dirs?
212  
213 2 ▶sub mk_specified_dirs {
Subroutine mk_specified_dirs: called CC=3 SCAR=11.1
Pod undocumented
214 2 my $x = shift;
215 2 my $dirs = get_dirs($x);
216 2 foreach my $dir(@$dirs) {
217 2 ▶ system("mkdir -p -m $dir->{ mode } $TOP/root$dir->{ dest }") && die "Failed to mkdir '$dir->{ dest }'";
Branch: 1 branch
BranchTrueFalseTotal
system qq[mkdir -p -m $dir->{"mode"} $TOP/root$dir->{"dest"}] and die qq[Failed to mkdir '$dir->{"dest"}'] 0 2 2
218 }
219 }
220  
221 #rpmbuild -bb --root `pwd`/root --define "_topdir /space/asher/sand/rpm/cdplayer-example/topdir" specfile2
222  
223 ## create rpm or die
224  
225 2 ▶sub xmk_rpm {
Subroutine xmk_rpm: called CC=2 SCAR=7.0
Pod undocumented
226 2 chomp (my $here = `pwd`);
227 2 my $rc = system(
228 qq[rpmbuild -bb --root $here/$TOP/root --define "_topdir $here/$TOP" $TOP/specfile > $TOP/rpm.out 2>&1]);
229 2 ▶ if($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 2 0 2
230 2 print STDERR "Error: see $TOP/rpm.out\n";
231 2 exit -1;
232 }
233 }
234  
235 ## given x and RPM, check that RPM has the right files or die
236  
237 0 ▶sub verify_rpm {
Subroutine verify_rpm: not called CC=3 SCAR=24.8
Pod undocumented
238 0 my($x, $rpm) = @_;
239 0 my $items = get_items($x);
240 0 my $want_files = join(' ', sort map { $_->{ dest } } @$items );
241 0 my $cmd = "rpm -q -p --filesbypkg $rpm";
242 0 chomp(my @res = `$cmd`);
243 0 ▶ my $have_files = join(' ', sort map { /\S+\s+(\S+)/ } @res)
Branch: 1 branch
BranchTrueFalseTotal
my $have_files = join(" ", sort(map({/\S+\s+(\S+)/;} @res))) or die "No files found with '${cmd}'" 0 0 0
244 or die "No files found with '$cmd'";
245 0 ▶ if($want_files ne $have_files) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_files ne $have_files) 0 0 0
246 0 print STDERR "RPM $rpm does not have expected files:\nWant: $want_files\n\nHave: $have_files\n\n$cmd\n";
247 0 exit -1;
248 }
249 }
250  
251 0 ▶sub get_rpm_path {
Subroutine get_rpm_path: not called CC=3 SCAR=24.8
Pod undocumented
252 0 chomp(my @res = `find $TOP/RPMS -type f`);
253 0 ▶ die "RPM not found" unless @res; ## should never happen
Branch: 1 branch
BranchTrueFalseTotal
unless @res 0 0 0
254 0 ▶ die "more than 1 rpm found" if @res > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @res > 1 0 0 0
255 0 $res[0];
256 }
257  
258 ## copy the new rpm up to this level or die
259  
260 0 ▶sub xcp_rpm_here {
Subroutine xcp_rpm_here: not called CC=3 SCAR=24.8
Pod undocumented
261 0 my $rpm_path = shift;
262 0 ▶ $rpm_path =~ m|([^/]+)$| or die "Invalid rpm_path: '$rpm_path'";
Branch: 1 branch
BranchTrueFalseTotal
$rpm_path =~ m[([^/]+)$] or die "Invalid rpm_path: '${rpm_path}'" 0 0 0
263 0 $FINAL_RPM_PATH = $1;
264 0 ▶ system("cp $rpm_path .") && exit -1;
Branch: 1 branch
BranchTrueFalseTotal
system "cp $rpm_path ." and exit -1 0 0 0
265 }
266  
267 2 ▶sub spew {
Subroutine spew: called CC=2 SCAR=7.0
Pod undocumented
268 2 my($fn, $page) = @_;
269 2 ▶ open F, ">$fn" or die "Can't open $fn: $!";
Branch: 1 branch
BranchTrueFalseTotal
open F, ">$fn" or die "Can't open ${fn}: $!" 2 0 2
270 2 print F $page;
271 2 close F;
272 }
273  
274 ## get files/dirs/all items, excluding defaults blocks
275  
276 2 ▶sub get_files {
Subroutine get_files: called CC=2 SCAR=6.9
Pod undocumented
277 2 my $x = shift;
278 2 ▶ [ grep { $_->{ type } eq 'file' && !$_->{ defaults } } @{ $x->{ items } } ];
Condition100%3/3
$_->{'type'} eq 'file' && !$_->{'defaults'}
!ll&&!rl&&r
324
MC/DC100%2/2
$_->{'type'} eq 'file' && !$_->{'defaults'}
$_->{'type'} eq 'file' !$_->{'defaults'}
Truth table100%3/3
A && not(B)
A= $_->{'type'} eq 'file'B= !$_->{'defaults'}
ABresult
0X0
100
111
279 }
280  
281 2 ▶sub get_dirs {
Subroutine get_dirs: called CC=2 SCAR=7.0
Pod undocumented
282 2 my $x = shift;
283 2 ▶ [ grep { $_->{ type } eq 'dir' && !$_->{ defaults } } @{ $x->{ items } } ];
Condition66%2/3
$_->{'type'} eq 'dir' && !$_->{'defaults'}
!ll&&!rl&&r
702
MC/DC50%1/2
$_->{'type'} eq 'dir' && !$_->{'defaults'}
$_->{'type'} eq 'dir' !$_->{'defaults'}
Truth table66%2/3
A && not(B)
A= $_->{'type'} eq 'dir'B= !$_->{'defaults'}
ABresult
0X0
100
111
284 }
285  
286 4 ▶sub get_items {
Subroutine get_items: called CC=1 SCAR=0.0
Pod undocumented
287 4 my $x = shift;
288 4 [ grep { !$_->{ defaults } } @{ $x->{ items } } ];
289 }
290  
291 ## return error msg or '' if valid
292  
293 10 ▶sub validate_hashref {
Subroutine validate_hashref: called CC=7 SCAR=21.5
Pod undocumented
294 10 my($val, $x) = @_;
295 10 my @err;
296  
297 10 foreach my $key(keys %$x) {
298 67 ▶ if(!$val->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val->{$key}) 0 67 67
299 0 push @err, "Unknown key: $key";
300 }
301 67 my $r0 = ref $val->{ $key }[0];
302 67 my $r1 = ref $x->{ $key };
303 67 ▶ if($r0 ne $r1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r0 ne $r1) 0 67 67
304 0 push @err, "Wrong type: $key (got '$r1', expected '$r0)";
305 }
306 }
307 10 foreach my $key(keys %$val) {
308 88 ▶ if($val->{ $key }[1] && !$x->{ $key }) { ## mand && missing
Branch: 1 branch
BranchTrueFalseTotal
if ($val->{$key}[1] and not $x->{$key}) 0 88 88
Condition66%2/3
$val->{$key}[1] and not $x->{$key}
!ll&&!rl&&r
40480
MC/DC0%0/2
$val->{$key}[1] and not $x->{$key}
$val->{$key}[1] not $x->{$key}
Truth table66%2/3
A and not(B)
A= $val->{$key}[1]B= not $x->{$key}
ABresult
0X0
100
111
309 0 push @err, "Missing key: $key";
310 }
311 }
312 10 join('; ', @err);
313 }
314  
315 ## validate or die with msg
316  
317 10 ▶sub xvalidate_hashref {
Subroutine xvalidate_hashref: called CC=2 SCAR=9.2
Pod undocumented
318 10 my($val, $x, $name) = @_;
319 10 ▶ my $err = validate_hashref($val, $x) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $err = validate_hashref($val, $x) or return 0 10 10
320 0 print STDERR "Error in $name: $err\n";
321 0 exit -1;
322 }
323
324 2 ▶sub validate_spec {
Subroutine validate_spec: called CC=2 SCAR=6.9
Pod undocumented
325 2 my $spec = shift;
326 2 xvalidate_hashref($TOP_VAL, $spec, 'top level');
327 2 xvalidate_hashref($TAGS_VAL, $spec->{ tags }, 'tags');
328 2 my $n = 0;
329 2 my $items = get_items($spec);
330 2 foreach my $item(@$items) {
331 6 xvalidate_hashref($ITEM_VAL, $item, "item $n");
332 6 $n ++;
333 }
334 2 1;
335 }
336  
337 ## add default vals to any items that lack them
338 ## modifies spec
339  
340 2 ▶sub apply_defaults {
Subroutine apply_defaults: called CC=7 SCAR=19.5
Pod undocumented
341 2 my($x) = @_;
342 2 my %d = ( type => 'file' );
343 2 foreach my $item(@{ $x->{ items } }) {
344 9 ▶ if($item->{ defaults }) { # it is a defaults block; modify our defaults
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{'defaults'}) { } 3 6 9
345 3 ▶ while(my($k, $v) = each %$item) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$item) 12 3 15
346 12 ▶ next if $k eq 'defaults';
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "defaults" 3 9 12
347 9 $d{ $k } = $v;
348 }
349 }
350 else { # apply defaults to this item
351 6 ▶ while(my($k, $v) = each %d) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %d) 24 6 30
352 24 ▶ $item->{ $k } = $v unless defined $item->{ $k };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{$k} 19 5 24
353 }
354 }
355 }
356 }
357  
358 2 ▶sub apply_global_defaults {
Subroutine apply_global_defaults: called CC=2 SCAR=7.2
Pod undocumented
359 2 my($x) = @_;
360 2 ▶ $x->{ cleanup } = 1 unless exists $x->{ cleanup };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $x->{"cleanup"} 0 2 2
361 }
362  
363 ## public
364  
365 0 ▶sub get_rpm_filename {
Subroutine get_rpm_filename: not called CC=1 SCAR=6.9
Pod documented
366 0 $FINAL_RPM_PATH;
367 }
368  
369 ## public
370  
371 1 ▶sub get_example_spec {
Subroutine get_example_spec: called CC=1 SCAR=0.0
Pod documented
372 1 $X;
373 }
374  
375 ## public
376 ## pass me a spec hashref
377  
378 2 ▶sub write_rpm {
Subroutine write_rpm: called CC=2 SCAR=8.5
Pod documented
379 2 my($spec) = shift;
380 2 apply_global_defaults($spec);
381 2 apply_defaults($spec);
382 2 validate_spec($spec);
383 2 mk_dirs();
384 2 write_spec($spec);
385 2 cp_files($spec);
386 2 mk_specified_dirs($spec);
387 2 xmk_rpm();
388 0 my $rpm_path = get_rpm_path();
389 0 verify_rpm($spec, $rpm_path);
390 0 xcp_rpm_here($rpm_path);
391 0 ▶ rm_dirs() if $spec->{ cleanup };
Branch: 1 branch
BranchTrueFalseTotal
if $spec->{"cleanup"} 0 0 0
392 0 1;
393 }
394  
395 3 1;
396 __END__
↑ Summary