source: ProjectBuilder/devel/pb-modules/lib/ProjectBuilder/Base.pm@ 1647

Last change on this file since 1647 was 1647, checked in by Bruno Cornec, 12 years ago
  • Render File::Temp and Pod::Usage optional, in order to support partly old perl versions (5.00.5 at least) such as on RH6.2, so that the Base.pm module can be used in such a VE/VM
File size: 14.6 KB
Line 
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-2012
7# Eric Anderson's changes are (c) Copyright 2012 Hewlett Packard
8# Provided under the GPL v2
9#
10# $Id$
11#
12
13package ProjectBuilder::Base;
14
15use strict;
16use lib qw (lib);
17use Carp qw/confess cluck/;
18use Cwd;
19use File::Path;
20use Data::Dumper;
21use Time::localtime qw(localtime);
22use English;
23use POSIX qw(locale_h);
24use ProjectBuilder::Version;
25
26# Inherit from the "Exporter" module which handles exporting functions.
27
28use vars qw($VERSION $REVISION @ISA @EXPORT);
29use Exporter;
30
31# Export, by default, all the functions into the namespace of
32# any code which uses this module.
33
34our $pbdebug = 0; # Global debug level
35our $pbLOG = \*STDOUT; # File descriptor of the log file
36our $pbsynmsg = "Error"; # Global error message
37our $pbdisplaytype = "text";
38 # default display mode for messages
39our $pblocale = "C";
40
41our @ISA = qw(Exporter);
42our @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 $pbdebug $pbLOG $pbdisplaytype $pblocale);
43($VERSION,$REVISION) = pb_version_init();
44
45=pod
46
47=head1 NAME
48
49ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
50
51=head1 DESCRIPTION
52
53This 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
102Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
103
104Based on File::Path mkpath.
105
106=cut
107
108sub pb_mkdir_p {
109my @dir = @_;
110my $ret = eval { mkpath(@dir, 0, 0755) };
111confess "pb_mkdir_p @dir failed in ".getcwd().": $@" if ($@);
112return($ret);
113}
114
115=item B<pb_rm_rf>
116
117Internal rm -rf function. Supports multiple parameters.
118
119Based on File::Path rmtree.
120
121=cut
122
123sub pb_rm_rf {
124my @dir = @_;
125my $ret = rmtree(@dir, 0, 0);
126return($ret);
127}
128
129=item B<pb_system>
130
131Encapsulate the "system" call for better output and return value test.
132Needs a $ENV{'PBTMP'} variable which is created by calling the pb_mktemp_init function.
133Needs pb_log support, so pb_log_init should have been called before.
134
135The first parameter is the shell command to call. This command should NOT use redirections.
136The second parameter is the message to print on screen. If none is given, then the command is printed.
137The 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.
138This function returns as a result the return value of the system command.
139
140If no error reported, it prints OK on the screen, just after the message. Else it prints the errors generated.
141
142=cut
143
144sub pb_system {
145
146my $cmd=shift;
147my $cmt=shift || $cmd;
148my $verbose=shift || undef;
149my $redir = "";
150
151pb_log(0,"$cmt... ") if ((! defined $verbose) || ($verbose ne "quiet"));
152pb_log(1,"Executing $cmd\n");
153unlink("$ENV{'PBTMP'}/system.$$.log") if (-f "$ENV{'PBTMP'}/system.$$.log");
154$redir = "2>> $ENV{'PBTMP'}/system.$$.log 1>> $ENV{'PBTMP'}/system.$$.log" if ((! defined $verbose) || ($verbose ne "noredir"));
155
156# If sudo used, then be more verbose
157pb_log(0,"Executing $cmd\n") if (($pbdebug < 1) && ($cmd =~ /^\s*\S*sudo/o) && (defined $Global::pb_show_sudo) && ($Global::pb_show_sudo =~ /true/oi));
158
159system("$cmd $redir");
160my $res = $?;
161# Exit now if the command may fail
162if ((defined $verbose) and ($verbose eq "mayfail")) {
163 pb_log(0,"NOT OK but non blocking\n") if ($res != 0);
164 pb_log(0,"OK\n") if ($res == 0);
165 return($res)
166}
167
168my $cwd = getcwd;
169my $error = undef;
170$error = "ERROR: failed to execute ($cmd) in $cwd: $!\n" if ($res == -1);
171$error = "ERROR: child ($cmd) died with signal ".($res & 127).", ".($res & 128) ? 'with' : 'without'." coredump\n" if ($res & 127);
172$error = "ERROR: child ($cmd) cwd=$cwd exited with value ".($res >> 8)."\n" if ($res != 0);
173
174if (defined $error) {
175 pb_log(0, $error) if (((! defined $verbose) || ($verbose ne "quiet")) || ($Global::pb_stop_on_error));
176 pb_display_file("$ENV{'PBTMP'}/system.$$.log") if ((-f "$ENV{'PBTMP'}/system.$$.log") and ((! defined $verbose) || ($verbose ne "quiet") || $Global::pb_stop_on_error));
177 if ($Global::pb_stop_on_error) {
178 confess("ERROR running command ($cmd) with cwd=$cwd, pid=$$");
179 } else {
180 pb_log(0,"ERROR running command ($cmd) with cwd=$cwd, pid=$$");
181 }
182} else {
183 pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
184 pb_display_file("$ENV{'PBTMP'}/system.$$.log") if ((-f "$ENV{'PBTMP'}/system.$$.log") and (defined $verbose) and ($verbose ne "quiet"));
185}
186
187return($res);
188}
189
190=item B<pb_get_uri>
191
192This function returns a list of 6 parameters indicating the protocol, account, password, server, port, and path contained in the URI passed in parameter.
193
194A URI has the format protocol://[ac@]host[:port][path[?query][#fragment]].
195
196Cf man URI.
197
198=cut
199
200sub pb_get_uri {
201
202my $uri = shift || undef;
203
204pb_log(2,"DEBUG: uri:" . (defined $uri ? $uri : '') . "\n");
205my ($scheme, $authority, $path, $query, $fragment) =
206 $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);
207my ($account,$host,$port) = $authority =~ m|(?:([^\@]+)\@)?([^:]+)(:(?:[0-9]+))?| if (defined $authority);
208
209$scheme = "" if (not defined $scheme);
210$authority = "" if (not defined $authority);
211$path = "" if (not defined $path);
212$account = "" if (not defined $account);
213$host = "" if (not defined $host);
214if (not defined $port) {
215 $port = ""
216} else {
217 # Remove extra : at start
218 $port =~ s/^://;
219}
220
221pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
222return($scheme, $account, $host, $port, $path);
223}
224
225=item B<pb_get_date>
226
227This 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.
228
229Cf: man ctime and description of the struct tm.
230
231=cut
232
233sub pb_get_date {
234
235return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
236}
237
238=item B<pb_log_init>
239
240This function initializes the global variables used by the pb_log function.
241
242The first parameter is the debug level which will be considered during the run of the program?
243The second parameter is a pointer on a file descriptor used to print the log info.
244
245As 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.
246
247The call to B<pb_log_init> is typically done after getting a parameter on the CLI indicating the level of verbosity expected.
248
249=cut
250
251sub pb_log_init {
252
253$pbdebug = shift || 0;
254$pbLOG = shift || \*STDOUT;
255pb_log(1,"Debug value: $pbdebug\n");
256
257}
258
259=item B<pb_log>
260
261This 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.
262
263Here is a usage example:
264
265 pb_log_init(2,\*STDERR);
266 pb_log(1,"Hello World 1\n");
267 pb_log(2,"Hello World 2\n");
268 pb_log(3,"Hello World 3\n");
269
270 will print:
271
272 Hello World 1
273 Hello World 2
274
275=cut
276
277sub pb_log {
278
279my $dlevel = shift || 0;
280my $msg = shift || "";
281
282$pbLOG = \*STDOUT if (not defined $pbLOG);
283
284print $pbLOG "$msg" if ($dlevel <= $pbdebug);
285print "$msg" if (($dlevel == 0) && ($pbLOG != \*STDOUT));
286}
287
288
289=item B<pb_display_file>
290
291This function print the content of the file passed in parameter.
292If a second parameter is given, this is the descriptor of the logfile to write to in addtion to STDOUT.
293
294This is a cat equivalent function.
295
296=cut
297
298sub pb_display_file {
299
300my $file=shift;
301my $desc=shift || undef;
302
303return if (not -f $file);
304my $cnt = pb_get_content($file);
305print "$cnt\n";
306print $desc "$cnt\n" if (defined $desc);
307}
308
309=item B<pb_get_content>
310
311This function returns the content of the file passed in parameter.
312
313=cut
314
315sub pb_get_content {
316
317my $file=shift;
318
319my $bkp = $/;
320undef $/;
321open(R,$file) || die "Unable to open $file: $!";
322my $content=<R>;
323close(R);
324chomp($content);
325$/ = $bkp;
326return($content);
327}
328
329
330=item B<pb_set_content>
331
332This function put the content of a variable passed as second parameter into the file passed as first parameter.
333
334=cut
335
336sub pb_set_content {
337
338my $file=shift;
339my $content=shift;
340
341my $bkp = $/;
342undef $/;
343open(R,"> $file") || die "Unable to write to $file: $!";
344print R "$content";
345close(R);
346$/ = $bkp;
347}
348
349=item B<pb_syntax_init>
350
351This function initializes the global variable used by the pb_syntax function.
352
353The parameter is the message string which will be printed when calling pb_syntax
354
355=cut
356
357sub pb_syntax_init {
358
359$pbsynmsg = shift || "Error";
360}
361
362=item B<pb_syntax>
363
364This function prints the syntax expected by the application, based on pod2usage, and exits.
365The first parameter is the return value of the exit.
366The second parameter is the verbosity as expected by pod2usage.
367
368Cf: man Pod::Usage
369
370=cut
371
372sub pb_syntax {
373
374my $exit_status = shift;
375my $verbose_level = shift;
376
377my $filehandle = \*STDERR;
378
379# Don't do it upper as before as when the value is 0
380# it is considered false and then exit was set to -1
381$exit_status = -1 if (not defined $exit_status);
382$verbose_level = 0 if (not defined $verbose_level);
383
384$filehandle = \*STDOUT if ($exit_status == 0);
385
386eval {
387 require Pod::Usage;
388 Pod::Usage->import();
389};
390if ($@) {
391 # No Pod::Usage found not printing usage. Ole perl only
392} else {
393 pod2usage( -message => $pbsynmsg,
394 -exitval => $exit_status,
395 -verbose => $verbose_level,
396 -output => $filehandle );
397}
398}
399
400=item B<pb_temp_init>
401
402This 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.
403
404=cut
405
406sub pb_temp_init {
407
408if (not defined $ENV{'TMPDIR'}) {
409 $ENV{'TMPDIR'}="/tmp";
410}
411
412# Makes this function compatible with perl 5.005x
413eval {
414 require File::Temp;
415 File::Temp->import("tempdir");
416};
417if ($@) {
418 # File::Temp not found, harcoding stuff
419 # Inspired by http://cpansearch.perl.org/src/TGUMMELS/File-MkTemp-1.0.6/File/MkTemp.pm
420 # Copyright 1999|2000 Travis Gummels. All rights reserved.
421 # This may be used and modified however you want.
422 my $template = "pb.XXXXXXXXXX";
423 my @template = split //, $template;
424 my @letters = split(//,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
425 for (my $i = $#template; $i >= 0 && ($template[$i] eq 'X'); $i--){
426 $template[$i] = $letters[int(rand 52)];
427 }
428 undef $template;
429 $template = pack "a" x @template, @template;
430 pb_mkdir_p("$ENV{'TMPDIR'}/$template");
431} else {
432 $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
433}
434}
435
436=item B<pb_get_osrelease>
437
438This function returns the release of our operating system
439
440=cut
441
442sub pb_get_osrelease {
443
444# On linux can also use /proc/sys/kernel/osrelease
445my $rel = `uname -r`;
446chomp($rel);
447return($rel);
448}
449
450
451=item B<pb_get_arch>
452
453This function returns the architecture of our local environment and
454standardize on i386 for those platforms. It also solves issues where a i386 VE on x86_64 returns x86_64 wrongly
455
456=cut
457
458sub pb_get_arch {
459
460my $arch = `uname -m`;
461chomp($arch);
462$arch =~ s/i[3456]86/i386/;
463# For Solaris
464$arch =~ s/i86pc/i386/;
465
466return($arch);
467}
468
469=item B<pb_check_requirements>
470
471This function checks that the commands needed for the subsystem are indeed present.
472The required commands are passed as a comma separated string as first parameter.
473The optional commands are passed as a comma separated string as second parameter.
474
475=cut
476
477sub pb_check_requirements {
478
479my $req = shift || undef;
480my $opt = shift || undef;
481my $appname = shift || undef;
482
483my ($req2,$opt2) = (undef,undef);
484$req2 = $req->{$appname} if (defined $req and defined $appname);
485$opt2 = $opt->{$appname} if (defined $opt and defined $appname);
486
487# cmds is a string of comma separated commands
488if (defined $req2) {
489 foreach my $file (split(/,/,$req2)) {
490 pb_check_req($file,0);
491 }
492}
493
494# opts is a string of comma separated commands
495if (defined $opt2) {
496 foreach my $file (split(/,/,$opt2)) {
497 pb_check_req($file,1);
498 }
499}
500}
501
502=item B<pb_check_req>
503
504This function checks existence of a command and return its full pathname or undef if not found.
505The command name is passed as first parameter.
506The second parameter should be 0 if the command is mandatory, 1 if optional.
507
508=cut
509
510sub pb_check_req {
511
512my $file = shift;
513my $opt = shift;
514my $found = undef;
515
516$opt = 1 if (not defined $opt);
517
518pb_log(2,"Checking availability of $file...");
519# Check for all dirs in the PATH
520foreach my $p (split(/:/,$ENV{'PATH'})) {
521 if (-x "$p/$file") {
522 $found = "$p/$file";
523 last;
524 }
525}
526
527if (not $found) {
528 pb_log(2,"KO\n");
529 if ($opt eq 1) {
530 pb_log(2,"Unable to find optional command $file\n");
531 } else {
532 die pb_log(0,"Unable to find required command $file\n");
533 }
534} else {
535 pb_log(2,"OK\n");
536}
537return($found);
538}
539
540=item B<pb_path_expand>
541
542Expand out a path by environment variables as ($ENV{XXX}) and ~
543
544=cut
545
546sub pb_path_expand {
547
548my $path = shift;
549
550eval { $path =~ s/(\$ENV.+\})/$1/eeg; };
551$path =~ s/^\~/$ENV{HOME}/;
552
553return($path);
554}
555
556=back
557
558=head1 WEB SITES
559
560The 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/>.
561
562=head1 USER MAILING LIST
563
564None exists for the moment.
565
566=head1 AUTHORS
567
568The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
569
570=head1 COPYRIGHT
571
572Project-Builder.org is distributed under the GPL v2.0 license
573described in the file C<COPYING> included with the distribution.
574
575=cut
576
5771;
Note: See TracBrowser for help on using the repository browser.