/[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.3 by joko, Fri Feb 14 14:17:04 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
20    ##  major/minor fixes?
21    ##
22    ##  Revision 1.9  2003/05/13 05:36:24  joko
23    ##  heavy modifications to run_cmd
24    ##  + sub get_executable
25    ##  + sub get_executable_wrapper
26    ##
27    ##  Revision 1.8  2003/04/04 17:31:59  joko
28    ##  + sub make_guid
29    ##
30    ##  Revision 1.7  2003/03/29 07:24:10  joko
31    ##  enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl')
32    ##
33    ##  Revision 1.6  2003/03/28 06:58:06  joko
34    ##  new: 'run_cmd' now asynchronous! (via IPC::Run...)
35    ##
36    ##  Revision 1.5  2003/02/22 17:26:13  joko
37    ##  + enhanced unix compatibility fix
38    ##
39    ##  Revision 1.4  2003/02/22 17:19:36  joko
40    ##  + unix compatibility fix
41    ##
42  ##  Revision 1.3  2003/02/14 14:17:04  joko  ##  Revision 1.3  2003/02/14 14:17:04  joko
43  ##  - shortened seperator  ##  - shortened seperator
44  ##  ##
# Line 27  our @EXPORT_OK = qw( Line 64  our @EXPORT_OK = qw(
64    run_cmd run_cmds    run_cmd run_cmds
65    get_chomped    get_chomped
66    bool2status    bool2status
67      make_guid
68  );  );
69    
70    
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main  
   
71  use Data::Dumper;  use Data::Dumper;
72  use POSIX qw(strftime);  use POSIX qw( strftime );
73    #use IPC::Run qw( run timeout );
74    use IPC::Run qw( start pump finish timeout run timer ) ;
75    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;
85  # see "perldoc -f localtime"  # see "perldoc -f localtime"
   
86  sub now {  sub now {
87    my $options = shift;    my $options = shift;
88    my $pattern = "%Y-%m-%d %H:%M:%S";    my $pattern = "%Y-%m-%d %H:%M:%S";
# Line 50  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 {
102      my $cmd = shift;
103      # FIXME: detect type of program and run with proper application/interpreter
104      # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!?
105      # => better use absolute path-names only?!
106      my $application = '';
107      if ($cmd =~ m/\w+\.pl\s*.*/) {
108          $application = get_interpreter_wrapper($cmd, 'perl');
109          #$cmd = "$application $cmd" if $application;
110          $application .= ' ';
111        
112      } else {
113        $application = './';
114      }
115      return $application;
116    }
117    
118    sub get_interpreter_wrapper {
119      my $cmd = shift;
120      my $language = shift;
121      $language ||= '';
122    
123      my $wrapper = '';
124    
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      
143      return $wrapper;
144    }
145    
146    
147  sub run_cmd {  sub run_cmd {
148    my $cmd = shift;    my $cmd = shift;
149    my $caption = shift;    my $caption = shift;
150    #$cmd = 'perl ' . $cmd;    my $options = shift;
151      
152      #print STDOUT "run_cmd - options: ", Dumper($options), "\n";
153      
154      # 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 $sep, "\n";    print STDOUT "\n", $sep, "\n";
160    system($cmd);    
161      # strip name of executable from full command string
162      $cmd =~ m/^(.+?)\s(.*)$/;
163      my $executable = $1;
164      my $executable_args = $2;
165      
166    =pod
167      # for unix: check if executable is in local directory, if so - prefix with './'
168      if (!RUNNING_IN_HELL()) {
169        #if ($cmd !~ m/\//) {
170        if (-e $executable) {
171        }
172      }
173    =cut
174    
175      # new of 2003-05-07: basedir option to be prepended to command string
176      my $basedir = $options->{BASEDIR};
177      my $use_path = $options->{USE_PATH};
178    
179      # for all systems: check existance of files - use basedir if given, try current directory otherwise
180      if ($basedir) {
181        -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
182        $basedir .= '/';
183        $cmd = "$basedir$cmd";
184      } elsif ($use_path) {
185        #$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 {
193        -e $executable or die("$executable does not exist.");
194        #$basedir = ".";
195        #$basedir .= './';
196        $basedir = "";
197        $cmd = "$basedir$cmd";
198      }
199    
200      # V1 - backticks or qq{}
201    #`$cmd`;    #`$cmd`;
202    print "ready.", "\n";    #qq{$cmd};
203      
204      # V2 - via 'system'
205      #system($cmd);
206    
207      #if (not $use_path) {
208        my $application = get_executable($cmd);
209        $cmd = "$application$cmd" if $application;
210      #}
211    
212      # V3 - using IPC (optional)
213      if ($options->{async}) {
214    
215        #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
216        my @cmd = split(' ', $cmd);
217        
218        
219        # V3.1 - using IPC::Run
220        #
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 {
285        print STDOUT "run_cmd: system('$cmd').", "\n";
286        system($cmd);
287      }
288      
289      print STDOUT "run_cmd: ready.", "\n";
290      
291  }  }
292    
293  sub run_cmds {  sub run_cmds {
294      my $options = {};
295      if (ref $_[$#_] eq 'HASH') {
296        #print "YAI", "\n";
297        $options = pop @_;
298      }
299    foreach (@_) {    foreach (@_) {
300      run_cmd($_);      run_cmd($_, '', $options);
301    }    }
302  }  }
303    
# Line 81  sub bool2status { Line 312  sub bool2status {
312    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
313  }  }
314    
315    # create global unique identifers using Data::UUID
316    # if updating this code, please also modify Tangram::Storage::make_guid
317    sub make_guid
318      {
319        my $self = shift;
320    
321        my $guid;
322    
323        # try to use Data::UUID first ...
324        eval("use Data::UUID;");
325        if (!$@) {
326          my $ug = Data::UUID->new();
327          $guid = $ug->create_str();
328          
329        # ... if this fails, try to fallback to Data::UUID::PurePerl instead ...
330        } else {
331          eval("use Data::UUID::PurePerl;");
332          if (!$@) {
333            $guid = Data::UUID::PurePerl::generate_id();
334          } else {
335            croak "couldn't create globally unique identifier";
336          }
337        }
338        
339        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.3  
changed lines
  Added in v.1.14

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