blib/lib/LCFG/Build/VCS.pm

filter: Statementstmt 37.5% 45 / 120 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 55.0% 11 / 20 Podpod 83.3% 5 / 6 totaltotal 31.9% 61 / 191 SCAR 53.3
CC
22
Cov
28%
mirror_file
90.0
update_changelog
72.0
run_cmd
20.0
CRAP 205.6
« blib/lib/LCFG/Build/VCS/SVN.pm ↑ Summary blib/lib/LCFG/Build/VCS/None.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 # $Id: VCS.pm.in 31796 2017-01-19 13:31:51Z squinney@INF.ED.AC.UK $
6 # $Source: /var/cvs/dice/LCFG-Build-VCS/lib/LCFG/Build/VCS.pm.in,v $
7 # $Revision: 31796 $
8 # $HeadURL: https://svn.lcfg.org/svn/source/tags/LCFG-Build-VCS/LCFG_Build_VCS_0_2_3/lib/LCFG/Build/VCS.pm.in $
9 # $Date: 2017-01-19 13:31:51 +0000 (Thu, 19 Jan 2017) $
10  
11 1 our $VERSION = '0.2.3';
12  
13 1 ▶use DateTime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use IPC::Run qw(run);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 subtype 'AbsPath'
25 => as 'Str'
26 0 ▶ => where { File::Spec->file_name_is_absolute($_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 ▶ => message { 'Directory must be an absolute path.' };
Subroutine __ANON__: not called CC=1 SCAR=1.2
28  
29 # coerce the input string (which is possibly a relative path) into an
30 # absolute path which does not have a trailing /
31  
32 coerce 'AbsPath'
33 => from 'Str'
34 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
35  
36 1 requires qw/checkcommitted genchangelog tagversion export export_devel import_project checkout_project/;
37  
38 1 has 'id' => (
39 is => 'ro',
40 isa => 'Str',
41 required => 1,
42 );
43  
44 1 has 'module' => (
45 is => 'rw',
46 isa => 'Str',
47 required => 1,
48 );
49  
50 1 has 'workdir' => (
51 is => 'rw',
52 isa => 'AbsPath',
53 required => 1,
54 coerce => 1,
55 default => q{.},
56 );
57  
58 1 has 'binpath' => (
59 is => 'rw',
60 isa => 'Str',
61 required => 0,
62 );
63  
64 1 has 'quiet' => (
65 is => 'rw',
66 isa => 'Bool',
67 default => 0,
68 required => 0,
69 );
70  
71 1 has 'dryrun' => (
72 is => 'rw',
73 isa => 'Bool',
74 default => 0,
75 required => 0,
76 );
77  
78 1 has 'logname' => (
79 is => 'rw',
80 isa => 'Str',
81 default => 'ChangeLog',
82 required => 1,
83 );
84  
85 sub build_cmd {
86 0 ▶ my ( $self, @args ) = @_;
Subroutine build_cmd: not called CC=1 SCAR=6.9
Pod undocumented
87  
88 0 my @cmd = ( $self->binpath, @args );
89  
90 0 return @cmd;
91 }
92  
93 sub run_cmd {
94 0 ▶ my ( $self, @args ) = @_;
Subroutine run_cmd: not called CC=4 SCAR=30.0
Pod documented
95  
96 0 my @cmd = $self->build_cmd(@args);
97  
98 0 my @out;
99 0 ▶ if ( $self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dryrun) { } 0 0 0
100 0 my $cmd = join q( ), @cmd;
101 0 print "Dry-run: $cmd\n";
102 }
103 else {
104 0 my ( $in, $out, $err );
105  
106 0 my $success = run \@cmd, \$in, \$out, \$err;
107 0 ▶ if ( !$success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
108 0 die "Error whilst running @cmd: $err\n";
109 }
110 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
111 0 warn "$err\n";
112 }
113  
114 0 @out = split /[\r\n]+/, $out;
115 }
116  
117 0 return @out;
118 }
119  
120 sub logfile {
121 0 ▶ my ($self) = @_;
Subroutine logfile: not called CC=1 SCAR=6.9
Pod documented
122  
123 0 return File::Spec->catfile( $self->workdir, $self->logname );
124 }
125  
126 sub gen_tag {
127 0 ▶ my ( $self, $version ) = @_;
Subroutine gen_tag: not called CC=3 SCAR=24.8
Pod documented
128  
129 # Build a tag from the name and version (if specified) and then
130 # replace any period or hyphen characters.
131 #
132 # name: lcfg-foo, version: 1.0.1, gives: lcfg_foo_1_0_1
133  
134 0 my $tag;
135 0 ▶ if ( !defined $version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $version) { } 0 0 0
136 0 $tag = $self->module;
137 }
138 else {
139  
140 0 ▶ if ( $version eq 'latest' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version eq 'latest') { } 0 0 0
141 0 $tag = 'latest';
142 }
143 else {
144 0 $tag = join q{_}, $self->module, $version;
145 }
146  
147 }
148  
149 0 $tag =~ s/\./_/g;
150 0 $tag =~ s/\-/_/g;
151  
152 0 return $tag;
153 }
154  
155 sub update_changelog {
156 0 ▶ my ( $self, $version ) = @_;
Subroutine update_changelog: not called CC=8 SCAR=42.8
Pod documented
157  
158 0 my $dir = $self->workdir;
159 0 my $logfile = $self->logfile;
160  
161 # If this is a dry-run we will need to clean up the temporary file
162 # at the end. Otherwise it gets renamed to the logfile.
163  
164 0 my $unlink = 0;
165 0 ▶ if ( $self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dryrun) 0 0 0
166 0 $unlink = 1;
167 }
168  
169 0 my $tmplog = File::Temp->new(
170 UNLINK => $unlink,
171 DIR => $dir,
172 SUFFIX => '.tmp',
173 );
174  
175 0 my $tmpname = $tmplog->filename;
176  
177 0 my $date = DateTime->now->ymd;
178  
179 0 my $id = $self->id;
180  
181 0 print {$tmplog} <<"EOT";
182 $date $id: new release
183  
184 \t* Release: $version
185  
186 EOT
187  
188 0 ▶ if ( -f $logfile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $logfile) 0 0 0
189 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
190 or die "Could not open $logfile: $!\n";
191  
192 0 ▶ while ( defined( my $line = <$log> ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $log)) 0 0 0
193 0 print {$tmplog} $line;
194 }
195  
196 0 $log->close;
197 }
198  
199 $tmplog->close
200 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
201  
202 0 ▶ if ( !$self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dryrun) 0 0 0
203 0 ▶ rename $tmpname, $logfile
Branch: 1 branch
BranchTrueFalseTotal
rename $tmpname, $logfile or die "Could not rename $tmpname as ${logfile}: $!\n" 0 0 0
204 or die "Could not rename $tmpname as $logfile: $!\n";
205 }
206  
207 0 return;
208 }
209  
210 sub mirror_file {
211 0 ▶ my ( $self, $workdir, $exportdir, $dirname, $fname ) = @_;
Subroutine mirror_file: not called CC=9 SCAR=45.0
Pod documented
212  
213 0 my $from_dir = File::Spec->catdir( $workdir, $dirname );
214 0 my $to_dir = File::Spec->catdir( $exportdir, $dirname );
215  
216 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
217 0 eval { File::Path::mkpath($to_dir) };
218 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
219 0 die "Could not create $to_dir: $@\n";
220 }
221  
222 0 my ($dev, $ino, $mode, $nlink, $uid,
223 $gid, $rdev, $size, $atime, $mtime,
224 $ctime, $blksize, $blocks
225 ) = stat $from_dir;
226  
227 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
228  
229 # We don't care about atime/mtime for directories
230 }
231  
232 0 my $from = File::Spec->catfile( $workdir, $dirname, $fname );
233 0 my $to = File::Spec->catfile( $exportdir, $dirname, $fname );
234  
235 0 my ($dev, $ino, $mode, $nlink, $uid,
236 $gid, $rdev, $size, $atime, $mtime,
237 $ctime, $blksize, $blocks
238 ) = stat $from;
239  
240 0 ▶ if ( $self->dryrun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dryrun) { } 0 0 0
241 0 print "Dry-run: $from -> $to\n";
242 }
243 else {
244 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
245 or die "Copy $from to $to failed: $!\n";
246  
247 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
248 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
249  
250 }
251  
252 0 return;
253 }
254  
255 1 1;
256 __END__
257  
258 =head1 NAME
259  
260 LCFG::Build::VCS - LCFG version-control infrastructure
261  
262 =head1 VERSION
263  
264 This documentation refers to LCFG::Build::VCS version 0.2.3
265  
266 =head1 SYNOPSIS
267  
268 my $vcs = LCFG::Build::VCS::CVS->new();
269  
270 $vcs->genchangelog();
271  
272 if ( $vcs->checkcommitted() ) {
273 $vcs->tagversion();
274 }
275  
276 =head1 DESCRIPTION
277  
278 This is a suite of tools designed to provide a standardised interface
279 to version-control systems so that the LCFG build tools can deal with
280 project version-control in a high-level abstract fashion. Typically
281 they provide support for procedures such as importing and exporting
282 projects, doing tagged releases, generating the project changelog from
283 the version-control log and checking all changes are committed.
284  
285 This is an interface, you should not attempt to create objects
286 directly using this module. You will need to implement a sub-class,
287 for example L<LCFG::Build::VCS::CVS>. This interface requires certain
288 attributes and methods be specified within any implementing sub-class,
289 see below for details. For complete details you should read the
290 documentation associated with the specific sub-class.
291  
292 More information on the LCFG build tools is available from the website
293 http://www.lcfg.org/doc/buildtools/
294  
295 =head1 ATTRIBUTES
296  
297 =over 4
298  
299 =item module
300  
301 The name of the software package in this repository. This is required
302 and there is no default value.
303  
304 =item workdir
305  
306 The directory in which the version-control system commands should be
307 carried out. This is required and if none is specified then it will
308 default to '.', the current working directory. This must be an
309 absolute path but if you pass in a relative path coercion will
310 automatically occur based on the current working directory.
311  
312 =item binpath
313  
314 The path to the version-control tool. This is required and it is
315 expected that any module which implements this interface will set a
316 suitable default command name.
317  
318 =item quiet
319  
320 This is a boolean value which controls the quietness of the
321 version-control system commands. By default it is false and commands,
322 such as CVS, will print lots of extra stuff to the screen.
323  
324 =item dryrun
325  
326 This is a boolean value which controls whether the commands will
327 actually have a real effect or just print out what would be done. By
328 default it is false.
329  
330 =item logname
331  
332 The name of the logfile to which information should be directed when
333 doing version updates. This is also the name of the logfile to be used
334 if you utilise the automatic changelog generation option. The default
335 file name is 'ChangeLog'.
336  
337 =back
338  
339 =head1 SUBROUTINES/METHODS
340  
341 This module provides a few fully-implemented methods which are likely
342 to be useful for all sub-classes which implement the interface.
343  
344 =over 4
345  
346 =item gen_tag($version)
347  
348 Generate a tag based on the package name and the specified
349 version. Tags are generated from the module name attribute and the
350 version information passed in by replacing any hyphens or dots with
351 underscores and joining the two fields with an underscore. For
352 example, lcfg-foo and 1.0.1 would become lcfg_foo_1_0_1. If no version
353 is specified then just the module name will be used.
354  
355 =item update_changelog($version)
356  
357 This will add a standard-format release tag entry to the top of the
358 change log file.
359  
360 =item mirror_file( $sourcedir, $targetdir, $reldir, $basename )
361  
362 This will copy a file from the source directory to the target
363 directory. The relative path of the file (within the source directory)
364 must be split into the relative directory path and filename. Effort is
365 made to preserve the mode and, in the case of files, atime and
366 mtime. This is used by various modules in the export_devel() method to
367 mirror the project directory into a build directory.
368  
369 =item logfile()
370  
371 This is a convenience method which returns the full path to the
372 logfile based on the workdir and logname attributes.
373  
374 =back
375  
376 As well as the methods above, any class which implements this
377 interface MUST provide methods for:
378  
379 =over 4
380  
381 =item checkcommitted()
382  
383 Test to see if there are any uncommitted files in the project
384 directory. Note this test does not spot files which have not been
385 added to the version-control system. In scalar context the subroutine
386 returns 1 if all files are committed and 0 (zero) otherwise. In list
387 context the subroutine will return this code along with a list of any
388 files which require committing.
389  
390 =item genchangelog()
391  
392 This method will generate a changelog (the name of which is controlled
393 by the logname attribute) from the log kept within the version-control
394 system.
395  
396 =item tagversion($version)
397  
398 This method is used to tag a set of files for a project at a
399 particular version. It will also update the changelog
400 appropriately. The tag name is generated using the I<gen_tag()>
401 method, see below for full details.
402  
403 =item run_cmd(@args)
404  
405 A method used to handle the running of commands for the particular
406 version-control system. This is required for systems like CVS where
407 shell commands have to be executed. Not all modules will need to
408 implement this method as they may well use a proper Perl module API
409 (e.g. subversion).
410  
411 =item export( $version, $dir )
412  
413 Exports the source code for the project tagged at the specified
414 release. The second argument specifies the directory into which the
415 exported project directory will be placed.
416  
417 =item export_devel( $version, $dir )
418  
419 Exports the current development version of the source code for the
420 project (i.e. your working copy). The second argument specifies the
421 directory into which the exported project directory will be placed.
422  
423 =item import_project( $dir, $version, $message )
424  
425 Imports a project source tree into the version-control system.
426  
427 =item checkout_project( $version, $dir )
428  
429 Does a check-out from the version-control system of the project tagged
430 at the specified version. Unlike the export() method this checked-out
431 copy will include the files necessary for the version-control system
432 (e.g. CVS or .svn directories).
433  
434 =head1 DEPENDENCIES
435  
436 This module is L<Moose> powered. It also requires L<DateTime> and L<IPC::Run>.
437  
438 =head1 SEE ALSO
439  
440 L<LCFG::Build::PkgSpec>, L<LCFG::Build::VCS::CVS>, L<LCFG::Build::VCS::None>, L<LCFG::Build::Tools>
441  
442 =head1 PLATFORMS
443  
444 This is the list of platforms on which we have tested this
445 software. We expect this software to work on any Unix-like platform
446 which is supported by Perl.
447  
448 FedoraCore5, FedoraCore6, ScientificLinux5
449  
450 =head1 BUGS AND LIMITATIONS
451  
452 There are no known bugs in this application. Please report any
453 problems to bugs@lcfg.org, feedback and patches are also always very
454 welcome.
455  
456 =head1 AUTHOR
457  
458 Stephen Quinney <squinney@inf.ed.ac.uk>
459  
460 =head1 LICENSE AND COPYRIGHT
461  
462 Copyright (C) 2008 University of Edinburgh. All rights reserved.
463  
464 This library is free software; you can redistribute it and/or modify
465 it under the terms of the GPL, version 2 or later.
466  
467 =cut
« blib/lib/LCFG/Build/VCS/SVN.pm ↑ Summary blib/lib/LCFG/Build/VCS/None.pm »