[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] | 12 | package ProjectBuilder::Base;
|
---|
| 13 |
|
---|
[18] | 14 | use strict;
|
---|
[5] | 15 | use lib qw (lib);
|
---|
[9] | 16 | use File::Path;
|
---|
[318] | 17 | use File::Temp qw(tempdir);
|
---|
[8] | 18 | use Data::Dumper;
|
---|
[318] | 19 | use Time::localtime qw(localtime);
|
---|
[397] | 20 | use Pod::Usage;
|
---|
[328] | 21 | use English;
|
---|
[495] | 22 | use locale;
|
---|
| 23 | use POSIX qw(setlocale);
|
---|
[2] | 24 |
|
---|
[318] | 25 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 26 |
|
---|
| 27 | use Exporter;
|
---|
| 28 |
|
---|
| 29 | # Export, by default, all the functions into the namespace of
|
---|
| 30 | # any code which uses this module.
|
---|
| 31 |
|
---|
[495] | 32 | our $pbdebug = 0; # Global debug level
|
---|
| 33 | our $pbLOG = \*STDOUT; # File descriptor of the log file
|
---|
| 34 | our $pbsynmsg = "Error"; # Global error message
|
---|
| 35 | our $pbdisplaytype = "text";
|
---|
| 36 | # default display mode for messages
|
---|
| 37 | our $pblocale = "C";
|
---|
[318] | 38 |
|
---|
| 39 | our @ISA = qw(Exporter);
|
---|
[556] | 40 | 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 $pbdebug $pbLOG $pbdisplaytype $pblocale);
|
---|
[318] | 41 |
|
---|
[395] | 42 | =pod
|
---|
[2] | 43 |
|
---|
[395] | 44 | =head1 NAME
|
---|
[2] | 45 |
|
---|
[395] | 46 | ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
|
---|
[355] | 47 |
|
---|
[395] | 48 | =head1 DESCRIPTION
|
---|
[69] | 49 |
|
---|
[395] | 50 | This modules provides generic functions suitable for perl project development
|
---|
[69] | 51 |
|
---|
[395] | 52 | =head1 SYNOPSIS
|
---|
[69] | 53 |
|
---|
[395] | 54 | use ProjectBuilder::Base;
|
---|
[313] | 55 |
|
---|
[395] | 56 | #
|
---|
| 57 | # Create a directory and its parents
|
---|
| 58 | #
|
---|
| 59 | pb_mkdir_p("/tmp/foo/bar");
|
---|
[313] | 60 |
|
---|
[395] | 61 | #
|
---|
| 62 | # Remove recursively a directory and its children
|
---|
| 63 | #
|
---|
| 64 | pb_rm_rf("/tmp/foo");
|
---|
[313] | 65 |
|
---|
[395] | 66 | #
|
---|
| 67 | # Encapsulate the system call for better output and return value test
|
---|
| 68 | #
|
---|
| 69 | pb_system("ls -l", "Printing directory content");
|
---|
[314] | 70 |
|
---|
[395] | 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");
|
---|
[313] | 75 |
|
---|
[395] | 76 | #
|
---|
| 77 | # Gives the current date in a table
|
---|
| 78 | #
|
---|
| 79 | @date = pb_get_date();
|
---|
[2] | 80 |
|
---|
[395] | 81 | #
|
---|
| 82 | # Manages logs of the program
|
---|
| 83 | #
|
---|
| 84 | pb_log_init(2,\*STDOUT);
|
---|
| 85 | pb_log(1,"Message to print\n");
|
---|
[313] | 86 |
|
---|
[395] | 87 | #
|
---|
| 88 | # Manages content of a file
|
---|
| 89 | #
|
---|
| 90 | pb_display_file("/etc/passwd");
|
---|
| 91 | my $cnt = pb_get_content("/etc/passwd");
|
---|
[313] | 92 |
|
---|
[395] | 93 | =head1 USAGE
|
---|
[320] | 94 |
|
---|
[395] | 95 | =over 4
|
---|
[323] | 96 |
|
---|
[395] | 97 | =item B<pb_mkdir_p>
|
---|
[314] | 98 |
|
---|
[395] | 99 | Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
|
---|
[358] | 100 |
|
---|
[396] | 101 | Based on File::Path mkpath.
|
---|
| 102 |
|
---|
[395] | 103 | =cut
|
---|
[273] | 104 |
|
---|
[74] | 105 | sub pb_mkdir_p {
|
---|
[29] | 106 | my @dir = @_;
|
---|
| 107 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 108 | return($ret);
|
---|
[9] | 109 | }
|
---|
| 110 |
|
---|
[396] | 111 | =item B<pb_rm_rf>
|
---|
[395] | 112 |
|
---|
| 113 | Internal rm -rf function. Supports multiple parameters.
|
---|
| 114 |
|
---|
[396] | 115 | Based on File::Path rmtree.
|
---|
| 116 |
|
---|
[395] | 117 | =cut
|
---|
| 118 |
|
---|
[74] | 119 | sub pb_rm_rf {
|
---|
[29] | 120 | my @dir = @_;
|
---|
| 121 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 122 | return($ret);
|
---|
[9] | 123 | }
|
---|
| 124 |
|
---|
[395] | 125 | =item B<pb_system>
|
---|
| 126 |
|
---|
| 127 | Encapsulate the "system" call for better output and return value test
|
---|
| 128 | Needs a $ENV{'PBTMP'} variable which is created by calling the pb_mktemp_init function
|
---|
| 129 | Needs pb_log support, so pb_log_init should have benn called before.
|
---|
| 130 |
|
---|
| 131 | The first parameter is the shell command to call.
|
---|
| 132 | The second parameter is the message to print on screen. If none is given, then the command is printed.
|
---|
[473] | 133 | 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).
|
---|
[395] | 134 | This function returns the result the return value of the system command.
|
---|
[396] | 135 |
|
---|
[395] | 136 | If no error reported, it prints OK on the screen, just after the message. Else it prints the errors generated.
|
---|
| 137 |
|
---|
| 138 | =cut
|
---|
| 139 |
|
---|
[74] | 140 | sub pb_system {
|
---|
[29] | 141 |
|
---|
| 142 | my $cmd=shift;
|
---|
[30] | 143 | my $cmt=shift || $cmd;
|
---|
[471] | 144 | my $verbose=shift || undef;
|
---|
[473] | 145 | my $redir = "";
|
---|
[29] | 146 |
|
---|
[503] | 147 | pb_log(0,"$cmt... ") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
[395] | 148 | pb_log(1,"Executing $cmd\n");
|
---|
[482] | 149 | unlink("$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"));
|
---|
[473] | 151 | system("$cmd $redir");
|
---|
[347] | 152 | my $res = $?;
|
---|
[500] | 153 | # Exit now if the command may fail
|
---|
| 154 | if ((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 | }
|
---|
[347] | 159 | if ($res == -1) {
|
---|
[503] | 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")));
|
---|
[347] | 162 | } elsif ($res & 127) {
|
---|
[503] | 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")));
|
---|
[347] | 165 | } elsif ($res == 0) {
|
---|
[503] | 166 | pb_log(0,"OK\n") if ((! defined $verbose) || ($verbose ne "quiet"));
|
---|
[500] | 167 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((defined $verbose) and (-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
[29] | 168 | } else {
|
---|
[503] | 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")));
|
---|
[29] | 171 | }
|
---|
[347] | 172 | return($res);
|
---|
[30] | 173 | }
|
---|
[74] | 174 |
|
---|
[395] | 175 | =item B<pb_get_uri>
|
---|
| 176 |
|
---|
| 177 | This function returns a list of 6 parameters indicating the protocol, account, password, server, port, and path contained in the URI passed in parameter.
|
---|
| 178 |
|
---|
| 179 | A URI has the format protocol://[ac@]host[:port][path[?query][#fragment]].
|
---|
[396] | 180 |
|
---|
[395] | 181 | Cf man URI.
|
---|
| 182 |
|
---|
| 183 | =cut
|
---|
| 184 |
|
---|
[314] | 185 | sub pb_get_uri {
|
---|
[313] | 186 |
|
---|
[314] | 187 | my $uri = shift || undef;
|
---|
[313] | 188 |
|
---|
[318] | 189 | pb_log(2,"DEBUG: uri:$uri\n");
|
---|
[314] | 190 | my ($scheme, $authority, $path, $query, $fragment) =
|
---|
[318] | 191 | $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);
|
---|
| 192 | my ($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 |
|
---|
[315] | 201 | pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
|
---|
[314] | 202 | return($scheme, $account, $host, $port, $path);
|
---|
[313] | 203 | }
|
---|
| 204 |
|
---|
[395] | 205 | =item B<pb_get_date>
|
---|
[313] | 206 |
|
---|
[395] | 207 | 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.
|
---|
[74] | 208 |
|
---|
[395] | 209 | Cf: man ctime and description of the struct tm.
|
---|
[74] | 210 |
|
---|
[395] | 211 | =cut
|
---|
[339] | 212 |
|
---|
[315] | 213 | sub pb_get_date {
|
---|
| 214 |
|
---|
| 215 | return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[395] | 218 | =item B<pb_log_init>
|
---|
[315] | 219 |
|
---|
[395] | 220 | This function initializes the global variables used by the pb_log function.
|
---|
[106] | 221 |
|
---|
[395] | 222 | The first parameter is the debug level which will be considered during the run of the program?
|
---|
| 223 | The second parameter is a pointer on a file descriptor used to print the log info.
|
---|
[315] | 224 |
|
---|
[396] | 225 | 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.
|
---|
| 226 |
|
---|
| 227 | The call to B<pb_log_init> is typically done after getting a parameter on the CLI indicating the level of verbosity expected.
|
---|
| 228 |
|
---|
[395] | 229 | =cut
|
---|
[319] | 230 |
|
---|
[315] | 231 | sub pb_log_init {
|
---|
[77] | 232 |
|
---|
[495] | 233 | $pbdebug = shift || 0;
|
---|
| 234 | $pbLOG = shift || \*STDOUT;
|
---|
| 235 | pb_log(1,"Debug value: $pbdebug\n");
|
---|
[315] | 236 |
|
---|
| 237 | }
|
---|
| 238 |
|
---|
[396] | 239 | =item B<pb_log>
|
---|
| 240 |
|
---|
| 241 | 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.
|
---|
| 242 |
|
---|
| 243 | Here 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 |
|
---|
[315] | 257 | sub pb_log {
|
---|
| 258 |
|
---|
| 259 | my $dlevel = shift;
|
---|
| 260 | my $msg = shift;
|
---|
| 261 |
|
---|
[495] | 262 | print $pbLOG "$msg" if ($dlevel <= $pbdebug);
|
---|
[315] | 263 | }
|
---|
| 264 |
|
---|
[495] | 265 |
|
---|
[396] | 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 |
|
---|
[395] | 274 | sub pb_display_file {
|
---|
[316] | 275 |
|
---|
[395] | 276 | my $file=shift;
|
---|
[316] | 277 |
|
---|
[395] | 278 | return if (not -f $file);
|
---|
| 279 | printf "%s\n",pb_get_content($file);
|
---|
[316] | 280 | }
|
---|
| 281 |
|
---|
[396] | 282 | =item B<pb_get_content>
|
---|
| 283 |
|
---|
| 284 | This function returns the content of the file passed in parameter.
|
---|
| 285 |
|
---|
| 286 | =cut
|
---|
| 287 |
|
---|
[395] | 288 | sub pb_get_content {
|
---|
[353] | 289 |
|
---|
[395] | 290 | my $file=shift;
|
---|
[353] | 291 |
|
---|
[395] | 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);
|
---|
[353] | 300 | }
|
---|
| 301 |
|
---|
[556] | 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 |
|
---|
[397] | 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 |
|
---|
[495] | 332 | $pbsynmsg = shift || "Error";
|
---|
[397] | 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 |
|
---|
[495] | 354 | pod2usage( { -message => $pbsynmsg,
|
---|
[397] | 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 | =back
|
---|
| 375 |
|
---|
| 376 | =head1 WEB SITES
|
---|
| 377 |
|
---|
| 378 | 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/>.
|
---|
| 379 |
|
---|
| 380 | =head1 USER MAILING LIST
|
---|
| 381 |
|
---|
| 382 | None exists for the moment.
|
---|
| 383 |
|
---|
| 384 | =head1 AUTHORS
|
---|
| 385 |
|
---|
| 386 | The Project-Builder.org team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.
|
---|
| 387 |
|
---|
| 388 | =head1 COPYRIGHT
|
---|
| 389 |
|
---|
| 390 | Project-Builder.org is distributed under the GPL v2.0 license
|
---|
| 391 | described in the file C<COPYING> included with the distribution.
|
---|
| 392 |
|
---|
| 393 | =cut
|
---|
| 394 |
|
---|
[2] | 395 | 1;
|
---|