/[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.13 by joko, Mon Jun 23 20:58:31 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.13  2003/06/23 20:58:31  joko
6    ##  restructured, hopefully makes Linux and Windows (and *BSD) more compatible...  what about IPC::Cmd???
7    ##
8    ##  Revision 1.12  2003/06/23 19:43:19  joko
9    ##  minor cleanup
10    ##  now using IPC::Session::NoShell
11    ##
12    ##  Revision 1.11  2003/06/23 17:41:50  jonen
13    ##  + NEW - used IPC::Session instead of IPC::Run to get better results at linux
14    ##
15  ##  Revision 1.10  2003/06/23 15:59:16  joko  ##  Revision 1.10  2003/06/23 15:59:16  joko
16  ##  major/minor fixes?  ##  major/minor fixes?
17  ##  ##
# Line 60  use POSIX qw( strftime ); Line 70  use POSIX qw( strftime );
70  use IPC::Run qw( start pump finish timeout run ) ;  use IPC::Run qw( start pump finish timeout run ) ;
71  use Carp;  use Carp;
72    
73    # NEW - 2003-06-23 for Linux (what about *BSD?)
74    use IPC::Session;
75    
76    
77  # $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 88  sub today {
88    return strftime("%Y-%m-%d", localtime);    return strftime("%Y-%m-%d", localtime);
89  }  }
90    
91    sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
92    
93    
94  sub get_executable {  sub get_executable {
95    my $cmd = shift;    my $cmd = shift;
96    # 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 98  sub get_executable {
98    # => better use absolute path-names only?!    # => better use absolute path-names only?!
99    my $application = '';    my $application = '';
100    if ($cmd =~ m/\w+\.pl\s*.*/) {    if ($cmd =~ m/\w+\.pl\s*.*/) {
101      $application = 'perl ';        $application = get_interpreter_wrapper($cmd, 'perl');
102          #$cmd = "$application $cmd" if $application;
103          $application .= ' ';
104        
105    } else {    } else {
106      $application = './';      $application = './';
107    }    }
108    return $application;    return $application;
109  }  }
110    
111  sub get_executable_wrapper {  sub get_interpreter_wrapper {
112    my $cmd = shift;    my $cmd = shift;
113    my $application = '';    my $language = shift;
114    # Required to adapt to IPC::Run on win32.    $language ||= '';
115    if (RUNNING_IN_HELL()) {  
116      #$application = 'cmd.exe /C';    my $wrapper = '';
117      $application = 'cmd.exe /C';  
118      if ($language eq 'perl') {
119      
120        if (RUNNING_IN_HELL()) {
121          # Required to adapt to IPC::Run on win32.
122          $wrapper = 'cmd.exe /C perl';
123        } else {
124          # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
125          #  whats about Win32?
126          $wrapper = 'perl';
127        }
128        
129      } else {
130        die("No wrapper for language '$language'.");
131    }    }
132    return $application;    
133      return $wrapper;
134  }  }
135    
136    
# Line 106  sub run_cmd { Line 138  sub run_cmd {
138    my $cmd = shift;    my $cmd = shift;
139    my $caption = shift;    my $caption = shift;
140    my $options = shift;    my $options = shift;
   #$cmd = 'perl ' . $cmd;  
141        
142    #print Dumper($options);    #print Dumper($options);
143        
144    # report - header    # report - header
145    my $sep = "-" x 60;    my $sep = "-" x 60;
146    print $sep, "\n";    print STDERR $sep, "\n";
147    #print "  ", $cmd, "\n";    print STDERR "  ", $cmd;
148    #print "  ", "  $caption", "\n" if $caption;    print STDERR " - ", $caption if $caption;
149    print "  ", $cmd;    print STDERR "\n", $sep, "\n";
   print " - ", $caption if $caption;  
   print "\n";  
   print $sep, "\n";  
150        
151    # strip name of executable from full command string    # strip name of executable from full command string
152    $cmd =~ m/(.+?)\s/;    $cmd =~ m/(.+?)\s/;
# Line 147  sub run_cmd { Line 175  sub run_cmd {
175      -e $executable or die("$executable does not exist.");      -e $executable or die("$executable does not exist.");
176      #$basedir = ".";      #$basedir = ".";
177      #$basedir .= './';      #$basedir .= './';
178        $basedir = "";
179    }    }
180    $cmd = "$basedir$cmd";    $cmd = "$basedir$cmd";
181    
# Line 162  sub run_cmd { Line 191  sub run_cmd {
191      $cmd = "$application$cmd" if $application;      $cmd = "$application$cmd" if $application;
192    }    }
193    
194    # V3 - using IPC::Run (optional)    # V3 - using IPC (optional)
195    if ($options->{async}) {    if ($options->{async}) {
196      my $application = get_executable_wrapper($cmd);  
     $cmd = "$application $cmd" if $application;  
       
     print "run_cmd: IPC::Run: $cmd", "\n";  
197      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
       
198      my @cmd = split(' ', $cmd);      my @cmd = split(' ', $cmd);
199            
     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'.");  
200            
201      #$IPC::Run::Timer::timeout = 2000;      # V3.1 - using IPC::Run
202      #start $cmd or die("IPC::Run could not start '$cmd'.");      #
203        # tests:
204        
205        if (RUNNING_IN_HELL()) {
206        
207          #my $in; my $out; my $err;
208          print STDERR "run_cmd: IPC::Run: $cmd", "\n";
209          
210          # no success!
211          #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");
212          #
213          # success on Win32, but seems broken at 'timeout' on linux:
214          run(\@cmd, timeout(4)) or croak("run_cmd: IPC::Run could not start '$cmd'.");
215              
216          # other tests ;)
217          #$IPC::Run::Timer::timeout = 2000;
218          #start $cmd or die("IPC::Run could not start '$cmd'.");
219          
220        } else {
221    
222          print STDERR "run_cmd: IPC::Session: $cmd", "\n";
223    
224          # V3.2 - using IPC::Session
225          #  success on Linux AND Win32 ??
226          #
227          # set timeout:
228          #  (don't really know why we needs 2 secconds
229          #   to wait for init of process !?!)
230          my $session_timeout = 3;
231          # set session name (default: cmd as string):
232          my $session_name = $cmd;
233          # create session:
234          my $session = IPC::Session->new($session_name, $session_timeout);
235          
236          # send 'cmd' to session - not required since complete command is sent via constructor above
237          $session->send(\@cmd);
238          
239          #print $session->stdout(), "\n";
240          
241          # optional switch case:
242          #for ($session->stdout()) {
243          #}
244          # optional get error:
245          #my $err = session->stderr();
246        }
247            
248    } else {    } else {
249      print "run_cmd: system('$cmd').", "\n";      print STDERR "run_cmd: system('$cmd').", "\n";
250      system($cmd);      system($cmd);
251    }    }
252        
253    print "run_cmd: ready.", "\n";    print STDERR "run_cmd: ready.", "\n";
254        
255  }  }
256    
# Line 211  sub bool2status { Line 276  sub bool2status {
276    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
277  }  }
278    
 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  
   
279  # create global unique identifers using Data::UUID  # create global unique identifers using Data::UUID
280  # if updating this code, please also modify Tangram::Storage::make_guid  # if updating this code, please also modify Tangram::Storage::make_guid
281  sub make_guid  sub make_guid
# Line 241  sub make_guid Line 304  sub make_guid
304    }    }
305    
306  1;  1;
307    __END__

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

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