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