/[cvs]/nfo/perl/libs/shortcuts.pm
ViewVC logotype

Contents of /nfo/perl/libs/shortcuts.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (show annotations)
Mon Jun 23 19:43:19 2003 UTC (20 years, 10 months ago) by joko
Branch: MAIN
Changes since 1.11: +23 -21 lines
minor cleanup
now using IPC::Session::NoShell

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

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