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