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

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

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