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