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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations)
Tue May 11 19:45:30 2004 UTC (19 years, 11 months ago) by joko
Branch: MAIN
Changes since 1.18: +5 -1 lines
now exporting RUNNING_IN_HELL

1 joko 1.1 ## ---------------------------------------------------------------------------
2 joko 1.19 ## $Id: shortcuts.pm,v 1.18 2003/12/05 04:58:04 joko Exp $
3 joko 1.1 ## ---------------------------------------------------------------------------
4     ## $Log: shortcuts.pm,v $
5 joko 1.19 ## Revision 1.18 2003/12/05 04:58:04 joko
6     ## + minor update: doesn't require IPC::Session anymore
7     ##
8 joko 1.18 ## Revision 1.17 2003/07/02 11:17:32 jonen
9     ## minor changes
10     ##
11 jonen 1.17 ## Revision 1.16 2003/06/25 22:49:56 joko
12     ## RUNNING_IN_HELL mode for detach option
13     ##
14 joko 1.16 ## 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 jonen 1.15 ## 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 joko 1.14 ## 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 joko 1.13 ## Revision 1.12 2003/06/23 19:43:19 joko
25     ## minor cleanup
26     ## now using IPC::Session::NoShell
27     ##
28 joko 1.12 ## 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 jonen 1.11 ## Revision 1.10 2003/06/23 15:59:16 joko
32     ## major/minor fixes?
33     ##
34 joko 1.10 ## 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 joko 1.9 ## Revision 1.8 2003/04/04 17:31:59 joko
40     ## + sub make_guid
41     ##
42 joko 1.8 ## 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 joko 1.7 ## Revision 1.6 2003/03/28 06:58:06 joko
46     ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
47     ##
48 joko 1.6 ## Revision 1.5 2003/02/22 17:26:13 joko
49     ## + enhanced unix compatibility fix
50     ##
51 joko 1.5 ## Revision 1.4 2003/02/22 17:19:36 joko
52     ## + unix compatibility fix
53     ##
54 joko 1.4 ## Revision 1.3 2003/02/14 14:17:04 joko
55     ## - shortened seperator
56     ##
57 joko 1.3 ## Revision 1.2 2003/02/11 05:14:28 joko
58     ## + refactored code from libp.pm
59     ##
60 joko 1.2 ## Revision 1.1 2003/02/09 04:49:45 joko
61     ## + shortcuts now refactored to this file
62     ##
63 joko 1.1 ## ---------------------------------------------------------------------------
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 joko 1.2 bool2status
79 joko 1.8 make_guid
80 joko 1.19 RUNNING_IN_HELL
81 joko 1.1 );
82    
83    
84     use Data::Dumper;
85 joko 1.6 use POSIX qw( strftime );
86     #use IPC::Run qw( run timeout );
87 joko 1.14 use IPC::Run qw( start pump finish timeout run timer ) ;
88 joko 1.8 use Carp;
89    
90 joko 1.13 # NEW - 2003-06-23 for Linux (what about *BSD?)
91 joko 1.18 #use IPC::Session;
92 joko 1.8
93 joko 1.14 use File::Spec;
94     use Proc::Background;
95    
96 joko 1.1
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 joko 1.12 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
112    
113    
114 joko 1.9 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 joko 1.13 $application = get_interpreter_wrapper($cmd, 'perl');
122     #$cmd = "$application $cmd" if $application;
123     $application .= ' ';
124    
125 joko 1.10 } else {
126     $application = './';
127 joko 1.9 }
128     return $application;
129     }
130    
131 joko 1.13 sub get_interpreter_wrapper {
132 joko 1.9 my $cmd = shift;
133 joko 1.13 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 joko 1.14 #$wrapper = 'cmd.exe /C perl';
143     #$wrapper = 'start perl';
144     $wrapper = 'perl';
145     #$wrapper = 'cmd.exe /C';
146 joko 1.13 } 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 joko 1.9 }
158    
159    
160 joko 1.1 sub run_cmd {
161     my $cmd = shift;
162     my $caption = shift;
163 joko 1.6 my $options = shift;
164    
165 joko 1.14 #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
166 joko 1.9
167 joko 1.6 # report - header
168 joko 1.3 my $sep = "-" x 60;
169 joko 1.14 print STDOUT $sep, "\n";
170     print STDOUT " ", $cmd;
171     print STDOUT " - ", $caption if $caption;
172     print STDOUT "\n", $sep, "\n";
173 joko 1.4
174 joko 1.5 # strip name of executable from full command string
175 joko 1.14 $cmd =~ m/^(.+?)\s(.*)$/;
176 joko 1.5 my $executable = $1;
177 joko 1.14 my $executable_args = $2;
178 joko 1.5
179 joko 1.9 =pod
180 joko 1.5 # for unix: check if executable is in local directory, if so - prefix with './'
181 joko 1.4 if (!RUNNING_IN_HELL()) {
182 joko 1.5 #if ($cmd !~ m/\//) {
183     if (-e $executable) {
184 joko 1.4 }
185     }
186 joko 1.9 =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 joko 1.14 $cmd = "$basedir$cmd";
197 joko 1.9 } elsif ($use_path) {
198 joko 1.14 #$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 joko 1.9 } else {
206     -e $executable or die("$executable does not exist.");
207     #$basedir = ".";
208 joko 1.10 #$basedir .= './';
209 joko 1.12 $basedir = "";
210 joko 1.14 $cmd = "$basedir$cmd";
211 joko 1.9 }
212    
213 joko 1.6 # V1 - backticks or qq{}
214 joko 1.1 #`$cmd`;
215 joko 1.6 #qq{$cmd};
216    
217     # V2 - via 'system'
218     #system($cmd);
219    
220 joko 1.14 #if (not $use_path) {
221 joko 1.10 my $application = get_executable($cmd);
222     $cmd = "$application$cmd" if $application;
223 joko 1.14 #}
224 joko 1.10
225 joko 1.16 my @cmd = split(' ', $cmd);
226    
227 jonen 1.11 # V3 - using IPC (optional)
228 joko 1.6 if ($options->{async}) {
229 jonen 1.11
230 joko 1.6 #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
231 joko 1.16
232     print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n";
233 joko 1.6
234 jonen 1.11 # V3.1 - using IPC::Run
235     #
236     # tests:
237 joko 1.13
238     if (RUNNING_IN_HELL()) {
239    
240     #my $in; my $out; my $err;
241 joko 1.14
242     #print STDOUT "findpath: ", findpath('rap.pl'), "\n";
243 joko 1.13
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 joko 1.14 #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 joko 1.13
254     # other tests ;)
255     #$IPC::Run::Timer::timeout = 2000;
256     #start $cmd or die("IPC::Run could not start '$cmd'.");
257    
258 joko 1.14 #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 joko 1.13 } else {
271    
272 jonen 1.15 #print STDOUT "run_cmd: IPC::Session: $cmd", "\n";
273 joko 1.13
274     # V3.2 - using IPC::Session
275     # success on Linux AND Win32 ??
276     #
277     # set timeout:
278 jonen 1.15 # (don't really know why we needs some secconds
279 joko 1.13 # to wait for init of process !?!)
280 jonen 1.15 #my $session_timeout = 15;
281 joko 1.13 # set session name (default: cmd as string):
282 jonen 1.15 #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 joko 1.13
287     # send 'cmd' to session - not required since complete command is sent via constructor above
288 jonen 1.15 #$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 joko 1.13
298     # optional switch case:
299     #for ($session->stdout()) {
300 jonen 1.15 # /_bootDataBases/ && do { print "WS::Admin started.\n" };
301 joko 1.13 #}
302     # optional get error:
303     #my $err = session->stderr();
304 jonen 1.15 #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 joko 1.13 }
329 jonen 1.15
330    
331     } elsif ($options->{detach}) {
332 joko 1.16
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 jonen 1.17 print STDOUT "run_cmd[detach]: system('$cmd' &).", "\n";
339 joko 1.16 system($cmd . ' &');
340     }
341 jonen 1.15
342 joko 1.6 } else {
343 joko 1.14 print STDOUT "run_cmd: system('$cmd').", "\n";
344 joko 1.18 #print $ENV{PERL5LIB}, "\n";
345 joko 1.6 system($cmd);
346     }
347    
348 joko 1.14 print STDOUT "run_cmd: ready.", "\n";
349 joko 1.6
350 joko 1.1 }
351    
352     sub run_cmds {
353 joko 1.9 my $options = {};
354     if (ref $_[$#_] eq 'HASH') {
355     #print "YAI", "\n";
356     $options = pop @_;
357     }
358 joko 1.1 foreach (@_) {
359 joko 1.9 run_cmd($_, '', $options);
360 joko 1.1 }
361     }
362    
363     sub get_chomped {
364     my $str = shift;
365     chomp($str);
366     return $str;
367 joko 1.2 }
368    
369     sub bool2status {
370     my $bool = shift;
371     return ($bool ? 'ok' : 'failed');
372 joko 1.1 }
373 joko 1.4
374 joko 1.8 # 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 joko 1.14
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 jonen 1.11
421 joko 1.1 1;
422 joko 1.12 __END__

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