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

Last change on this file since 1907 was 1907, checked in by Bruno Cornec, 10 years ago
Fix some default initializations (
undef e.g.)
  • VE.pm doesn't need to have pbstep (use pbforce simply)
  • Fix sbx2setupve in a docker context
  • Add entries for prepve|vm|rm
  • Move sandbox management into send2target so that all files to process are managed here
File size: 15.9 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;
[8]20use Data::Dumper;
[318]21use Time::localtime qw(localtime);
[328]22use English;
[681]23use POSIX qw(locale_h);
[1148]24use ProjectBuilder::Version;
[2]25
[318]26# Inherit from the "Exporter" module which handles exporting functions.
27
[1156]28use vars qw($VERSION $REVISION @ISA @EXPORT);
[318]29use Exporter;
30
31# Export, by default, all the functions into the namespace of
32# any code which uses this module.
33
[495]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";
[318]40
41our @ISA = qw(Exporter);
[1661]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 pb_exit $pbdebug $pbLOG $pbdisplaytype $pblocale);
[1156]43($VERSION,$REVISION) = pb_version_init();
[318]44
[395]45=pod
[2]46
[395]47=head1 NAME
[2]48
[395]49ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
[355]50
[395]51=head1 DESCRIPTION
[69]52
[1400]53This module provides generic functions suitable for perl project development
[69]54
[395]55=head1 SYNOPSIS
[69]56
[395]57 use ProjectBuilder::Base;
[313]58
[395]59 #
60 # Create a directory and its parents
61 #
62 pb_mkdir_p("/tmp/foo/bar");
[313]63
[395]64 #
65 # Remove recursively a directory and its children
66 #
67 pb_rm_rf("/tmp/foo");
[313]68
[395]69 #
70 # Encapsulate the system call for better output and return value test
71 #
72 pb_system("ls -l", "Printing directory content");
[314]73
[395]74 #
75 # Analysis a URI and return its components in a table
76 #
[1076]77 my ($scheme, $account, $host, $port, $path) = pb_get_uri("svn+ssh://ac@my.server.org:port/path/to/dir");
[313]78
[395]79 #
80 # Gives the current date in a table
81 #
82 @date = pb_get_date();
[2]83
[395]84 #
85 # Manages logs of the program
86 #
87 pb_log_init(2,\*STDOUT);
88 pb_log(1,"Message to print\n");
[313]89
[395]90 #
91 # Manages content of a file
92 #
[1063]93 pb_display_file("/etc/passwd",\*STDERR);
[395]94 my $cnt = pb_get_content("/etc/passwd");
[313]95
[395]96=head1 USAGE
[320]97
[395]98=over 4
[323]99
[395]100=item B<pb_mkdir_p>
[314]101
[395]102Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
[358]103
[396]104Based on File::Path mkpath.
105
[395]106=cut
[273]107
[74]108sub pb_mkdir_p {
[29]109my @dir = @_;
[1515]110my $ret = eval { mkpath(@dir, 0, 0755) };
111confess "pb_mkdir_p @dir failed in ".getcwd().": $@" if ($@);
[1486]112return($ret);
[9]113}
114
[396]115=item B<pb_rm_rf>
[395]116
117Internal rm -rf function. Supports multiple parameters.
118
[396]119Based on File::Path rmtree.
120
[395]121=cut
122
[74]123sub pb_rm_rf {
[29]124my @dir = @_;
125my $ret = rmtree(@dir, 0, 0);
126return($ret);
[9]127}
128
[395]129=item B<pb_system>
130
[1486]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.
[395]134
[1486]135The first parameter is the shell command to call. This command should NOT use redirections.
[395]136The second parameter is the message to print on screen. If none is given, then the command is printed.
[1711]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. 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.
[1486]138This function returns as a result the return value of the system command.
[396]139
[395]140If no error reported, it prints OK on the screen, just after the message. Else it prints the errors generated.
141
142=cut
143
[74]144sub pb_system {
[29]145
146my $cmd=shift;
[30]147my $cmt=shift || $cmd;
[1907]148my $verbose=shift;
[473]149my $redir = "";
[29]150
[503]151pb_log(0,"$cmt... ") if ((! defined $verbose) || ($verbose ne "quiet"));
[395]152pb_log(1,"Executing $cmd\n");
[1137]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"));
[1506]155
156# If sudo used, then be more verbose
[1597]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));
[1506]158
[473]159system("$cmd $redir");
[347]160my $res = $?;
[500]161# Exit now if the command may fail
[1651]162if ((defined $verbose) and ($verbose =~ /mayfail/)) {
[1604]163 pb_log(0,"NOT OK but non blocking\n") if ($res != 0);
[500]164 pb_log(0,"OK\n") if ($res == 0);
[1851]165 pb_display_file("$ENV{'PBTMP'}/system.$$.log",undef,$verbose) if ((-f "$ENV{'PBTMP'}/system.$$.log") and ($verbose =~ /verbose/));
[500]166 return($res)
[1603]167}
[1505]168
169my $cwd = getcwd;
170my $error = undef;
[1602]171$error = "ERROR: failed to execute ($cmd) in $cwd: $!\n" if ($res == -1);
172$error = "ERROR: child ($cmd) died with signal ".($res & 127).", ".($res & 128) ? 'with' : 'without'." coredump\n" if ($res & 127);
173$error = "ERROR: child ($cmd) cwd=$cwd exited with value ".($res >> 8)."\n" if ($res != 0);
[1505]174
175if (defined $error) {
[1595]176 pb_log(0, $error) if (((! defined $verbose) || ($verbose ne "quiet")) || ($Global::pb_stop_on_error));
[1711]177 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));
[1595]178 if ($Global::pb_stop_on_error) {
[1602]179 confess("ERROR running command ($cmd) with cwd=$cwd, pid=$$");
180 } else {
[1651]181 pb_log(0,"ERROR running command ($cmd) with cwd=$cwd, pid=$$\n");
[1603]182 }
[1505]183} else {
[503]184 pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
[1711]185 pb_display_file("$ENV{'PBTMP'}/system.$$.log",undef,$verbose) 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
[1907]203my $uri = shift;
[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
[1907]254$pbdebug = shift;
255$pbLOG = shift;
256
257$pbdebug = 0 if (not defined $pbdebug);
258$pbLOG = \*STDOUT if (not defined $pbLOG);
[495]259pb_log(1,"Debug value: $pbdebug\n");
[315]260
261}
262
[396]263=item B<pb_log>
264
265This 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
267Here 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
[315]281sub pb_log {
282
[1907]283my $dlevel = shift;
284my $msg = shift;
[315]285
[1907]286$dlevel = 0 if (not defined $dlevel);
287$msg = "" if (not defined $msg);
[1585]288$pbLOG = \*STDOUT if (not defined $pbLOG);
289
[495]290print $pbLOG "$msg" if ($dlevel <= $pbdebug);
[1060]291print "$msg" if (($dlevel == 0) && ($pbLOG != \*STDOUT));
[315]292}
293
[495]294
[396]295=item B<pb_display_file>
296
[1711]297This function prints the content of the file passed in parameter.
[1063]298If a second parameter is given, this is the descriptor of the logfile to write to in addtion to STDOUT.
[1711]299If 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.
[396]300
301This is a cat equivalent function.
302
303=cut
304
[395]305sub pb_display_file {
[316]306
[395]307my $file=shift;
[1851]308my $desc=shift;
[1711]309my $prefix=shift;
[316]310
[395]311return if (not -f $file);
[1063]312my $cnt = pb_get_content($file);
[1711]313# If we have a prefix, then add it at each line
314if ((defined $prefix) and ($prefix =~ "_")) {
315 $prefix =~ s/verbose_//;
[1852]316 $cnt =~ s/(.*)\n/$prefix$1\n/g;
[1711]317} else {
318 $prefix = "";
[316]319}
[1851]320print "$prefix$cnt";
321print $desc "$prefix$cnt" if (defined $desc);
[1711]322}
[316]323
[396]324=item B<pb_get_content>
325
326This function returns the content of the file passed in parameter.
327
328=cut
[395]329sub pb_get_content {
[353]330
[395]331my $file=shift;
[353]332
[395]333open(R,$file) || die "Unable to open $file: $!";
[1851]334local $/;
[395]335my $content=<R>;
336close(R);
337return($content);
[353]338}
339
[556]340
341=item B<pb_set_content>
342
[1137]343This function put the content of a variable passed as second parameter into the file passed as first parameter.
[556]344
345=cut
346
347sub pb_set_content {
348
349my $file=shift;
350my $content=shift;
351
352my $bkp = $/;
353undef $/;
354open(R,"> $file") || die "Unable to write to $file: $!";
355print R "$content";
356close(R);
357$/ = $bkp;
358}
359
[1663]360=item B<pb_exit>
361
362Fundtion to call before exiting pb so cleanup is done
363
364=cut
365
366sub pb_exit {
367
[1907]368my $ret = shift;
369$ret = 0 if (not defined $ret);
[1663]370pb_log(0,"Please remove manually $ENV{'PBTMP'} after debug analysis\n") if ($pbdebug > 1);
371exit($ret);
372}
373
[397]374=item B<pb_syntax_init>
375
376This function initializes the global variable used by the pb_syntax function.
377
378The parameter is the message string which will be printed when calling pb_syntax
379
380=cut
381
382sub pb_syntax_init {
383
[495]384$pbsynmsg = shift || "Error";
[397]385}
386
387=item B<pb_syntax>
388
389This function prints the syntax expected by the application, based on pod2usage, and exits.
390The first parameter is the return value of the exit.
391The second parameter is the verbosity as expected by pod2usage.
392
393Cf: man Pod::Usage
394
395=cut
396
397sub pb_syntax {
398
[1585]399my $exit_status = shift;
400my $verbose_level = shift;
[397]401
402my $filehandle = \*STDERR;
403
[1585]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$exit_status = -1 if (not defined $exit_status);
407$verbose_level = 0 if (not defined $verbose_level);
408
[397]409$filehandle = \*STDOUT if ($exit_status == 0);
410
[1647]411eval {
412 require Pod::Usage;
413 Pod::Usage->import();
414};
415if ($@) {
[1661]416 # No Pod::Usage found not printing usage. Old perl only
417 pb_exit();
[1647]418} else {
419 pod2usage( -message => $pbsynmsg,
[1585]420 -exitval => $exit_status,
421 -verbose => $verbose_level,
422 -output => $filehandle );
[397]423}
[1647]424}
[397]425
426=item B<pb_temp_init>
427
428This 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
432sub pb_temp_init {
433
[1907]434my $pbkeep = shift;
[1903]435
[1907]436# Do not keep temp files by default
437$pbkeep = 0 if (not defined $pbkeep);
438
[397]439if (not defined $ENV{'TMPDIR'}) {
440 $ENV{'TMPDIR'}="/tmp";
441}
[1647]442
443# Makes this function compatible with perl 5.005x
444eval {
445 require File::Temp;
446 File::Temp->import("tempdir");
447};
448if ($@) {
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 my $template = "pb.XXXXXXXXXX";
454 my @template = split //, $template;
455 my @letters = split(//,"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
456 for (my $i = $#template; $i >= 0 && ($template[$i] eq 'X'); $i--){
457 $template[$i] = $letters[int(rand 52)];
458 }
459 undef $template;
460 $template = pack "a" x @template, @template;
461 pb_mkdir_p("$ENV{'TMPDIR'}/$template");
462} else {
[1903]463 if (($pbdebug > 1) || ($pbkeep == 1)) {
[1663]464 $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'});
465 pb_log(2,"DEBUG: Creating a non-volatile temporary directory ($ENV{'PBTMP'})\n");
466 } else {
467 $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
468 }
[397]469}
[1647]470}
[397]471
[1400]472=item B<pb_get_osrelease>
473
474This function returns the release of our operating system
475
476=cut
477
478sub pb_get_osrelease {
479
480# On linux can also use /proc/sys/kernel/osrelease
[1408]481my $rel = `uname -r`;
[1402]482chomp($rel);
483return($rel);
[1400]484}
485
486
[749]487=item B<pb_get_arch>
488
489This function returns the architecture of our local environment and
[1652]490standardize on i386 for those platforms.
[749]491
492=cut
493
494sub pb_get_arch {
495
496my $arch = `uname -m`;
497chomp($arch);
[1564]498$arch =~ s/i[3456]86/i386/;
[873]499# For Solaris
500$arch =~ s/i86pc/i386/;
[749]501
502return($arch);
503}
504
[974]505=item B<pb_check_requirements>
506
507This function checks that the commands needed for the subsystem are indeed present.
[1558]508The required commands are passed as a comma separated string as first parameter.
509The optional commands are passed as a comma separated string as second parameter.
[974]510
511=cut
512
513sub pb_check_requirements {
514
[1907]515my $req = shift;
516my $opt = shift;
517my $appname = shift;
[974]518
[1128]519my ($req2,$opt2) = (undef,undef);
520$req2 = $req->{$appname} if (defined $req and defined $appname);
521$opt2 = $opt->{$appname} if (defined $opt and defined $appname);
522
[1558]523# cmds is a string of comma separated commands
[1128]524if (defined $req2) {
525 foreach my $file (split(/,/,$req2)) {
526 pb_check_req($file,0);
527 }
[974]528}
529
[1558]530# opts is a string of comma separated commands
[1128]531if (defined $opt2) {
532 foreach my $file (split(/,/,$opt2)) {
533 pb_check_req($file,1);
534 }
[974]535}
536}
537
[1127]538=item B<pb_check_req>
539
[1433]540This function checks existence of a command and return its full pathname or undef if not found.
[1127]541The command name is passed as first parameter.
542The second parameter should be 0 if the command is mandatory, 1 if optional.
[1896]543It returns the full path name of the command if found, undef otherwise and dies if that was a mandatory command
[1127]544
545=cut
546
[974]547sub pb_check_req {
548
549my $file = shift;
[1592]550my $opt = shift;
[1127]551my $found = undef;
[974]552
[1592]553$opt = 1 if (not defined $opt);
554
[974]555pb_log(2,"Checking availability of $file...");
556# Check for all dirs in the PATH
557foreach my $p (split(/:/,$ENV{'PATH'})) {
[1127]558 if (-x "$p/$file") {
559 $found = "$p/$file";
560 last;
561 }
[974]562}
[1127]563
564if (not $found) {
[974]565 pb_log(2,"KO\n");
566 if ($opt eq 1) {
567 pb_log(2,"Unable to find optional command $file\n");
568 } else {
569 die pb_log(0,"Unable to find required command $file\n");
570 }
571} else {
572 pb_log(2,"OK\n");
573}
[1402]574return($found);
[974]575}
576
[1506]577=item B<pb_path_expand>
578
579Expand out a path by environment variables as ($ENV{XXX}) and ~
580
581=cut
582
583sub pb_path_expand {
584
585my $path = shift;
586
587eval { $path =~ s/(\$ENV.+\})/$1/eeg; };
588$path =~ s/^\~/$ENV{HOME}/;
589
590return($path);
591}
592
[397]593=back
594
595=head1 WEB SITES
596
597The 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/>.
598
599=head1 USER MAILING LIST
600
601None exists for the moment.
602
603=head1 AUTHORS
604
605The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
606
607=head1 COPYRIGHT
608
609Project-Builder.org is distributed under the GPL v2.0 license
610described in the file C<COPYING> included with the distribution.
611
612=cut
613
[2]6141;
Note: See TracBrowser for help on using the repository browser.