/[cvs]/nfo/perl/libs/shortcuts.pm
ViewVC logotype

Contents of /nfo/perl/libs/shortcuts.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations)
Tue Jun 24 20:21:12 2003 UTC (20 years, 10 months ago) by jonen
Branch: MAIN
Changes since 1.14: +52 -11 lines
+ changed linux part of run_cmd to use Proc::Background instead of IPC::...

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed