1 |
## --------------------------------------------------------------------------- |
2 |
## $Id: shortcuts.pm,v 1.13 2003/06/23 20:58:31 joko Exp $ |
3 |
## --------------------------------------------------------------------------- |
4 |
## $Log: shortcuts.pm,v $ |
5 |
## 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 |
## Revision 1.12 2003/06/23 19:43:19 joko |
9 |
## minor cleanup |
10 |
## now using IPC::Session::NoShell |
11 |
## |
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 |
## Revision 1.10 2003/06/23 15:59:16 joko |
16 |
## major/minor fixes? |
17 |
## |
18 |
## 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 |
## Revision 1.8 2003/04/04 17:31:59 joko |
24 |
## + sub make_guid |
25 |
## |
26 |
## 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 |
## Revision 1.6 2003/03/28 06:58:06 joko |
30 |
## new: 'run_cmd' now asynchronous! (via IPC::Run...) |
31 |
## |
32 |
## Revision 1.5 2003/02/22 17:26:13 joko |
33 |
## + enhanced unix compatibility fix |
34 |
## |
35 |
## Revision 1.4 2003/02/22 17:19:36 joko |
36 |
## + unix compatibility fix |
37 |
## |
38 |
## Revision 1.3 2003/02/14 14:17:04 joko |
39 |
## - shortened seperator |
40 |
## |
41 |
## Revision 1.2 2003/02/11 05:14:28 joko |
42 |
## + refactored code from libp.pm |
43 |
## |
44 |
## Revision 1.1 2003/02/09 04:49:45 joko |
45 |
## + shortcuts now refactored to this file |
46 |
## |
47 |
## --------------------------------------------------------------------------- |
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 |
bool2status |
63 |
make_guid |
64 |
); |
65 |
|
66 |
|
67 |
use Data::Dumper; |
68 |
use POSIX qw( strftime ); |
69 |
#use IPC::Run qw( run timeout ); |
70 |
use IPC::Run qw( start pump finish timeout run timer ) ; |
71 |
use Carp; |
72 |
|
73 |
# NEW - 2003-06-23 for Linux (what about *BSD?) |
74 |
use IPC::Session; |
75 |
|
76 |
use File::Spec; |
77 |
use Proc::Background; |
78 |
|
79 |
|
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 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
95 |
|
96 |
|
97 |
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 |
$application = get_interpreter_wrapper($cmd, 'perl'); |
105 |
#$cmd = "$application $cmd" if $application; |
106 |
$application .= ' '; |
107 |
|
108 |
} else { |
109 |
$application = './'; |
110 |
} |
111 |
return $application; |
112 |
} |
113 |
|
114 |
sub get_interpreter_wrapper { |
115 |
my $cmd = shift; |
116 |
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 |
#$wrapper = 'cmd.exe /C perl'; |
126 |
#$wrapper = 'start perl'; |
127 |
$wrapper = 'perl'; |
128 |
#$wrapper = 'cmd.exe /C'; |
129 |
} 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 |
} |
141 |
|
142 |
|
143 |
sub run_cmd { |
144 |
my $cmd = shift; |
145 |
my $caption = shift; |
146 |
my $options = shift; |
147 |
|
148 |
#print STDOUT "run_cmd - options: ", Dumper($options), "\n"; |
149 |
|
150 |
# report - header |
151 |
my $sep = "-" x 60; |
152 |
print STDOUT $sep, "\n"; |
153 |
print STDOUT " ", $cmd; |
154 |
print STDOUT " - ", $caption if $caption; |
155 |
print STDOUT "\n", $sep, "\n"; |
156 |
|
157 |
# strip name of executable from full command string |
158 |
$cmd =~ m/^(.+?)\s(.*)$/; |
159 |
my $executable = $1; |
160 |
my $executable_args = $2; |
161 |
|
162 |
=pod |
163 |
# for unix: check if executable is in local directory, if so - prefix with './' |
164 |
if (!RUNNING_IN_HELL()) { |
165 |
#if ($cmd !~ m/\//) { |
166 |
if (-e $executable) { |
167 |
} |
168 |
} |
169 |
=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 |
$cmd = "$basedir$cmd"; |
180 |
} elsif ($use_path) { |
181 |
#$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 |
} else { |
189 |
-e $executable or die("$executable does not exist."); |
190 |
#$basedir = "."; |
191 |
#$basedir .= './'; |
192 |
$basedir = ""; |
193 |
$cmd = "$basedir$cmd"; |
194 |
} |
195 |
|
196 |
# V1 - backticks or qq{} |
197 |
#`$cmd`; |
198 |
#qq{$cmd}; |
199 |
|
200 |
# V2 - via 'system' |
201 |
#system($cmd); |
202 |
|
203 |
#if (not $use_path) { |
204 |
my $application = get_executable($cmd); |
205 |
$cmd = "$application$cmd" if $application; |
206 |
#} |
207 |
|
208 |
# V3 - using IPC (optional) |
209 |
if ($options->{async}) { |
210 |
|
211 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
212 |
my @cmd = split(' ', $cmd); |
213 |
|
214 |
|
215 |
# V3.1 - using IPC::Run |
216 |
# |
217 |
# tests: |
218 |
|
219 |
if (RUNNING_IN_HELL()) { |
220 |
|
221 |
#my $in; my $out; my $err; |
222 |
print STDOUT "run_cmd: IPC::Run: $cmd", "\n"; |
223 |
|
224 |
#print STDOUT "findpath: ", findpath('rap.pl'), "\n"; |
225 |
|
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 |
#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 |
|
236 |
# other tests ;) |
237 |
#$IPC::Run::Timer::timeout = 2000; |
238 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
239 |
|
240 |
#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 |
} else { |
253 |
|
254 |
print STDOUT "run_cmd: IPC::Session: $cmd", "\n"; |
255 |
|
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 |
|
280 |
} else { |
281 |
print STDOUT "run_cmd: system('$cmd').", "\n"; |
282 |
system($cmd); |
283 |
} |
284 |
|
285 |
print STDOUT "run_cmd: ready.", "\n"; |
286 |
|
287 |
} |
288 |
|
289 |
sub run_cmds { |
290 |
my $options = {}; |
291 |
if (ref $_[$#_] eq 'HASH') { |
292 |
#print "YAI", "\n"; |
293 |
$options = pop @_; |
294 |
} |
295 |
foreach (@_) { |
296 |
run_cmd($_, '', $options); |
297 |
} |
298 |
} |
299 |
|
300 |
sub get_chomped { |
301 |
my $str = shift; |
302 |
chomp($str); |
303 |
return $str; |
304 |
} |
305 |
|
306 |
sub bool2status { |
307 |
my $bool = shift; |
308 |
return ($bool ? 'ok' : 'failed'); |
309 |
} |
310 |
|
311 |
# 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 |
|
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 |
|
358 |
1; |
359 |
__END__ |