2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.14 2003/06/24 20:13:18 joko |
6 |
|
## + sub findpatch |
7 |
|
## + now using findpatch and Proc::Background for win32/perl |
8 |
|
## |
9 |
## Revision 1.13 2003/06/23 20:58:31 joko |
## Revision 1.13 2003/06/23 20:58:31 joko |
10 |
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
## restructured, hopefully makes Linux and Windows (and *BSD) more compatible... what about IPC::Cmd??? |
11 |
## |
## |
71 |
use Data::Dumper; |
use Data::Dumper; |
72 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
73 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
74 |
use IPC::Run qw( start pump finish timeout run ) ; |
use IPC::Run qw( start pump finish timeout run timer ) ; |
75 |
use Carp; |
use Carp; |
76 |
|
|
77 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
78 |
use IPC::Session; |
use IPC::Session; |
79 |
|
|
80 |
|
use File::Spec; |
81 |
|
use Proc::Background; |
82 |
|
|
83 |
|
|
84 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
85 |
# see "perldoc -f localtime" |
# see "perldoc -f localtime" |
126 |
|
|
127 |
if (RUNNING_IN_HELL()) { |
if (RUNNING_IN_HELL()) { |
128 |
# Required to adapt to IPC::Run on win32. |
# Required to adapt to IPC::Run on win32. |
129 |
$wrapper = 'cmd.exe /C perl'; |
#$wrapper = 'cmd.exe /C perl'; |
130 |
|
#$wrapper = 'start perl'; |
131 |
|
$wrapper = 'perl'; |
132 |
|
#$wrapper = 'cmd.exe /C'; |
133 |
} else { |
} else { |
134 |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
135 |
# whats about Win32? |
# whats about Win32? |
149 |
my $caption = shift; |
my $caption = shift; |
150 |
my $options = shift; |
my $options = shift; |
151 |
|
|
152 |
#print Dumper($options); |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
153 |
|
|
154 |
# report - header |
# report - header |
155 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
156 |
print STDERR $sep, "\n"; |
print STDOUT $sep, "\n"; |
157 |
print STDERR " ", $cmd; |
print STDOUT " ", $cmd; |
158 |
print STDERR " - ", $caption if $caption; |
print STDOUT " - ", $caption if $caption; |
159 |
print STDERR "\n", $sep, "\n"; |
print STDOUT "\n", $sep, "\n"; |
160 |
|
|
161 |
# strip name of executable from full command string |
# strip name of executable from full command string |
162 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/^(.+?)\s(.*)$/; |
163 |
my $executable = $1; |
my $executable = $1; |
164 |
|
my $executable_args = $2; |
165 |
|
|
166 |
=pod |
=pod |
167 |
# 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 './' |
180 |
if ($basedir) { |
if ($basedir) { |
181 |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
182 |
$basedir .= '/'; |
$basedir .= '/'; |
183 |
|
$cmd = "$basedir$cmd"; |
184 |
} elsif ($use_path) { |
} elsif ($use_path) { |
185 |
$basedir = ""; |
#$basedir = ""; |
186 |
|
$basedir = findpath($executable); |
187 |
|
#print "basedir: $basedir", "\n"; |
188 |
|
my $abspath = File::Spec->catfile($basedir, $executable); |
189 |
|
#print STDOUT "PATH: ", $abspath, "\n"; |
190 |
|
-e $abspath or die("$abspath does not exist."); |
191 |
|
$cmd = $abspath . ' ' . $executable_args; |
192 |
} else { |
} else { |
193 |
-e $executable or die("$executable does not exist."); |
-e $executable or die("$executable does not exist."); |
194 |
#$basedir = "."; |
#$basedir = "."; |
195 |
#$basedir .= './'; |
#$basedir .= './'; |
196 |
$basedir = ""; |
$basedir = ""; |
197 |
|
$cmd = "$basedir$cmd"; |
198 |
} |
} |
|
$cmd = "$basedir$cmd"; |
|
199 |
|
|
200 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
201 |
#`$cmd`; |
#`$cmd`; |
204 |
# V2 - via 'system' |
# V2 - via 'system' |
205 |
#system($cmd); |
#system($cmd); |
206 |
|
|
207 |
if (not $use_path) { |
#if (not $use_path) { |
208 |
my $application = get_executable($cmd); |
my $application = get_executable($cmd); |
209 |
$cmd = "$application$cmd" if $application; |
$cmd = "$application$cmd" if $application; |
210 |
} |
#} |
211 |
|
|
212 |
# V3 - using IPC (optional) |
# V3 - using IPC (optional) |
213 |
if ($options->{async}) { |
if ($options->{async}) { |
223 |
if (RUNNING_IN_HELL()) { |
if (RUNNING_IN_HELL()) { |
224 |
|
|
225 |
#my $in; my $out; my $err; |
#my $in; my $out; my $err; |
226 |
print STDERR "run_cmd: IPC::Run: $cmd", "\n"; |
print STDOUT "run_cmd: IPC::Run: $cmd", "\n"; |
227 |
|
|
228 |
|
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
229 |
|
|
230 |
# no success! |
# no success! |
231 |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
232 |
# |
# |
233 |
# success on Win32, but seems broken at 'timeout' on linux: |
# success on Win32, but seems broken at 'timeout' on linux: |
234 |
run(\@cmd, timeout(4)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
#run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
235 |
|
#start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
236 |
|
#start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
237 |
|
#start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'."); |
238 |
|
#start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
239 |
|
|
240 |
# other tests ;) |
# other tests ;) |
241 |
#$IPC::Run::Timer::timeout = 2000; |
#$IPC::Run::Timer::timeout = 2000; |
242 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
243 |
|
|
244 |
|
#my $in; my $out; my $t; |
245 |
|
#my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ; |
246 |
|
#my $harness = start( \@cmd ) ; |
247 |
|
#$harness->pump_nb(); |
248 |
|
|
249 |
|
#my $postfix = '2>&1 |'; |
250 |
|
#open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!"); |
251 |
|
|
252 |
|
my $proc1 = Proc::Background->new(@cmd); |
253 |
|
print "pid: ", $proc1->pid(), "\n"; |
254 |
|
|
255 |
|
|
256 |
} else { |
} else { |
257 |
|
|
258 |
print STDERR "run_cmd: IPC::Session: $cmd", "\n"; |
print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
259 |
|
|
260 |
# V3.2 - using IPC::Session |
# V3.2 - using IPC::Session |
261 |
# success on Linux AND Win32 ?? |
# success on Linux AND Win32 ?? |
282 |
} |
} |
283 |
|
|
284 |
} else { |
} else { |
285 |
print STDERR "run_cmd: system('$cmd').", "\n"; |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
286 |
system($cmd); |
system($cmd); |
287 |
} |
} |
288 |
|
|
289 |
print STDERR "run_cmd: ready.", "\n"; |
print STDOUT "run_cmd: ready.", "\n"; |
290 |
|
|
291 |
} |
} |
292 |
|
|
339 |
return $guid; |
return $guid; |
340 |
} |
} |
341 |
|
|
342 |
|
# [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure |
343 |
|
sub findpath { |
344 |
|
#local($path); |
345 |
|
#local($arg) = shift; |
346 |
|
my $arg = shift; |
347 |
|
my $path; |
348 |
|
#for my $dir (split(/:/,$ENV{'PATH'})) { |
349 |
|
for my $dir (File::Spec->path()) { |
350 |
|
#if (-x "$dir/$arg" && -f _) { |
351 |
|
#print STDOUT "scanning: ", "$dir/$arg", "\n"; |
352 |
|
if (-e "$dir/$arg") { |
353 |
|
#$path = "$dir/$arg"; |
354 |
|
#$path = "$dir/"; |
355 |
|
$path = $dir; |
356 |
|
last; |
357 |
|
} |
358 |
|
} |
359 |
|
$path; |
360 |
|
} |
361 |
|
|
362 |
1; |
1; |
363 |
__END__ |
__END__ |