blib/lib/Tapper/Base.pm

filter: Statementstmt 84.0% 58 / 69 Branchbran 43.3% 13 / 30 Conditioncond 25.0% 2 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 90.0% 9 / 10 Podpod 100% 4 / 4 totaltotal 68.2% 86 / 126 SCAR 34.2
CC
19
Cov
68%
makedir
30.0
log_and_exec
8.6
run_one
7.4
CRAP 30.6
↑ Summary
1 package Tapper::Base;
2 # git description: v4.1.1-1-g1855edc
3  
4 BEGIN {
5 3 ▶ $Tapper::Base::AUTHORITY = 'cpan:AMD';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 }
7 {
8 3 $Tapper::Base::VERSION = '4.1.4';
9 }
10 # ABSTRACT: Tapper - Common functions for all Tapper classes
11  
12 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use LockFile::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 with 'MooseX::Log::Log4perl';
21  
22  
23 sub kill_instance
24 {
25 4 ▶ my ($self, $pid_file) = @_;
Subroutine kill_instance: called CC=4 SCAR=14.0
Pod documented
26  
27 # try to kill previous incarnations
28 4 ▶ if ((-e $pid_file) and open(my $fh, "<", $pid_file)) {{
Branch: 1 branch
BranchTrueFalseTotal
if (-e $pid_file and open my $fh, "<", $pid_file) 4 0 4
Condition33%1/3
-e $pid_file and open my $fh, "<", $pid_file
!ll&&!rl&&r
004
MC/DC0%0/2
-e $pid_file and open my $fh, "<", $pid_file
-e $pid_file open my $fh, "<", $pid_file
Truth table33%1/3
A and B
A= -e $pid_fileB= open my $fh, "<", $pid_file
ABresult
0X0
100
111
29 4 my $pid = do {local $\; <$fh>}; # slurp
30 4 ($pid) = $pid =~ m/(\d+)/;
31 4 ▶ last unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 2 2 4
32 2 kill 15, $pid;
33 2 sleep(2);
34 2 kill 9, $pid;
35 2 close $fh;
36 }}
37 4 return 0;
38  
39 }
40  
41  
42 sub run_one
43 {
44 4 ▶ my ($self, $conf) = @_;
Subroutine run_one: called CC=7 SCAR=20.1
Pod documented
45  
46 4 my $command = $conf->{command};
47 4 my $pid_file = $conf->{pid_file};
48 4 ▶ my @argv = @{$conf->{argv} // [] } ;
Condition50%1/2
$conf->{"argv"} // []
l!l
40
MC/DC0%0/1
$conf->{"argv"} // []
$conf->{"argv"}
Truth table50%1/2
A
A= $conf->{"argv"}
Aresult
11
00
49  
50 4 $self->kill_instance($pid_file);
51  
52 4 ▶ return qq(Can not execute "$command" because it's not an executable) unless -x $command;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $command 0 4 4
53 4 my $pid = fork();
54 4 ▶ return qq(Can not execute "$command". Fork failed: $!) unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 4 4
55  
56 4 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 1 3 4
57 1 exec $command, @argv;
58 0 exit 0;
59 }
60  
61 3 ▶ return 0 unless $pid_file;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid_file 0 3 3
62 3 ▶ open(my $fh, ">", $pid_file) or return qq(Can not open "$pid_file" for pid $pid:$!);
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $pid_file or return qq[Can not open "$pid_file" for pid ${pid}:$!] 0 3 3
63 3 print $fh $pid;
64 3 close $fh;
65 3 return 0;
66 }
67  
68  
69  
70  
71 sub makedir
72 {
73 0 ▶ my ($self, $dir) = @_;
Subroutine makedir: not called CC=5 SCAR=34.0
Pod documented
74 0 ▶ return 0 if -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dir 0 0 0
75 0 ▶ if (-e $dir and not -d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir and not -d $dir) 0 0 0
Condition0%0/3
-e $dir and not -d $dir
!ll&&!rl&&r
000
MC/DC0%0/2
-e $dir and not -d $dir
-e $dir not -d $dir
Truth table0%0/3
A and not(B)
A= -e $dirB= not -d $dir
ABresult
0X0
100
111
76 0 unlink $dir;
77 }
78 0 ▶ system("mkdir","-p",$dir) == 0 or return "Can't create $dir:$!";
Branch: 1 branch
BranchTrueFalseTotal
system("mkdir", "-p", $dir) == 0 or return "Can't create ${dir}:$!" 0 0 0
79 0 return 0;
80 }
81  
82  
83  
84 sub log_and_exec
85 {
86 2 ▶ my ($self, @cmd) = @_;
Subroutine log_and_exec: called CC=6 SCAR=21.5
Pod documented
87 2 my $cmd = join " ",@cmd;
88 2 $self->log->debug( $cmd );
89 2 my $output=`$cmd 2>&1`;
90 2 my $retval=$?;
91 2 ▶ if (not defined($output)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $output) 0 2 2
92 0 $output = "Executing $cmd failed";
93 0 $retval = 1;
94 }
95 2 ▶ chomp $output if $output;
Branch: 1 branch
BranchTrueFalseTotal
if $output 0 2 2
96 2 ▶ if ($retval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retval) 0 2 2
97 0 ▶ return ($retval >> 8, $output) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
98 0 return $output;
99 }
100 2 ▶ return (0, $output) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
101 2 return 0;
102 }
103  
104 3 1; # End of Tapper::Base
105  
106 __END__
107 =pod
108  
109 =encoding utf-8
110  
111 =head1 NAME
112  
113 Tapper::Base - Tapper - Common functions for all Tapper classes
114  
115 =head1 SYNOPSIS
116  
117 package Tapper::Some::Class;
118 use Moose;
119 extends 'Tapper::Base';
120  
121 =head1 FUNCTIONS
122  
123 =head2 kill_instance
124  
125 Kill the process whose id is in the given pidfile.
126  
127 @param string - pid file name
128  
129 @return success - 0
130 @return error - error string
131  
132 =head2 run_one
133  
134 Run one instance of the given command. Kill previous incarnations if necessary.
135  
136 @param hash ref - {command => command to execute,
137 pid_file => pid file containing the ID of last incarnation,
138 argv => array ref containg (optional) arguments}
139  
140 @return success - 0
141 @return error - error string
142  
143 =head2 makedir
144  
145 Checks whether a given directory exists and creates it if not.
146  
147 @param string - directory to create
148  
149 @return success - 0
150 @return error - error string
151  
152 =head2 log_and_exec
153  
154 Execute a given command. Make sure the command is logged if requested and none
155 of its output pollutes the console. In scalar context the function returns 0
156 for success and the output of the command on error. In array context the
157 function always return a list containing the return value of the command and
158 the output of the command.
159  
160 @param string - command
161  
162 @return success - 0
163 @return error - error string
164 @returnlist success - (0, output)
165 @returnlist error - (return value of command, output)
166  
167 =head1 AUTHOR
168  
169 AMD OSRC Tapper Team <tapper@amd64.org>
170  
171 =head1 COPYRIGHT AND LICENSE
172  
173 This software is Copyright (c) 2012 by Advanced Micro Devices, Inc..
174  
175 This is free software, licensed under:
176  
177 The (two-clause) FreeBSD License
178  
179 =cut
180  
↑ Summary