/[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.6 by joko, Fri Mar 28 06:58:06 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  ##  Revision 1.6  2003/03/28 06:58:06  joko
20  ##  new: 'run_cmd' now asynchronous! (via IPC::Run...)  ##  new: 'run_cmd' now asynchronous! (via IPC::Run...)
21  ##  ##
# Line 36  our @EXPORT_OK = qw( Line 50  our @EXPORT_OK = qw(
50    run_cmd run_cmds    run_cmd run_cmds
51    get_chomped    get_chomped
52    bool2status    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 );  #use IPC::Run qw( run timeout );
60  use IPC::Run qw( start pump finish timeout ) ;  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 61  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;    my $options = shift;
109    #$cmd = 'perl ' . $cmd;    #$cmd = 'perl ' . $cmd;
110        
111      #print Dumper($options);
112      
113    # report - header    # report - header
114    my $sep = "-" x 60;    my $sep = "-" x 60;
115    print $sep, "\n";    print $sep, "\n";
# Line 81  sub run_cmd { Line 124  sub run_cmd {
124    $cmd =~ m/(.+?)\s/;    $cmd =~ m/(.+?)\s/;
125    my $executable = $1;    my $executable = $1;
126        
127    =pod
128    # for unix: check if executable is in local directory, if so - prefix with './'    # for unix: check if executable is in local directory, if so - prefix with './'
129    if (!RUNNING_IN_HELL()) {    if (!RUNNING_IN_HELL()) {
130      #if ($cmd !~ m/\//) {      #if ($cmd !~ m/\//) {
131      if (-e $executable) {      if (-e $executable) {
       $cmd = "./$cmd";  
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{}    # V1 - backticks or qq{}
154    #`$cmd`;    #`$cmd`;
155    #qq{$cmd};    #qq{$cmd};
# Line 96  sub run_cmd { Line 157  sub run_cmd {
157    # V2 - via 'system'    # V2 - via 'system'
158    #system($cmd);    #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)    # V3 - using IPC::Run (optional)
166    if ($options->{async}) {    if ($options->{async}) {
167      # FIXME: detect type of program and run with proper application/interpreter      my $application = get_executable_wrapper($cmd);
168      # using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here!      $cmd = "$application $cmd" if $application;
169      # => better use absolute path-names only?!      
170      $cmd = "perl $cmd";      print "run_cmd: IPC::Run: $cmd", "\n";
     print "IPC::Run: $cmd", "\n";  
171      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";      #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
172            
173      my @cmd = split(' ', $cmd);      my @cmd = split(' ', $cmd);
174            
175      my $in; my $out; my $err;      my $in; my $out; my $err;
176      start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'.");      #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;      #$IPC::Run::Timer::timeout = 2000;
181      #start $cmd or die("IPC::Run could not start '$cmd'.");      #start $cmd or die("IPC::Run could not start '$cmd'.");
182            
183    } else {    } else {
184        print "run_cmd: system('$cmd').", "\n";
185      system($cmd);      system($cmd);
186    }    }
187        
188    print "ready.", "\n";    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 140  sub bool2status { Line 213  sub bool2status {
213    
214  sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }  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.6  
changed lines
  Added in v.1.10

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