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

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