/[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.10 by joko, Mon Jun 23 15:59:16 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  ##  Revision 1.10  2003/06/23 15:59:16  joko
38  ##  major/minor fixes?  ##  major/minor fixes?
39  ##  ##
# Line 51  our @EXPORT_OK = qw( Line 83  our @EXPORT_OK = qw(
83    get_chomped    get_chomped
84    bool2status    bool2status
85    make_guid    make_guid
86      RUNNING_IN_HELL
87      RUNNING_IN_HEAVEN
88  );  );
89    
90    
91  use Data::Dumper;  use Data::Dumper;
92  use POSIX qw( strftime );  use POSIX qw( strftime );
93  #use IPC::Run qw( run timeout );  #use IPC::Run qw( run timeout );
94  use IPC::Run qw( start pump finish timeout run ) ;  use IPC::Run qw( start pump finish timeout run timer ) ;
95  use Carp;  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;
# Line 76  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 {  sub get_executable {
123    my $cmd = shift;    my $cmd = shift;
124    # FIXME: detect type of program and run with proper application/interpreter    # FIXME: detect type of program and run with proper application/interpreter
# Line 83  sub get_executable { Line 126  sub get_executable {
126    # => better use absolute path-names only?!    # => better use absolute path-names only?!
127    my $application = '';    my $application = '';
128    if ($cmd =~ m/\w+\.pl\s*.*/) {    if ($cmd =~ m/\w+\.pl\s*.*/) {
129      $application = 'perl ';        $application = get_interpreter_wrapper($cmd, 'perl');
130          #$cmd = "$application $cmd" if $application;
131          $application .= ' ';
132        
133    } else {    } else {
134      $application = './';      $application = './';
135    }    }
136    return $application;    return $application;
137  }  }
138    
139  sub get_executable_wrapper {  sub get_interpreter_wrapper {
140    my $cmd = shift;    my $cmd = shift;
141    my $application = '';    my $language = shift;
142    # Required to adapt to IPC::Run on win32.    $language ||= '';
143    if (RUNNING_IN_HELL()) {  
144      #$application = 'cmd.exe /C';    my $wrapper = '';
145      $application = 'cmd.exe /C';  
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    return $application;    
164      return $wrapper;
165  }  }
166    
167    
# Line 106  sub run_cmd { Line 169  sub run_cmd {
169    my $cmd = shift;    my $cmd = shift;
170    my $caption = shift;    my $caption = shift;
171    my $options = shift;    my $options = shift;
   #$cmd = 'perl ' . $cmd;  
172        
173    #print Dumper($options);    #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
174        
175    # report - header    # 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 "  ", $cmd;    print STDOUT "\n", $sep, "\n";
   print " - ", $caption if $caption;  
   print "\n";  
   print $sep, "\n";  
181        
182    # strip name of executable from full command string    # strip name of executable from full command string
183    $cmd =~ m/(.+?)\s/;    $cmd =~ m/^(.+?)\s(.*)$/;
184    my $executable = $1;    my $executable = $1;
185      my $executable_args = $2;
186        
187  =pod  =pod
188    # 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 141  sub run_cmd { Line 201  sub run_cmd {
201    if ($basedir) {    if ($basedir) {
202      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
203      $basedir .= '/';      $basedir .= '/';
204        $cmd = "$basedir$cmd";
205    } elsif ($use_path) {    } elsif ($use_path) {
206      $basedir = "";      #$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 {    } else {
214      -e $executable or die("$executable does not exist.");      -e $executable or die("$executable does not exist.");
215      #$basedir = ".";      #$basedir = ".";
216      #$basedir .= './';      #$basedir .= './';
217        $basedir = "";
218        $cmd = "$basedir$cmd";
219    }    }
   $cmd = "$basedir$cmd";  
220    
221    # V1 - backticks or qq{}    # V1 - backticks or qq{}
222    #`$cmd`;    #`$cmd`;
# Line 157  sub run_cmd { Line 225  sub run_cmd {
225    # V2 - via 'system'    # V2 - via 'system'
226    #system($cmd);    #system($cmd);
227    
228    if (not $use_path) {    #if (not $use_path) {
229      my $application = get_executable($cmd);      my $application = get_executable($cmd);
230      $cmd = "$application$cmd" if $application;      $cmd = "$application$cmd" if $application;
231    }    #}
232    
233    # V3 - using IPC::Run (optional)    my @cmd = split(' ', $cmd);
234    
235      # V3 - using IPC (optional)
236    if ($options->{async}) {    if ($options->{async}) {
237      my $application = get_executable_wrapper($cmd);  
     $cmd = "$application $cmd" if $application;  
       
     print "run_cmd: IPC::Run: $cmd", "\n";  
238      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
239    
240        print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n";
241            
242      my @cmd = split(' ', $cmd);      # V3.1 - using IPC::Run
243            #
244      my $in; my $out; my $err;      # tests:
     #print "IPC::Run: $cmd", "\n";  
     #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");  
     run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'.");  
245            
246      #$IPC::Run::Timer::timeout = 2000;      if (RUNNING_IN_HELL()) {
     #start $cmd or die("IPC::Run could not start '$cmd'.");  
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 {    } else {
351      print "run_cmd: system('$cmd').", "\n";      print STDOUT "run_cmd: system('$cmd').", "\n";
352        #print $ENV{PERL5LIB}, "\n";
353      system($cmd);      system($cmd);
354    }    }
355        
356    print "run_cmd: ready.", "\n";    print STDOUT "run_cmd: ready.", "\n";
357        
358  }  }
359    
# Line 211  sub bool2status { Line 379  sub bool2status {
379    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
380  }  }
381    
 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  
   
382  # create global unique identifers using Data::UUID  # create global unique identifers using Data::UUID
383  # if updating this code, please also modify Tangram::Storage::make_guid  # if updating this code, please also modify Tangram::Storage::make_guid
384  sub make_guid  sub make_guid
# Line 240  sub make_guid Line 406  sub make_guid
406      return $guid;      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.10  
changed lines
  Added in v.1.20

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