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

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

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