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

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

announce function - begining of coding

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