[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);
|
---|
[328] | 20 | use English;
|
---|
[2] | 21 |
|
---|
[318] | 22 | # Inherit from the "Exporter" module which handles exporting functions.
|
---|
| 23 |
|
---|
| 24 | use Exporter;
|
---|
| 25 |
|
---|
| 26 | # Export, by default, all the functions into the namespace of
|
---|
| 27 | # any code which uses this module.
|
---|
| 28 |
|
---|
| 29 | our $debug = 0;
|
---|
| 30 | our $LOG = \*STDOUT;
|
---|
| 31 |
|
---|
| 32 | our @ISA = qw(Exporter);
|
---|
[395] | 33 | our @EXPORT = qw(pb_conf_read pb_conf_read_if 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 $debug $LOG);
|
---|
[318] | 34 |
|
---|
[395] | 35 | =pod
|
---|
[2] | 36 |
|
---|
[395] | 37 | =head1 NAME
|
---|
[2] | 38 |
|
---|
[395] | 39 | ProjectBuilder::Base, part of the project-builder.org - module dealing with generic functions suitable for perl project development
|
---|
[355] | 40 |
|
---|
[395] | 41 | =head1 DESCRIPTION
|
---|
[69] | 42 |
|
---|
[395] | 43 | This modules provides generic functions suitable for perl project development
|
---|
[69] | 44 |
|
---|
[395] | 45 | =head1 SYNOPSIS
|
---|
[69] | 46 |
|
---|
[395] | 47 | use ProjectBuilder::Base;
|
---|
[313] | 48 |
|
---|
[395] | 49 | #
|
---|
| 50 | # Create a directory and its parents
|
---|
| 51 | #
|
---|
| 52 | pb_mkdir_p("/tmp/foo/bar");
|
---|
[313] | 53 |
|
---|
[395] | 54 | #
|
---|
| 55 | # Remove recursively a directory and its children
|
---|
| 56 | #
|
---|
| 57 | pb_rm_rf("/tmp/foo");
|
---|
[313] | 58 |
|
---|
[395] | 59 | #
|
---|
| 60 | # Encapsulate the system call for better output and return value test
|
---|
| 61 | #
|
---|
| 62 | pb_system("ls -l", "Printing directory content");
|
---|
[314] | 63 |
|
---|
[395] | 64 | #
|
---|
| 65 | # Read hash codes of values from a configuration file and return table of pointers
|
---|
| 66 | #
|
---|
| 67 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","key1","key2");
|
---|
| 68 | my ($k) = pb_conf_read("$ENV{'HOME'}/.pbrc","key");
|
---|
[69] | 69 |
|
---|
[395] | 70 | #
|
---|
| 71 | # Analysis a URI and return its components in a table
|
---|
| 72 | #
|
---|
| 73 | my ($scheme, $account, $host, $port, $path) = pb_get_uri("svn+ssh://ac@my.server.org/path/to/dir");
|
---|
[313] | 74 |
|
---|
[395] | 75 | #
|
---|
| 76 | # Gives the current date in a table
|
---|
| 77 | #
|
---|
| 78 | @date = pb_get_date();
|
---|
[2] | 79 |
|
---|
[395] | 80 | #
|
---|
| 81 | # Manages logs of the program
|
---|
| 82 | #
|
---|
| 83 | pb_log_init(2,\*STDOUT);
|
---|
| 84 | pb_log(1,"Message to print\n");
|
---|
[313] | 85 |
|
---|
[395] | 86 | #
|
---|
| 87 | # Manages content of a file
|
---|
| 88 | #
|
---|
| 89 | pb_display_file("/etc/passwd");
|
---|
| 90 | my $cnt = pb_get_content("/etc/passwd");
|
---|
[313] | 91 |
|
---|
[395] | 92 | =head1 USAGE
|
---|
[320] | 93 |
|
---|
[395] | 94 | =over 4
|
---|
[323] | 95 |
|
---|
[395] | 96 | =item B<pb_mkdir_p>
|
---|
[314] | 97 |
|
---|
[395] | 98 | Internal mkdir -p function. Forces mode to 755. Supports multiple parameters.
|
---|
| 99 | Based in File::Path mkpath.
|
---|
[358] | 100 |
|
---|
[395] | 101 | =cut
|
---|
[273] | 102 |
|
---|
[74] | 103 | sub pb_mkdir_p {
|
---|
[29] | 104 | my @dir = @_;
|
---|
| 105 | my $ret = mkpath(@dir, 0, 0755);
|
---|
| 106 | return($ret);
|
---|
[9] | 107 | }
|
---|
| 108 |
|
---|
[395] | 109 | =item B<pb_mkdir_p>
|
---|
| 110 |
|
---|
| 111 | Internal rm -rf function. Supports multiple parameters.
|
---|
| 112 | Based in File::Path rmtree.
|
---|
| 113 |
|
---|
| 114 | =cut
|
---|
| 115 |
|
---|
[74] | 116 | sub pb_rm_rf {
|
---|
[29] | 117 | my @dir = @_;
|
---|
| 118 | my $ret = rmtree(@dir, 0, 0);
|
---|
| 119 | return($ret);
|
---|
[9] | 120 | }
|
---|
| 121 |
|
---|
[395] | 122 | =item B<pb_system>
|
---|
| 123 |
|
---|
| 124 | Encapsulate the "system" call for better output and return value test
|
---|
| 125 | Needs a $ENV{'PBTMP'} variable which is created by calling the pb_mktemp_init function
|
---|
| 126 | Needs pb_log support, so pb_log_init should have benn called before.
|
---|
| 127 |
|
---|
| 128 | The first parameter is the shell command to call.
|
---|
| 129 | The second parameter is the message to print on screen. If none is given, then the command is printed.
|
---|
| 130 | This function returns the result the return value of the system command.
|
---|
| 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;
|
---|
[29] | 139 |
|
---|
[319] | 140 | pb_log(0,"$cmt... ");
|
---|
[395] | 141 | pb_log(1,"Executing $cmd\n");
|
---|
[293] | 142 | system($cmd);
|
---|
[347] | 143 | my $res = $?;
|
---|
| 144 | if ($res == -1) {
|
---|
[319] | 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");
|
---|
[29] | 152 | } else {
|
---|
[319] | 153 | pb_log(0, "child ($cmd) exited with value ".($? >> 8)."\n");
|
---|
[106] | 154 | pb_display_file("$ENV{'PBTMP'}/system.log");
|
---|
[29] | 155 | }
|
---|
[347] | 156 | return($res);
|
---|
[30] | 157 | }
|
---|
[74] | 158 |
|
---|
[395] | 159 | =item B<pb_conf_read_if>
|
---|
[106] | 160 |
|
---|
[395] | 161 | This function returns a table of pointers on hashes
|
---|
| 162 | corresponding to the keys in a configuration file passed in parameter.
|
---|
| 163 | If that file doesn't exist, it returns undef.
|
---|
[106] | 164 |
|
---|
[395] | 165 | The format of the configuration file is as follows:
|
---|
[106] | 166 |
|
---|
[395] | 167 | key tag = value1,value2,...
|
---|
[88] | 168 |
|
---|
[395] | 169 | Supposing the file is called "$ENV{'HOME'}/.pbrc", containing the following:
|
---|
[88] | 170 |
|
---|
[395] | 171 | $ cat $HOME/.pbrc
|
---|
| 172 | pbver pb = 3
|
---|
| 173 | pbver default = 1
|
---|
| 174 | pblist pb = 12,25
|
---|
[313] | 175 |
|
---|
[395] | 176 | calling it like this:
|
---|
[313] | 177 |
|
---|
[395] | 178 | my ($k1, $k2) = pb_conf_read_if("$ENV{'HOME'}/.pbrc","pbver","pblist");
|
---|
[313] | 179 |
|
---|
[395] | 180 | will allow to get the mapping
|
---|
| 181 | $k1->{'pb'} contains 3
|
---|
| 182 | $ka->{'default'} contains 1
|
---|
| 183 | $k2->{'pb'} contains 12,25
|
---|
[313] | 184 |
|
---|
[395] | 185 | Valid chars for keys and tags are letters, numbers, '-' and '_'.
|
---|
[89] | 186 |
|
---|
[395] | 187 | =cut
|
---|
[242] | 188 |
|
---|
[313] | 189 | sub pb_conf_read_if {
|
---|
| 190 |
|
---|
| 191 | my $conffile = shift;
|
---|
| 192 | my @param = @_;
|
---|
| 193 |
|
---|
| 194 | open(CONF,$conffile) || return((undef));
|
---|
| 195 | close(CONF);
|
---|
| 196 | return(pb_conf_read($conffile,@param));
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[395] | 199 | =item B<pb_conf_read>
|
---|
| 200 |
|
---|
| 201 | This function is similar to B<pb_conf_read_if> except that it dies when the file in parameter doesn't exist.
|
---|
| 202 |
|
---|
| 203 | =cut
|
---|
| 204 |
|
---|
[74] | 205 | sub pb_conf_read {
|
---|
| 206 |
|
---|
| 207 | my $conffile = shift;
|
---|
| 208 | my @param = @_;
|
---|
| 209 | my $trace;
|
---|
| 210 | my @ptr;
|
---|
[291] | 211 | my %h;
|
---|
[74] | 212 |
|
---|
[291] | 213 | open(CONF,$conffile) || die "Unable to open $conffile";
|
---|
| 214 | while(<CONF>) {
|
---|
| 215 | if (/^\s*([A-z0-9-_]+)\s+([[A-z0-9-_]+)\s*=\s*(.+)$/) {
|
---|
[327] | 216 | pb_log(3,"DEBUG: 1:$1 2:$2 3:$3\n");
|
---|
[291] | 217 | $h{$1}{$2}=$3;
|
---|
| 218 | }
|
---|
[74] | 219 | }
|
---|
[291] | 220 | close(CONF);
|
---|
[74] | 221 |
|
---|
| 222 | for my $param (@param) {
|
---|
[291] | 223 | push @ptr,$h{$param};
|
---|
[74] | 224 | }
|
---|
[89] | 225 | return(@ptr);
|
---|
[74] | 226 | }
|
---|
| 227 |
|
---|
[395] | 228 | =item B<pb_get_uri>
|
---|
| 229 |
|
---|
| 230 | This function returns a list of 6 parameters indicating the protocol, account, password, server, port, and path contained in the URI passed in parameter.
|
---|
| 231 |
|
---|
| 232 | A URI has the format protocol://[ac@]host[:port][path[?query][#fragment]].
|
---|
| 233 | Cf man URI.
|
---|
| 234 |
|
---|
| 235 | =cut
|
---|
| 236 |
|
---|
[314] | 237 | sub pb_get_uri {
|
---|
[313] | 238 |
|
---|
[314] | 239 | my $uri = shift || undef;
|
---|
[313] | 240 |
|
---|
[318] | 241 | pb_log(2,"DEBUG: uri:$uri\n");
|
---|
[314] | 242 | my ($scheme, $authority, $path, $query, $fragment) =
|
---|
[318] | 243 | $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?| if (defined $uri);
|
---|
| 244 | my ($account,$host,$port) = $authority =~ m|(?:([^\@]+)\@)?([^:]+)(:(?:[0-9]+))?| if (defined $authority);
|
---|
| 245 |
|
---|
| 246 | $scheme = "" if (not defined $scheme);
|
---|
| 247 | $authority = "" if (not defined $authority);
|
---|
| 248 | $path = "" if (not defined $path);
|
---|
| 249 | $account = "" if (not defined $account);
|
---|
| 250 | $host = "" if (not defined $host);
|
---|
| 251 | $port = "" if (not defined $port);
|
---|
| 252 |
|
---|
[315] | 253 | pb_log(2,"DEBUG: scheme:$scheme ac:$account host:$host port:$port path:$path\n");
|
---|
[314] | 254 | return($scheme, $account, $host, $port, $path);
|
---|
[313] | 255 | }
|
---|
| 256 |
|
---|
[395] | 257 | =item B<pb_get_date>
|
---|
[313] | 258 |
|
---|
[395] | 259 | 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] | 260 |
|
---|
[395] | 261 | Cf: man ctime and description of the struct tm.
|
---|
[74] | 262 |
|
---|
[395] | 263 | =cut
|
---|
[339] | 264 |
|
---|
[315] | 265 | sub pb_get_date {
|
---|
| 266 |
|
---|
| 267 | return(localtime->sec(), localtime->min(), localtime->hour(), localtime->mday(), localtime->mon(), localtime->year(), localtime->wday(), localtime->yday(), localtime->isdst());
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[395] | 270 | =item B<pb_log_init>
|
---|
[315] | 271 |
|
---|
[395] | 272 | This function initializes the global variables used by the pb_log function.
|
---|
[106] | 273 |
|
---|
[395] | 274 | The first parameter is the debug level which will be considered during the run of the program?
|
---|
| 275 | The second parameter is a pointer on a file descriptor used to print the log info.
|
---|
[315] | 276 |
|
---|
[395] | 277 | =cut
|
---|
[319] | 278 |
|
---|
[315] | 279 | sub pb_log_init {
|
---|
[77] | 280 |
|
---|
[315] | 281 | $debug = shift || 0;
|
---|
| 282 | $LOG = shift || \*STDOUT;
|
---|
| 283 |
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | sub pb_log {
|
---|
| 287 |
|
---|
| 288 | my $dlevel = shift;
|
---|
| 289 | my $msg = shift;
|
---|
| 290 |
|
---|
[318] | 291 | print $LOG "$msg" if ($dlevel <= $debug);
|
---|
[315] | 292 | }
|
---|
| 293 |
|
---|
[395] | 294 | # cat equivalent function
|
---|
| 295 | sub pb_display_file {
|
---|
[316] | 296 |
|
---|
[395] | 297 | my $file=shift;
|
---|
[316] | 298 |
|
---|
[395] | 299 | return if (not -f $file);
|
---|
| 300 | printf "%s\n",pb_get_content($file);
|
---|
[316] | 301 | }
|
---|
| 302 |
|
---|
[395] | 303 | # get content of a file in a variable
|
---|
| 304 | sub pb_get_content {
|
---|
[353] | 305 |
|
---|
[395] | 306 | my $file=shift;
|
---|
[353] | 307 |
|
---|
[395] | 308 | my $bkp = $/;
|
---|
| 309 | undef $/;
|
---|
| 310 | open(R,$file) || die "Unable to open $file: $!";
|
---|
| 311 | my $content=<R>;
|
---|
| 312 | close(R);
|
---|
| 313 | chomp($content);
|
---|
| 314 | $/ = $bkp;
|
---|
| 315 | return($content);
|
---|
[353] | 316 | }
|
---|
| 317 |
|
---|
[2] | 318 | 1;
|
---|