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 |
## Revision 1.6 2003/03/28 06:58:06 joko |
17 |
## new: 'run_cmd' now asynchronous! (via IPC::Run...) |
## new: 'run_cmd' now asynchronous! (via IPC::Run...) |
18 |
## |
## |
47 |
run_cmd run_cmds |
run_cmd run_cmds |
48 |
get_chomped |
get_chomped |
49 |
bool2status |
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 ); |
#use IPC::Run qw( run timeout ); |
57 |
use IPC::Run qw( start pump finish timeout ) ; |
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"; |
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; |
my $options = shift; |
104 |
#$cmd = 'perl ' . $cmd; |
#$cmd = 'perl ' . $cmd; |
105 |
|
|
106 |
|
#print Dumper($options); |
107 |
|
|
108 |
# report - header |
# report - header |
109 |
my $sep = "-" x 60; |
my $sep = "-" x 60; |
110 |
print $sep, "\n"; |
print $sep, "\n"; |
119 |
$cmd =~ m/(.+?)\s/; |
$cmd =~ m/(.+?)\s/; |
120 |
my $executable = $1; |
my $executable = $1; |
121 |
|
|
122 |
|
=pod |
123 |
# 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 './' |
124 |
if (!RUNNING_IN_HELL()) { |
if (!RUNNING_IN_HELL()) { |
125 |
#if ($cmd !~ m/\//) { |
#if ($cmd !~ m/\//) { |
126 |
if (-e $executable) { |
if (-e $executable) { |
|
$cmd = "./$cmd"; |
|
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{} |
# V1 - backticks or qq{} |
149 |
#`$cmd`; |
#`$cmd`; |
150 |
#qq{$cmd}; |
#qq{$cmd}; |
154 |
|
|
155 |
# V3 - using IPC::Run (optional) |
# V3 - using IPC::Run (optional) |
156 |
if ($options->{async}) { |
if ($options->{async}) { |
157 |
# FIXME: detect type of program and run with proper application/interpreter |
my $application = get_executable_wrapper($cmd); |
158 |
# using IPC::Run we have to dispatch this on our own! *no* os-interaction or interpolation here! |
$cmd = "$application $cmd" if $application; |
159 |
# => better use absolute path-names only?! |
|
160 |
$cmd = "perl $cmd"; |
print "run_cmd: IPC::Run: $cmd", "\n"; |
|
print "IPC::Run: $cmd", "\n"; |
|
161 |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
#run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"; |
162 |
|
|
163 |
my @cmd = split(' ', $cmd); |
my @cmd = split(' ', $cmd); |
164 |
|
|
165 |
my $in; my $out; my $err; |
my $in; my $out; my $err; |
166 |
start \@cmd, timeout(0) or die("IPC::Run could not start '$cmd'."); |
#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; |
#$IPC::Run::Timer::timeout = 2000; |
171 |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
#start $cmd or die("IPC::Run could not start '$cmd'."); |
172 |
|
|
173 |
} else { |
} 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); |
system($cmd); |
180 |
} |
} |
181 |
|
|
182 |
print "ready.", "\n"; |
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 |
|
|
207 |
|
|
208 |
sub RUNNING_IN_HELL () { $^O eq 'MSWin32' } |
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; |