[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 Locale::gettext;
|
---|
| 24 | use POSIX qw(setlocale);
|
---|
[2] | 25 |
|
---|
[318] | 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 |
|
---|
[495] | 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";
|
---|
[318] | 39 |
|
---|
| 40 | our @ISA = qw(Exporter);
|
---|
[495] | 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);
|
---|
[318] | 42 |
|
---|
[395] | 43 | =pod
|
---|
[2] | 44 |
|
---|
[395] | 45 | =head1 NAME
|
---|
[2] | 46 |
|
---|
[395] | 47 | ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
|
---|
[355] | 48 |
|
---|
[395] | 49 | =head1 DESCRIPTION
|
---|
[69] | 50 |
|
---|
[395] | 51 | This modules provides generic functions suitable for perl project development
|
---|
[69] | 52 |
|
---|
[395] | 53 | =head1 SYNOPSIS
|
---|
[69] | 54 |
|
---|
[395] | 55 | use ProjectBuilder::Base;
|
---|
[313] | 56 |
|
---|
[395] | 57 | #
|
---|
| 58 | # Create a directory and its parents
|
---|
| 59 | #
|
---|
| 60 | pb_mkdir_p("/tmp/foo/bar");
|
---|
[313] | 61 |
|
---|
[395] | 62 | #
|
---|
| 63 | # Remove recursively a directory and its children
|
---|
| 64 | #
|
---|
| 65 | pb_rm_rf("/tmp/foo");
|
---|
[313] | 66 |
|
---|
[395] | 67 | #
|
---|
| 68 | # Encapsulate the system call for better output and return value test
|
---|
| 69 | #
|
---|
| 70 | pb_system("ls -l", "Printing directory content");
|
---|
[314] | 71 |
|
---|
[395] | 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");
|
---|
[313] | 76 |
|
---|
[395] | 77 | #
|
---|
| 78 | # Gives the current date in a table
|
---|
| 79 | #
|
---|
| 80 | @date = pb_get_date();
|
---|
[2] | 81 |
|
---|
[395] | 82 | #
|
---|
| 83 | # Manages logs of the program
|
---|
| 84 | #
|
---|
| 85 | pb_log_init(2,\*STDOUT);
|
---|
| 86 | pb_log(1,"Message to print\n");
|
---|
[313] | 87 |
|
---|
[395] | 88 | #
|
---|
[495] | 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 | #
|
---|
[395] | 95 | # Manages content of a file
|
---|
| 96 | #
|
---|
| 97 | pb_display_file("/etc/passwd");
|
---|
| 98 | my $cnt = pb_get_content("/etc/passwd");
|
---|
[313] | 99 |
|
---|
[395] | 100 | =head1 USAGE
|
---|
[320] | 101 |
|
---|
[395] | 102 | =over 4
|
---|
[323] | 103 |
|
---|
[395] | 104 | =item B<pb_mkdir_p>
|
---|
[314] | 105 |
|
---|
[395] | 106 | Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
|
---|
[358] | 107 |
|
---|
[396] | 108 | Based on File::Path mkpath.
|
---|
| 109 |
|
---|
[395] | 110 | =cut
|
---|
[273] | 111 |
|
---|
[74] | 112 | sub pb_mkdir_p {
|
---|
[29] | 113 | my @dir = @_;
|
---|
| 114 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 115 | return($ret);
|
---|
[9] | 116 | }
|
---|
| 117 |
|
---|
[396] | 118 | =item B<pb_rm_rf>
|
---|
[395] | 119 |
|
---|
| 120 | Internal rm -rf function. Supports multiple parameters.
|
---|
| 121 |
|
---|
[396] | 122 | Based on File::Path rmtree.
|
---|
| 123 |
|
---|
[395] | 124 | =cut
|
---|
| 125 |
|
---|
[74] | 126 | sub pb_rm_rf {
|
---|
[29] | 127 | my @dir = @_;
|
---|
| 128 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 129 | return($ret);
|
---|
[9] | 130 | }
|
---|
| 131 |
|
---|
[395] | 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.
|
---|
[473] | 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).
|
---|
[395] | 141 | This function returns the result the return value of the system command.
|
---|
[396] | 142 |
|
---|
[395] | 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 |
|
---|
[74] | 147 | sub pb_system {
|
---|
[29] | 148 |
|
---|
| 149 | my $cmd=shift;
|
---|
[30] | 150 | my $cmt=shift || $cmd;
|
---|
[471] | 151 | my $verbose=shift || undef;
|
---|
[473] | 152 | my $redir = "";
|
---|
[29] | 153 |
|
---|
[500] | 154 | pb_log(0,"$cmt... ") if ($verbose ne "quiet");
|
---|
[395] | 155 | pb_log(1,"Executing $cmd\n");
|
---|
[482] | 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"));
|
---|
[473] | 158 | system("$cmd $redir");
|
---|
[347] | 159 | my $res = $?;
|
---|
[500] | 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 | }
|
---|
[347] | 166 | if ($res == -1) {
|
---|
[500] | 167 | pb_log(0,"failed to execute ($cmd): $!\n") if ($verbose ne "quiet");
|
---|
| 168 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
[347] | 169 | } elsif ($res & 127) {
|
---|
[500] | 170 | pb_log(0, "child ($cmd) died with signal ".($? & 127).", ".($? & 128) ? 'with' : 'without'." coredump\n") if ($verbose ne "quiet");
|
---|
| 171 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
[347] | 172 | } elsif ($res == 0) {
|
---|
[500] | 173 | pb_log(0,"OK\n") if ($verbose ne "quiet");
|
---|
| 174 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((defined $verbose) and (-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
[29] | 175 | } else {
|
---|
[500] | 176 | pb_log(0, "child ($cmd) exited with value ".($? >> 8)."\n") if ($verbose ne "quiet");
|
---|
| 177 | pb_display_file("$ENV{'PBTMP'}/system.log") if ((-f "$ENV{'PBTMP'}/system.log") and ($verbose ne "quiet"));
|
---|
[29] | 178 | }
|
---|
[347] | 179 | return($res);
|
---|
[30] | 180 | }
|
---|
[74] | 181 |
|
---|
[395] | 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]].
|
---|
[396] | 187 |
|
---|
[395] | 188 | Cf man URI.
|
---|
| 189 |
|
---|
| 190 | =cut
|
---|
| 191 |
|
---|
[314] | 192 | sub pb_get_uri {
|
---|
[313] | 193 |
|
---|
[314] | 194 | my $uri = shift || undef;
|
---|
[313] | 195 |
|
---|
[318] | 196 | pb_log(2,"DEBUG: uri:$uri\n");
|
---|
[314] | 197 | my ($scheme, $authority, $path, $query, $fragment) =
|
---|
[318] | 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 |
|
---|
[315] | 208 | pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
|
---|
[314] | 209 | return($scheme, $account, $host, $port, $path);
|
---|
[313] | 210 | }
|
---|
| 211 |
|
---|
[395] | 212 | =item B<pb_get_date>
|
---|
[313] | 213 |
|
---|
[395] | 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.
|
---|
[74] | 215 |
|
---|
[395] | 216 | Cf: man ctime and description of the struct tm.
|
---|
[74] | 217 |
|
---|
[395] | 218 | =cut
|
---|
[339] | 219 |
|
---|
[315] | 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 |
|
---|
[395] | 225 | =item B<pb_log_init>
|
---|
[315] | 226 |
|
---|
[395] | 227 | This function initializes the global variables used by the pb_log function.
|
---|
[106] | 228 |
|
---|
[395] | 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.
|
---|
[315] | 231 |
|
---|
[396] | 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 |
|
---|
[395] | 236 | =cut
|
---|
[319] | 237 |
|
---|
[315] | 238 | sub pb_log_init {
|
---|
[77] | 239 |
|
---|
[495] | 240 | $pbdebug = shift || 0;
|
---|
| 241 | $pbLOG = shift || \*STDOUT;
|
---|
| 242 | pb_log(1,"Debug value: $pbdebug\n");
|
---|
[315] | 243 |
|
---|
| 244 | }
|
---|
| 245 |
|
---|
[396] | 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 |
|
---|
[315] | 264 | sub pb_log {
|
---|
| 265 |
|
---|
| 266 | my $dlevel = shift;
|
---|
| 267 | my $msg = shift;
|
---|
| 268 |
|
---|
[495] | 269 | print $pbLOG "$msg" if ($dlevel <= $pbdebug);
|
---|
[315] | 270 | }
|
---|
| 271 |
|
---|
[495] | 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 |
|
---|
[396] | 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 |
|
---|
[395] | 331 | sub pb_display_file {
|
---|
[316] | 332 |
|
---|
[395] | 333 | my $file=shift;
|
---|
[316] | 334 |
|
---|
[395] | 335 | return if (not -f $file);
|
---|
| 336 | printf "%s\n",pb_get_content($file);
|
---|
[316] | 337 | }
|
---|
| 338 |
|
---|
[396] | 339 | =item B<pb_get_content>
|
---|
| 340 |
|
---|
| 341 | This function returns the content of the file passed in parameter.
|
---|
| 342 |
|
---|
| 343 | =cut
|
---|
| 344 |
|
---|
[395] | 345 | sub pb_get_content {
|
---|
[353] | 346 |
|
---|
[395] | 347 | my $file=shift;
|
---|
[353] | 348 |
|
---|
[395] | 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);
|
---|
[353] | 357 | }
|
---|
| 358 |
|
---|
[397] | 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 |
|
---|
[495] | 369 | $pbsynmsg = shift || "Error";
|
---|
[397] | 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 |
|
---|
[495] | 391 | pod2usage( { -message => $pbsynmsg,
|
---|
[397] | 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 |
|
---|
[2] | 432 | 1;
|
---|