VCS.pm

filter: Statementstmt 26.6% 50 / 188 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 52.0% 13 / 25 Podpod 66.6% 6 / 9 totaltotal 20.2% 69 / 340 CC 49 SCAR 47.4
CC
49
Cov
17%
update_debian_changelog
342.0
update_lcfg_changelog
90.0
mirror_file
90.0
CRAP 115.0
↑ Summary VCS/CVS.pm »
1 package LCFG::Build::VCS; # -*-perl-*-
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # $Id: VCS.pm.in 35396 2019-01-17 12:01:51Z squinney@INF.ED.AC.UK $
8 # $Source: /var/cvs/dice/LCFG-Build-VCS/lib/LCFG/Build/VCS.pm.in,v $
9 # $Revision: 35396 $
10 # $HeadURL: https://svn.lcfg.org/svn/source/tags/LCFG-Build-VCS/LCFG_Build_VCS_0_3_9/lib/LCFG/Build/VCS.pm.in $
11 # $Date: 2019-01-17 12:01:51 +0000 (Thu, 17 Jan 2019) $
12  
13 1 our $VERSION = '0.3.9';
14  
15 1 ▶use DateTime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use IPC::Run qw(run);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Template v2.14 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 subtype 'AbsPath'
28 => as 'Str'
29 0 ▶ => where { File::Spec->file_name_is_absolute($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 ▶ => message { 'Directory must be an absolute path.' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
31  
32 # coerce the input string (which is possibly a relative path) into an
33 # absolute path which does not have a trailing /
34  
35 coerce 'AbsPath'
36 => from 'Str'
37 0 ▶ => via { my $path = File::Spec->file_name_is_absolute($_) ? $_ : File::Spec->rel2abs($_); $path =~ s{/$}{}; $path };
Subroutine __ANON__: not called CC=2 SCAR=14.6
Branch: 1 branch
BranchTrueFalseTotal
'File::Spec'->file_name_is_absolute($_) ? : 0 0 0
38  
39 1 requires qw/checkcommitted genchangelog tagversion export export_devel import_project checkout_project/;
40  
41 1 has 'id' => (
42 is => 'ro',
43 isa => 'Str',
44 required => 1,
45 );
46  
47 1 has 'module' => (
48 is => 'rw',
49 isa => 'Str',
50 required => 1,
51 );
52  
53 1 has 'workdir' => (
54 is => 'rw',
55 isa => 'AbsPath',
56 required => 1,
57 coerce => 1,
58 default => q{.},
59 );
60  
61 1 has 'binpath' => (
62 is => 'rw',
63 isa => 'Str',
64 required => 0,
65 );
66  
67 1 has 'quiet' => (
68 is => 'rw',
69 isa => 'Bool',
70 default => 0,
71 required => 0,
72 );
73  
74 1 has 'dryrun' => (
75 is => 'rw',
76 isa => 'Bool',
77 default => 0,
78 required => 0,
79 );
80  
81 1 has 'logname' => (
82 is => 'rw',
83 isa => 'Str',
84 default => 'ChangeLog',
85 required => 1,
86 );
87  
88 0 ▶sub build_cmd {
Subroutine build_cmd: not called CC=1 SCAR=6.9
Pod undocumented
89 0 my ( $self, @args ) = @_;
90  
91 0 my @cmd = ( $self->binpath, @args );
92  
93 0 return @cmd;
94 }
95  
96 0 ▶sub run_cmd {
Subroutine run_cmd: not called CC=4 SCAR=30.0
Pod documented
97 0 my ( $self, @args ) = @_;
98  
99 0 my @cmd = $self->build_cmd(@args);
100  
101 0 my @out;
102 0 ▶ if ( $self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dryrun) { } 0 0 0
103 0 my $cmd = join q( ), @cmd;
104 0 print "Dry-run: $cmd\n";
105 }
106 else {
107 0 my ( $in, $out, $err );
108  
109 0 my $success = run \@cmd, \$in, \$out, \$err;
110 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
111 0 die "Error whilst running @cmd: $err\n";
112 }
113 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
114 0 warn "$err\n";
115 }
116  
117 0 @out = split /[\r\n]+/, $out;
118 }
119  
120 0 return @out;
121 }
122  
123 0 ▶sub logfile {
Subroutine logfile: not called CC=1 SCAR=6.9
Pod documented
124 0 my ($self) = @_;
125  
126 0 return File::Spec->catfile( $self->workdir, $self->logname );
127 }
128  
129 0 ▶sub gen_tag {
Subroutine gen_tag: not called CC=3 SCAR=24.8
Pod documented
130 0 my ( $self, $version ) = @_;
131  
132 # Build a tag from the name and version (if specified) and then
133 # replace any period or hyphen characters.
134 #
135 # name: lcfg-foo, version: 1.0.1, gives: lcfg_foo_1_0_1
136  
137 0 my $tag;
138 0 ▶ if ( !defined $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version) { } 0 0 0
139 0 $tag = $self->module;
140 }
141 else {
142  
143 0 ▶ if ( $version eq 'latest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 'latest') { } 0 0 0
144 0 $tag = 'latest';
145 }
146 else {
147 0 $tag = join q{_}, $self->module, $version;
148 }
149  
150 }
151  
152 0 $tag =~ s/\./_/g;
153 0 $tag =~ s/\-/_/g;
154  
155 0 return $tag;
156 }
157  
158 0 ▶sub update_changelog {
Subroutine update_changelog: not called CC=7 SCAR=40.3
Pod documented
159 0 my ( $self, $version, $options ) = @_;
160 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
161  
162 0 $options->{dryrun} = $self->dryrun;
163 0 $options->{id} = $self->id;
164 0 $options->{version} = $version;
165 0 ▶ $options->{style} ||= 'default';
Condition0%0/2
$options->{"style"} ||= "default"
l!l
00
MC/DC0%0/1
$options->{"style"} ||= "default"
$options->{"style"}
Truth table0%0/2
A
A= $options->{"style"}
Aresult
11
00
166  
167 0 my $dir = $self->workdir;
168  
169 0 my ( $logfile, $needs_add );
170 0 ▶ if ( $options->{style} eq 'debian' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'style'} eq 'debian') { } 0 0 0
171 0 $logfile = File::Spec->catfile( $dir, 'debian', 'changelog' );
172 0 $needs_add = !-e $logfile;
173  
174 0 ▶ if ( !$options->{pkgname} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"pkgname"}) 0 0 0
175  
176 # Cook up something sensible which looks like a Debian package
177 # name
178  
179 0 $options->{pkgname} = lc $self->module;
180  
181 # underscores are not permitted, helpfully replace with dashes
182 0 $options->{pkgname} =~ s/_/-/g;
183  
184 # For safety remove any other invalid characters
185 0 $options->{pkgname} =~ s/[^a-z0-9-]//;
186 }
187  
188 0 update_debian_changelog( $logfile, $options );
189 } else {
190 0 $logfile = $self->logfile;
191 0 $needs_add = !-e $logfile;
192  
193 0 update_lcfg_changelog( $logfile, $options );
194 }
195  
196 0 ▶ if ( !$self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dryrun) 0 0 0
197 0 ▶ if ($needs_add) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_add) 0 0 0
198 0 $self->run_cmd( 'add', $logfile );
199 }
200 }
201  
202 0 return;
203 }
204  
205 0 ▶sub update_lcfg_changelog {
Subroutine update_lcfg_changelog: not called CC=9 SCAR=45.0
Pod undocumented
206 0 my ( $logfile, $options ) = @_;
207 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
208  
209 0 ▶ $options->{date} ||= DateTime->now->ymd;
Condition0%0/3
$options->{"date"} ||= "DateTime"->now->ymd
l!l&&r!l&&!r
000
MC/DC0%0/2
$options->{"date"} ||= "DateTime"->now->ymd
$options->{"date"} "DateTime"->now->ymd
Truth table0%0/3
A ||= B
A= $options->{"date"}B= "DateTime"->now->ymd
ABresult
1X1
011
000
210  
211 0 my $dir = (File::Spec->splitpath($logfile))[1];
212  
213 0 my $tmplog = File::Temp->new(
214 TEMPLATE => 'lcfgXXXXXX',
215 UNLINK => 1,
216 DIR => $dir,
217 );
218  
219 0 my $tmpname = $tmplog->filename;
220  
221 0 $tmplog->print(<<"EOT");
222 $options->{date} $options->{id}: new release
223  
224 \t* Release: $options->{version}
225  
226 EOT
227  
228 0 ▶ if ( -f $logfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $logfile) 0 0 0
229 0 ▶ my $log = IO::File->new( $logfile, 'r' )
Branch: 1 branch
BranchTrueFalseTotal
my $log = "IO::File"->new($logfile, "r") or die "Could not open ${logfile}: $!\n" 0 0 0
230 or die "Could not open $logfile: $!\n";
231  
232 0 ▶ while ( defined( my $line = <$log> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $log)) 0 0 0
233 0 $tmplog->print($line);
234 }
235  
236 0 $log->close;
237 }
238  
239 $tmplog->close
240 0 ▶ or die "Could not close temporary file, $tmpname: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$tmplog->close or die "Could not close temporary file, ${tmpname}: $!\n" 0 0 0
241  
242 0 ▶ if ( !$options->{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"dryrun"}) 0 0 0
243 0 ▶ rename $tmpname, $logfile
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $logfile or die "Could not rename $tmpname as ${logfile}: $!\n" 0 0 0
244 or die "Could not rename $tmpname as $logfile: $!\n";
245 }
246  
247 0 return;
248 }
249  
250 # These update_*_changelog subroutines are also used externally from
251 # places which do not have access to the VCS object so they are not
252 # class methods.
253  
254 0 ▶sub update_debian_changelog {
Subroutine update_debian_changelog: not called CC=18 SCAR=58.3
Pod undocumented
255 0 my ( $logfile, $options ) = @_;
256 0 ▶ $options //= {};
Condition0%0/2
$options //= {}
l!l
00
MC/DC0%0/1
$options //= {}
$options
Truth table0%0/2
A
A= $options
Aresult
11
00
257  
258 0 ▶ $options->{urgency} ||= 'low';
Condition0%0/2
$options->{"urgency"} ||= "low"
l!l
00
MC/DC0%0/1
$options->{"urgency"} ||= "low"
$options->{"urgency"}
Truth table0%0/2
A
A= $options->{"urgency"}
Aresult
11
00
259 0 ▶ $options->{distribution} ||= 'unstable';
Condition0%0/2
$options->{"distribution"} ||= "unstable"
l!l
00
MC/DC0%0/1
$options->{"distribution"} ||= "unstable"
$options->{"distribution"}
Truth table0%0/2
A
A= $options->{"distribution"}
Aresult
11
00
260 0 ▶ $options->{release} //= 1;
Condition0%0/2
$options->{"release"} //= 1
l!l
00
MC/DC0%0/1
$options->{"release"} //= 1
$options->{"release"}
Truth table0%0/2
A
A= $options->{"release"}
Aresult
11
00
261 0 ▶ $options->{message} ||= 'New upstream release';
Condition0%0/2
$options->{"message"} ||= "New upstream release"
l!l
00
MC/DC0%0/1
$options->{"message"} ||= "New upstream release"
$options->{"message"}
Truth table0%0/2
A
A= $options->{"message"}
Aresult
11
00
262  
263 # RFC822 date
264 0 $options->{date} = DateTime->now->strftime('%a, %d %b %Y %H:%M:%S %z');
265  
266 0 ▶ if ( !$options->{email} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"email"}) 0 0 0
267 0 my $user_name = (getpwuid($<))[0];
268  
269 0 ▶ my $email_addr = $ENV{DEBEMAIL} || $ENV{EMAIL};
Condition0%0/3
$ENV{'DEBEMAIL'} || $ENV{'EMAIL'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'DEBEMAIL'} || $ENV{'EMAIL'}
$ENV{'DEBEMAIL'} $ENV{'EMAIL'}
Truth table0%0/3
A || B
A= $ENV{'DEBEMAIL'}B= $ENV{'EMAIL'}
ABresult
1X1
011
000
270  
271 0 ▶ if ( !$email_addr ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_addr) 0 0 0
272 0 require Net::Domain;
273  
274 0 my $domain = Net::Domain::hostdomain();
275
276 0 $email_addr = join '@', $user_name, $domain;
277 }
278  
279 # trim any leading or trailing whitespace
280 0 $email_addr =~ s/^\s+//; $email_addr =~ s/\s+$//;
281  
282 0 ▶ if ( $email_addr !~ m/<.+>/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($email_addr =~ /<.+>/) 0 0 0
283 0 ▶ my $email_name = $ENV{DEBFULLNAME} || $ENV{NAME} || $user_name;
Condition0%0/6
$ENV{'DEBFULLNAME'} || $ENV{'NAME'} || $user_name
l!l&&r!l&&!r
000
$ENV{"DEBFULLNAME"} or $ENV{"NAME"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$ENV{'DEBFULLNAME'} || $ENV{'NAME'} || $user_name
$ENV{"DEBFULLNAME"} $ENV{"NAME"} $user_name
Truth table0%0/4
A or B || C
A= $ENV{"DEBFULLNAME"}B= $ENV{"NAME"}C= $user_name
ABCresult
1XX1
01X1
0011
0000
284 0 $email_name =~ s/^\s+//; $email_name =~ s/\s+$//;
285  
286 0 $email_addr = "$email_name <$email_addr>";
287 }
288  
289 0 $options->{email} = $email_addr;
290 }
291  
292 0 my ( $dir, $basename ) = (File::Spec->splitpath($logfile))[1,2];
293  
294 0 my $tmplog = File::Temp->new(
295 TEMPLATE => 'lcfgXXXXXX',
296 UNLINK => 1,
297 DIR => $dir,
298 );
299 0 my $tmpname = $tmplog->filename;
300  
301 0 ▶ my $tt = Template->new(
Branch: 1 branch
BranchTrueFalseTotal
my $tt = "Template"->new({"INCLUDE_PATH", $dir}) or die $Template::ERROR . "\n" 0 0 0
302 {
303 INCLUDE_PATH => $dir,
304 }
305 ) or die $Template::ERROR . "\n";
306  
307 0 my $template = q{
308 [%- FOREACH entry IN entries -%]
309 [% entry.item('pkgname') %] ([% entry.item('version') %]-[% entry.item('release') %]) [% entry.item('distribution') %]; urgency=[% entry.item('urgency') %]
310  
311 * [% entry.item('message') %]
312  
313 -- [% entry.item('email') %] [% entry.item('date') %]
314  
315 [% END -%]
316 [% IF current_logfile %][% INSERT $current_logfile %][% END -%]
317 };
318  
319 0 my %args = (
320 entries => [$options],
321 );
322  
323 0 ▶ if ( -e $logfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $logfile) 0 0 0
324 0 $args{current_logfile} = $basename;
325 }
326  
327 0 ▶ $tt->process( \$template, \%args, $tmplog )
Branch: 1 branch
BranchTrueFalseTotal
$tt->process(\$template, \%args, $tmplog) or die $tt->error . "\n" 0 0 0
328 or die $tt->error() . "\n";
329  
330 0 ▶ $tmplog->close
Branch: 1 branch
BranchTrueFalseTotal
$tmplog->close or die "Could not close temporary file, ${tmpname}: $!\n" 0 0 0
331 or die "Could not close temporary file, $tmpname: $!\n";
332  
333 0 ▶ if ( !$options->{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"dryrun"}) 0 0 0
334 0 ▶ rename $tmpname, $logfile
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $logfile or die "Could not rename $tmpname as ${logfile}: $!\n" 0 0 0
335 or die "Could not rename $tmpname as $logfile: $!\n";
336 }
337  
338 0 return;
339  
340 }
341  
342 0 ▶sub mirror_file {
Subroutine mirror_file: not called CC=9 SCAR=45.0
Pod documented
343 0 my ( $self, $workdir, $exportdir, $dirname, $fname ) = @_;
344  
345 0 my $from_dir = File::Spec->catdir( $workdir, $dirname );
346 0 my $to_dir = File::Spec->catdir( $exportdir, $dirname );
347  
348 0 ▶ if ( !$self->dryrun && !-d $to_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dryrun or -d $to_dir) 0 0 0
Condition0%0/3
$self->dryrun or -d $to_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->dryrun or -d $to_dir
$self->dryrun -d $to_dir
Truth table0%0/3
A or B
A= $self->dryrunB= -d $to_dir
ABresult
1X1
011
000
349 0 eval { File::Path::mkpath($to_dir) };
350 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
351 0 die "Could not create $to_dir: $@\n";
352 }
353  
354 0 my ($dev, $ino, $mode, $nlink, $uid,
355 $gid, $rdev, $size, $atime, $mtime,
356 $ctime, $blksize, $blocks
357 ) = stat $from_dir;
358  
359 0 ▶ chmod $mode, $to_dir or die "chmod on $to_dir failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, $to_dir or die "chmod on $to_dir failed: $!\n" 0 0 0
360  
361 # We don't care about atime/mtime for directories
362 }
363  
364 0 my $from = File::Spec->catfile( $workdir, $dirname, $fname );
365 0 my $to = File::Spec->catfile( $exportdir, $dirname, $fname );
366  
367 0 my ($dev, $ino, $mode, $nlink, $uid,
368 $gid, $rdev, $size, $atime, $mtime,
369 $ctime, $blksize, $blocks
370 ) = stat $from;
371  
372 0 ▶ if ( $self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dryrun) { } 0 0 0
373 0 print "Dry-run: $from -> $to\n";
374 }
375 else {
376 0 ▶ File::Copy::syscopy( $from, $to )
Branch: 1 branch
BranchTrueFalseTotal
File::Copy::syscopy($from, $to) or die "Copy $from to $to failed: $!\n" 0 0 0
377 or die "Copy $from to $to failed: $!\n";
378  
379 0 ▶ chmod $mode, $to or die "chmod on $to to ($mode) failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod $mode, $to or die "chmod on $to to ($mode) failed: $!\n" 0 0 0
380 0 ▶ utime $atime, $mtime, $to or die "utime on $to to ($atime, $mtime) failed: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
utime $atime, $mtime, $to or die "utime on $to to ($atime, $mtime) failed: $!\n" 0 0 0
381  
382 }
383  
384 0 return;
385 }
386  
387 0 ▶sub store_version {
Subroutine store_version: not called CC=4 SCAR=30.0
Pod documented
388 0 my ( $self, $version ) = @_;
389  
390 0 warn "Updating build ID file\n";
391  
392 0 my $dir = $self->workdir;
393 0 my $version_file = 'lcfg-build-id.txt';
394  
395 0 my $tmpfh = File::Temp->new(
396 UNLINK => 1,
397 DIR => $dir,
398 SUFFIX => '.tmp',
399 );
400  
401 0 my $tmpname = $tmpfh->filename;
402  
403 0 $tmpfh->say($version);
404  
405 0 ▶ $tmpfh->close
Branch: 1 branch
BranchTrueFalseTotal
$tmpfh->close or die "Could not close temporary file, ${tmpname}: $!\n" 0 0 0
406 or die "Could not close temporary file, $tmpname: $!\n";
407  
408 0 ▶ if ( !$self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dryrun) 0 0 0
409 0 ▶ rename $tmpname, $version_file
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $version_file or die "Could not rename $tmpname as ${version_file}: $!\n" 0 0 0
410 or die "Could not rename $tmpname as $version_file: $!\n";
411 }
412  
413 0 return;
414 }
415  
416 1 1;
417 __END__
↑ Summary VCS/CVS.pm »