blib/lib/ProjectBuilder/Base.pm

filter: Statementstmt 20.9% 45 / 215 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 68 MC/DCmcdc 0.0% 0 / 39 Subroutinesub 38.7% 12 / 31 Podpod 100% 19 / 19 totaltotal 15.5% 76 / 490 SCAR 85.7
CC
86
Cov
11%
pb_system
1260.0
pb_get_uri
110.0
pb_check_requirements
90.0
CRAP 5261.0
« lib/ProjectBuilder/Distribution.pm ↑ Summary lib/ProjectBuilder/Log.pm »
1 #!/usr/bin/perl -w
2 #
3 # Base subroutines brought by the the Project-Builder project
4 # which can be easily used by whatever perl project
5 #
6 # Copyright B. Cornec 2007-2016
7 # Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
8 # Provided under the GPL v2
9 #
10 # $Id$
11 #
12  
13 package ProjectBuilder::Base;
14  
15 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use lib qw (lib);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Carp qw/confess cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use Time::localtime qw(localtime);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use POSIX qw(locale_h);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use ProjectBuilder::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Inherit from the "Exporter" module which handles exporting functions.
27
28 3 ▶use vars qw($VERSION $REVISION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
30
31 # Export, by default, all the functions into the namespace of
32 # any code which uses this module.
33
34 3 our $pbdebug = 0; # Global debug level
35 3 our $pbLOG = \*STDOUT; # File descriptor of the log file
36 3 our $pbsynmsg = "Error"; # Global error message
37 3 our $pbdisplaytype = "text";
38 # default display mode for messages
39 3 our $pblocale = "C";
40  
41 3 our @ISA = qw(Exporter);
42 3 our @EXPORT = qw(pb_mkdir_p pb_system pb_rm_rf pb_get_date pb_log pb_log_init pb_get_uri pb_get_content pb_set_content pb_display_file pb_syntax_init pb_syntax pb_temp_init pb_get_arch pb_get_osrelease pb_check_requirements pb_check_req pb_path_expand pb_exit $pbdebug $pbLOG $pbdisplaytype $pblocale);
43 3 ($VERSION,$REVISION) = pb_version_init();
44  
45 =pod
46  
47 =head1 NAME
48  
49 ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
50  
51 =head1 DESCRIPTION
52  
53 This module provides generic functions suitable for perl project development
54  
55 =head1 SYNOPSIS
56  
57 use ProjectBuilder::Base;
58  
59 #
60 # Create a directory and its parents
61 #
62 pb_mkdir_p("/tmp/foo/bar");
63  
64 #
65 # Remove recursively a directory and its children
66 #
67 pb_rm_rf("/tmp/foo");
68  
69 #
70 # Encapsulate the system call for better output and return value test
71 #
72 pb_system("ls -l", "Printing directory content");
73  
74 #
75 # Analysis a URI and return its components in a table
76 #
77 my ($scheme, $account, $host, $port, $path) = pb_get_uri("svn+ssh://ac@my.server.org:port/path/to/dir");
78  
79 #
80 # Gives the current date in a table
81 #
82 @date = pb_get_date();
83  
84 #
85 # Manages logs of the program
86 #
87 pb_log_init(2,\*STDOUT);
88 pb_log(1,"Message to print\n");
89  
90 #
91 # Manages content of a file
92 #
93 pb_display_file("/etc/passwd",\*STDERR);
94 my $cnt = pb_get_content("/etc/passwd");
95  
96 =head1 USAGE
97  
98 =over 4
99  
100 =item B<pb_mkdir_p>
101  
102 Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
103  
104 Based on File::Path mkpath.
105  
106 =cut
107  
108 sub pb_mkdir_p {
109 0 ▶my @dir = @_;
Subroutine pb_mkdir_p: not called CC=2 SCAR=17.9
Pod documented
110 0 my $ret = eval { mkpath(@dir, 0, 0755) };
111 0 ▶confess "pb_mkdir_p @dir failed in ".getcwd().": $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
112 0 return($ret);
113 }
114  
115 =item B<pb_rm_rf>
116  
117 Internal rm -rf function. Supports multiple parameters.
118  
119 Based on File::Path rmtree.
120  
121 =cut
122  
123 sub pb_rm_rf {
124 0 ▶my @dir = @_;
Subroutine pb_rm_rf: not called CC=1 SCAR=6.9
Pod documented
125 0 my $ret = rmtree(@dir, 0, 0);
126 0 return($ret);
127 }
128  
129 =item B<pb_system>
130  
131 Encapsulate the "system" call for better output and return value test.
132 Needs a $ENV{'PBTMP'} variable which is created by calling the pb_mktemp_init function.
133 Needs pb_log support, so pb_log_init should have been called before.
134  
135 The first parameter is the shell command to call. This command should NOT use redirections.
136 The second parameter is the message to print on screen. If none is given, then the command is printed.
137 The third parameter prints the result of the command after correct execution if value is "verbose". If value is "noredir", it avoids redirecting outputs (e.g. for vi). If value is "quiet", doesn't print anything at all. If value is "mayfail", failure of the command is ok even if $Global::pb_stop_on_error is set, because the caller will be handling the error. A "verbose" can be added to mayfail to have it explain why it failed. If value is verbose_PREF, then every output command will be prefixed with PREF.
138 This function returns as a result the return value of the system command.
139  
140 If no error reported, it prints OK on the screen, just after the message. Else it prints the errors generated.
141  
142 =cut
143  
144 sub pb_system {
145  
146 0 ▶my $cmd=shift;
Subroutine pb_system: not called CC=35 SCAR=71.4
Pod documented
147 0 ▶my $cmt=shift || $cmd;
Condition0%0/3
shift() || $cmd
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $cmd
shift() $cmd
Truth table0%0/3
A || B
A= shift()B= $cmd
ABresult
1X1
011
000
148 0 my $verbose=shift;
149 0 my $redir = "";
150  
151 0 ▶pb_log(0,"$cmt... ") if ((! defined $verbose) || ($verbose ne "quiet"));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verbose or $verbose ne "quiet" 0 0 0
Condition0%0/3
not defined $verbose or $verbose ne "quiet"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $verbose or $verbose ne "quiet"
not defined $verbose $verbose ne "quiet"
Truth table0%0/3
not(A) or B
A= not defined $verboseB= $verbose ne "quiet"
ABresult
1X1
011
000
152 0 pb_log(1,"Executing $cmd\n");
153 0 ▶unlink("$ENV{'PBTMP'}/system.$$.log") if (-f "$ENV{'PBTMP'}/system.$$.log");
Branch: 1 branch
BranchTrueFalseTotal
if -f qq[$ENV{"PBTMP"}/system.${$}.log] 0 0 0
154 0 ▶$redir = "2>> $ENV{'PBTMP'}/system.$$.log 1>> $ENV{'PBTMP'}/system.$$.log" if ((! defined $verbose) || ($verbose ne "noredir"));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verbose or $verbose ne "noredir" 0 0 0
Condition0%0/3
not defined $verbose or $verbose ne "noredir"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $verbose or $verbose ne "noredir"
not defined $verbose $verbose ne "noredir"
Truth table0%0/3
not(A) or B
A= not defined $verboseB= $verbose ne "noredir"
ABresult
1X1
011
000
155  
156 # If sudo used, then be more verbose
157 0 ▶pb_log(0,"Executing $cmd\n") if (($pbdebug < 1) && ($cmd =~ /^\s*\S*sudo/o) && (defined $Global::pb_show_sudo) && ($Global::pb_show_sudo =~ /true/oi));
Branch: 1 branch
BranchTrueFalseTotal
if $ProjectBuilder::Base::pbdebug < 1 and $cmd =~ /^\s*\S*sudo/o and defined $Global::pb_show_sudo and $Global::pb_show_sudo =~ /true/io 0 0 0
Condition0%0/9
$ProjectBuilder::Base::pbdebug < 1 and $cmd =~ /^\s*\S*sudo/o and defined $Global::pb_show_sudo and $Global::pb_show_sudo =~ /true/io
!ll&&!rl&&r
000
$ProjectBuilder::Base::pbdebug < 1 and $cmd =~ /^\s*\S*sudo/o and defined $Global::pb_show_sudo
!ll&&!rl&&r
000
$ProjectBuilder::Base::pbdebug < 1 and $cmd =~ /^\s*\S*sudo/o
!ll&&!rl&&r
000
MC/DC0%0/4
$ProjectBuilder::Base::pbdebug < 1 and $cmd =~ /^\s*\S*sudo/o and defined $Global::pb_show_sudo and $Global::pb_show_sudo =~ /true/io
$ProjectBuilder::Base::pbdebug < 1 $cmd =~ /^\s*\S*sudo/o defined $Global::pb_show_sudo $Global::pb_show_sudo =~ /true/io
Truth table0%0/5
A and B and C and D
A= $ProjectBuilder::Base::pbdebug < 1B= $cmd =~ /^\s*\S*sudo/oC= defined $Global::pb_show_sudoD= $Global::pb_show_sudo =~ /true/io
ABCDresult
0XXX0
10XX0
110X0
11100
11111
158  
159 0 system("$cmd $redir");
160 0 my $res = $?;
161 # Exit now if the command may fail
162 0 ▶if ((defined $verbose) and ($verbose =~ /mayfail/)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $verbose and $verbose =~ /mayfail/) 0 0 0
Condition0%0/3
defined $verbose and $verbose =~ /mayfail/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $verbose and $verbose =~ /mayfail/
defined $verbose $verbose =~ /mayfail/
Truth table0%0/3
A and B
A= defined $verboseB= $verbose =~ /mayfail/
ABresult
0X0
100
111
163 0 ▶ pb_log(0,"NOT OK but non blocking\n") if ($res != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
164 0 ▶ pb_log(0,"OK\n") if ($res == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $res == 0 0 0 0
165 0 ▶ pb_display_file("$ENV{'PBTMP'}/system.$$.log",undef,$verbose) if ((-f "$ENV{'PBTMP'}/system.$$.log") and ($verbose =~ /verbose/));
Branch: 1 branch
BranchTrueFalseTotal
if -f qq[$ENV{"PBTMP"}/system.${$}.log] and $verbose =~ /verbose/ 0 0 0
Condition0%0/3
-f qq[$ENV{"PBTMP"}/system.${$}.log] and $verbose =~ /verbose/
!ll&&!rl&&r
000
MC/DC0%0/2
-f qq[$ENV{"PBTMP"}/system.${$}.log] and $verbose =~ /verbose/
-f qq[$ENV{"PBTMP"}/system.${$}.log] $verbose =~ /verbose/
Truth table0%0/3
A and B
A= -f qq[$ENV{"PBTMP"}/system.${$}.log]B= $verbose =~ /verbose/
ABresult
0X0
100
111
166 0 return($res)
167 }
168  
169 0 my $cwd = getcwd;
170 0 my $error = undef;
171 0 ▶$error = "ERROR: failed to execute ($cmd) in $cwd: $!\n" if ($res == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $res == -1 0 0 0
172 0 ▶$error = "ERROR: child ($cmd) died with signal ".($res & 127).", ".($res & 128) ? 'with' : 'without'." coredump\n" if ($res & 127);
Branch: 2 branches
BranchTrueFalseTotal
if $res & 127 0 0 0
"ERROR: child ($cmd) died with signal " . ($res & 127) . ', ' . ($res & 128) ? : 0 0 0
173 0 ▶$error = "ERROR: child ($cmd) cwd=$cwd exited with value ".($res >> 8)."\n" if ($res != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $res != 0 0 0 0
174  
175 0 ▶if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) { } 0 0 0
176 0 ▶ pb_log(0, $error) if (((! defined $verbose) || ($verbose ne "quiet")) || ($Global::pb_stop_on_error));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verbose or $verbose ne "quiet" or $Global::pb_stop_on_error 0 0 0
Condition0%0/6
not defined $verbose or $verbose ne "quiet" or $Global::pb_stop_on_error
l!l&&r!l&&!r
000
not defined $verbose or $verbose ne "quiet"
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $verbose or $verbose ne "quiet" or $Global::pb_stop_on_error
not defined $verbose $verbose ne "quiet" $Global::pb_stop_on_error
Truth table0%0/4
not(A) or B or C
A= not defined $verboseB= $verbose ne "quiet"C= $Global::pb_stop_on_error
ABCresult
1XX1
01X1
0011
0000
177 0 ▶ pb_display_file("$ENV{'PBTMP'}/system.$$.log",undef,$verbose) if ((-f "$ENV{'PBTMP'}/system.$$.log") and ((! defined $verbose) || ($verbose ne "quiet") || $Global::pb_stop_on_error));
Branch: 1 branch
BranchTrueFalseTotal
if -f qq[$ENV{"PBTMP"}/system.${$}.log] and !defined($verbose) || $verbose ne "quiet" || $Global::pb_stop_on_error 0 0 0
Condition0%0/9
-f qq[$ENV{"PBTMP"}/system.${$}.log] and !defined($verbose) || $verbose ne "quiet" || $Global::pb_stop_on_error
!ll&&!rl&&r
000
not defined $verbose or $verbose ne "quiet" or $Global::pb_stop_on_error
l!l&&r!l&&!r
000
not defined $verbose or $verbose ne "quiet"
l!l&&r!l&&!r
000
MC/DC0%0/4
-f qq[$ENV{"PBTMP"}/system.${$}.log] and !defined($verbose) || $verbose ne "quiet" || $Global::pb_stop_on_error
-f qq[$ENV{"PBTMP"}/system.${$}.log] not defined $verbose $verbose ne "quiet" $Global::pb_stop_on_error
Truth table0%0/5
A and not(B) or C or D
A= -f qq[$ENV{"PBTMP"}/system.${$}.log]B= not defined $verboseC= $verbose ne "quiet"D= $Global::pb_stop_on_error
ABCDresult
0XXX0
10000
11XX1
101X1
10011
178 0 ▶ if ($Global::pb_stop_on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Global::pb_stop_on_error) { } 0 0 0
179 0 confess("ERROR running command ($cmd) with cwd=$cwd, pid=$$");
180 } else {
181 0 pb_log(0,"ERROR running command ($cmd) with cwd=$cwd, pid=$$\n");
182 }
183 } else {
184 0 ▶ pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $verbose or $verbose ne "quiet" 0 0 0
Condition0%0/3
not defined $verbose or $verbose ne "quiet"
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $verbose or $verbose ne "quiet"
not defined $verbose $verbose ne "quiet"
Truth table0%0/3
not(A) or B
A= not defined $verboseB= $verbose ne "quiet"
ABresult
1X1
011
000
185 0 ▶ pb_display_file("$ENV{'PBTMP'}/system.$$.log",undef,$verbose) if ((-f "$ENV{'PBTMP'}/system.$$.log") and (defined $verbose) and ($verbose ne "quiet"));
Branch: 1 branch
BranchTrueFalseTotal
if -f qq[$ENV{"PBTMP"}/system.${$}.log] and defined $verbose and $verbose ne "quiet" 0 0 0
Condition0%0/6
-f qq[$ENV{"PBTMP"}/system.${$}.log] and defined $verbose and $verbose ne "quiet"
!ll&&!rl&&r
000
-f qq[$ENV{"PBTMP"}/system.${$}.log] and defined $verbose
!ll&&!rl&&r
000
MC/DC0%0/3
-f qq[$ENV{"PBTMP"}/system.${$}.log] and defined $verbose and $verbose ne "quiet"
-f qq[$ENV{"PBTMP"}/system.${$}.log] defined $verbose $verbose ne "quiet"
Truth table0%0/4
A and B and C
A= -f qq[$ENV{"PBTMP"}/system.${$}.log]B= defined $verboseC= $verbose ne "quiet"
ABCresult
0XX0
10X0
1100
1111
186 }
187  
188 0 return($res);
189 }
190  
191 =item B<pb_get_uri>
192  
193 This function returns a list of 6 parameters indicating the protocol, account, password, server, port, and path contained in the URI passed in parameter.
194  
195 A URI has the format protocol://[ac@]host[:port][path[?query][#fragment]].
196  
197 Cf man URI.
198  
199 =cut
200  
201 sub pb_get_uri {
202  
203 0 ▶my $uri = shift;
Subroutine pb_get_uri: not called CC=10 SCAR=47.0
Pod documented
204  
205 0 ▶pb_log(2,"DEBUG: uri:" . (defined $uri ? $uri : '') . "\n");
Branch: 1 branch
BranchTrueFalseTotal
defined $uri ? : 0 0 0
206 0 ▶my ($scheme, $authority, $path, $query, $fragment) =
Branch: 1 branch
BranchTrueFalseTotal
if defined $uri 0 0 0
207 $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);
208 0 ▶my ($account,$host,$port) = $authority =~ m|(?:([^\@]+)\@)?([^:]+)(:(?:[0-9]+))?| if (defined $authority);
Branch: 1 branch
BranchTrueFalseTotal
if defined $authority 0 0 0
209  
210 0 ▶$scheme = "" if (not defined $scheme);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scheme 0 0 0
211 0 ▶$authority = "" if (not defined $authority);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $authority 0 0 0
212 0 ▶$path = "" if (not defined $path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
213 0 ▶$account = "" if (not defined $account);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $account 0 0 0
214 0 ▶$host = "" if (not defined $host);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 0 0 0
215 0 ▶if (not defined $port) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $port) { } 0 0 0
216 0 $port = ""
217 } else {
218 # Remove extra : at start
219 0 $port =~ s/^://;
220 }
221  
222 0 pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
223 0 return($scheme, $account, $host, $port, $path);
224 }
225  
226 =item B<pb_get_date>
227  
228 This function returns a list of 9 parameters indicating the seconds, minutes, hours, day, month, year, day in the week, day in the year, and daylight saving time flag of the current time.
229  
230 Cf: man ctime and description of the struct tm.
231  
232 =cut
233  
234 sub pb_get_date {
235
236 0 ▶return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
Subroutine pb_get_date: not called CC=1 SCAR=6.9
Pod documented
237 }
238  
239 =item B<pb_log_init>
240  
241 This function initializes the global variables used by the pb_log function.
242  
243 The first parameter is the debug level which will be considered during the run of the program?
244 The second parameter is a pointer on a file descriptor used to print the log info.
245  
246 As an example, if you set the debug level to 2 in that function, every call to pb_log which contains a value less or equal than 2 will be printed. Calls with a value greater than 2 won't be printed.
247  
248 The call to B<pb_log_init> is typically done after getting a parameter on the CLI indicating the level of verbosity expected.
249  
250 =cut
251  
252 sub pb_log_init {
253  
254 0 ▶$pbdebug = shift;
Subroutine pb_log_init: not called CC=3 SCAR=24.8
Pod documented
255 0 $pbLOG = shift;
256  
257 0 ▶$pbdebug = 0 if (not defined $pbdebug);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ProjectBuilder::Base::pbdebug 0 0 0
258 0 ▶$pbLOG = \*STDOUT if (not defined $pbLOG);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ProjectBuilder::Base::pbLOG 0 0 0
259 0 pb_log(1,"Debug value: $pbdebug\n");
260  
261 }
262  
263 =item B<pb_log>
264  
265 This function logs the messages passed as the second parameter if the value passed as first parameter is lesser or equal than the value passed to the B<pb_log_init> function.
266  
267 Here is a usage example:
268  
269 pb_log_init(2,\*STDERR);
270 pb_log(1,"Hello World 1\n");
271 pb_log(2,"Hello World 2\n");
272 pb_log(3,"Hello World 3\n");
273  
274 will print:
275
276 Hello World 1
277 Hello World 2
278  
279 =cut
280  
281 sub pb_log {
282  
283 0 ▶my $dlevel = shift;
Subroutine pb_log: not called CC=7 SCAR=40.3
Pod documented
284 0 my $msg = shift;
285  
286 0 ▶$dlevel = 0 if (not defined $dlevel);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dlevel 0 0 0
287 0 ▶$msg = "" if (not defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
288 0 ▶$pbLOG = \*STDOUT if (not defined $pbLOG);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ProjectBuilder::Base::pbLOG 0 0 0
289  
290 0 ▶print $pbLOG "$msg" if ($dlevel <= $pbdebug);
Branch: 1 branch
BranchTrueFalseTotal
if $dlevel <= $ProjectBuilder::Base::pbdebug 0 0 0
291 0 ▶print "$msg" if (($dlevel == 0) && ($pbLOG != \*STDOUT));
Branch: 1 branch
BranchTrueFalseTotal
if $dlevel == 0 and $ProjectBuilder::Base::pbLOG != \*STDOUT 0 0 0
Condition0%0/3
$dlevel == 0 and $ProjectBuilder::Base::pbLOG != \*STDOUT
!ll&&!rl&&r
000
MC/DC0%0/2
$dlevel == 0 and $ProjectBuilder::Base::pbLOG != \*STDOUT
$dlevel == 0 $ProjectBuilder::Base::pbLOG != \*STDOUT
Truth table0%0/3
A and B
A= $dlevel == 0B= $ProjectBuilder::Base::pbLOG != \*STDOUT
ABresult
0X0
100
111
292 }
293  
294  
295 =item B<pb_display_file>
296  
297 This function prints the content of the file passed in parameter.
298 If a second parameter is given, this is the descriptor of the logfile to write to in addtion to STDOUT.
299 If a third parameter is given, this is the prefix providing it's writen as verbose_PREF. In which case the PREF string will be added before the line output.
300  
301 This is a cat equivalent function.
302  
303 =cut
304  
305 sub pb_display_file {
306  
307 0 ▶my $file=shift;
Subroutine pb_display_file: not called CC=5 SCAR=34.0
Pod documented
308 0 my $desc=shift;
309 0 my $prefix=shift;
310  
311 0 ▶return if (not -f $file);
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
312 0 my $cnt = pb_get_content($file);
313 # If we have a prefix, then add it at each line
314 0 ▶if ((defined $prefix) and ($prefix =~ "_")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix =~ /_/) { } 0 0 0
Condition0%0/3
defined $prefix and $prefix =~ /_/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $prefix and $prefix =~ /_/
defined $prefix $prefix =~ /_/
Truth table0%0/3
A and B
A= defined $prefixB= $prefix =~ /_/
ABresult
0X0
100
111
315 0 $prefix =~ s/verbose_//;
316 0 $cnt =~ s/(.*)\n/$prefix$1\n/g;
317 } else {
318 0 $prefix = "";
319 }
320 0 print "$prefix$cnt";
321 0 ▶print $desc "$prefix$cnt" if (defined $desc);
Branch: 1 branch
BranchTrueFalseTotal
if defined $desc 0 0 0
322 }
323  
324 =item B<pb_get_content>
325  
326 This function returns the content of the file passed in parameter.
327  
328 =cut
329 sub pb_get_content {
330  
331 0 ▶my $file=shift;
Subroutine pb_get_content: not called CC=2 SCAR=17.9
Pod documented
332  
333 0 ▶open(R,$file) || die "Unable to open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open R, $file or die "Unable to open ${file}: $!" 0 0 0
334 0 local $/;
335 0 my $content=<R>;
336 0 close(R);
337 0 return($content);
338 }
339  
340  
341 =item B<pb_set_content>
342  
343 This function put the content of a variable passed as second parameter into the file passed as first parameter.
344  
345 =cut
346  
347 sub pb_set_content {
348  
349 0 ▶my $file=shift;
Subroutine pb_set_content: not called CC=2 SCAR=17.9
Pod documented
350 0 my $content=shift;
351  
352 0 my $bkp = $/;
353 0 undef $/;
354 0 ▶open(R,"> $file") || die "Unable to write to $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open R, "> $file" or die "Unable to write to ${file}: $!" 0 0 0
355 0 print R "$content";
356 0 close(R);
357 0 $/ = $bkp;
358 }
359  
360 =item B<pb_exit>
361  
362 Fundtion to call before exiting pb so cleanup is done
363  
364 =cut
365  
366 sub pb_exit {
367  
368 0 ▶my $ret = shift;
Subroutine pb_exit: not called CC=3 SCAR=24.8
Pod documented
369 0 ▶$ret = 0 if (not defined $ret);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 0 0 0
370 0 ▶pb_log(0,"Please remove manually $ENV{'PBTMP'} after debug analysis\n") if ($pbdebug > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $ProjectBuilder::Base::pbdebug > 1 0 0 0
371 0 exit($ret);
372 }
373  
374 =item B<pb_syntax_init>
375  
376 This function initializes the global variable used by the pb_syntax function.
377  
378 The parameter is the message string which will be printed when calling pb_syntax
379  
380 =cut
381  
382 sub pb_syntax_init {
383  
384 0 ▶$pbsynmsg = shift || "Error";
Subroutine pb_syntax_init: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
shift() || 'Error'
l!l
00
MC/DC0%0/1
shift() || 'Error'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
385 }
386  
387 =item B<pb_syntax>
388  
389 This function prints the syntax expected by the application, based on pod2usage, and exits.
390 The first parameter is the return value of the exit.
391 The second parameter is the verbosity as expected by pod2usage.
392  
393 Cf: man Pod::Usage
394  
395 =cut
396  
397 sub pb_syntax {
398  
399 0 ▶my $exit_status = shift;
Subroutine pb_syntax: not called CC=5 SCAR=34.0
Pod documented
400 0 my $verbose_level = shift;
401  
402 0 my $filehandle = \*STDERR;
403  
404 # Don't do it upper as before as when the value is 0
405 # it is considered false and then exit was set to -1
406 0 ▶$exit_status = -1 if (not defined $exit_status);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exit_status 0 0 0
407 0 ▶$verbose_level = 0 if (not defined $verbose_level);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $verbose_level 0 0 0
408  
409 0 ▶$filehandle = \*STDOUT if ($exit_status == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $exit_status == 0 0 0 0
410  
411 0 eval {
412 0 require Pod::Usage;
413 0 Pod::Usage->import();
414 };
415 0 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
416 # No Pod::Usage found not printing usage. Old perl only
417 0 pb_exit();
418 } else {
419 0 pod2usage( -message => $pbsynmsg,
420 -exitval => $exit_status,
421 -verbose => $verbose_level,
422 -output => $filehandle );
423 }
424 }
425  
426 =item B<pb_temp_init>
427  
428 This function initializes the environemnt variable PBTMP to a random value. This directory can be safely used during the whole program, it will be removed at the end automatically.
429  
430 =cut
431  
432 sub pb_temp_init {
433  
434 0 ▶my $pbkeep = shift;
Subroutine pb_temp_init: not called CC=8 SCAR=42.8
Pod documented
435  
436 # Do not keep temp files by default
437 0 ▶$pbkeep = 0 if (not defined $pbkeep);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pbkeep 0 0 0
438  
439 0 ▶if (not defined $ENV{'TMPDIR'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ENV{"TMPDIR"}) 0 0 0
440 0 $ENV{'TMPDIR'}="/tmp";
441 }
442  
443 # Makes this function compatible with perl 5.005x
444 0 eval {
445 0 require File::Temp;
446 0 File::Temp->import("tempdir");
447 };
448 0 ▶if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
449 # File::Temp not found, harcoding stuff
450 # Inspired by http://cpansearch.perl.org/src/TGUMMELS/File-MkTemp-1.0.6/File/MkTemp.pm
451 # Copyright 1999|2000 Travis Gummels. All rights reserved.
452 # This may be used and modified however you want.
453 0 my $template = "pb.XXXXXXXXXX";
454 0 my @template = split //, $template;
455 0 my @letters = split(//,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
456 0 ▶ for (my $i = $#template; $i >= 0 && ($template[$i] eq 'X'); $i--){
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0 and $template[$i] eq "X") 0 0 0
Condition0%0/3
$i >= 0 and $template[$i] eq "X"
!ll&&!rl&&r
000
MC/DC0%0/2
$i >= 0 and $template[$i] eq "X"
$i >= 0 $template[$i] eq "X"
Truth table0%0/3
A and B
A= $i >= 0B= $template[$i] eq "X"
ABresult
0X0
100
111
457 0 $template[$i] = $letters[int(rand 52)];
458 }
459 0 undef $template;
460 0 $template = pack "a" x @template, @template;
461 0 $ENV{'PBTMP'} = "$ENV{'TMPDIR'}/$template";
462 0 pb_mkdir_p($ENV{'PBTMP'});
463 } else {
464 0 ▶ if (($pbdebug > 1) || ($pbkeep == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ProjectBuilder::Base::pbdebug > 1 or $pbkeep == 1) { } 0 0 0
Condition0%0/3
$ProjectBuilder::Base::pbdebug > 1 or $pbkeep == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$ProjectBuilder::Base::pbdebug > 1 or $pbkeep == 1
$ProjectBuilder::Base::pbdebug > 1 $pbkeep == 1
Truth table0%0/3
A or B
A= $ProjectBuilder::Base::pbdebug > 1B= $pbkeep == 1
ABresult
1X1
011
000
465 0 $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'});
466 0 pb_log(2,"DEBUG: Creating a non-volatile temporary directory ($ENV{'PBTMP'})\n");
467 } else {
468 0 $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
469 }
470 }
471 }
472  
473 =item B<pb_get_osrelease>
474  
475 This function returns the release of our operating system
476  
477 =cut
478  
479 sub pb_get_osrelease {
480  
481 # On linux can also use /proc/sys/kernel/osrelease
482 0 ▶my $rel = `uname -r`;
Subroutine pb_get_osrelease: not called CC=1 SCAR=6.9
Pod documented
483 0 chomp($rel);
484 0 return($rel);
485 }
486  
487  
488 =item B<pb_get_arch>
489  
490 This function returns the architecture of our local environment and
491 standardize on i386 for those platforms.
492  
493 =cut
494  
495 sub pb_get_arch {
496  
497 0 ▶my $arch = `uname -m`;
Subroutine pb_get_arch: not called CC=1 SCAR=6.9
Pod documented
498 0 chomp($arch);
499 0 $arch =~ s/i[3456]86/i386/;
500 # For Solaris
501 0 $arch =~ s/i86pc/i386/;
502  
503 0 return($arch);
504 }
505  
506 =item B<pb_check_requirements>
507  
508 This function checks that the commands needed for the subsystem are indeed present.
509 The required commands are passed as a comma separated string as first parameter.
510 The optional commands are passed as a comma separated string as second parameter.
511  
512 =cut
513  
514 sub pb_check_requirements {
515  
516 0 ▶my $req = shift;
Subroutine pb_check_requirements: not called CC=9 SCAR=45.0
Pod documented
517 0 my $opt = shift;
518 0 my $appname = shift;
519  
520 0 my ($req2,$opt2) = (undef,undef);
521 0 ▶$req2 = $req->{$appname} if (defined $req and defined $appname);
Branch: 1 branch
BranchTrueFalseTotal
if defined $req and defined $appname 0 0 0
Condition0%0/3
defined $req and defined $appname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $req and defined $appname
defined $req defined $appname
Truth table0%0/3
A and B
A= defined $reqB= defined $appname
ABresult
0X0
100
111
522 0 ▶$opt2 = $opt->{$appname} if (defined $opt and defined $appname);
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt and defined $appname 0 0 0
Condition0%0/3
defined $opt and defined $appname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt and defined $appname
defined $opt defined $appname
Truth table0%0/3
A and B
A= defined $optB= defined $appname
ABresult
0X0
100
111
523  
524 # cmds is a string of comma separated commands
525 0 ▶if (defined $req2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $req2) 0 0 0
526 0 foreach my $file (split(/,/,$req2)) {
527 0 pb_check_req($file,0);
528 }
529 }
530  
531 # opts is a string of comma separated commands
532 0 ▶if (defined $opt2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt2) 0 0 0
533 0 foreach my $file (split(/,/,$opt2)) {
534 0 pb_check_req($file,1);
535 }
536 }
537 }
538  
539 =item B<pb_check_req>
540  
541 This function checks existence of a command and return its full pathname or undef if not found.
542 The command name is passed as first parameter.
543 The second parameter should be 0 if the command is mandatory, 1 if optional.
544 It returns the full path name of the command if found, undef otherwise and dies if that was a mandatory command
545  
546 =cut
547  
548 sub pb_check_req {
549  
550 0 ▶my $file = shift;
Subroutine pb_check_req: not called CC=6 SCAR=37.4
Pod documented
551 0 my $opt = shift;
552 0 my $found = undef;
553  
554 0 ▶$opt = 1 if (not defined $opt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 0 0 0
555  
556 0 pb_log(2,"Checking availability of $file...");
557 # Check for all dirs in the PATH
558 0 foreach my $p (split(/:/,$ENV{'PATH'})) {
559 0 ▶ if (-x "$p/$file") {
Branch: 1 branch
BranchTrueFalseTotal
if (-x "$p/$file") 0 0 0
560 0 $found = "$p/$file";
561 0 last;
562 }
563 }
564  
565 0 ▶if (not $found) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $found) { } 0 0 0
566 0 pb_log(2,"KO\n");
567 0 ▶ if ($opt eq 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt eq 1) { } 0 0 0
568 0 pb_log(2,"Unable to find optional command $file\n");
569 } else {
570 0 die pb_log(0,"Unable to find required command $file\n");
571 }
572 } else {
573 0 pb_log(2,"OK\n");
574 }
575 0 return($found);
576 }
577  
578 =item B<pb_path_expand>
579  
580 Expand out a path by environment variables as ($ENV{XXX}) and ~
581  
582 =cut
583  
584 sub pb_path_expand {
585  
586 0 ▶my $path = shift;
Subroutine pb_path_expand: not called CC=1 SCAR=6.9
Pod documented
587  
588 0 eval { $path =~ s/(\$ENV.+\})/$1/eeg; };
589 0 $path =~ s/^\~/$ENV{HOME}/;
590  
591 0 return($path);
592 }
593  
594 =back
595  
596 =head1 WEB SITES
597  
598 The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.
599  
600 =head1 USER MAILING LIST
601  
602 None exists for the moment.
603  
604 =head1 AUTHORS
605  
606 The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
607  
608 =head1 COPYRIGHT
609  
610 Project-Builder.org is distributed under the GPL v2.0 license
611 described in the file C<COPYING> included with the distribution.
612  
613 =cut
614  
615 3 1;
« lib/ProjectBuilder/Distribution.pm ↑ Summary lib/ProjectBuilder/Log.pm »