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