/[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.14 by joko, Tue Jun 24 20:13:18 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.14  2003/06/24 20:13:18  joko
6    ##  + sub findpatch
7    ##  + now using findpatch and Proc::Background for win32/perl
8    ##
9    ##  Revision 1.13  2003/06/23 20:58:31  joko
10    ##  restructured, hopefully makes Linux and Windows (and *BSD) more compatible...  what about IPC::Cmd???
11    ##
12    ##  Revision 1.12  2003/06/23 19:43:19  joko
13    ##  minor cleanup
14    ##  now using IPC::Session::NoShell
15    ##
16    ##  Revision 1.11  2003/06/23 17:41:50  jonen
17    ##  + NEW - used IPC::Session instead of IPC::Run to get better results at linux
18    ##
19  ##  Revision 1.10  2003/06/23 15:59:16  joko  ##  Revision 1.10  2003/06/23 15:59:16  joko
20  ##  major/minor fixes?  ##  major/minor fixes?
21  ##  ##
# Line 57  our @EXPORT_OK = qw( Line 71  our @EXPORT_OK = qw(
71  use Data::Dumper;  use Data::Dumper;
72  use POSIX qw( strftime );  use POSIX qw( strftime );
73  #use IPC::Run qw( run timeout );  #use IPC::Run qw( run timeout );
74  use IPC::Run qw( start pump finish timeout run ) ;  use IPC::Run qw( start pump finish timeout run timer ) ;
75  use Carp;  use Carp;
76    
77    # NEW - 2003-06-23 for Linux (what about *BSD?)
78    use IPC::Session;
79    
80    use File::Spec;
81    use Proc::Background;
82    
83    
84  # $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 95  sub today {
95    return strftime("%Y-%m-%d", localtime);    return strftime("%Y-%m-%d", localtime);
96  }  }
97    
98    sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
99    
100    
101  sub get_executable {  sub get_executable {
102    my $cmd = shift;    my $cmd = shift;
103    # 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 105  sub get_executable {
105    # => better use absolute path-names only?!    # => better use absolute path-names only?!
106    my $application = '';    my $application = '';
107    if ($cmd =~ m/\w+\.pl\s*.*/) {    if ($cmd =~ m/\w+\.pl\s*.*/) {
108      $application = 'perl ';        $application = get_interpreter_wrapper($cmd, 'perl');
109          #$cmd = "$application $cmd" if $application;
110          $application .= ' ';
111        
112    } else {    } else {
113      $application = './';      $application = './';
114    }    }
115    return $application;    return $application;
116  }  }
117    
118  sub get_executable_wrapper {  sub get_interpreter_wrapper {
119    my $cmd = shift;    my $cmd = shift;
120    my $application = '';    my $language = shift;
121    # Required to adapt to IPC::Run on win32.    $language ||= '';
122    if (RUNNING_IN_HELL()) {  
123      #$application = 'cmd.exe /C';    my $wrapper = '';
124      $application = 'cmd.exe /C';  
125      if ($language eq 'perl') {
126      
127        if (RUNNING_IN_HELL()) {
128          # Required to adapt to IPC::Run on win32.
129          #$wrapper = 'cmd.exe /C perl';
130          #$wrapper = 'start perl';
131          $wrapper = 'perl';
132          #$wrapper = 'cmd.exe /C';
133        } else {
134          # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
135          #  whats about Win32?
136          $wrapper = 'perl';
137        }
138        
139      } else {
140        die("No wrapper for language '$language'.");
141    }    }
142    return $application;    
143      return $wrapper;
144  }  }
145    
146    
# Line 106  sub run_cmd { Line 148  sub run_cmd {
148    my $cmd = shift;    my $cmd = shift;
149    my $caption = shift;    my $caption = shift;
150    my $options = shift;    my $options = shift;
   #$cmd = 'perl ' . $cmd;  
151        
152    #print Dumper($options);    #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
153        
154    # report - header    # report - header
155    my $sep = "-" x 60;    my $sep = "-" x 60;
156    print $sep, "\n";    print STDOUT $sep, "\n";
157    #print "  ", $cmd, "\n";    print STDOUT "  ", $cmd;
158    #print "  ", "  $caption", "\n" if $caption;    print STDOUT " - ", $caption if $caption;
159    print "  ", $cmd;    print STDOUT "\n", $sep, "\n";
   print " - ", $caption if $caption;  
   print "\n";  
   print $sep, "\n";  
160        
161    # strip name of executable from full command string    # strip name of executable from full command string
162    $cmd =~ m/(.+?)\s/;    $cmd =~ m/^(.+?)\s(.*)$/;
163    my $executable = $1;    my $executable = $1;
164      my $executable_args = $2;
165        
166  =pod  =pod
167    # 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 180  sub run_cmd {
180    if ($basedir) {    if ($basedir) {
181      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");      -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
182      $basedir .= '/';      $basedir .= '/';
183        $cmd = "$basedir$cmd";
184    } elsif ($use_path) {    } elsif ($use_path) {
185      $basedir = "";      #$basedir = "";
186        $basedir = findpath($executable);
187        #print "basedir: $basedir", "\n";
188        my $abspath = File::Spec->catfile($basedir, $executable);
189        #print STDOUT "PATH: ", $abspath, "\n";
190        -e $abspath or die("$abspath does not exist.");
191        $cmd = $abspath . ' ' . $executable_args;
192    } else {    } else {
193      -e $executable or die("$executable does not exist.");      -e $executable or die("$executable does not exist.");
194      #$basedir = ".";      #$basedir = ".";
195      #$basedir .= './';      #$basedir .= './';
196        $basedir = "";
197        $cmd = "$basedir$cmd";
198    }    }
   $cmd = "$basedir$cmd";  
