/[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.13 by joko, Mon Jun 23 20:58:31 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  ##  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???  ##  restructured, hopefully makes Linux and Windows (and *BSD) more compatible...  what about IPC::Cmd???
26  ##  ##
# Line 61  our @EXPORT_OK = qw( Line 80  our @EXPORT_OK = qw(
80    get_chomped    get_chomped
81    bool2status    bool2status
82    make_guid    make_guid
83      RUNNING_IN_HELL
84  );  );
85    
86    
87  use Data::Dumper;  use Data::Dumper;
88  use POSIX qw( strftime );  use POSIX qw( strftime );
89  #use IPC::Run qw( run timeout );  #use IPC::Run qw( run timeout );
90  use IPC::Run qw( start pump finish timeout run ) ;  use IPC::Run qw( start pump finish timeout run timer ) ;
91  use Carp;  use Carp;
92    
93  # NEW - 2003-06-23 for Linux (what about *BSD?)  # NEW - 2003-06-23 for Linux (what about *BSD?)
94  use IPC::Session;  #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;
# Line 119  sub get_interpreter_wrapper { Line 142  sub get_interpreter_wrapper {
142        
143      if (RUNNING_IN_HELL()) {      if (RUNNING_IN_HELL()) {
144        # Required to adapt to IPC::Run on win32.        # Required to adapt to IPC::Run on win32.
145        $wrapper = 'cmd.exe /C perl';        #$wrapper = 'cmd.exe /C perl';
146          #$wrapper = 'start perl';
147          $wrapper = 'perl';
148          #$wrapper = 'cmd.exe /C';
149      } else {      } else {
150        # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)        # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
151        #  whats about Win32?        #  whats about Win32?
# Line 139  sub run_cmd { Line 165  sub run_cmd {
165    my $caption = shift;    my $caption = shift;
166    my $options = shift;    my $options = shift;
167        
168    #print Dumper($options);    #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
169        
170    # report - header    # report - header
171    my $sep = "-" x 60;    my $sep = "-" x 60;
172    print STDERR $sep, "\n";    print STDOUT $sep, "\n";
173    print STDERR "  ", $cmd;    print STDOUT "  ", $cmd;
174    print STDERR " - ", $caption if $caption;    print STDOUT " - ", $caption if $caption;
175    print STDERR "\n", $sep, "\n";    print STDOUT "\n", $sep, "\n";
176        
177    # strip name of executable from full command string    # strip name of executable from full command string
178    $cmd =~ m/(.+?)\s/;    $cmd =~ m/^(.+?)\s(.*)$/;
179    my $executable = $1;    my $executable = $1;
180      my $executable_args = $2;
181        
182  =pod  =pod
183    # for unix: check if executable is in local directory, if so - prefix with './'    # for unix: check if executable is in local directory, if so - prefix with './'
# Line 169  sub run_cmd { Line 196  sub run_cmd {
196    if ($basedir) {    if ($basedir) {
197      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
198      $basedir .= '/';      $basedir .= '/';
199        $cmd = "$basedir$cmd";
200    } elsif ($use_path) {    } elsif ($use_path) {
201      $basedir = "";      #$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 {    } else {
209      -e $executable or die("$executable does not exist.");      -e $executable or die("$executable does not exist.");
210      #$basedir = ".";      #$basedir = ".";
211      #$basedir .= './';      #$basedir .= './';
212      $basedir = "";      $basedir = "";
213        $cmd = "$basedir$cmd";
214    }    }
   $cmd = "$basedir$cmd";  
215    
216    # V1 - backticks or qq{}    # V1 - backticks or qq{}
217    #`$cmd`;    #`$cmd`;
# Line 186  sub run_cmd { Line 220  sub run_cmd {
220    # V2 - via 'system'    # V2 - via 'system'
221    #system($cmd);    #system($cmd);
222    
223    if (not $use_path) {    #if (not $use_path) {
224      my $application = get_executable($cmd);      my $application = get_executable($cmd);
225      $cmd = "$application$cmd" if $application;      $cmd = "$application$cmd" if $application;
226    }    #}
227    
228      my @cmd = split(' ', $cmd);
229    
230    # V3 - using IPC (optional)    # V3 - using IPC (optional)
231    if ($options->{async}) {    if ($options->{async}) {
232    
233      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
234      my @cmd = split(' ', $cmd);  
235            print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n";
236            
237      # V3.1 - using IPC::Run      # V3.1 - using IPC::Run
238      #      #
# Line 205  sub run_cmd { Line 241  sub run_cmd {
241      if (RUNNING_IN_HELL()) {      if (RUNNING_IN_HELL()) {
242            
243        #my $in; my $out; my $err;        #my $in; my $out; my $err;
244        print STDERR "run_cmd: IPC::Run: $cmd", "\n";        
245          #print STDOUT "findpath: ", findpath('rap.pl'), "\n";
246                
247        # no success!        # no success!
248        #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");        #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:        # success on Win32, but seems broken at 'timeout' on linux:
251        run(\@cmd, timeout(4)) or croak("run_cmd: IPC::Run could not start '$cmd'.");        #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 ;)        # other tests ;)
258        #$IPC::Run::Timer::timeout = 2000;        #$IPC::Run::Timer::timeout = 2000;
259        #start $cmd or die("IPC::Run could not start '$cmd'.");        #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 {      } else {
274    
275        print STDERR "run_cmd: IPC::Session: $cmd", "\n";        #print STDOUT "run_cmd: IPC::Session: $cmd", "\n";
276    
277        # V3.2 - using IPC::Session        # V3.2 - using IPC::Session
278        #  success on Linux AND Win32 ??        #  success on Linux AND Win32 ??
279        #        #
280        # set timeout:        # set timeout:
281        #  (don't really know why we needs 2 secconds        #  (don't really know why we needs some secconds
282        #   to wait for init of process !?!)        #   to wait for init of process !?!)
283        my $session_timeout = 3;        #my $session_timeout = 15;
284        # set session name (default: cmd as string):        # set session name (default: cmd as string):
285        my $session_name = $cmd;        #my $session_command = $cmd;
286        # create session:        #my $session_shell = "/bin/sh";
287        my $session = IPC::Session->new($session_name, $session_timeout);        # 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        # send 'cmd' to session - not required since complete command is sent via constructor above
291        $session->send(\@cmd);        #$session->send($cmd);
292          
293        #print $session->stdout(), "\n";        #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:        # optional switch case:
302        #for ($session->stdout()) {        #for ($session->stdout()) {
303          #  /_bootDataBases/ && do { print "WS::Admin started.\n" };
304        #}        #}
305        # optional get error:        # optional get error:
306        #my $err = session->stderr();        #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 {    } else {
346      print STDERR "run_cmd: system('$cmd').", "\n";      print STDOUT "run_cmd: system('$cmd').", "\n";
347        #print $ENV{PERL5LIB}, "\n";
348      system($cmd);      system($cmd);
349    }    }
350        
351    print STDERR "run_cmd: ready.", "\n";    print STDOUT "run_cmd: ready.", "\n";
352        
353  }  }
354    
# Line 303  sub make_guid Line 401  sub make_guid
401      return $guid;      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__  __END__

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

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