/[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.18 - (hide annotations)
Fri Dec 5 04:58:04 2003 UTC (20 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.17: +6 -2 lines
+ minor update: doesn't require IPC::Session anymore

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

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