2 |
## $Id$ |
## $Id$ |
3 |
## --------------------------------------------------------------------------- |
## --------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.11 2003/06/23 17:41:50 jonen |
6 |
|
## + NEW - used IPC::Session instead of IPC::Run to get better results at linux |
7 |
|
## |
8 |
|
## Revision 1.10 2003/06/23 15:59:16 joko |
9 |
|
## major/minor fixes? |
10 |
|
## |
11 |
|
## Revision 1.9 2003/05/13 05:36:24 joko |
12 |
|
## heavy modifications to run_cmd |
13 |
|
## + sub get_executable |
14 |
|
## + sub get_executable_wrapper |
15 |
|
## |
16 |
## Revision 1.8 2003/04/04 17:31:59 joko |
## Revision 1.8 2003/04/04 17:31:59 joko |
17 |
## + sub make_guid |
## + sub make_guid |
18 |
## |
## |
60 |
use Data::Dumper; |
use Data::Dumper; |
61 |
use POSIX qw( strftime ); |
use POSIX qw( strftime ); |
62 |
#use IPC::Run qw( run timeout ); |
#use IPC::Run qw( run timeout ); |
63 |
use IPC::Run qw( start pump finish timeout ) ; |
use IPC::Run qw( start pump finish timeout run ) ; |
64 |
use Carp; |
use Carp; |
65 |
|
|
66 |
|
# NEW - 2003-06-23 |
67 |
|
use IPC::Session; |
68 |
|
|
69 |
|
|
70 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
81 |
return strftime("%Y-%m-%d", localtime); |
return strftime("%Y-%m-%d", localtime); |
82 |
} |
} |
83 |
|
|
84 |
|
sub get_executable { |
85 |
|
my $cmd = shift; |
86 |
|
# FIXME: detect type of program and run with proper application/interpreter |
87 |
|
# using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!? |
88 |
|
# => better use absolute path-names only?! |
89 |
|
my $application = ''; |
90 |
|
if ($cmd =~ m/\w+\.pl\s*.*/) { |
91 |
|
# NEW 2003-06-23 - needed if used with IPC::Session (at Linux) |
92 |
|
# whats about Win32? |
93 |
|
$application = 'perl '; |
94 |
|
} else { |
95 |
|
$application = './'; |
96 |
|
} |
97 |
|
return $application; |
98 |
|
} |
99 |
|
|
100 |
|
sub get_executable_wrapper { |
101 |
|
my $cmd = shift; |
102 |
|
my $application = ''; |
103 |
|
# Required to adapt to IPC::Run on win32. |
104 |
|
#if (RUNNING_IN_HELL()) { |
105 |
|
#$application = 'cmd.exe /C'; |
106 |
|
#} |
107 |
|
return $application; |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
sub run_cmd { |
sub run_cmd { |
112 |
my $cmd = shift; |
my $cmd = shift; |
113 |
my $caption = shift; |
my $caption = shift; |
114 |
my $options = shift; |
my $options = shift; |
115 |
#$cmd = 'perl ' . $cmd; |
#$cmd = 'perl ' . $cmd; |
116 |
|
|
117 |
|
#print Dumper($options); |
118 |
|
|
119 |
# report - header |
# report - header |
120 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
121 |
print $sep, "\n"; |
print $sep, "\n"; |
130 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/(.+?)\s/; |
131 |
my $executable = $1; |
my $executable = $1; |
132 |
|
|
133 |
|
=pod |
134 |
# 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 './' |
135 |
if (!RUNNING_IN_HELL()) { |
if (!RUNNING_IN_HELL()) { |
136 |
#if ($cmd !~ m/\//) { |
#if ($cmd !~ m/\//) { |
137 |
if (-e $executable) { |
if (-e $executable) { |
|
$cmd = "./$cmd"; |
|
138 |
} |
} |
139 |
} |
} |
140 |
|
=cut |
141 |
|
|
142 |
|
# new of 2003-05-07: basedir option to be prepended to command string |
143 |
|
my $basedir = $options->{BASEDIR}; |
144 |
|
my $use_path = $options->{USE_PATH}; |
145 |
|
|
146 |
|
# for all systems: check existance of files - use basedir if given, try current directory otherwise |
147 |
|
if ($basedir) { |
148 |
|
-e "$basedir/$executable" or die("$basedir/$executable does not exist."); |
149 |
|
$basedir .= '/'; |
150 |
|
} elsif ($use_path) { |
151 |
|
$basedir = ""; |
152 |
|
} else { |
153 |
|
-e $executable or die("$executable does not exist."); |
154 |
|
#$basedir = "."; |
155 |
|
#$basedir .= './'; |
156 |
|
} |
157 |
|
$cmd = "$basedir$cmd"; |
158 |
|
|
159 |
# V1 - backticks or qq{} |
# V1 - backticks or qq{} |
160 |
#`$cmd`; |
#`$cmd`; |
161 |
#qq{$cmd}; |
#qq{$cmd}; |
163 |
# V2 - via 'system' |
# V2 - via 'system' |
164 |
#system($cmd); |
#system($cmd); |
165 |
|
|
166 |
# V3 - using IPC::Run (optional) |
if (not $use_path) { |
167 |
|
my $application = get_executable($cmd); |
168 |
|
$cmd = "$application$cmd" if $application; |
169 |
|
} |
170 |
|
|
171 |
|
# V3 - using IPC (optional) |
172 |
if ($options->{async}) { |
if ($options->{async}) { |
173 |
# FIXME: detect type of program and run with proper application/interpreter |
|
174 |
# using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here! |
#$cmd = "$application $cmd" if $application; |
175 |
# => better use absolute path-names only?! |
#my $application = get_executable_wrapper($cmd); |
|
my $application = ''; |
|
|
if (RUNNING_IN_HELL()) { |
|
|
$application = 'cmd.exe /C'; |
|
|
} |
|
|
|
|
|
if ($cmd =~ m/\w+\.pl\s*.*/) { |
|
|
$application = 'perl'; |
|
|
} |
|
|
|
|
|
$cmd = "$application $cmd" if $application; |
|
176 |
|
|
177 |
print "IPC::Run: $cmd", "\n"; |
print "run_cmd: IPC::Run: $cmd", "\n"; |
178 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
179 |
|
|
180 |
my @cmd = split(' ', $cmd); |
my @cmd = split(' ', $cmd); |
181 |
|
|
|
my $in; my $out; my $err; |
|
|
start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'."); |
|
182 |
|
|
183 |
|
# V3.1 - using IPC::Run |
184 |
|
# |
185 |
|
# tests: |
186 |
|
#my $in; my $out; my $err; |
187 |
|
#print "IPC::Run: $cmd", "\n"; |
188 |
|
#start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
189 |
|
# |
190 |
|
# success on Win32, but seems broken at 'timeout' on linux: |
191 |
|
#run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'."); |
192 |
|
|
193 |
|
# other tests ;) |
194 |
#$IPC::Run::Timer::timeout = 2000; |
#$IPC::Run::Timer::timeout = 2000; |
195 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
196 |
|
|
197 |
|
|
198 |
|
# V3.2 - using IPC::Session |
199 |
|
# success on Linux AND Win32 ?? |
200 |
|
# |
201 |
|
# set timeout: |
202 |
|
# (don't really know why we needs 2 secconds |
203 |
|
# to wait for init of process !?!) |
204 |
|
my $session_timeout = 2; |
205 |
|
# set session name (default: cmd as string): |
206 |
|
my $session_name = $cmd; |
207 |
|
# create session: |
208 |
|
my $session = new IPC::Session($session_name, $session_timeout); |
209 |
|
# send 'cmd' to session: |
210 |
|
$session->send(\@cmd); |
211 |
|
# optional switch case: |
212 |
|
#for ($session->stdout()) { |
213 |
|
#} |
214 |
|
# optional get error: |
215 |
|
#my $err = session->stderr(); |
216 |
|
|
217 |
|
|
218 |
} else { |
} else { |
219 |
|
print "run_cmd: system('$cmd').", "\n"; |
220 |
system($cmd); |
system($cmd); |
221 |
} |
} |
222 |
|
|
223 |
print "ready.", "\n"; |
print "run_cmd: ready.", "\n"; |
224 |
|
|
225 |
} |
} |
226 |
|
|
227 |
sub run_cmds { |
sub run_cmds { |
228 |
|
my $options = {}; |
229 |
|
if (ref $_[$#_] eq 'HASH') { |
230 |
|
#print "YAI", "\n"; |
231 |
|
$options = pop @_; |
232 |
|
} |
233 |
foreach (@_) { |
foreach (@_) { |
234 |
run_cmd($_); |
run_cmd($_, '', $options); |
235 |
} |
} |
236 |
} |
} |
237 |
|
|
275 |
return $guid; |
return $guid; |
276 |
} |
} |
277 |
|
|
278 |
|
|
279 |
|
|
280 |
1; |
1; |