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

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

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