/[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.16 - (hide annotations)
Wed Jun 25 22:49:56 2003 UTC (20 years, 10 months ago) by joko
Branch: MAIN
Changes since 1.15: +17 -6 lines
RUNNING_IN_HELL mode for detach option

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

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