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 |
|
---|
12 | package ProjectBuilder::Base;
|
---|
13 |
|
---|
14 | use strict;
|
---|
15 | use lib qw (lib);
|
---|
16 | use File::Path;
|
---|
17 | use File::Temp qw(tempdir);
|
---|
18 | use Data::Dumper;
|
---|
19 | use Time::localtime qw(localtime);
|
---|
20 | use Pod::Usage;
|
---|
21 | use English;
|
---|
22 | use POSIX qw(locale_h);
|
---|
23 |
|
---|
24 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
25 |
|
---|
26 | use Exporter;
|
---|
27 |
|
---|
28 | # Export, by default, all the functions into the namespace of
|
---|
29 | # any code which uses this module.
|
---|
30 |
|
---|
31 | our $pbdebug = 0; # Global debug level
|
---|
32 | our $pbLOG = \*STDOUT; # File descriptor of the log file
|
---|
33 | our $pbsynmsg = "Error"; # Global error message
|
---|
34 | our $pbdisplaytype = "text";
|
---|
35 | # default display mode for messages
|
---|
36 | our $pblocale = "C";
|
---|
37 |
|
---|
38 | our @ISA = qw(Exporter);
|
---|
39 | our @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_check_requirements $pbdebug $pbLOG $pbdisplaytype $pblocale);
|
---|
40 |
|
---|
41 | =pod
|
---|
42 |
|
---|
43 | =head1 NAME
|
---|
44 |
|
---|
45 | ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
|
---|
46 |
|
---|
47 | =head1 DESCRIPTION
|
---|
48 |
|
---|
49 | This modules provides generic functions suitable for perl project development
|
---|
50 |
|
---|
51 | =head1 SYNOPSIS
|
---|
52 |
|
---|
53 | use ProjectBuilder::Base;
|
---|
54 |
|
---|
55 | #
|
---|
56 | # Create a directory and its parents
|
---|
57 | #
|
---|
58 | pb_mkdir_p("/tmp/foo/bar");
|
---|
59 |
|
---|
60 | #
|
---|
61 | # Remove recursively a directory and its children
|
---|
62 | #
|
---|
63 | pb_rm_rf("/tmp/foo");
|
---|
64 |
|
---|
65 | #
|
---|
66 | # Encapsulate the system call for better output and return value test
|
---|
67 | #
|
---|
68 | pb_system("ls -l", "Printing directory content");
|
---|
69 |
|
---|
70 | #
|
---|
71 | # Analysis a URI and return its components in a table
|
---|
72 | #
|
---|
73 | my ($scheme, $account, $host, $port, $path) = pb_get_uri("svn+ssh://ac@my.server.org/path/to/dir");
|
---|
74 |
|
---|
75 | #
|
---|
76 | # Gives the current date in a table
|
---|
77 | #
|
---|
78 | @date = pb_get_date();
|
---|
79 |
|
---|
80 | #
|
---|
81 | # Manages logs of the program
|
---|
82 | #
|
---|
83 | pb_log_init(2,\*STDOUT);
|
---|
84 | pb_log(1,"Message to print\n");
|
---|
85 |
|
---|
86 | #
|
---|
87 | # Manages content of a file
|
---|
88 | #
|
---|
89 | pb_display_file("/etc/passwd");
|
---|
90 | my $cnt = pb_get_content("/etc/passwd");
|
---|
91 |
|
---|
92 | =head1 USAGE
|
---|
93 |
|
---|
94 | =over 4
|
---|
95 |
|
---|
96 | =item B<pb_mkdir_p>
|
---|
97 |
|
---|
98 | Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
|
---|
99 |
|
---|
100 | Based on File::Path mkpath.
|
---|
101 |
|
---|
102 | =cut
|
---|
103 |
|
---|
104 | sub pb_mkdir_p {
|
---|
105 | my @dir = @_;
|
---|
106 | my $ret = mkpath(@dir, 0, 0755);
|
---|
107 | return($ret);
|
---|
108 | }
|
---|
109 |
|
---|
110 | =item B<pb_rm_rf>
|
---|
111 |
|
---|
112 | Internal rm -rf function. Supports multiple parameters.
|
---|
113 |
|
---|
114 | Based on File::Path rmtree.
|
---|
115 |
|
---|
116 | =cut
|
---|
117 |
|
---|
118 | sub pb_rm_rf {
|
---|
119 | my @dir = @_;
|
---|
120 | my $ret = rmtree(@dir, 0, 0);
|
---|
121 | return($ret);
|
---|
122 | }
|
---|
123 |
|
---|
124 | =item B<pb_system>
|
---|
125 |
|
---|
126 | Encapsulate the "system" call for better output and return value test
|
---|
127 | Needs a $ENV{'PBTMP'} variable which is created by calling the pb_mktemp_init function
|
---|
128 | Needs pb_log support, so pb_log_init should have benn called before.
|
---|
129 |
|
---|
130 | The first parameter is the shell command to call.
|
---|
131 | The second parameter is the message to print on screen. If none is given, then the command is printed.
|
---|
132 | The 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).
|
---|
133 | This function returns the result the return value of the system command.
|
---|
134 |
|
---|
135 | If no error reported, it prints OK on the screen, just after the message. Else it prints the errors generated.
|
---|
136 |
|
---|
137 | =cut
|
---|
138 |
|
---|
139 | sub pb_system {
|
---|
140 |
|
---|
141 | my $cmd=shift;
|
---|
142 | my $cmt=shift || $cmd;
|
---|
143 | my $verbose=shift || undef;
|
---|
144 | my $redir = "";
|
---|
145 |
|
---|
146 | pb_log(0,"$cmt... ") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
147 | pb_log(1,"Executing $cmd\n");
|
---|
148 | unlink("$ENV{'PBTMP'}/system.log") if (-f "$ENV{'PBTMP'}/system.log");
|
---|
149 | $redir = "2>> $ENV{'PBTMP'}/system.log 1>> $ENV{'PBTMP'}/system.log" if ((! defined $verbose) || ($verbose ne "noredir"));
|
---|
150 | system("$cmd $redir");
|
---|
151 | my $res = $?;
|
---|
152 | # Exit now if the command may fail
|
---|
153 | if ((defined $verbose) and ($verbose eq "mayfail")) {
|
---|
154 | pb_log(0,"N/A\n") if ($res != 0);
|
---|
155 | pb_log(0,"OK\n") if ($res == 0);
|
---|
156 | return($res)
|
---|
157 | }
|
---|
158 | if ($res == -1) {
|
---|
159 | pb_log(0,"failed to execute ($cmd): $!\n") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
160 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ((! defined $verbose) || ($verbose ne "quiet")));
|
---|
161 | } elsif ($res & 127) {
|
---|
162 | pb_log(0, "child ($cmd) died with signal ".($? & 127).", ".($? & 128) ? 'with' : 'without'." coredump\n") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
163 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ((! defined $verbose) || ($verbose ne "quiet")));
|
---|
164 | } elsif ($res == 0) {
|
---|
165 | pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
166 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((defined $verbose) and (-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
167 | } else {
|
---|
168 | pb_log(0, "child ($cmd) exited with value ".($? >> 8)."\n") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
169 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ((! defined $verbose) || ($verbose ne "quiet")));
|
---|
170 | }
|
---|
171 | return($res);
|
---|
172 | }
|
---|
173 |
|
---|
174 | =item B<pb_get_uri>
|
---|
175 |
|
---|
176 | This function returns a list of 6 parameters indicating the protocol, account, password, server, port, and path contained in the URI passed in parameter.
|
---|
177 |
|
---|
178 | A URI has the format protocol://[ac@]host[:port][path[?query][#fragment]].
|
---|
179 |
|
---|
180 | Cf man URI.
|
---|
181 |
|
---|
182 | =cut
|
---|
183 |
|
---|
184 | sub pb_get_uri {
|
---|
185 |
|
---|
186 | my $uri = shift || undef;
|
---|
187 |
|
---|
188 | pb_log(2,"DEBUG: uri:$uri\n");
|
---|
189 | my ($scheme, $authority, $path, $query, $fragment) =
|
---|
190 | $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);
|
---|
191 | my ($account,$host,$port) = $authority =~ m|(?:([^\@]+)\@)?([^:]+)(:(?:[0-9]+))?| if (defined $authority);
|
---|
192 |
|
---|
193 | $scheme = "" if (not defined $scheme);
|
---|
194 | $authority = "" if (not defined $authority);
|
---|
195 | $path = "" if (not defined $path);
|
---|
196 | $account = "" if (not defined $account);
|
---|
197 | $host = "" if (not defined $host);
|
---|
198 | $port = "" if (not defined $port);
|
---|
199 |
|
---|
200 | pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
|
---|
201 | return($scheme, $account, $host, $port, $path);
|
---|
202 | }
|
---|
203 |
|
---|
204 | =item B<pb_get_date>
|
---|
205 |
|
---|
206 | This 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.
|
---|
207 |
|
---|
208 | Cf: man ctime and description of the struct tm.
|
---|
209 |
|
---|
210 | =cut
|
---|
211 |
|
---|
212 | sub pb_get_date {
|
---|
213 |
|
---|
214 | return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
|
---|
215 | }
|
---|
216 |
|
---|
217 | =item B<pb_log_init>
|
---|
218 |
|
---|
219 | This function initializes the global variables used by the pb_log function.
|
---|
220 |
|
---|
221 | The first parameter is the debug level which will be considered during the run of the program?
|
---|
222 | The second parameter is a pointer on a file descriptor used to print the log info.
|
---|
223 |
|
---|
224 | As 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.
|
---|
225 |
|
---|
226 | The call to B<pb_log_init> is typically done after getting a parameter on the CLI indicating the level of verbosity expected.
|
---|
227 |
|
---|
228 | =cut
|
---|
229 |
|
---|
230 | sub pb_log_init {
|
---|
231 |
|
---|
232 | $pbdebug = shift || 0;
|
---|
233 | $pbLOG = shift || \*STDOUT;
|
---|
234 | pb_log(1,"Debug value: $pbdebug\n");
|
---|
235 |
|
---|
236 | }
|
---|
237 |
|
---|
238 | =item B<pb_log>
|
---|
239 |
|
---|
240 | This 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.
|
---|
241 |
|
---|
242 | Here is a usage example:
|
---|
243 |
|
---|
244 | pb_log_init(2,\*STDERR);
|
---|
245 | pb_log(1,"Hello World 1\n");
|
---|
246 | pb_log(2,"Hello World 2\n");
|
---|
247 | pb_log(3,"Hello World 3\n");
|
---|
248 |
|
---|
249 | will print:
|
---|
250 |
|
---|
251 | Hello World 1
|
---|
252 | Hello World 2
|
---|
253 |
|
---|
254 | =cut
|
---|
255 |
|
---|
256 | sub pb_log {
|
---|
257 |
|
---|
258 | my $dlevel = shift;
|
---|
259 | my $msg = shift;
|
---|
260 |
|
---|
261 | print $pbLOG "$msg" if ($dlevel <= $pbdebug);
|
---|
262 | print "$msg" if (($dlevel == 0) && ($pbLOG != \*STDOUT));
|
---|
263 | }
|
---|
264 |
|
---|
265 |
|
---|
266 | =item B<pb_display_file>
|
---|
267 |
|
---|
268 | This function print the content of the file passed in parameter.
|
---|
269 |
|
---|
270 | This is a cat equivalent function.
|
---|
271 |
|
---|
272 | =cut
|
---|
273 |
|
---|
274 | sub pb_display_file {
|
---|
275 |
|
---|
276 | my $file=shift;
|
---|
277 |
|
---|
278 | return if (not -f $file);
|
---|
279 | printf "%s\n",pb_get_content($file);
|
---|
280 | }
|
---|
281 |
|
---|
282 | =item B<pb_get_content>
|
---|
283 |
|
---|
284 | This function returns the content of the file passed in parameter.
|
---|
285 |
|
---|
286 | =cut
|
---|
287 |
|
---|
288 | sub pb_get_content {
|
---|
289 |
|
---|
290 | my $file=shift;
|
---|
291 |
|
---|
292 | my $bkp = $/;
|
---|
293 | undef $/;
|
---|
294 | open(R,$file) || die "Unable to open $file: $!";
|
---|
295 | my $content=<R>;
|
---|
296 | close(R);
|
---|
297 | chomp($content);
|
---|
298 | $/ = $bkp;
|
---|
299 | return($content);
|
---|
300 | }
|
---|
301 |
|
---|
302 |
|
---|
303 | =item B<pb_set_content>
|
---|
304 |
|
---|
305 | This function put the content of a file into the file passed in parameter.
|
---|
306 |
|
---|
307 | =cut
|
---|
308 |
|
---|
309 | sub pb_set_content {
|
---|
310 |
|
---|
311 | my $file=shift;
|
---|
312 | my $content=shift;
|
---|
313 |
|
---|
314 | my $bkp = $/;
|
---|
315 | undef $/;
|
---|
316 | open(R,"> $file") || die "Unable to write to $file: $!";
|
---|
317 | print R "$content";
|
---|
318 | close(R);
|
---|
319 | $/ = $bkp;
|
---|
320 | }
|
---|
321 |
|
---|
322 | =item B<pb_syntax_init>
|
---|
323 |
|
---|
324 | This function initializes the global variable used by the pb_syntax function.
|
---|
325 |
|
---|
326 | The parameter is the message string which will be printed when calling pb_syntax
|
---|
327 |
|
---|
328 | =cut
|
---|
329 |
|
---|
330 | sub pb_syntax_init {
|
---|
331 |
|
---|
332 | $pbsynmsg = shift || "Error";
|
---|
333 | }
|
---|
334 |
|
---|
335 | =item B<pb_syntax>
|
---|
336 |
|
---|
337 | This function prints the syntax expected by the application, based on pod2usage, and exits.
|
---|
338 | The first parameter is the return value of the exit.
|
---|
339 | The second parameter is the verbosity as expected by pod2usage.
|
---|
340 |
|
---|
341 | Cf: man Pod::Usage
|
---|
342 |
|
---|
343 | =cut
|
---|
344 |
|
---|
345 | sub pb_syntax {
|
---|
346 |
|
---|
347 | my $exit_status = shift || -1;
|
---|
348 | my $verbose_level = shift || 0;
|
---|
349 |
|
---|
350 | my $filehandle = \*STDERR;
|
---|
351 |
|
---|
352 | $filehandle = \*STDOUT if ($exit_status == 0);
|
---|
353 |
|
---|
354 | pod2usage( { -message => $pbsynmsg,
|
---|
355 | -exitval => $exit_status ,
|
---|
356 | -verbose => $verbose_level,
|
---|
357 | -output => $filehandle } );
|
---|
358 | }
|
---|
359 |
|
---|
360 | =item B<pb_temp_init>
|
---|
361 |
|
---|
362 | This 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.
|
---|
363 |
|
---|
364 | =cut
|
---|
365 |
|
---|
366 | sub pb_temp_init {
|
---|
367 |
|
---|
368 | if (not defined $ENV{'TMPDIR'}) {
|
---|
369 | $ENV{'TMPDIR'}="/tmp";
|
---|
370 | }
|
---|
371 | $ENV{'PBTMP'} = tempdir( "pb.XXXXXXXXXX", DIR => $ENV{'TMPDIR'}, CLEANUP => 1 );
|
---|
372 | }
|
---|
373 |
|
---|
374 | =item B<pb_get_arch>
|
---|
375 |
|
---|
376 | This function returns the architecture of our local environment and
|
---|
377 | standardize on i386 for those platforms. It also solves issues where a i386 VE on x86_64 returns x86_64 wrongly
|
---|
378 |
|
---|
379 | =cut
|
---|
380 |
|
---|
381 | sub pb_get_arch {
|
---|
382 |
|
---|
383 | my $arch = `uname -m`;
|
---|
384 | chomp($arch);
|
---|
385 | $arch =~ s/i.86/i386/;
|
---|
386 | # For Solaris
|
---|
387 | $arch =~ s/i86pc/i386/;
|
---|
388 |
|
---|
389 | return($arch);
|
---|
390 | }
|
---|
391 |
|
---|
392 | =item B<pb_check_requirements>
|
---|
393 |
|
---|
394 | This function checks that the commands needed for the subsystem are indeed present.
|
---|
395 | The required comands are passed as a coma separated string as first parameter.
|
---|
396 | The optional comands are passed as a coma separated string as second parameter.
|
---|
397 |
|
---|
398 | =cut
|
---|
399 |
|
---|
400 | sub pb_check_requirements {
|
---|
401 |
|
---|
402 | my $cmds = shift || "";
|
---|
403 | my $options = shift || "";
|
---|
404 |
|
---|
405 | # cmds is a string of coma separated commands
|
---|
406 | foreach my $file (split(/,/,$cmds)) {
|
---|
407 | pb_check_req($file,0);
|
---|
408 | }
|
---|
409 |
|
---|
410 | # opts is a string of coma separated commands
|
---|
411 | foreach my $file (split(/,/,$options)) {
|
---|
412 | pb_check_req($file,1);
|
---|
413 | }
|
---|
414 | }
|
---|
415 |
|
---|
416 | sub pb_check_req {
|
---|
417 |
|
---|
418 | my $file = shift;
|
---|
419 | my $opt = shift || 1;
|
---|
420 | my $found = 0;
|
---|
421 |
|
---|
422 | pb_log(2,"Checking availability of $file...");
|
---|
423 | # Check for all dirs in the PATH
|
---|
424 | foreach my $p (split(/:/,$ENV{'PATH'})) {
|
---|
425 | $found = 1 if (-x "$p/$file");
|
---|
426 | }
|
---|
427 | if ($found eq 0) {
|
---|
428 | pb_log(2,"KO\n");
|
---|
429 | if ($opt eq 1) {
|
---|
430 | pb_log(2,"Unable to find optional command $file\n");
|
---|
431 | } else {
|
---|
432 | die pb_log(0,"Unable to find required command $file\n");
|
---|
433 | }
|
---|
434 | } else {
|
---|
435 | pb_log(2,"OK\n");
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | =back
|
---|
440 |
|
---|
441 | =head1 WEB SITES
|
---|
442 |
|
---|
443 | The 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/>.
|
---|
444 |
|
---|
445 | =head1 USER MAILING LIST
|
---|
446 |
|
---|
447 | None exists for the moment.
|
---|
448 |
|
---|
449 | =head1 AUTHORS
|
---|
450 |
|
---|
451 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
452 |
|
---|
453 | =head1 COPYRIGHT
|
---|
454 |
|
---|
455 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
456 | described in the file C<COPYING> included with the distribution.
|
---|
457 |
|
---|
458 | =cut
|
---|
459 |
|
---|
460 | 1;
|
---|