1 |
## --------------------------------------------------------------------------- |
2 |
## $Id: shortcuts.pm,v 1.19 2004/05/11 19:45:30 joko Exp $ |
3 |
## --------------------------------------------------------------------------- |
4 |
## $Log: shortcuts.pm,v $ |
5 |
## Revision 1.19 2004/05/11 19:45:30 joko |
6 |
## now exporting RUNNING_IN_HELL |
7 |
## |
8 |
## Revision 1.18 2003/12/05 04:58:04 joko |
9 |
## + minor update: doesn't require IPC::Session anymore |
10 |
## |
11 |
## Revision 1.17 2003/07/02 11:17:32 jonen |
12 |
## minor changes |
13 |
## |
14 |
## Revision 1.16 2003/06/25 22:49:56 joko |
15 |
## RUNNING_IN_HELL mode for detach option |
16 |
## |
17 |
## Revision 1.15 2003/06/24 20:21:12 jonen |
18 |
## + changed linux part of run_cmd to use Proc::Background instead of IPC::... |
19 |
## |
20 |
## Revision 1.14 2003/06/24 20:13:18 joko |
21 |
## + sub findpatch |
22 |
## + now using findpatch and Proc::Background for win32/perl |
23 |
## |
24 |
## Revision 1.13 2003/06/23 20:58:31 joko |
25 |
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
26 |
## |
27 |
## Revision 1.12 2003/06/23 19:43:19 joko |
28 |
## minor cleanup |
29 |
## now using IPC::Session::NoShell |
30 |
## |
31 |
## Revision 1.11 2003/06/23 17:41:50 jonen |
32 |
## + NEW - used IPC::Session instead of IPC::Run to get better results at linux |
33 |
## |
34 |
## Revision 1.10 2003/06/23 15:59:16 joko |
35 |
## major/minor fixes? |
36 |
## |
37 |
## Revision 1.9 2003/05/13 05:36:24 joko |
38 |
## heavy modifications to run_cmd |
39 |
## + sub get_executable |
40 |
## + sub get_executable_wrapper |
41 |
## |
42 |
## Revision 1.8 2003/04/04 17:31:59 joko |
43 |
## + sub make_guid |
44 |
## |
45 |
## Revision 1.7 2003/03/29 07:24:10 joko |
46 |
## enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl') |
47 |
## |
48 |
## Revision 1.6 2003/03/28 06:58:06 joko |
49 |
## new: 'run_cmd' now asynchronous! (via IPC::Run...) |
50 |
## |
51 |
## Revision 1.5 2003/02/22 17:26:13 joko |
52 |
## + enhanced unix compatibility fix |
53 |
## |
54 |
## Revision 1.4 2003/02/22 17:19:36 joko |
55 |
## + unix compatibility fix |
56 |
## |
57 |
## Revision 1.3 2003/02/14 14:17:04 joko |
58 |
## - shortened seperator |
59 |
## |
60 |
## Revision 1.2 2003/02/11 05:14:28 joko |
61 |
## + refactored code from libp.pm |
62 |
## |
63 |
## Revision 1.1 2003/02/09 04:49:45 joko |
64 |
## + shortcuts now refactored to this file |
65 |
## |
66 |
## --------------------------------------------------------------------------- |
67 |
|
68 |
|
69 |
package shortcuts; |
70 |
|
71 |
use strict; |
72 |
use warnings; |
73 |
|
74 |
require Exporter; |
75 |
our @ISA = qw( Exporter ); |
76 |
our @EXPORT_OK = qw( |
77 |
strftime |
78 |
now today |
79 |
run_cmd run_cmds |
80 |
get_chomped |
81 |
bool2status |
82 |
make_guid |
83 |
RUNNING_IN_HELL |
84 |
RUNNING_IN_HEAVEN |
85 |
); |
86 |
|
87 |
|
88 |
use Data::Dumper; |
89 |
use POSIX qw( strftime ); |
90 |
#use IPC::Run qw( run timeout ); |
91 |
use IPC::Run qw( start pump finish timeout run timer ) ; |
92 |
use Carp; |
93 |
|
94 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
95 |
#use IPC::Session; |
96 |
|
97 |
use File::Spec; |
98 |
use Proc::Background; |
99 |
|
100 |
|
101 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
102 |
# see "perldoc -f localtime" |
103 |
sub now { |
104 |
my $options = shift; |
105 |
my $pattern = "%Y-%m-%d %H:%M:%S"; |
106 |
$pattern = "%Y-%m-%d_%H-%M-%S" if $options->{fs}; |
107 |
my $result = strftime($pattern, localtime); |
108 |
return $result; |
109 |
} |
110 |
|
111 |
sub today { |
112 |
return strftime("%Y-%m-%d", localtime); |
113 |
} |
114 |
|
115 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
116 |
sub RUNNING_IN_HEAVEN () { $^O =~ m/bsd/ } |
117 |
|
118 |
|
119 |
sub get_executable { |
120 |
my $cmd = shift; |
121 |
# FIXME: detect type of program and run with proper application/interpreter |
122 |
# using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!? |
123 |
# => better use absolute path-names only?! |
124 |
my $application = ''; |
125 |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
126 |
$application = get_interpreter_wrapper($cmd, 'perl'); |
127 |
#$cmd = "$application $cmd" if $application; |
128 |
$application .= ' '; |
129 |
|
130 |
} else { |
131 |
$application = './'; |
132 |
} |
133 |
return $application; |
134 |
} |
135 |
|
136 |
sub get_interpreter_wrapper { |
137 |
my $cmd = shift; |
138 |
my $language = shift; |
139 |
$language ||= ''; |
140 |
|
141 |
my $wrapper = ''; |
142 |
|
143 |
if ($language eq 'perl') { |
144 |
|
145 |
if (RUNNING_IN_HELL()) { |
146 |
# Required to adapt to IPC::Run on win32. |
147 |
#$wrapper = 'cmd.exe /C perl'; |
148 |
#$wrapper = 'start perl'; |
149 |
$wrapper = 'perl'; |
150 |
#$wrapper = 'cmd.exe /C'; |
151 |
} else { |
152 |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
153 |
# whats about Win32? |
154 |
$wrapper = 'perl'; |
155 |
} |
156 |
|
157 |
} else { |
158 |
die("No wrapper for language '$language'."); |
159 |
} |
160 |
|
161 |
return $wrapper; |
162 |
} |
163 |
|
164 |
|
165 |
sub run_cmd { |
166 |
my $cmd = shift; |
167 |
my $caption = shift; |
168 |
my $options = shift; |
169 |
|
170 |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
171 |
|
172 |
# report - header |
173 |
my $sep = "-" x 60; |
174 |
print STDOUT $sep, "\n"; |
175 |
print STDOUT " ", $cmd; |
176 |
print STDOUT " - ", $caption if $caption; |
177 |
print STDOUT "\n", $sep, "\n"; |
178 |
|
179 |
# strip name of executable from full command string |
180 |
$cmd =~ m/^(.+?)\s(.*)$/; |
181 |
my $executable = $1; |
182 |
my $executable_args = $2; |
183 |
|
184 |
=pod |
185 |
# for unix: check if executable is in local directory, if so - prefix with './' |
186 |
if (!RUNNING_IN_HELL()) { |
187 |
#if ($cmd !~ m/\//) { |
188 |
if (-e $executable) { |
189 |
} |
190 |
} |
191 |
=cut |
192 |
|
193 |
# new of 2003-05-07: basedir option to be prepended to command string |
194 |
my $basedir = $options->{BASEDIR}; |
195 |
my $use_path = $options->{USE_PATH}; |
196 |
|
197 |
# for all systems: check existance of files - use basedir if given, try current directory otherwise |
198 |
if ($basedir) { |
199 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
200 |
$basedir .= '/'; |
201 |
$cmd = "$basedir$cmd"; |
202 |
} elsif ($use_path) { |
203 |
#$basedir = ""; |
204 |
$basedir = findpath($executable); |
205 |
#print "basedir: $basedir", "\n"; |
206 |
my $abspath = File::Spec->catfile($basedir, $executable); |
207 |
#print STDOUT "PATH: ", $abspath, "\n"; |
208 |
-e $abspath or die("$abspath does not exist."); |
209 |
$cmd = $abspath . ' ' . $executable_args; |
210 |
} else { |
211 |
-e $executable or die("$executable does not exist."); |
212 |
#$basedir = "."; |
213 |
#$basedir .= './'; |
214 |
$basedir = ""; |
215 |
$cmd = "$basedir$cmd"; |
216 |
} |
217 |
|
218 |
# V1 - backticks or qq{} |
219 |
#`$cmd`; |
220 |
#qq{$cmd}; |
221 |
|
222 |
# V2 - via 'system' |
223 |
#system($cmd); |
224 |
|
225 |
#if (not $use_path) { |
226 |
my $application = get_executable($cmd); |
227 |
$cmd = "$application$cmd" if $application; |
228 |
#} |
229 |
|
230 |
my @cmd = split(' ', $cmd); |
231 |
|
232 |
# V3 - using IPC (optional) |
233 |
if ($options->{async}) { |
234 |
|
235 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
236 |
|
237 |
print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; |
238 |
|
239 |
# V3.1 - using IPC::Run |
240 |
# |
241 |
# tests: |
242 |
|
243 |
if (RUNNING_IN_HELL()) { |
244 |
|
245 |
#my $in; my $out; my $err; |
246 |
|
247 |
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
248 |
|
249 |
# no success! |
250 |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
251 |
# |
252 |
# success on Win32, but seems broken at 'timeout' on linux: |
253 |
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
254 |
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
255 |
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
256 |
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
257 |
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
258 |
|
259 |
# other tests ;) |
260 |
#$IPC::Run::Timer::timeout = 2000; |
261 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
262 |
|
263 |
#my $in; my $out; my $t; |
264 |
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
265 |
#my $harness = start( \@cmd ) ; |
266 |
#$harness->pump_nb(); |
267 |
|
268 |
#my $postfix = '2>&1 |'; |
269 |
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
270 |
|
271 |
my $proc1 = Proc::Background->new(@cmd); |
272 |
print "pid: ", $proc1->pid(), "\n"; |
273 |
|
274 |
|
275 |
} else { |
276 |
|
277 |
#print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
278 |
|
279 |
# V3.2 - using IPC::Session |
280 |
# success on Linux AND Win32 ?? |
281 |
# |
282 |
# set timeout: |
283 |
# (don't really know why we needs some secconds |
284 |
# to wait for init of process !?!) |
285 |
#my $session_timeout = 15; |
286 |
# set session name (default: cmd as string): |
287 |
#my $session_command = $cmd; |
288 |
#my $session_shell = "/bin/sh"; |
289 |
# create session (beware of using '->new' here!?): |
290 |
#my $session = new IPC::Session($session_shell, $session_timeout); |
291 |
|
292 |
# send 'cmd' to session - not required since complete command is sent via constructor above |
293 |
#$session->send($cmd); |
294 |
|
295 |
#my $output = $session->stdout(); |
296 |
#print "WS::Admin started.\n" if $output = ' '; |
297 |
|
298 |
# tests |
299 |
#$session->send("echo hello"); |
300 |
#chomp(my $hello = $session->stdout()); |
301 |
#print "ok 3\n" if $hello eq "hello"; |
302 |
|
303 |
# optional switch case: |
304 |
#for ($session->stdout()) { |
305 |
# /_bootDataBases/ && do { print "WS::Admin started.\n" }; |
306 |
#} |
307 |
# optional get error: |
308 |
#my $err = session->stderr(); |
309 |
#print "ERR: " . Dumper($err) . "\n"; |
310 |
|
311 |
#open( *OUT, ">out.txt" ) ; |
312 |
#open( *ERR, ">err.txt" ) ; |
313 |
#run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
314 |
#my ($out, $err); |
315 |
#my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
316 |
#finish $h if $err =~ /error/; |
317 |
|
318 |
# get child pid |
319 |
#my $kid; |
320 |
#do { $kid=wait(); } until $kid > 0; |
321 |
#print "Child PID: " . $kid . "n"; |
322 |
|
323 |
#finish $h; |
324 |
|
325 |
|
326 |
# test using Proc::Background - success !! |
327 |
my $proc = Proc::Background->new($cmd); |
328 |
my $kid = $proc->pid(); |
329 |
print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; |
330 |
$proc->wait(); |
331 |
$proc->die(); |
332 |
|
333 |
} |
334 |
|
335 |
|
336 |
} elsif ($options->{detach}) { |
337 |
|
338 |
if (RUNNING_IN_HELL()) { |
339 |
print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; |
340 |
my $proc1 = Proc::Background->new(@cmd); |
341 |
print "pid: ", $proc1->pid(), "\n"; |
342 |
} else { |
343 |
print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; |
344 |
system($cmd . ' &'); |
345 |
} |
346 |
|
347 |
} else { |
348 |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
349 |
#print $ENV{PERL5LIB}, "\n"; |
350 |
system($cmd); |
351 |
} |
352 |
|
353 |
print STDOUT "run_cmd: ready.", "\n"; |
354 |
|
355 |
} |
356 |
|
357 |
sub run_cmds { |
358 |
my $options = {}; |
359 |
if (ref $_[$#_] eq 'HASH') { |
360 |
#print "YAI", "\n"; |
361 |
$options = pop @_; |
362 |
} |
363 |
foreach (@_) { |
364 |
run_cmd($_, '', $options); |
365 |
} |
366 |
} |
367 |
|
368 |
sub get_chomped { |
369 |
my $str = shift; |
370 |
chomp($str); |
371 |
return $str; |
372 |
} |
373 |
|
374 |
sub bool2status { |
375 |
my $bool = shift; |
376 |
return ($bool ? 'ok' : 'failed'); |
377 |
} |
378 |
|
379 |
# create global unique identifers using Data::UUID |
380 |
# if updating this code, please also modify Tangram::Storage::make_guid |
381 |
sub make_guid |
382 |
{ |
383 |
my $self = shift; |
384 |
|
385 |
my $guid; |
386 |
|
387 |
# try to use Data::UUID first ... |
388 |
eval("use Data::UUID;"); |
389 |
if (!$@) { |
390 |
my $ug = Data::UUID->new(); |
391 |
$guid = $ug->create_str(); |
392 |
|
393 |
# ... if this fails, try to fallback to Data::UUID::PurePerl instead ... |
394 |
} else { |
395 |
eval("use Data::UUID::PurePerl;"); |
396 |
if (!$@) { |
397 |
$guid = Data::UUID::PurePerl::generate_id(); |
398 |
} else { |
399 |
croak "couldn't create globally unique identifier"; |
400 |
} |
401 |
} |
402 |
|
403 |
return $guid; |
404 |
} |
405 |
|
406 |
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
407 |
sub findpath { |
408 |
#local($path); |
409 |
#local($arg) = shift; |
410 |
my $arg = shift; |
411 |
my $path; |
412 |
#for my $dir (split(/:/,$ENV{'PATH'})) { |
413 |
for my $dir (File::Spec->path()) { |
414 |
#if (-x "$dir/$arg" && -f _) { |
415 |
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
416 |
if (-e "$dir/$arg") { |
417 |
#$path = "$dir/$arg"; |
418 |
#$path = "$dir/"; |
419 |
$path = $dir; |
420 |
last; |
421 |
} |
422 |
} |
423 |
$path; |
424 |
} |
425 |
|
426 |
1; |
427 |
__END__ |