/[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.12 by joko, Mon Jun 23 19:43:19 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.12  2003/06/23 19:43:19  joko
6    ##  minor cleanup
7    ##  now using IPC::Session::NoShell
8    ##
9    ##  Revision 1.11  2003/06/23 17:41:50  jonen
10    ##  + NEW - used IPC::Session instead of IPC::Run to get better results at linux
11    ##
12  ##  Revision 1.10  2003/06/23 15:59:16  joko  ##  Revision 1.10  2003/06/23 15:59:16  joko
13  ##  major/minor fixes?  ##  major/minor fixes?
14  ##  ##
# Line 60  use POSIX qw( strftime ); Line 67  use POSIX qw( strftime );
67  use IPC::Run qw( start pump finish timeout run ) ;  use IPC::Run qw( start pump finish timeout run ) ;
68  use Carp;  use Carp;
69    
70    # NEW - 2003-06-23
71    use IPC::Session::NoShell;
72    
73    
74  # $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 85  sub today {
85    return strftime("%Y-%m-%d", localtime);    return strftime("%Y-%m-%d", localtime);
86  }  }
87    
88    sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
89    
90    
91  sub get_executable {  sub get_executable {
92    my $cmd = shift;    my $cmd = shift;
93    # 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 95  sub get_executable {
95    # => better use absolute path-names only?!    # => better use absolute path-names only?!
96    my $application = '';    my $application = '';
97    if ($cmd =~ m/\w+\.pl\s*.*/) {    if ($cmd =~ m/\w+\.pl\s*.*/) {
98        # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
99        #  whats about Win32?
100      $application = 'perl ';      $application = 'perl ';
101    } else {    } else {
102      $application = './';      $application = './';
# Line 94  sub get_executable_wrapper { Line 108  sub get_executable_wrapper {
108    my $cmd = shift;    my $cmd = shift;
109    my $application = '';    my $application = '';
110    # Required to adapt to IPC::Run on win32.    # Required to adapt to IPC::Run on win32.
111    if (RUNNING_IN_HELL()) {    #if (RUNNING_IN_HELL()) {
112      #$application = 'cmd.exe /C';      #$application = 'cmd.exe /C';
113      $application = 'cmd.exe /C';    #}
   }  
114    return $application;    return $application;
115  }  }
116    
# Line 106  sub run_cmd { Line 119  sub run_cmd {
119    my $cmd = shift;    my $cmd = shift;
120    my $caption = shift;    my $caption = shift;
121    my $options = shift;    my $options = shift;
   #$cmd = 'perl ' . $cmd;  
122        
123    #print Dumper($options);    #print Dumper($options);
124        
125    # report - header    # report - header
126    my $sep = "-" x 60;    my $sep = "-" x 60;
127    print $sep, "\n";    print STDERR $sep, "\n";
128    #print "  ", $cmd, "\n";    print STDERR "  ", $cmd;
129    #print "  ", "  $caption", "\n" if $caption;    print STDERR " - ", $caption if $caption;
130    print "  ", $cmd;    print STDERR "\n", $sep, "\n";
   print " - ", $caption if $caption;  
   print "\n";  
   print $sep, "\n";  
131        
132    # strip name of executable from full command string    # strip name of executable from full command string
133    $cmd =~ m/(.+?)\s/;    $cmd =~ m/(.+?)\s/;
# Line 147  sub run_cmd { Line 156  sub run_cmd {
156      -e $executable or die("$executable does not exist.");      -e $executable or die("$executable does not exist.");
157      #$basedir = ".";      #$basedir = ".";
158      #$basedir .= './';      #$basedir .= './';
159        $basedir = "";
160    }    }
161    $cmd = "$basedir$cmd";    $cmd = "$basedir$cmd";
162    
# Line 162  sub run_cmd { Line 172  sub run_cmd {
172      $cmd = "$application$cmd" if $application;      $cmd = "$application$cmd" if $application;
173    }    }
174    
175    # V3 - using IPC::Run (optional)    # V3 - using IPC (optional)
176    if ($options->{async}) {    if ($options->{async}) {
177      my $application = get_executable_wrapper($cmd);  
178      $cmd = "$application $cmd" if $application;      #$cmd = "$application $cmd" if $application;
179        #my $application = get_executable_wrapper($cmd);
180            
181      print "run_cmd: IPC::Run: $cmd", "\n";      print STDERR "run_cmd: IPC::Session::NoShell: $cmd", "\n";
182      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
183                
184      my @cmd = split(' ', $cmd);      my @cmd = split(' ', $cmd);
185            
186      my $in; my $out; my $err;      
187        # V3.1 - using IPC::Run
188        #
189        # tests:
190        #my $in; my $out; my $err;
191      #print "IPC::Run: $cmd", "\n";      #print "IPC::Run: $cmd", "\n";
192      #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");      #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");
193      run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'.");      #
194            # success on Win32, but seems broken at 'timeout' on linux:
195        #run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'.");
196            
197        # other tests ;)
198      #$IPC::Run::Timer::timeout = 2000;      #$IPC::Run::Timer::timeout = 2000;
199      #start $cmd or die("IPC::Run could not start '$cmd'.");      #start $cmd or die("IPC::Run could not start '$cmd'.");
200    
201    
202        # V3.2 - using IPC::Session
203        #  success on Linux AND Win32 ??
204        #
205        # set timeout:
206        #  (don't really know why we needs 2 secconds
207        #   to wait for init of process !?!)
208        my $session_timeout = 2;
209        # set session name (default: cmd as string):
210        my $session_command = $cmd;
211        # create session:
212        my $session = new IPC::Session::NoShell($session_command, $session_timeout);
213        
214        # send 'cmd' to session - not required since complete command is sent via constructor above
215        #$session->send(\@cmd);
216        
217        # optional switch case:
218        #for ($session->stdout()) {
219        #}
220        # optional get error:
221        #my $err = session->stderr();
222    
223            
224    } else {    } else {
225      print "run_cmd: system('$cmd').", "\n";      print STDERR "run_cmd: system('$cmd').", "\n";
226      system($cmd);      system($cmd);
227    }    }
228        
229    print "run_cmd: ready.", "\n";    print STDERR "run_cmd: ready.", "\n";
230        
231  }  }
232    
# Line 211  sub bool2status { Line 252  sub bool2status {
252    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
253  }  }
254    
 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  
   
255  # create global unique identifers using Data::UUID  # create global unique identifers using Data::UUID
256  # if updating this code, please also modify Tangram::Storage::make_guid  # if updating this code, please also modify Tangram::Storage::make_guid
257  sub make_guid  sub make_guid
# Line 241  sub make_guid Line 280  sub make_guid
280    }    }
281    
282  1;  1;
283    __END__

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

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