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