/[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.11 by jonen, Mon Jun 23 17:41:50 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ---------------------------------------------------------------------------  ## ---------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.11  2003/06/23 17:41:50  jonen
6    ##  + NEW - used IPC::Session instead of IPC::Run to get better results at linux
7    ##
8    ##  Revision 1.10  2003/06/23 15:59:16  joko
9    ##  major/minor fixes?
10    ##
11    ##  Revision 1.9  2003/05/13 05:36:24  joko
12    ##  heavy modifications to run_cmd
13    ##  + sub get_executable
14    ##  + sub get_executable_wrapper
15    ##
16    ##  Revision 1.8  2003/04/04 17:31:59  joko
17    ##  + sub make_guid
18    ##
19    ##  Revision 1.7  2003/03/29 07:24:10  joko
20    ##  enhanced 'run_cmd': now tries to execute program with appropriate application (e.g. 'cmd.exe' or 'perl')
21    ##
22    ##  Revision 1.6  2003/03/28 06:58:06  joko
23    ##  new: 'run_cmd' now asynchronous! (via IPC::Run...)
24    ##
25    ##  Revision 1.5  2003/02/22 17:26:13  joko
26    ##  + enhanced unix compatibility fix
27    ##
28    ##  Revision 1.4  2003/02/22 17:19:36  joko
29    ##  + unix compatibility fix
30    ##
31  ##  Revision 1.3  2003/02/14 14:17:04  joko  ##  Revision 1.3  2003/02/14 14:17:04  joko
32  ##  - shortened seperator  ##  - shortened seperator
33  ##  ##
# Line 27  our @EXPORT_OK = qw( Line 53  our @EXPORT_OK = qw(
53    run_cmd run_cmds    run_cmd run_cmds
54    get_chomped    get_chomped
55    bool2status    bool2status
56      make_guid
57  );  );
58    
59    
 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main  
   
60  use Data::Dumper;  use Data::Dumper;
61  use POSIX qw(strftime);  use POSIX qw( strftime );
62    #use IPC::Run qw( run timeout );
63    use IPC::Run qw( start pump finish timeout run ) ;
64    use Carp;
65    
66    # NEW - 2003-06-23
67    use IPC::Session;
68    
69    
70  # $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;  # $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime;
71  # see "perldoc -f localtime"  # see "perldoc -f localtime"
   
