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