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

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