| 2 |
## $Id$ |
## $Id$ |
| 3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
| 4 |
## $Log$ |
## $Log$ |
| 5 |
|
## Revision 1.15 2003/06/24 20:21:12 jonen |
| 6 |
|
## + changed linux part of run_cmd to use Proc::Background instead of IPC::... |
| 7 |
|
## |
| 8 |
|
## Revision 1.14 2003/06/24 20:13:18 joko |
| 9 |
|
## + sub findpatch |
| 10 |
|
## + now using findpatch and Proc::Background for win32/perl |
| 11 |
|
## |
| 12 |
|
## Revision 1.13 2003/06/23 20:58:31 joko |
| 13 |
|
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
| 14 |
|
## |
| 15 |
|
## Revision 1.12 2003/06/23 19:43:19 joko |
| 16 |
|
## minor cleanup |
| 17 |
|
## now using IPC::Session::NoShell |
| 18 |
|
## |
| 19 |
## Revision 1.11 2003/06/23 17:41:50 jonen |
## Revision 1.11 2003/06/23 17:41:50 jonen |
| 20 |
## + NEW - used IPC::Session instead of IPC::Run to get better results at linux |
## + NEW - used IPC::Session instead of IPC::Run to get better results at linux |
| 21 |
## |
## |
| 74 |
use Data::Dumper; |
use Data::Dumper; |
| 75 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
| 76 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
| 77 |
use IPC::Run qw( start pump finish timeout run ) ; |
use IPC::Run qw( start pump finish timeout run timer ) ; |
| 78 |
use Carp; |
use Carp; |
| 79 |
|
|
| 80 |
# NEW - 2003-06-23 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
| 81 |
use IPC::Session; |
use IPC::Session; |
| 82 |
|
|
| 83 |
|
use File::Spec; |
| 84 |
|
use Proc::Background; |
| 85 |
|
|
| 86 |
|
|
| 87 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
| 88 |
# see "perldoc -f localtime" |
# see "perldoc -f localtime" |
| 98 |
return strftime("%Y-%m-%d", localtime); |
return strftime("%Y-%m-%d", localtime); |
| 99 |
} |
} |
| 100 |
|
|
| 101 |
|
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
| 102 |
|
|
| 103 |
|
|
| 104 |
sub get_executable { |
sub get_executable { |
| 105 |
my $cmd = shift; |
my $cmd = shift; |
| 106 |
# FIXME: detect type of program and run with proper application/interpreter |
# FIXME: detect type of program and run with proper application/interpreter |
| 108 |
# => better use absolute path-names only?! |
# => better use absolute path-names only?! |
| 109 |
my $application = ''; |
my $application = ''; |
| 110 |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
if ($cmd =~ m/\w+\.pl\s*.*/) { |
| 111 |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
$application = get_interpreter_wrapper($cmd, 'perl'); |
| 112 |
# whats about Win32? |
#$cmd = "$application $cmd" if $application; |
| 113 |
$application = 'perl '; |
$application .= ' '; |
| 114 |
|
|
| 115 |
} else { |
} else { |
| 116 |
$application = './'; |
$application = './'; |
| 117 |
} |
} |
| 118 |
return $application; |
return $application; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
sub get_executable_wrapper { |
sub get_interpreter_wrapper { |
| 122 |
my $cmd = shift; |
my $cmd = shift; |
| 123 |
my $application = ''; |
my $language = shift; |
| 124 |
# Required to adapt to IPC::Run on win32. |
$language ||= ''; |
| 125 |
#if (RUNNING_IN_HELL()) { |
|
| 126 |
#$application = 'cmd.exe /C'; |
my $wrapper = ''; |
| 127 |
#} |
|
| 128 |
return $application; |
if ($language eq 'perl') { |
| 129 |
|
|
| 130 |
|
if (RUNNING_IN_HELL()) { |
| 131 |
|
# Required to adapt to IPC::Run on win32. |
| 132 |
|
#$wrapper = 'cmd.exe /C perl'; |
| 133 |
|
#$wrapper = 'start perl'; |
| 134 |
|
$wrapper = 'perl'; |
| 135 |
|
#$wrapper = 'cmd.exe /C'; |
| 136 |
|
} else { |
| 137 |
|
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
| 138 |
|
# whats about Win32? |
| 139 |
|
$wrapper = 'perl'; |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
} else { |
| 143 |
|
die("No wrapper for language '$language'."); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
return $wrapper; |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
|
|
| 151 |
my $cmd = shift; |
my $cmd = shift; |
| 152 |
my $caption = shift; |
my $caption = shift; |
| 153 |
my $options = shift; |
my $options = shift; |
|
#$cmd = 'perl ' . $cmd; |
|
| 154 |
|
|
| 155 |
#print Dumper($options); |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
| 156 |
|
|
| 157 |
# report - header |
# report - header |
| 158 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
| 159 |
print $sep, "\n"; |
print STDOUT $sep, "\n"; |
| 160 |
#print " ", $cmd, "\n"; |
print STDOUT " ", $cmd; |
| 161 |
#print " ", " $caption", "\n" if $caption; |
print STDOUT " - ", $caption if $caption; |
| 162 |
print " ", $cmd; |
print STDOUT "\n", $sep, "\n"; |
|
print " - ", $caption if $caption; |
|
|
print "\n"; |
|
|
print $sep, "\n"; |
|
| 163 |
|
|
| 164 |
# strip name of executable from full command string |
# strip name of executable from full command string |
| 165 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/^(.+?)\s(.*)$/; |
| 166 |
my $executable = $1; |
my $executable = $1; |
| 167 |
|
my $executable_args = $2; |
| 168 |
|
|
| 169 |
=pod |
=pod |
| 170 |
# 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 './' |
| 183 |
if ($basedir) { |
if ($basedir) { |
| 184 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
| 185 |
$basedir .= '/'; |
$basedir .= '/'; |
| 186 |
|
$cmd = "$basedir$cmd"; |
| 187 |
} elsif ($use_path) { |
} elsif ($use_path) { |
| 188 |
$basedir = ""; |
#$basedir = ""; |
| 189 |
|
$basedir = findpath($executable); |
| 190 |
|
#print "basedir: $basedir", "\n"; |
| 191 |
|
my $abspath = File::Spec->catfile($basedir, $executable); |
| 192 |
|
#print STDOUT "PATH: ", $abspath, "\n"; |
| 193 |
|
-e $abspath or die("$abspath does not exist."); |
| 194 |
|
$cmd = $abspath . ' ' . $executable_args; |
| 195 |
} else { |
} else { |
| 196 |
-e $executable or die("$executable does not exist."); |
-e $executable or die("$executable does not exist."); |
| 197 |
#$basedir = "."; |
#$basedir = "."; |
| 198 |
#$basedir .= './'; |
#$basedir .= './'; |
| 199 |
|
$basedir = ""; |
| 200 |
|
$cmd = "$basedir$cmd"; |
| 201 |
} |
} |
|
$cmd = "$basedir$cmd"; |
|
| 202 |
|
|
| 203 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
| 204 |
#`$cmd`; |
#`$cmd`; |
| 207 |
# V2 - via 'system' |
# V2 - via 'system' |
| 208 |
#system($cmd); |
#system($cmd); |
| 209 |
|
|
| 210 |
if (not $use_path) { |
#if (not $use_path) { |
| 211 |
my $application = get_executable($cmd); |
my $application = get_executable($cmd); |
| 212 |
$cmd = "$application$cmd" if $application; |
$cmd = "$application$cmd" if $application; |
| 213 |
} |
#} |
| 214 |
|
|
| 215 |
# V3 - using IPC (optional) |
# V3 - using IPC (optional) |
| 216 |
if ($options->{async}) { |
if ($options->{async}) { |
| 217 |
|
|
|
#$cmd = "$application $cmd" if $application; |
|
|
#my $application = get_executable_wrapper($cmd); |
|
|
|
|
|
print "run_cmd: IPC::Run: $cmd", "\n"; |
|
| 218 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
|
|
|
| 219 |
my @cmd = split(' ', $cmd); |
my @cmd = split(' ', $cmd); |
| 220 |
|
|
| 221 |
|
|
| 222 |
# V3.1 - using IPC::Run |
# V3.1 - using IPC::Run |
| 223 |
# |
# |
| 224 |
# tests: |
# tests: |
| 225 |
#my $in; my $out; my $err; |
|
| 226 |
#print "IPC::Run: $cmd", "\n"; |
if (RUNNING_IN_HELL()) { |
| 227 |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
| 228 |
# |
#my $in; my $out; my $err; |
| 229 |
# success on Win32, but seems broken at 'timeout' on linux: |
print STDOUT "run_cmd: IPC::Run: $cmd", "\n"; |
| 230 |
#run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
|
| 231 |
|
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
| 232 |
# other tests ;) |
|
| 233 |
#$IPC::Run::Timer::timeout = 2000; |
# no success! |
| 234 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 235 |
|
# |
| 236 |
|
# success on Win32, but seems broken at 'timeout' on linux: |
| 237 |
|
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 238 |
|
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 239 |
|
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 240 |
|
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 241 |
|
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 242 |
|
|
| 243 |
|
# other tests ;) |
| 244 |
|
#$IPC::Run::Timer::timeout = 2000; |
| 245 |
|
#start $cmd or die("IPC::Run could not start '$cmd'."); |
| 246 |
|
|
| 247 |
|
#my $in; my $out; my $t; |
| 248 |
|
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
| 249 |
|
#my $harness = start( \@cmd ) ; |
| 250 |
|
#$harness->pump_nb(); |
| 251 |
|
|
| 252 |
|
#my $postfix = '2>&1 |'; |
| 253 |
|
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
| 254 |
|
|
| 255 |
|
my $proc1 = Proc::Background->new(@cmd); |
| 256 |
|
print "pid: ", $proc1->pid(), "\n"; |
| 257 |
|
|
| 258 |
|
|
| 259 |
|
} else { |
| 260 |
|
|
| 261 |
# V3.2 - using IPC::Session |
#print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
| 262 |
# success on Linux AND Win32 ?? |
|
| 263 |
# |
# V3.2 - using IPC::Session |
| 264 |
# set timeout: |
# success on Linux AND Win32 ?? |
| 265 |
# (don't really know why we needs 2 secconds |
# |
| 266 |
# to wait for init of process !?!) |
# set timeout: |
| 267 |
my $session_timeout = 2; |
# (don't really know why we needs some secconds |
| 268 |
# set session name (default: cmd as string): |
# to wait for init of process !?!) |
| 269 |
my $session_name = $cmd; |
#my $session_timeout = 15; |
| 270 |
# create session: |
# set session name (default: cmd as string): |
| 271 |
my $session = new IPC::Session($session_name, $session_timeout); |
#my $session_command = $cmd; |
| 272 |
# send 'cmd' to session: |
#my $session_shell = "/bin/sh"; |
| 273 |
$session->send(\@cmd); |
# create session (beware of using '->new' here!?): |
| 274 |
# optional switch case: |
#my $session = new IPC::Session($session_shell, $session_timeout); |
| 275 |
#for ($session->stdout()) { |
|
| 276 |
#} |
# send 'cmd' to session - not required since complete command is sent via constructor above |
| 277 |
# optional get error: |
#$session->send($cmd); |
| 278 |
#my $err = session->stderr(); |
|
| 279 |
|
#my $output = $session->stdout(); |
| 280 |
|
#print "WS::Admin started.\n" if $output = ' '; |
| 281 |
|
|
| 282 |
|
# tests |
| 283 |
|
#$session->send("echo hello"); |
| 284 |
|
#chomp(my $hello = $session->stdout()); |
| 285 |
|
#print "ok 3\n" if $hello eq "hello"; |
| 286 |
|
|
| 287 |
|
# optional switch case: |
| 288 |
|
#for ($session->stdout()) { |
| 289 |
|
# /_bootDataBases/ && do { print "WS::Admin started.\n" }; |
| 290 |
|
#} |
| 291 |
|
# optional get error: |
| 292 |
|
#my $err = session->stderr(); |
| 293 |
|
#print "ERR: " . Dumper($err) . "\n"; |
| 294 |
|
|
| 295 |
|
#open( *OUT, ">out.txt" ) ; |
| 296 |
|
#open( *ERR, ">err.txt" ) ; |
| 297 |
|
#run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 298 |
|
#my ($out, $err); |
| 299 |
|
#my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
| 300 |
|
#finish $h if $err =~ /error/; |
| 301 |
|
|
| 302 |
|
# get child pid |
| 303 |
|
#my $kid; |
| 304 |
|
#do { $kid=wait(); } until $kid > 0; |
| 305 |
|
#print "Child PID: " . $kid . "n"; |
| 306 |
|
|
| 307 |
|
#finish $h; |
| 308 |
|
|
| 309 |
|
|
| 310 |
|
# test using Proc::Background - success !! |
| 311 |
|
my $proc = Proc::Background->new($cmd); |
| 312 |
|
my $kid = $proc->pid(); |
| 313 |
|
print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; |
| 314 |
|
$proc->wait(); |
| 315 |
|
$proc->die(); |
| 316 |
|
|
| 317 |
|
} |
| 318 |
|
|
| 319 |
|
|
| 320 |
|
} elsif ($options->{detach}) { |
| 321 |
|
print STDERR "run_cmd: system('$cmd' &).", "\n"; |
| 322 |
|
system($cmd . ' &'); |
| 323 |
|
|
|
|
|
| 324 |
} else { |
} else { |
| 325 |
print "run_cmd: system('$cmd').", "\n"; |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
| 326 |
system($cmd); |
system($cmd); |
| 327 |
} |
} |
| 328 |
|
|
| 329 |
print "run_cmd: ready.", "\n"; |
print STDOUT "run_cmd: ready.", "\n"; |
| 330 |
|
|
| 331 |
} |
} |
| 332 |
|
|
| 352 |
return ($bool ? 'ok' : 'failed'); |
return ($bool ? 'ok' : 'failed'); |
| 353 |
} |
} |
| 354 |
|
|
|
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
|
|
|
|
| 355 |
# create global unique identifers using Data::UUID |
# create global unique identifers using Data::UUID |
| 356 |
# if updating this code, please also modify Tangram::Storage::make_guid |
# if updating this code, please also modify Tangram::Storage::make_guid |
| 357 |
sub make_guid |
sub make_guid |
| 379 |
return $guid; |
return $guid; |
| 380 |
} |
} |
| 381 |
|
|
| 382 |
|
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
| 383 |
|
sub findpath { |
| 384 |
|
#local($path); |
| 385 |
|
#local($arg) = shift; |
| 386 |
|
my $arg = shift; |
| 387 |
|
my $path; |
| 388 |
|
#for my $dir (split(/:/,$ENV{'PATH'})) { |
| 389 |
|
for my $dir (File::Spec->path()) { |
| 390 |
|
#if (-x "$dir/$arg" && -f _) { |
| 391 |
|
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
| 392 |
|
if (-e "$dir/$arg") { |
| 393 |
|
#$path = "$dir/$arg"; |
| 394 |
|
#$path = "$dir/"; |
| 395 |
|
$path = $dir; |
| 396 |
|
last; |
| 397 |
|
} |
| 398 |
|
} |
| 399 |
|
$path; |
| 400 |
|
} |
| 401 |
|
|
| 402 |
1; |
1; |
| 403 |
|
__END__ |