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

Last change on this file since 1597 was 1597, checked in by Bruno Cornec, 12 years ago
  • Adds option pbshowsudo (false by default) to check whether we display the detail of sudo commands (to match security requiremetns) or not (to have a nicer output)
  • Fix a template generation error in Env.pm for deb rules file (line was split)
  • In VE.pm fix modes of the chroot after the snapshot phase detection to avoid error which make the code die
  • In pb create the RPM build dirs before entering in the parallel loop as sometimes 2 identical were created simultaneously, leading to an error leading to a die
  • In pb revert the code added by Eric to check deb content creation as it was failing in my environement (naming issues). Could be added after 0.12.1 is out as an additional check, but anyway as the files are copied after, it's not that important.
  • Use full path of chown in sudo for pb
  • Make the test account a variable in pbtest
  • Start to add test case for a VM in pbtest
  • This patch makes pb build back in a debian 6 VE.
File size: 13.6 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")) {
165 pb_log(0,"N/A\n") if ($res != 0);
166 pb_log(0,"OK\n") if ($res == 0);
167 return($res)
168 }
[1505]169
170my $cwd = getcwd;
171my $error = undef;
172$error = "failed to execute ($cmd) in $cwd: $!\n" if ($res == -1);
173$error = "child ($cmd) died with signal ".($res & 127).", ".($res & 128) ? 'with' : 'without'." coredump\n" if ($res & 127);
174$error = "child ($cmd) cwd=$cwd exited with value ".($res >> 8)."\n" if ($res != 0);
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) {
[1518]180 confess "error running command ($cmd) with cwd=$cwd, pid=$$";
[1505]181 }
182} else {
[503]183 pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
[1505]184 pb_display_file("$ENV{'PBTMP'}/system.$$.log") if ((-f "$ENV{'PBTMP'}/system.$$.log") and (defined $verbose) and ($verbose ne "quiet"));
[29]185}
[1505]186
[347]187return($res);
[30]188}
[74]189
[395]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]].
[396]195
[395]196Cf man URI.
197
198=cut
199
[314]200sub pb_get_uri {
[313]201
[314]202my $uri = shift || undef;
[313]203
[1504]204pb_log(2,"DEBUG: uri:" . (defined $uri ? $uri : '') . "\n");
[314]205my ($scheme, $authority, $path, $query, $fragment) =
[318]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);
[1076]214if (not defined $port) {
215 $port = ""
216} else {
217 # Remove extra : at start
218 $port =~ s/^://;
219}
[318]220
[315]221pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
[314]222return($scheme, $account, $host, $port, $path);
[313]223}
224
[395]225=item B<pb_get_date>
[313]226
[395]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.
[74]228
[395]229Cf: man ctime and description of the struct tm.
[74]230
[395]231=cut
[339]232
[315]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
[395]238=item B<pb_log_init>
[315]239
[395]240This function initializes the global variables used by the pb_log function.
[106]241
[395]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.
[315]244
[396]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
[395]249=cut
[319]250
[315]251sub pb_log_init {
[77]252
[495]253$pbdebug = shift || 0;
254$pbLOG = shift || \*STDOUT;
255pb_log(1,"Debug value: $pbdebug\n");
[315]256
257}
258
[396]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
[315]277sub pb_log {
278
[1585]279my $dlevel = shift || 0;
280my $msg = shift || "";
[315]281
[1585]282$pbLOG = \*STDOUT if (not defined $pbLOG);
283
[495]284print $pbLOG "$msg" if ($dlevel <= $pbdebug);
[1060]285print "$msg" if (($dlevel == 0) && ($pbLOG != \*STDOUT));
[315]286}
287
[495]288
[396]289=item B<pb_display_file>
290
291This function print the content of the file passed in parameter.
[1063]292If a second parameter is given, this is the descriptor of the logfile to write to in addtion to STDOUT.
[396]293
294This is a cat equivalent function.
295
296=cut
297
[395]298sub pb_display_file {
[316]299
[395]300my $file=shift;
[1063]301my $desc=shift || undef;
[316]302
[395]303return if (not -f $file);
[1063]304my $cnt = pb_get_content($file);
305print "$cnt\n";
306print $desc "$cnt\n" if (defined $desc);
[316]307}
308
[396]309=item B<pb_get_content>
310
311This function returns the content of the file passed in parameter.
312
313=cut
314
[395]315sub pb_get_content {
[353]316
[395]317my $file=shift;
[353]318
[395]319my $bkp = $/;
320undef $/;
321open(R,$file) || die "Unable to open $file: $!";
322my $content=<R>;
323close(R);
324chomp($content);
325$/ = $bkp;
326return($content);
[353]327}
328
[556]329
330=item B<pb_set_content>
331
[1137]332This function put the content of a variable passed as second parameter into the file passed as first parameter.
[556]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
[397]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
[495]359$pbsynmsg = shift || "Error";
[397]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
[1585]374my $exit_status = shift;
375my $verbose_level = shift;
[397]376
377my $filehandle = \*STDERR;
378
[1585]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
[397]384$filehandle = \*STDOUT if ($exit_status == 0);
385
[1585]386pod2usage( -message => $pbsynmsg,
387 -exitval => $exit_status,
388 -verbose => $verbose_level,
389 -output => $filehandle );
[397]390}
391
392=item B<pb_temp_init>
393
394This 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.
395
396=cut
397
398sub pb_temp_init {
399
400if (not defined $ENV{'TMPDIR'}) {
401 $ENV{'TMPDIR'}="/tmp";
402}
403$ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
404}
405
[1400]406=item B<pb_get_osrelease>
407
408This function returns the release of our operating system
409
410=cut
411
412sub pb_get_osrelease {
413
414# On linux can also use /proc/sys/kernel/osrelease
[1408]415my $rel = `uname -r`;
[1402]416chomp($rel);
417return($rel);
[1400]418}
419
420
[749]421=item B<pb_get_arch>
422
423This function returns the architecture of our local environment and
424standardize on i386 for those platforms. It also solves issues where a i386 VE on x86_64 returns x86_64 wrongly
425
426=cut
427
428sub pb_get_arch {
429
430my $arch = `uname -m`;
431chomp($arch);
[1564]432$arch =~ s/i[3456]86/i386/;
[873]433# For Solaris
434$arch =~ s/i86pc/i386/;
[749]435
436return($arch);
437}
438
[974]439=item B<pb_check_requirements>
440
441This function checks that the commands needed for the subsystem are indeed present.
[1558]442The required commands are passed as a comma separated string as first parameter.
443The optional commands are passed as a comma separated string as second parameter.
[974]444
445=cut
446
447sub pb_check_requirements {
448
[1128]449my $req = shift || undef;
450my $opt = shift || undef;
451my $appname = shift || undef;
[974]452
[1128]453my ($req2,$opt2) = (undef,undef);
454$req2 = $req->{$appname} if (defined $req and defined $appname);
455$opt2 = $opt->{$appname} if (defined $opt and defined $appname);
456
[1558]457# cmds is a string of comma separated commands
[1128]458if (defined $req2) {
459 foreach my $file (split(/,/,$req2)) {
460 pb_check_req($file,0);
461 }
[974]462}
463
[1558]464# opts is a string of comma separated commands
[1128]465if (defined $opt2) {
466 foreach my $file (split(/,/,$opt2)) {
467 pb_check_req($file,1);
468 }
[974]469}
470}
471
[1127]472=item B<pb_check_req>
473
[1433]474This function checks existence of a command and return its full pathname or undef if not found.
[1127]475The command name is passed as first parameter.
476The second parameter should be 0 if the command is mandatory, 1 if optional.
477
478=cut
479
[974]480sub pb_check_req {
481
482my $file = shift;
[1592]483my $opt = shift;
[1127]484my $found = undef;
[974]485
[1592]486$opt = 1 if (not defined $opt);
487
[974]488pb_log(2,"Checking availability of $file...");
489# Check for all dirs in the PATH
490foreach my $p (split(/:/,$ENV{'PATH'})) {
[1127]491 if (-x "$p/$file") {
492 $found = "$p/$file";
493 last;
494 }
[974]495}
[1127]496
497if (not $found) {
[974]498 pb_log(2,"KO\n");
499 if ($opt eq 1) {
500 pb_log(2,"Unable to find optional command $file\n");
501 } else {
502 die pb_log(0,"Unable to find required command $file\n");
503 }
504} else {
505 pb_log(2,"OK\n");
506}
[1402]507return($found);
[974]508}
509
[1506]510=item B<pb_path_expand>
511
512Expand out a path by environment variables as ($ENV{XXX}) and ~
513
514=cut
515
516sub pb_path_expand {
517
518my $path = shift;
519
520eval { $path =~ s/(\$ENV.+\})/$1/eeg; };
521$path =~ s/^\~/$ENV{HOME}/;
522
523return($path);
524}
525
[397]526=back
527
528=head1 WEB SITES
529
530The 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/>.
531
532=head1 USER MAILING LIST
533
534None exists for the moment.
535
536=head1 AUTHORS
537
538The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
539
540=head1 COPYRIGHT
541
542Project-Builder.org is distributed under the GPL v2.0 license
543described in the file C<COPYING> included with the distribution.
544
545=cut
546
[2]5471;
Note: See TracBrowser for help on using the repository browser.