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