/[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.8 by joko, Fri Apr 4 17:31:59 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
16    ##  major/minor fixes?
17    ##
18    ##  Revision 1.9  2003/05/13 05:36:24  joko
19    ##  heavy modifications to run_cmd
20    ##  + sub get_executable
21    ##  + sub get_executable_wrapper
22    ##
23  ##  Revision 1.8  2003/04/04 17:31:59  joko  ##  Revision 1.8  2003/04/04 17:31:59  joko
24  ##  + sub make_guid  ##  + sub make_guid
25  ##  ##
# Line 49  our @EXPORT_OK = qw( Line 67  our @EXPORT_OK = qw(
67  use Data::Dumper;  use Data::Dumper;
68  use POSIX qw( strftime );  use POSIX qw( strftime );
69  #use IPC::Run qw( run timeout );  #use IPC::Run qw( run timeout );
70  use IPC::Run qw( start pump finish timeout ) ;  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 68  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 {
95      my $cmd = shift;
96      # FIXME: detect type of program and run with proper application/interpreter
97      # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!?
98      # => better use absolute path-names only?!
99      my $application = '';
100      if ($cmd =~ m/\w+\.pl\s*.*/) {
101          $application = get_interpreter_wrapper($cmd, 'perl');
102          #$cmd = "$application $cmd" if $application;
103          $application .= ' ';
104        
105      } else {
106        $application = './';
107      }
108      return $application;
109    }
110    
111    sub get_interpreter_wrapper {
112      my $cmd = shift;
113      my $language = shift;
114      $language ||= '';
115    
116      my $wrapper = '';
117    
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      
133      return $wrapper;
134    }
135    
136    
137  sub run_cmd {  sub run_cmd {
138    my $cmd = shift;    my $cmd = shift;
139    my $caption = shift;    my $caption = shift;
140    my $options = shift;    my $options = shift;
141    #$cmd = 'perl ' . $cmd;    
142      #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/;
153    my $executable = $1;    my $executable = $1;
154        
155    =pod
156    # 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 './'
157    if (!RUNNING_IN_HELL()) {    if (!RUNNING_IN_HELL()) {
158      #if ($cmd !~ m/\//) {      #if ($cmd !~ m/\//) {
159      if (-e $executable) {      if (-e $executable) {
       $cmd = "./$cmd";  
160      }      }
161    }    }
162      =cut
163    
164      # new of 2003-05-07: basedir option to be prepended to command string
165      my $basedir = $options->{BASEDIR};
166      my $use_path = $options->{USE_PATH};
167    
168      # for all systems: check existance of files - use basedir if given, try current directory otherwise
169      if ($basedir) {
170        -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
171        $basedir .= '/';
172      } elsif ($use_path) {
173        $basedir = "";
174      } else {
175        -e $executable or die("$executable does not exist.");
176        #$basedir = ".";
177        #$basedir .= './';
178        $basedir = "";
179      }
180      $cmd = "$basedir$cmd";
181    
182    # V1 - backticks or qq{}    # V1 - backticks or qq{}
183    #`$cmd`;    #`$cmd`;
184    #qq{$cmd};    #qq{$cmd};
# Line 103  sub run_cmd { Line 186  sub run_cmd {
186    # V2 - via 'system'    # V2 - via 'system'
187    #system($cmd);    #system($cmd);
188    
189    # V3 - using IPC::Run (optional)    if (not $use_path) {
190        my $application = get_executable($cmd);
191        $cmd = "$application$cmd" if $application;
192      }
193    
194      # V3 - using IPC (optional)
195    if ($options->{async}) {    if ($options->{async}) {
196      # FIXME: detect type of program and run with proper application/interpreter  
     # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!  
     # => better use absolute path-names only?!  
     my $application = '';  
     if (RUNNING_IN_HELL()) {  
       $application = 'cmd.exe /C';  
     }  
       
     if ($cmd =~ m/\w+\.pl\s*.*/) {  
       $application = 'perl';  
     }  
       
     $cmd = "$application $cmd" if $application;  
       
     print "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;  
     start \@cmd, timeout(0) or die("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 STDERR "run_cmd: system('$cmd').", "\n";
250      system($cmd);      system($cmd);
251    }    }
252        
253    print "ready.", "\n";    print STDERR "run_cmd: ready.", "\n";
254        
255  }  }
256    
257  sub run_cmds {  sub run_cmds {
258      my $options = {};
259      if (ref $_[$#_] eq 'HASH') {
260        #print "YAI", "\n";
261        $options = pop @_;
262      }
263    foreach (@_) {    foreach (@_) {
264      run_cmd($_);      run_cmd($_, '', $options);
265    }    }
266  }  }
267    
# Line 155  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 185  sub make_guid Line 304  sub make_guid
304    }    }
305    
306  1;  1;
307    __END__

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

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