72  sub now {  sub now {
73    my $options = shift;    my $options = shift;
74    my $pattern = "%Y-%m-%d %H:%M:%S";    my $pattern = "%Y-%m-%d %H:%M:%S";
# Line 50  sub today { Line 81  sub today {
81    return strftime("%Y-%m-%d", localtime);    return strftime("%Y-%m-%d", localtime);
82  }  }
83    
84    sub get_executable {
85      my $cmd = shift;
86      # FIXME: detect type of program and run with proper application/interpreter
87      # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!?
88      # => better use absolute path-names only?!
89      my $application = '';
90      if ($cmd =~ m/\w+\.pl\s*.*/) {
91        # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
92        #  whats about Win32?
93        $application = 'perl ';
94      } else {
95        $application = './';
96      }
97      return $application;
98    }
99    
100    sub get_executable_wrapper {
101      my $cmd = shift;
102      my $application = '';
103      # Required to adapt to IPC::Run on win32.
104      #if (RUNNING_IN_HELL()) {
105        #$application = 'cmd.exe /C';
106      #}
107      return $application;
108    }
109    
110    
111  sub run_cmd {  sub run_cmd {
112    my $cmd = shift;    my $cmd = shift;
113    my $caption = shift;    my $caption = shift;
114      my $options = shift;
115    #$cmd = 'perl ' . $cmd;    #$cmd = 'perl ' . $cmd;
116      
117      #print Dumper($options);
118      
119      # report - header
120    my $sep = "-" x 60;    my $sep = "-" x 60;
121    print $sep, "\n";    print $sep, "\n";
122    print "  ", $cmd, "\n";    #print "  ", $cmd, "\n";
123    print "  ", $caption, "\n" if $caption;    #print "  ", "  $caption", "\n" if $caption;
124      print "  ", $cmd;
125      print " - ", $caption if $caption;
126      print "\n";
127    print $sep, "\n";    print $sep, "\n";
128    system($cmd);    
129      # strip name of executable from full command string
130      $cmd =~ m/(.+?)\s/;
131      my $executable = $1;
132      
133    =pod
134      # for unix: check if executable is in local directory, if so - prefix with './'
135      if (!RUNNING_IN_HELL()) {
136        #if ($cmd !~ m/\//) {
137        if (-e $executable) {
138        }
139      }
140    =cut
141    
142      # new of 2003-05-07: basedir option to be prepended to command string
143      my $basedir = $options->{BASEDIR};
144      my $use_path = $options->{USE_PATH};
145    
146      # for all systems: check existance of files - use basedir if given, try current directory otherwise
147      if ($basedir) {
148        -e "$basedir/$executable" or die("$basedir/$executable does not exist.");
149        $basedir .= '/';
150      } elsif ($use_path) {
151        $basedir = "";
152      } else {
153        -e $executable or die("$executable does not exist.");
154        #$basedir = ".";
155        #$basedir .= './';
156      }
157      $cmd = "$basedir$cmd";
158    
159      # V1 - backticks or qq{}
160    #`$cmd`;    #`$cmd`;
161    print "ready.", "\n";    #qq{$cmd};
162      
163      # V2 - via 'system'
164      #system($cmd);
165    
166      if (not $use_path) {
167        my $application = get_executable($cmd);
168        $cmd = "$application$cmd" if $application;
169      }
170    
171      # V3 - using IPC (optional)
172      if ($options->{async}) {
173    
174        #$cmd = "$application $cmd" if $application;
175        #my $application = get_executable_wrapper($cmd);
176        
177        print "run_cmd: IPC::Run: $cmd", "\n";
178        #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
179            
180        my @cmd = split(' ', $cmd);
181        
182        
183        # V3.1 - using IPC::Run
184        #
185        # tests:
186        #my $in; my $out; my $err;
187        #print "IPC::Run: $cmd", "\n";
188        #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");
189        #
190        # success on Win32, but seems broken at 'timeout' on linux:
191        #run(\@cmd, timeout(2)) or croak("run_cmd: IPC::Run could not start '$cmd'.");
192            
193        # other tests ;)
194        #$IPC::Run::Timer::timeout = 2000;
195        #start $cmd or die("IPC::Run could not start '$cmd'.");
196    
197    
198        # V3.2 - using IPC::Session
199        #  success on Linux AND Win32 ??
200        #
201        # set timeout:
202        #  (don't really know why we needs 2 secconds
203        #   to wait for init of process !?!)
204        my $session_timeout = 2;
205        # set session name (default: cmd as string):
206        my $session_name = $cmd;
207        # create session:
208        my $session = new IPC::Session($session_name, $session_timeout);
209        # send 'cmd' to session:
210        $session->send(\@cmd);
211        # optional switch case:
212        #for ($session->stdout()) {
213        #}
214        # optional get error:
215        #my $err = session->stderr();
216    
217        
218      } else {
219        print "run_cmd: system('$cmd').", "\n";
220        system($cmd);
221      }
222      
223      print "run_cmd: ready.", "\n";
224      
225  }  }
226    
227  sub run_cmds {  sub run_cmds {
228      my $options = {};
229      if (ref $_[$#_] eq 'HASH') {
230        #print "YAI", "\n";
231        $options = pop @_;
232      }
233    foreach (@_) {    foreach (@_) {
234      run_cmd($_);      run_cmd($_, '', $options);
235    }    }
236  }  }
237    
# Line 81  sub bool2status { Line 246  sub bool2status {
246    return ($bool ? 'ok' : 'failed');    return ($bool ? 'ok' : 'failed');
247  }  }
248    
249    sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
250    
251    # create global unique identifers using Data::UUID
252    # if updating this code, please also modify Tangram::Storage::make_guid
253    sub make_guid
254      {
255        my $self = shift;
256    
257        my $guid;
258    
259        # try to use Data::UUID first ...
260        eval("use Data::UUID;");
261        if (!$@) {
262          my $ug = Data::UUID->new();
263          $guid = $ug->create_str();
264          
265        # ... if this fails, try to fallback to Data::UUID::PurePerl instead ...
266        } else {
267          eval("use Data::UUID::PurePerl;");
268          if (!$@) {
269            $guid = Data::UUID::PurePerl::generate_id();
270          } else {
271            croak "couldn't create globally unique identifier";
272          }
273        }
274        
275        return $guid;
276      }
277    
278    
279    
280  1;  1;

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

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