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