/[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.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  ##  Revision 1.10  2003/06/23 15:59:16  joko
35  ##  major/minor fixes?  ##  major/minor fixes?
36  ##  ##
# Line 51  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?)
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;
# Line 76  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 {  sub get_executable {
118    my $cmd = shift;    my $cmd = shift;
119    # 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 121  sub get_executable {
121    # => better use absolute path-names only?!    # => better use absolute path-names only?!
122    my $application = '';    my $application = '';
123    if ($cmd =~ m/\w+\.pl\s*.*/) {    if ($cmd =~ m/\w+\.pl\s*.*/) {
124      $application = 'perl ';        $application = get_interpreter_wrapper($cmd, 'perl');
125          #$cmd = "$application $cmd" if $application;
126          $application .= ' ';
127        
128    } else {    } else {
129      $application = './';      $application = './';
130    }    }
131    return $application;    return $application;
132  }  }
133    
134  sub get_executable_wrapper {  sub get_interpreter_wrapper {
135    my $cmd = shift;    my $cmd = shift;
136    my $application = '';    my $language = shift;
137    # Required to adapt to IPC::Run on win32.    $language ||= '';
138    if (RUNNING_IN_HELL()) {  
139      #$application = 'cmd.exe /C';    my $wrapper = '';
140      $application = 'cmd.exe /C';  
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    return $application;    
159      return $wrapper;
160  }  }
161    
162    
# Line 106  sub run_cmd { Line 164  sub run_cmd {
164    my $cmd = shift;    my $cmd = shift;
165    my $caption = shift;    my $caption = shift;
166    my $options = shift;    my $options = shift;
   #$cmd = 'perl ' . $cmd;  
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 $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 "  ", $cmd;    print STDOUT "\n", $sep, "\n";
   print " - ", $caption if $caption;  
   print "\n";  
   print $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 141  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 = "";
213        $cmd = "$basedir$cmd";
214    }    }
   $cmd = "$basedir$cmd";  
215    
216    # V1 - backticks or qq{}    # V1 - backticks or qq{}
217    #`$cmd`;    #`$cmd`;
# Line 157  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::Run (optional)    # V3 - using IPC (optional)
231    if ($options->{async}) {    if ($options->{async}) {
232      my $application = get_executable_wrapper($cmd);  
     $cmd = "$application $cmd" if $application;  
       
     print "run_cmd: IPC::Run: $cmd", "\n";  
233      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
234    
235        print STDOUT "run_cmd[async]: Proc::Background: $cmd", "\n";
236            
237      my @cmd = split(' ', $cmd);      # V3.1 - using IPC::Run
238            #
239      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'.");  
240            
241      #$IPC::Run::Timer::timeout = 2000;      if (RUNNING_IN_HELL()) {
     #start $cmd or die("IPC::Run could not start '$cmd'.");  
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 {    } else {
346      print "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 "run_cmd: ready.", "\n";    print STDOUT "run_cmd: ready.", "\n";
352        
353  }  }
354    
# Line 211  sub bool2status { Line 374  sub bool2status {
374    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
375  }  }
376    
 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  
   
377  # create global unique identifers using Data::UUID  # create global unique identifers using Data::UUID
378  # if updating this code, please also modify Tangram::Storage::make_guid  # if updating this code, please also modify Tangram::Storage::make_guid
379  sub make_guid  sub make_guid
# Line 240  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__

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

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