199    
200    # V1 - backticks or qq{}    # V1 - backticks or qq{}
201    #`$cmd`;    #`$cmd`;
# Line 157  sub run_cmd { Line 204  sub run_cmd {
204    # V2 - via 'system'    # V2 - via 'system'
205    #system($cmd);    #system($cmd);
206    
207    if (not $use_path) {    #if (not $use_path) {
208      my $application = get_executable($cmd);      my $application = get_executable($cmd);
209      $cmd = "$application$cmd" if $application;      $cmd = "$application$cmd" if $application;
210    }    #}
211    
212    # V3 - using IPC::Run (optional)    # V3 - using IPC (optional)
213    if ($options->{async}) {    if ($options->{async}) {
214      my $application = get_executable_wrapper($cmd);  
     $cmd = "$application $cmd" if $application;  
       
     print "run_cmd: IPC::Run: $cmd", "\n";  
215      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
       
216      my @cmd = split(' ', $cmd);      my @cmd = split(' ', $cmd);
217            
     my $in; my $out; my $err;  
     #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'.");  
218            
219      #$IPC::Run::Timer::timeout = 2000;      # V3.1 - using IPC::Run
220      #start $cmd or die("IPC::Run could not start '$cmd'.");      #
221        # tests:
222        
223        if (RUNNING_IN_HELL()) {
224        
225          #my $in; my $out; my $err;
226          print STDOUT "run_cmd: IPC::Run: $cmd", "\n";
227          
228          #print STDOUT "findpath: ", findpath('rap.pl'), "\n";
229          
230          # no success!
231          #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");
232          #
233          # success on Win32, but seems broken at 'timeout' on linux:
234          #run(\@cmd, timeout(3)) or croak("run_cmd: IPC::Run could not start '$cmd'.");
235          #start(\@cmd, timer(2)) or croak("run_cmd: IPC::Run could not start '$cmd'.");
236          #start(\@cmd, \undef) or croak("run_cmd: IPC::Run could not start '$cmd'.");
237          #start \@cmd or croak("run_cmd: IPC::Run could not start '$cmd'.");
238          #start(\@cmd, timeout(1)) or croak("run_cmd: IPC::Run could not start '$cmd'.");      
239              
240          # other tests ;)
241          #$IPC::Run::Timer::timeout = 2000;
242          #start $cmd or die("IPC::Run could not start '$cmd'.");
243          
244          #my $in; my $out; my $t;
245          #my $harness = start( \@cmd, \$in, \$out, $t = timer( 5 ) ) ;
246          #my $harness = start( \@cmd ) ;
247          #$harness->pump_nb();
248          
249          #my $postfix = '2>&1 |';
250          #open PIPE, "$cmd $postfix" or die("run_cmd: could not run in background via open!");
251          
252          my $proc1 = Proc::Background->new(@cmd);
253          print "pid: ", $proc1->pid(), "\n";
254    
255          
256        } else {
257    
258          print STDOUT "run_cmd: IPC::Session: $cmd", "\n";
259    
260          # V3.2 - using IPC::Session
261          #  success on Linux AND Win32 ??
262          #
263          # set timeout:
264          #  (don't really know why we needs 2 secconds
265          #   to wait for init of process !?!)
266          my $session_timeout = 3;
267          # set session name (default: cmd as string):
268          my $session_name = $cmd;
269          # create session:
270          my $session = IPC::Session->new($session_name, $session_timeout);
271          
272          # send 'cmd' to session - not required since complete command is sent via constructor above
273          $session->send(\@cmd);
274          
275          #print $session->stdout(), "\n";
276          
277          # optional switch case:
278          #for ($session->stdout()) {
279          #}
280          # optional get error:
281          #my $err = session->stderr();
282        }
283            
284    } else {    } else {
285      print "run_cmd: system('$cmd').", "\n";      print STDOUT "run_cmd: system('$cmd').", "\n";
286      system($cmd);      system($cmd);
287    }    }
288        
289    print "run_cmd: ready.", "\n";    print STDOUT "run_cmd: ready.", "\n";
290        
291  }  }
292    
# Line 211  sub bool2status { Line 312  sub bool2status {
312    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
313  }  }
314    
 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  
   
315  # create global unique identifers using Data::UUID  # create global unique identifers using Data::UUID
316  # if updating this code, please also modify Tangram::Storage::make_guid  # if updating this code, please also modify Tangram::Storage::make_guid
317  sub make_guid  sub make_guid
# Line 240  sub make_guid Line 339  sub make_guid
339      return $guid;      return $guid;
340    }    }
341    
342    # [modified] from: http://www.mit.edu/afs/athena/contrib/watchmaker/src/pt/Configure
343    sub findpath {
344        #local($path);
345        #local($arg) = shift;
346        my $arg = shift;
347        my $path;
348        #for my $dir (split(/:/,$ENV{'PATH'})) {
349        for my $dir (File::Spec->path()) {
350            #if (-x "$dir/$arg" && -f _) {
351            #print STDOUT "scanning: ", "$dir/$arg", "\n";
352            if (-e "$dir/$arg") {
353                #$path = "$dir/$arg";
354                #$path = "$dir/";
355                $path = $dir;
356                last;
357            }
358        }
359        $path;
360    }
361    
362  1;  1;
363    __END__

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

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