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