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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.19

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