/[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.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
24    ##  + sub make_guid
25    ##
26    ##  Revision 1.7  2003/03/29 07:24:10  joko
27    ##  enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl')
28    ##
29    ##  Revision 1.6  2003/03/28 06:58:06  joko
30    ##  new: 'run_cmd' now asynchronous! (via IPC::Run...)
31    ##
32    ##  Revision 1.5  2003/02/22 17:26:13  joko
33    ##  + enhanced unix compatibility fix
34    ##
35    ##  Revision 1.4  2003/02/22 17:19:36  joko
36    ##  + unix compatibility fix
37    ##
38  ##  Revision 1.3  2003/02/14 14:17:04  joko  ##  Revision 1.3  2003/02/14 14:17:04  joko
39  ##  - shortened seperator  ##  - shortened seperator
40  ##  ##
# Line 27  our @EXPORT_OK = qw( Line 60  our @EXPORT_OK = qw(
60    run_cmd run_cmds    run_cmd run_cmds
61    get_chomped    get_chomped
62    bool2status    bool2status
63      make_guid
64  );  );
65    
66    
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main  
   
67  use Data::Dumper;  use Data::Dumper;
68  use POSIX qw(strftime);  use POSIX qw( strftime );
69    #use IPC::Run qw( run timeout );
70    use IPC::Run qw( start pump finish timeout run ) ;
71    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;
78  # see "perldoc -f localtime"  # see "perldoc -f localtime"
   
79  sub now {  sub now {
80    my $options = shift;    my $options = shift;
81    my $pattern = "%Y-%m-%d %H:%M:%S";    my $pattern = "%Y-%m-%d %H:%M:%S";
# Line 50  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    #$cmd = 'perl ' . $cmd;    my $options = shift;
141      
142      #print Dumper($options);
143      
144      # 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 $sep, "\n";    print STDERR "\n", $sep, "\n";
150    system($cmd);    
151      # strip name of executable from full command string
152      $cmd =~ m/(.+?)\s/;
153      my $executable = $1;
154      
155    =pod
156      # for unix: check if executable is in local directory, if so - prefix with './'
157      if (!RUNNING_IN_HELL()) {
158        #if ($cmd !~ m/\//) {
159        if (-e $executable) {
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{}
183    #`$cmd`;    #`$cmd`;
184    print "ready.", "\n";    #qq{$cmd};
185      
186      # V2 - via 'system'
187      #system($cmd);
188    
189      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}) {
196    
197        #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
198        my @cmd = split(' ', $cmd);
199        
200        
201        # V3.1 - using IPC::Run
202        #
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 {
249        print STDERR "run_cmd: system('$cmd').", "\n";
250        system($cmd);
251      }
252      
253      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 81  sub bool2status { Line 276  sub bool2status {
276    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
277  }  }
278    
279    # create global unique identifers using Data::UUID
280    # if updating this code, please also modify Tangram::Storage::make_guid
281    sub make_guid
282      {
283        my $self = shift;
284    
285        my $guid;
286    
287        # try to use Data::UUID first ...
288        eval("use Data::UUID;");
289        if (!$@) {
290          my $ug = Data::UUID->new();
291          $guid = $ug->create_str();
292          
293        # ... if this fails, try to fallback to Data::UUID::PurePerl instead ...
294        } else {
295          eval("use Data::UUID::PurePerl;");
296          if (!$@) {
297            $guid = Data::UUID::PurePerl::generate_id();
298          } else {
299            croak "couldn't create globally unique identifier";
300          }
301        }
302        
303        return $guid;
304      }
305    
306  1;  1;
307    __END__

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

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