--- nfo/perl/libs/shortcuts.pm 2003/04/04 17:31:59 1.8 +++ nfo/perl/libs/shortcuts.pm 2003/12/05 04:58:04 1.18 @@ -1,7 +1,41 @@ ## --------------------------------------------------------------------------- -## $Id: shortcuts.pm,v 1.8 2003/04/04 17:31:59 joko Exp $ +## $Id: shortcuts.pm,v 1.18 2003/12/05 04:58:04 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: shortcuts.pm,v $ +## Revision 1.18 2003/12/05 04:58:04 joko +## + minor update: doesn't require IPC::Session anymore +## +## Revision 1.17 2003/07/02 11:17:32 jonen +## minor changes +## +## Revision 1.16 2003/06/25 22:49:56 joko +## RUNNING_IN_HELL mode for detach option +## +## Revision 1.15 2003/06/24 20:21:12 jonen +## + changed linux part of run_cmd to use Proc::Background instead of IPC::... +## +## Revision 1.14 2003/06/24 20:13:18 joko +## + sub findpatch +## + now using findpatch and Proc::Background for win32/perl +## +## Revision 1.13 2003/06/23 20:58:31 joko +## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? +## +## Revision 1.12 2003/06/23 19:43:19 joko +## minor cleanup +## now using IPC::Session::NoShell +## +## Revision 1.11 2003/06/23 17:41:50 jonen +## + NEW - used IPC::Session instead of IPC::Run to get better results at linux +## +## Revision 1.10 2003/06/23 15:59:16 joko +## major/minor fixes? +## +## Revision 1.9 2003/05/13 05:36:24 joko +## heavy modifications to run_cmd +## + sub get_executable +## + sub get_executable_wrapper +## ## Revision 1.8 2003/04/04 17:31:59 joko ## + sub make_guid ## @@ -49,9 +83,14 @@ use Data::Dumper; use POSIX qw( strftime ); #use IPC::Run qw( run timeout ); -use IPC::Run qw( start pump finish timeout ) ; +use IPC::Run qw( start pump finish timeout run timer ) ; use Carp; +# NEW - 2003-06-23 for Linux (what about *BSD?) +#use IPC::Session; + +use File::Spec; +use Proc::Background; # $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; @@ -68,34 +107,108 @@ return strftime("%Y-%m-%d", localtime); } +sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } + + +sub get_executable { + my $cmd = shift; + # FIXME: detect type of program and run with proper application/interpreter + # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!? + # => better use absolute path-names only?! + my $application = ''; + if ($cmd =~ m/\w+\.pl\s*.*/) { + $application = get_interpreter_wrapper($cmd, 'perl'); + #$cmd = "$application $cmd" if $application; + $application .= ' '; + + } else { + $application = './'; + } + return $application; +} + +sub get_interpreter_wrapper { + my $cmd = shift; + my $language = shift; + $language ||= ''; + + my $wrapper = ''; + + if ($language eq 'perl') { + + if (RUNNING_IN_HELL()) { + # Required to adapt to IPC::Run on win32. + #$wrapper = 'cmd.exe /C perl'; + #$wrapper = 'start perl'; + $wrapper = 'perl'; + #$wrapper = 'cmd.exe /C'; + } else { + # NEW 2003-06-23 - needed if used with IPC::Session (at Linux) + # whats about Win32? + $wrapper = 'perl'; + } + + } else { + die("No wrapper for language '$language'."); + } + + return $wrapper; +} + + sub run_cmd { my $cmd = shift; my $caption = shift; my $options = shift; - #$cmd = 'perl ' . $cmd; + + #print STDOUT "run_cmd - options: ", Dumper($options), "\n"; # report - header my $sep = "-" x 60; - print $sep, "\n"; - #print " ", $cmd, "\n"; - #print " ", " $caption", "\n" if $caption; - print " ", $cmd; - print " - ", $caption if $caption; - print "\n"; - print $sep, "\n"; + print STDOUT $sep, "\n"; + print STDOUT " ", $cmd; + print STDOUT " - ", $caption if $caption; + print STDOUT "\n", $sep, "\n"; # strip name of executable from full command string - $cmd =~ m/(.+?)\s/; + $cmd =~ m/^(.+?)\s(.*)$/; my $executable = $1; + my $executable_args = $2; +=pod # for unix: check if executable is in local directory, if so - prefix with './' if (!RUNNING_IN_HELL()) { #if ($cmd !~ m/\//) { if (-e $executable) { - $cmd = "./$cmd"; } } - +=cut + + # new of 2003-05-07: basedir option to be prepended to command string + my $basedir = $options->{BASEDIR}; + my $use_path = $options->{USE_PATH}; + + # for all systems: check existance of files - use basedir if given, try current directory otherwise + if ($basedir) { + -e "$basedir/$executable" or die("$basedir/$executable does not exist."); + $basedir .= '/'; + $cmd = "$basedir$cmd"; + } elsif ($use_path) { + #$basedir = ""; + $basedir = findpath($executable); + #print "basedir: $basedir", "\n"; + my $abspath = File::Spec->catfile($basedir, $executable); + #print STDOUT "PATH: ", $abspath, "\n"; + -e $abspath or die("$abspath does not exist."); + $cmd = $abspath . ' ' . $executable_args; + } else { + -e $executable or die("$executable does not exist."); + #$basedir = "."; + #$basedir .= './'; + $basedir = ""; + $cmd = "$basedir$cmd"; + } + # V1 - backticks or qq{} #`$cmd`; #qq{$cmd}; @@ -103,44 +216,146 @@ # V2 - via 'system' #system($cmd); - # V3 - using IPC::Run (optional) + #if (not $use_path) { + my $application = get_executable($cmd); + $cmd = "$application$cmd" if $application; + #} + + my @cmd = split(' ', $cmd); + + # V3 - using IPC (optional) if ($options->{async}) { - # FIXME: detect type of program and run with proper application/interpreter - # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here! - # => better use absolute path-names only?! - my $application = ''; - if (RUNNING_IN_HELL()) { - $application = 'cmd.exe /C'; - } - - if ($cmd =~ m/\w+\.pl\s*.*/) { - $application = 'perl'; - } - - $cmd = "$application $cmd" if $application; - - print "IPC::Run: $cmd", "\n"; + #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; + + print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n"; - my @cmd = split(' ', $cmd); - - my $in; my $out; my $err; - start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'."); + # V3.1 - using IPC::Run + # + # tests: - #$IPC::Run::Timer::timeout = 2000; - #start $cmd or die("IPC::Run could not start '$cmd'."); + if (RUNNING_IN_HELL()) { + #my $in; my $out; my $err; + + #print STDOUT "findpath: ", findpath('rap.pl'), "\n"; + + # no success! + #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); + # + # success on Win32, but seems broken at 'timeout' on linux: + #run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); + #start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); + + # other tests ;) + #$IPC::Run::Timer::timeout = 2000; + #start $cmd or die("IPC::Run could not start '$cmd'."); + + #my $in; my $out; my $t; + #my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; + #my $harness = start( \@cmd ) ; + #$harness->pump_nb(); + + #my $postfix = '2>&1 |'; + #open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); + + my $proc1 = Proc::Background->new(@cmd); + print "pid: ", $proc1->pid(), "\n"; + + + } else { + + #print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; + + # V3.2 - using IPC::Session + # success on Linux AND Win32 ?? + # + # set timeout: + # (don't really know why we needs some secconds + # to wait for init of process !?!) + #my $session_timeout = 15; + # set session name (default: cmd as string): + #my $session_command = $cmd; + #my $session_shell = "/bin/sh"; + # create session (beware of using '->new' here!?): + #my $session = new IPC::Session($session_shell, $session_timeout); + + # send 'cmd' to session - not required since complete command is sent via constructor above + #$session->send($cmd); + + #my $output = $session->stdout(); + #print "WS::Admin started.\n" if $output = ' '; + + # tests + #$session->send("echo hello"); + #chomp(my $hello = $session->stdout()); + #print "ok 3\n" if $hello eq "hello"; + + # optional switch case: + #for ($session->stdout()) { + # /_bootDataBases/ && do { print "WS::Admin started.\n" }; + #} + # optional get error: + #my $err = session->stderr(); + #print "ERR: " . Dumper($err) . "\n"; + + #open( *OUT, ">out.txt" ) ; + #open( *ERR, ">err.txt" ) ; + #run(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #my ($out, $err); + #my $h = start(\@cmd, \undef, \*OUT, \*ERR ) or croak("run_cmd: IPC::Run could not start '$cmd'."); + #finish $h if $err =~ /error/; + + # get child pid + #my $kid; + #do { $kid=wait(); } until $kid > 0; + #print "Child PID: " . $kid . "n"; + + #finish $h; + + + # test using Proc::Background - success !! + my $proc = Proc::Background->new($cmd); + my $kid = $proc->pid(); + print STDOUT "run_cmd: Proc::Background: $cmd, child PID $kid", "\n"; + $proc->wait(); + $proc->die(); + + } + + + } elsif ($options->{detach}) { + + if (RUNNING_IN_HELL()) { + print STDOUT "run_cmd[detach]: Proc::Background: $cmd", "\n"; + my $proc1 = Proc::Background->new(@cmd); + print "pid: ", $proc1->pid(), "\n"; + } else { + print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n"; + system($cmd . ' &'); + } + } else { + print STDOUT "run_cmd: system('$cmd').", "\n"; + #print $ENV{PERL5LIB}, "\n"; system($cmd); } - print "ready.", "\n"; + print STDOUT "run_cmd: ready.", "\n"; } sub run_cmds { + my $options = {}; + if (ref $_[$#_] eq 'HASH') { + #print "YAI", "\n"; + $options = pop @_; + } foreach (@_) { - run_cmd($_); + run_cmd($_, '', $options); } } @@ -155,8 +370,6 @@ return ($bool ? 'ok' : 'failed'); } -sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } - # create global unique identifers using Data::UUID # if updating this code, please also modify Tangram::Storage::make_guid sub make_guid @@ -184,4 +397,25 @@ return $guid; } +# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure +sub findpath { + #local($path); + #local($arg) = shift; + my $arg = shift; + my $path; + #for my $dir (split(/:/,$ENV{'PATH'})) { + for my $dir (File::Spec->path()) { + #if (-x "$dir/$arg" && -f _) { + #print STDOUT "scanning: ", "$dir/$arg", "\n"; + if (-e "$dir/$arg") { + #$path = "$dir/$arg"; + #$path = "$dir/"; + $path = $dir; + last; + } + } + $path; +} + 1; +__END__