/[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.15 - (hide 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 joko 1.1 ## ---------------------------------------------------------------------------
2 jonen 1.15 ## $Id: shortcuts.pm,v 1.14 2003/06/24 20:13:18 joko Exp $
3 joko 1.1 ## ---------------------------------------------------------------------------
4     ## $Log: shortcuts.pm,v $
5 jonen 1.15 ## 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 joko 1.14 ## 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 joko 1.13 ## Revision 1.12 2003/06/23 19:43:19 joko
13     ## minor cleanup
14     ## now using IPC::Session::NoShell
15     ##
16 joko 1.12 ## 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 jonen 1.11 ## Revision 1.10 2003/06/23 15:59:16 joko
20     ## major/minor fixes?
21     ##
22 joko 1.10 ## 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 joko 1.9 ## Revision 1.8 2003/04/04 17:31:59 joko
28     ## + sub make_guid
29     ##
30 joko 1.8 ## 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 joko 1.7 ## Revision 1.6 2003/03/28 06:58:06 joko
34     ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
35     ##
36 joko 1.6 ## Revision 1.5 2003/02/22 17:26:13 joko
37     ## + enhanced unix compatibility fix
38     ##
39 joko 1.5 ## Revision 1.4 2003/02/22 17:19:36 joko
40     ## + unix compatibility fix
41     ##
42 joko 1.4 ## Revision 1.3 2003/02/14 14:17:04 joko
43     ## - shortened seperator
44     ##
45 joko 1.3 ## Revision 1.2 2003/02/11 05:14:28 joko
46     ## + refactored code from libp.pm
47     ##
48 joko 1.2 ## Revision 1.1 2003/02/09 04:49:45 joko
49     ## + shortcuts now refactored to this file
50     ##
51 joko 1.1 ## ---------------------------------------------------------------------------
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 joko 1.2 bool2status
67 joko 1.8 make_guid
68 joko 1.1 );
69    
70    
71     use Data::Dumper;
72 joko 1.6 use POSIX qw( strftime );
73     #use IPC::Run qw( run timeout );
74 joko 1.14 use IPC::Run qw( start pump finish timeout run timer ) ;
75 joko 1.8 use Carp;
76    
77 joko 1.13 # NEW - 2003-06-23 for Linux (what about *BSD?)
78     use IPC::Session;
79 joko 1.8
80 joko 1.14 use File::Spec;
81     use Proc::Background;
82    
83 joko 1.1
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 joko 1.12 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
99    
100    
101 joko 1.9 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 joko 1.13 $application = get_interpreter_wrapper($cmd, 'perl');
109     #$cmd = "$application $cmd" if $application;
110     $application .= ' ';
111    
112 joko 1.10 } else {
113     $application = './';
114 joko 1.9 }
115     return $application;
116     }
117    
118 joko 1.13 sub get_interpreter_wrapper {
119 joko 1.9 my $cmd = shift;
120 joko 1.13 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 joko 1.14 #$wrapper = 'cmd.exe /C perl';
130     #$wrapper = 'start perl';
131     $wrapper = 'perl';
132     #$wrapper = 'cmd.exe /C';
133 joko 1.13 } 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 joko 1.9 }
145    
146    
147 joko 1.1 sub run_cmd {
148     my $cmd = shift;
149     my $caption = shift;
150 joko 1.6 my $options = shift;
151    
152 joko 1.14 #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
153 joko 1.9
154 joko 1.6 # report - header
155 joko 1.3 my $sep = "-" x 60;
156 joko 1.14 print STDOUT $sep, "\n";
157     print STDOUT " ", $cmd;
158     print STDOUT " - ", $caption if $caption;
159     print STDOUT "\n", $sep, "\n";
160 joko 1.4
161 joko 1.5 # strip name of executable from full command string
162 joko 1.14 $cmd =~ m/^(.+?)\s(.*)$/;
163 joko 1.5 my $executable = $1;
164 joko 1.14 my $executable_args = $2;
165 joko 1.5
166 joko 1.9 =pod
167 joko 1.5 # for unix: check if executable is in local directory, if so - prefix with './'
168 joko 1.4 if (!RUNNING_IN_HELL()) {
169 joko 1.5 #if ($cmd !~ m/\//) {
170     if (-e $executable) {
171 joko 1.4 }
172     }
173 joko 1.9 =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 joko 1.14 $cmd = "$basedir$cmd";
184 joko 1.9 } elsif ($use_path) {
185 joko 1.14 #$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 joko 1.9 } else {
193     -e $executable or die("$executable does not exist.");
194     #$basedir = ".";
195 joko 1.10 #$basedir .= './';
196 joko 1.12 $basedir = "";
197 joko 1.14 $cmd = "$basedir$cmd";
198 joko 1.9 }
199    
200 joko 1.6 # V1 - backticks or qq{}
201 joko 1.1 #`$cmd`;
202 joko 1.6 #qq{$cmd};
203    
204     # V2 - via 'system'
205     #system($cmd);
206    
207 joko 1.14 #if (not $use_path) {
208 joko 1.10 my $application = get_executable($cmd);
209     $cmd = "$application$cmd" if $application;
210 joko 1.14 #}
211 joko 1.10
212 jonen 1.11 # V3 - using IPC (optional)
213 joko 1.6 if ($options->{async}) {
214 jonen 1.11
215 joko 1.6 #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
216 jonen 1.11 my @cmd = split(' ', $cmd);
217 joko 1.6
218    
219 jonen 1.11 # V3.1 - using IPC::Run
220     #
221     # tests:
222 joko 1.13
223     if (RUNNING_IN_HELL()) {
224    
225     #my $in; my $out; my $err;
226 joko 1.14 print STDOUT "run_cmd: IPC::Run: $cmd", "\n";
227    
228     #print STDOUT "findpath: ", findpath('rap.pl'), "\n";
229 joko 1.13
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 joko 1.14 #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 joko 1.13
240     # other tests ;)
241     #$IPC::Run::Timer::timeout = 2000;
242     #start $cmd or die("IPC::Run could not start '$cmd'.");
243    
244 joko 1.14 #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 joko 1.13 } else {
257    
258 jonen 1.15 #print STDOUT "run_cmd: IPC::Session: $cmd", "\n";
259 joko 1.13
260     # V3.2 - using IPC::Session
261     # success on Linux AND Win32 ??
262     #
263     # set timeout:
264 jonen 1.15 # (don't really know why we needs some secconds
265 joko 1.13 # to wait for init of process !?!)
266 jonen 1.15 #my $session_timeout = 15;
267 joko 1.13 # set session name (default: cmd as string):
268 jonen 1.15 #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 joko 1.13
273     # send 'cmd' to session - not required since complete command is sent via constructor above
274 jonen 1.15 #$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 joko 1.13
284     # optional switch case:
285     #for ($session->stdout()) {
286 jonen 1.15 # /_bootDataBases/ && do { print "WS::Admin started.\n" };
287 joko 1.13 #}
288     # optional get error:
289     #my $err = session->stderr();
290 jonen 1.15 #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 joko 1.13 }
315 jonen 1.15
316    
317     } elsif ($options->{detach}) {
318     print STDERR "run_cmd: system('$cmd' &).", "\n";
319     system($cmd . ' &');
320    
321 joko 1.6 } else {
322 joko 1.14 print STDOUT "run_cmd: system('$cmd').", "\n";
323 joko 1.6 system($cmd);
324     }
325    
326 joko 1.14 print STDOUT "run_cmd: ready.", "\n";
327 joko 1.6
328 joko 1.1 }
329    
330     sub run_cmds {
331 joko 1.9 my $options = {};
332     if (ref $_[$#_] eq 'HASH') {
333     #print "YAI", "\n";
334     $options = pop @_;
335     }
336 joko 1.1 foreach (@_) {
337 joko 1.9 run_cmd($_, '', $options);
338 joko 1.1 }
339     }
340    
341     sub get_chomped {
342     my $str = shift;
343     chomp($str);
344     return $str;
345 joko 1.2 }
346    
347     sub bool2status {
348     my $bool = shift;
349     return ($bool ? 'ok' : 'failed');
350 joko 1.1 }
351 joko 1.4
352 joko 1.8 # 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 joko 1.14
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 jonen 1.11
399 joko 1.1 1;
400 joko 1.12 __END__

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