2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.18 2003/12/05 04:58:04 joko |
6 |
|
## + minor update: doesn't require IPC::Session anymore |
7 |
|
## |
8 |
|
## Revision 1.17 2003/07/02 11:17:32 jonen |
9 |
|
## minor changes |
10 |
|
## |
11 |
|
## Revision 1.16 2003/06/25 22:49:56 joko |
12 |
|
## RUNNING_IN_HELL mode for detach option |
13 |
|
## |
14 |
|
## Revision 1.15 2003/06/24 20:21:12 jonen |
15 |
|
## + changed linux part of run_cmd to use Proc::Background instead of IPC::... |
16 |
|
## |
17 |
|
## Revision 1.14 2003/06/24 20:13:18 joko |
18 |
|
## + sub findpatch |
19 |
|
## + now using findpatch and Proc::Background for win32/perl |
20 |
|
## |
21 |
|
## Revision 1.13 2003/06/23 20:58:31 joko |
22 |
|
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
23 |
|
## |
24 |
|
## Revision 1.12 2003/06/23 19:43:19 joko |
25 |
|
## minor cleanup |
26 |
|
## now using IPC::Session::NoShell |
27 |
|
## |
28 |
|
## Revision 1.11 2003/06/23 17:41:50 jonen |
29 |
|
## + NEW - used IPC::Session instead of IPC::Run to get better results at linux |
30 |
|
## |
31 |
## Revision 1.10 2003/06/23 15:59:16 joko |
## Revision 1.10 2003/06/23 15:59:16 joko |
32 |
## major/minor fixes? |
## major/minor fixes? |
33 |
## |
## |
83 |
use Data::Dumper; |
use Data::Dumper; |
84 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
85 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
86 |
use IPC::Run qw( start pump finish timeout run ) ; |
use IPC::Run qw( start pump finish timeout run timer ) ; |
87 |
use Carp; |
use Carp; |
88 |
|
|
89 |
|
# NEW - 2003-06-23 for Linux (what about *BSD?) |
90 |
|
#use IPC::Session; |
91 |
|
|
92 |
|
use File::Spec; |
93 |
|
use Proc::Background; |
94 |
|
|
95 |
|
|
96 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
107 |
return strftime("%Y-%m-%d", localtime); |
return strftime("%Y-%m-%d", localtime); |
108 |
} |
} |
109 |
|
|
110 |
|
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
111 |
|
|
112 |
|
|
113 |
sub get_executable { |
sub get_executable { |
114 |
my $cmd = shift; |
my $cmd = shift; |
115 |
# FIXME: detect type of program and run with proper application/interpreter |
# FIXME: detect type of program and run with proper application/interpreter |
117 |
# => better use absolute path-names only?! |
# => better use absolute path-names only?! |
118 |
my $application = ''; |
my $application = ''; |
119 |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
120 |
$application = 'perl '; |
$application = get_interpreter_wrapper($cmd, 'perl'); |
121 |
|
#$cmd = "$application $cmd" if $application; |
122 |
|
$application .= ' '; |
123 |
|
|
124 |
} else { |
} else { |
125 |
$application = './'; |
$application = './'; |
126 |
} |
} |
127 |
return $application; |
return $application; |
128 |
} |
} |
129 |
|
|
130 |
sub get_executable_wrapper { |
sub get_interpreter_wrapper { |
131 |
my $cmd = shift; |
my $cmd = shift; |
132 |
my $application = ''; |
my $language = shift; |
133 |
# Required to adapt to IPC::Run on win32. |
$language ||= ''; |
134 |
if (RUNNING_IN_HELL()) { |
|
135 |
#$application = 'cmd.exe /C'; |
my $wrapper = ''; |
136 |
$application = 'cmd.exe /C'; |
|
137 |
|
if ($language eq 'perl') { |
138 |
|
|
139 |
|
if (RUNNING_IN_HELL()) { |
140 |
|
# Required to adapt to IPC::Run on win32. |
141 |
|
#$wrapper = 'cmd.exe /C perl'; |
142 |
|
#$wrapper = 'start perl'; |
143 |
|
$wrapper = 'perl'; |
144 |
|
#$wrapper = 'cmd.exe /C'; |
145 |
|
} else { |
146 |
|
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
147 |
|
# whats about Win32? |
148 |
|
$wrapper = 'perl'; |
149 |
|
} |
150 |
|
|
151 |
|
} else { |
152 |
|
die("No wrapper for language '$language'."); |
153 |
} |
} |
154 |
return $application; |
|
155 |
|
return $wrapper; |
156 |
} |
} |
157 |
|
|
158 |
|
|
160 |
my $cmd = shift; |
my $cmd = shift; |
161 |
my $caption = shift; |
my $caption = shift; |
162 |
my $options = shift; |
my $options = shift; |
|
#$cmd = 'perl ' . $cmd; |
|
163 |
|
|
164 |
#print Dumper($options); |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
165 |
|
|
166 |
# report - header |
# report - header |
167 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
168 |
print $sep, "\n"; |
print STDOUT $sep, "\n"; |
169 |
#print " ", $cmd, "\n"; |
print STDOUT " ", $cmd; |
170 |
#print " ", " $caption", "\n" if $caption; |
print STDOUT " - ", $caption if $caption; |
171 |
print " ", $cmd; |
print STDOUT "\n", $sep, "\n"; |
|
print " - ", $caption if $caption; |
|
|
print "\n"; |
|
|
print $sep, "\n"; |
|
172 |
|
|
173 |
# strip name of executable from full command string |
# strip name of executable from full command string |
174 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/^(.+?)\s(.*)$/; |
175 |
my $executable = $1; |
my $executable = $1; |
176 |
|
my $executable_args = $2; |
177 |
|
|
178 |
=pod |
=pod |
179 |
# for unix: check if executable is in local directory, if so - prefix with './' |
# for unix: check if executable is in local directory, if so - prefix with './' |
192 |
if ($basedir) { |
if ($basedir) { |
193 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
194 |
$basedir .= '/'; |
$basedir .= '/'; |
195 |
|
$cmd = "$basedir$cmd"; |
196 |
} elsif ($use_path) { |
} elsif ($use_path) { |
197 |
$basedir = ""; |
#$basedir = ""; |
198 |
|
$basedir = findpath($executable); |
199 |
|
#print "basedir: $basedir", "\n"; |
200 |
|
my $abspath = File::Spec->catfile($basedir, $executable); |
201 |
|
#print STDOUT "PATH: ", $abspath, "\n"; |
202 |
|
-e $abspath or die("$abspath does not exist."); |
203 |
|
$cmd = $abspath . ' ' . $executable_args; |
204 |
} else { |
} else { |
205 |
-e $executable or die("$executable does not exist."); |
-e $executable or die("$executable does not exist."); |
206 |
#$basedir = "."; |
#$basedir = "."; |
207 |
#$basedir .= './'; |
#$basedir .= './'; |
208 |
|
$basedir = ""; |
209 |
|
$cmd = "$basedir$cmd"; |
210 |
} |
} |
|
$cmd = "$basedir$cmd"; |
|
211 |
|
|
212 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
213 |
#`$cmd`; |
#`$cmd`; |
216 |
# V2 - via 'system' |
# V2 - via 'system' |
217 |
#system($cmd); |
#system($cmd); |
218 |
|
|
219 |
if (not $use_path) { |
#if (not $use_path) { |
220 |
my $application = get_executable($cmd); |
my $application = get_executable($cmd); |
221 |
$cmd = "$application$cmd" if $application; |
$cmd = "$application$cmd" if $application; |
222 |
} |
#} |
223 |
|
|
224 |
# V3 - using IPC::Run (optional) |
my @cmd = split(' ', $cmd); |
225 |
|
|
226 |
|
# V3 - using IPC (optional) |
227 |
if ($options->{async}) { |
if ($options->{async}) { |
228 |
my $application = get_executable_wrapper($cmd); |
|
|
$cmd = "$application $cmd" if $application; |
|
|
|
|
|
print "run_cmd: IPC::Run: $cmd", "\n"; |
|
229 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
230 |
|
|
231 |
|
print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; |
232 |
|
|
233 |
my @cmd = split(' ', $cmd); |
# V3.1 - using IPC::Run |
234 |
|
# |
235 |
my $in; my $out; my $err; |
# tests: |
|
#print "IPC::Run: $cmd", "\n"; |
|
|
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
|
run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
236 |
|
|
237 |
#$IPC::Run::Timer::timeout = 2000; |
if (RUNNING_IN_HELL()) { |
|
#start $cmd or die("IPC::Run could not start '$cmd'."); |
|
238 |
|
|
239 |
|
#my $in; my $out; my $err; |
240 |
|
|
241 |
|
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
242 |
|
|
243 |
|
# no success! |
244 |
|
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
245 |
|
# |
246 |
|
# success on Win32, but seems broken at 'timeout' on linux: |
247 |
|
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
248 |
|
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
249 |
|
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
250 |
|
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
251 |
|
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
252 |
|
|
253 |
|
# other tests ;) |
254 |
|
#$IPC::Run::Timer::timeout = 2000; |
255 |
|
#start $cmd or die("IPC::Run could not start '$cmd'."); |
256 |
|
|
257 |
|
#my $in; my $out; my $t; |
258 |
|
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
259 |
|
#my $harness = start( \@cmd ) ; |
260 |
|
#$harness->pump_nb(); |
261 |
|
|
262 |
|
#my $postfix = '2>&1 |'; |
263 |
|
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
264 |
|
|
265 |
|
my $proc1 = Proc::Background->new(@cmd); |
266 |
|
print "pid: ", $proc1->pid(), "\n"; |
267 |
|
|
268 |
|
|
269 |
|
} else { |
270 |
|
|
271 |
|
#print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
272 |
|
|
273 |
|
# V3.2 - using IPC::Session |
274 |
|
# success on Linux AND Win32 ?? |
275 |
|
# |
276 |
|
# set timeout: |
277 |
|
# (don't really know why we needs some secconds |
278 |
|
# to wait for init of process !?!) |
279 |
|
#my $session_timeout = 15; |
280 |
|
# set session name (default: cmd as string): |
281 |
|
#my $session_command = $cmd; |
282 |
|
#my $session_shell = "/bin/sh"; |
283 |
|
# create session (beware of using '->new' here!?): |
284 |
|
#my $session = new IPC::Session($session_shell, $session_timeout); |
285 |
|
|
286 |
|
# send 'cmd' to session - not required since complete command is sent via constructor above |
287 |
|
#$session->send($cmd); |
288 |
|
|
289 |
|
#my $output = $session->stdout(); |
290 |
|
#print "WS::Admin started.\n" if $output = ' '; |
291 |
|
|
292 |
|
# tests |
293 |
|
#$session->send("echo hello"); |
294 |
|
#chomp(my $hello = $session->stdout()); |
295 |
|
#print "ok 3\n" if $hello eq "hello"; |
296 |
|
|
297 |
|
# optional switch case: |
298 |
|
#for ($session->stdout()) { |
299 |
|
# /_bootDataBases/ && do { print "WS::Admin started.\n" }; |
300 |
|
#} |
301 |
|
# optional get error: |
302 |
|
#my $err = session->stderr(); |
303 |
|
#print "ERR: " . Dumper($err) . "\n"; |
304 |
|
|
305 |
|
#open( *OUT, ">out.txt" ) ; |
306 |
|
#open( *ERR, ">err.txt" ) ; |
307 |
|
#run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
308 |
|
#my ($out, $err); |
309 |
|
#my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
310 |
|
#finish $h if $err =~ /error/; |
311 |
|
|
312 |
|
# get child pid |
313 |
|
#my $kid; |
314 |
|
#do { $kid=wait(); } until $kid > 0; |
315 |
|
#print "Child PID: " . $kid . "n"; |
316 |
|
|
317 |
|
#finish $h; |
318 |
|
|
319 |
|
|
320 |
|
# test using Proc::Background - success !! |
321 |
|
my $proc = Proc::Background->new($cmd); |
322 |
|
my $kid = $proc->pid(); |
323 |
|
print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; |
324 |
|
$proc->wait(); |
325 |
|
$proc->die(); |
326 |
|
|
327 |
|
} |
328 |
|
|
329 |
|
|
330 |
|
} elsif ($options->{detach}) { |
331 |
|
|
332 |
|
if (RUNNING_IN_HELL()) { |
333 |
|
print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; |
334 |
|
my $proc1 = Proc::Background->new(@cmd); |
335 |
|
print "pid: ", $proc1->pid(), "\n"; |
336 |
|
} else { |
337 |
|
print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; |
338 |
|
system($cmd . ' &'); |
339 |
|
} |
340 |
|
|
341 |
} else { |
} else { |
342 |
print "run_cmd: system('$cmd').", "\n"; |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
343 |
|
#print $ENV{PERL5LIB}, "\n"; |
344 |
system($cmd); |
system($cmd); |
345 |
} |
} |
346 |
|
|
347 |
print "run_cmd: ready.", "\n"; |
print STDOUT "run_cmd: ready.", "\n"; |
348 |
|
|
349 |
} |
} |
350 |
|
|
370 |
return ($bool ? 'ok' : 'failed'); |
return ($bool ? 'ok' : 'failed'); |
371 |
} |
} |
372 |
|
|
|
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
|
|
|
|
373 |
# create global unique identifers using Data::UUID |
# create global unique identifers using Data::UUID |
374 |
# if updating this code, please also modify Tangram::Storage::make_guid |
# if updating this code, please also modify Tangram::Storage::make_guid |
375 |
sub make_guid |
sub make_guid |
397 |
return $guid; |
return $guid; |
398 |
} |
} |
399 |
|
|
400 |
|
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
401 |
|
sub findpath { |
402 |
|
#local($path); |
403 |
|
#local($arg) = shift; |
404 |
|
my $arg = shift; |
405 |
|
my $path; |
406 |
|
#for my $dir (split(/:/,$ENV{'PATH'})) { |
407 |
|
for my $dir (File::Spec->path()) { |
408 |
|
#if (-x "$dir/$arg" && -f _) { |
409 |
|
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
410 |
|
if (-e "$dir/$arg") { |
411 |
|
#$path = "$dir/$arg"; |
412 |
|
#$path = "$dir/"; |
413 |
|
$path = $dir; |
414 |
|
last; |
415 |
|
} |
416 |
|
} |
417 |
|
$path; |
418 |
|
} |
419 |
|
|
420 |
1; |
1; |
421 |
|
__END__ |