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

Last change on this file since 512 was 512, checked in by Bruno Cornec, 16 years ago

Transport pb_display functions using gettext from Base into a separate module to allow Base to have only basic perl deps only abd be used in setupvm easily.

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