--- nfo/perl/libs/shortcuts.pm 2003/02/22 17:19:36 1.4 +++ nfo/perl/libs/shortcuts.pm 2003/06/23 19:43:19 1.12 @@ -1,7 +1,34 @@ ## --------------------------------------------------------------------------- -## $Id: shortcuts.pm,v 1.4 2003/02/22 17:19:36 joko Exp $ +## $Id: shortcuts.pm,v 1.12 2003/06/23 19:43:19 joko Exp $ ## --------------------------------------------------------------------------- ## $Log: shortcuts.pm,v $ +## 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 +## +## Revision 1.7 2003/03/29 07:24:10 joko +## enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl') +## +## Revision 1.6 2003/03/28 06:58:06 joko +## new: 'run_cmd' now asynchronous! (via IPC::Run...) +## +## Revision 1.5 2003/02/22 17:26:13 joko +## + enhanced unix compatibility fix +## ## Revision 1.4 2003/02/22 17:19:36 joko ## + unix compatibility fix ## @@ -30,17 +57,22 @@ run_cmd run_cmds get_chomped bool2status + make_guid ); -# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main - use Data::Dumper; -use POSIX qw(strftime); +use POSIX qw( strftime ); +#use IPC::Run qw( run timeout ); +use IPC::Run qw( start pump finish timeout run ) ; +use Carp; + +# NEW - 2003-06-23 +use IPC::Session::NoShell; + # $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; # see "perldoc -f localtime" - sub now { my $options = shift; my $pattern = "%Y-%m-%d %H:%M:%S"; @@ -53,31 +85,159 @@ 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*.*/) { + # NEW 2003-06-23 - needed if used with IPC::Session (at Linux) + # whats about Win32? + $application = 'perl '; + } else { + $application = './'; + } + return $application; +} + +sub get_executable_wrapper { + my $cmd = shift; + my $application = ''; + # Required to adapt to IPC::Run on win32. + #if (RUNNING_IN_HELL()) { + #$application = 'cmd.exe /C'; + #} + return $application; +} + + sub run_cmd { my $cmd = shift; my $caption = shift; - #$cmd = 'perl ' . $cmd; + my $options = shift; + + #print Dumper($options); + + # report - header my $sep = "-" x 60; - print $sep, "\n"; - print " ", $cmd, "\n"; - print " ", $caption, "\n" if $caption; - print $sep, "\n"; + print STDERR $sep, "\n"; + print STDERR " ", $cmd; + print STDERR " - ", $caption if $caption; + print STDERR "\n", $sep, "\n"; + + # strip name of executable from full command string + $cmd =~ m/(.+?)\s/; + my $executable = $1; - # fix for unix: prefix command with './' if no pathname (relative or absolute) included +=pod + # for unix: check if executable is in local directory, if so - prefix with './' if (!RUNNING_IN_HELL()) { - if ($cmd !~ m/\//) { - $cmd = "./$cmd"; + #if ($cmd !~ m/\//) { + if (-e $executable) { } } - - system($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 .= '/'; + } elsif ($use_path) { + $basedir = ""; + } else { + -e $executable or die("$executable does not exist."); + #$basedir = "."; + #$basedir .= './'; + $basedir = ""; + } + $cmd = "$basedir$cmd"; + + # V1 - backticks or qq{} #`$cmd`; - print "ready.", "\n"; + #qq{$cmd}; + + # V2 - via 'system' + #system($cmd); + + if (not $use_path) { + my $application = get_executable($cmd); + $cmd = "$application$cmd" if $application; + } + + # V3 - using IPC (optional) + if ($options->{async}) { + + #$cmd = "$application $cmd" if $application; + #my $application = get_executable_wrapper($cmd); + + print STDERR "run_cmd: IPC::Session::NoShell: $cmd", "\n"; + #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; + + my @cmd = split(' ', $cmd); + + + # V3.1 - using IPC::Run + # + # tests: + #my $in; my $out; my $err; + #print "IPC::Run: $cmd", "\n"; + #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(2)) 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'."); + + + # V3.2 - using IPC::Session + # success on Linux AND Win32 ?? + # + # set timeout: + # (don't really know why we needs 2 secconds + # to wait for init of process !?!) + my $session_timeout = 2; + # set session name (default: cmd as string): + my $session_command = $cmd; + # create session: + my $session = new IPC::Session::NoShell($session_command, $session_timeout); + + # send 'cmd' to session - not required since complete command is sent via constructor above + #$session->send(\@cmd); + + # optional switch case: + #for ($session->stdout()) { + #} + # optional get error: + #my $err = session->stderr(); + + + } else { + print STDERR "run_cmd: system('$cmd').", "\n"; + system($cmd); + } + + print STDERR "run_cmd: ready.", "\n"; + } sub run_cmds { + my $options = {}; + if (ref $_[$#_] eq 'HASH') { + #print "YAI", "\n"; + $options = pop @_; + } foreach (@_) { - run_cmd($_); + run_cmd($_, '', $options); } } @@ -92,7 +252,32 @@ 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 + { + my $self = shift; + + my $guid; + + # try to use Data::UUID first ... + eval("use Data::UUID;"); + if (!$@) { + my $ug = Data::UUID->new(); + $guid = $ug->create_str(); + + # ... if this fails, try to fallback to Data::UUID::PurePerl instead ... + } else { + eval("use Data::UUID::PurePerl;"); + if (!$@) { + $guid = Data::UUID::PurePerl::generate_id(); + } else { + croak "couldn't create globally unique identifier"; + } + } + + return $guid; + } 1; +__END__