/[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.10 - (show annotations)
Mon Jun 23 15:59:16 2003 UTC (20 years, 10 months ago) by joko
Branch: MAIN
Changes since 1.9: +15 -7 lines
major/minor fixes?

1 ## ---------------------------------------------------------------------------
2 ## $Id: shortcuts.pm,v 1.9 2003/05/13 05:36:24 joko Exp $
3 ## ---------------------------------------------------------------------------
4 ## $Log: shortcuts.pm,v $
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
17 ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
18 ##
19 ## Revision 1.5 2003/02/22 17:26:13 joko
20 ## + enhanced unix compatibility fix
21 ##
22 ## Revision 1.4 2003/02/22 17:19:36 joko
23 ## + unix compatibility fix
24 ##
25 ## Revision 1.3 2003/02/14 14:17:04 joko
26 ## - shortened seperator
27 ##
28 ## Revision 1.2 2003/02/11 05:14:28 joko
29 ## + refactored code from libp.pm
30 ##
31 ## Revision 1.1 2003/02/09 04:49:45 joko
32 ## + shortcuts now refactored to this file
33 ##
34 ## ---------------------------------------------------------------------------
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 bool2status
50 make_guid
51 );
52
53
54 use Data::Dumper;
55 use POSIX qw( strftime );
56 #use IPC::Run qw( run timeout );
57 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;
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 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 } else {
85 $application = './';
86 }
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 sub run_cmd {
103 my $cmd = shift;
104 my $caption = shift;
105 my $options = shift;
106 #$cmd = 'perl ' . $cmd;
107
108 #print Dumper($options);
109
110 # report - header
111 my $sep = "-" x 60;
112 print $sep, "\n";
113 #print " ", $cmd, "\n";
114 #print " ", " $caption", "\n" if $caption;
115 print " ", $cmd;
116 print " - ", $caption if $caption;
117 print "\n";
118 print $sep, "\n";
119
120 # strip name of executable from full command string
121 $cmd =~ m/(.+?)\s/;
122 my $executable = $1;
123
124 =pod
125 # for unix: check if executable is in local directory, if so - prefix with './'
126 if (!RUNNING_IN_HELL()) {
127 #if ($cmd !~ m/\//) {
128 if (-e $executable) {
129 }
130 }
131 =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 #$basedir .= './';
147 }
148 $cmd = "$basedir$cmd";
149
150 # V1 - backticks or qq{}
151 #`$cmd`;
152 #qq{$cmd};
153
154 # V2 - via 'system'
155 #system($cmd);
156
157 if (not $use_path) {
158 my $application = get_executable($cmd);
159 $cmd = "$application$cmd" if $application;
160 }
161
162 # V3 - using IPC::Run (optional)
163 if ($options->{async}) {
164 my $application = get_executable_wrapper($cmd);
165 $cmd = "$application $cmd" if $application;
166
167 print "run_cmd: IPC::Run: $cmd", "\n";
168 #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 #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
177 #$IPC::Run::Timer::timeout = 2000;
178 #start $cmd or die("IPC::Run could not start '$cmd'.");
179
180 } else {
181 print "run_cmd: system('$cmd').", "\n";
182 system($cmd);
183 }
184
185 print "run_cmd: ready.", "\n";
186
187 }
188
189 sub run_cmds {
190 my $options = {};
191 if (ref $_[$#_] eq 'HASH') {
192 #print "YAI", "\n";
193 $options = pop @_;
194 }
195 foreach (@_) {
196 run_cmd($_, '', $options);
197 }
198 }
199
200 sub get_chomped {
201 my $str = shift;
202 chomp($str);
203 return $str;
204 }
205
206 sub bool2status {
207 my $bool = shift;
208 return ($bool ? 'ok' : 'failed');
209 }
210
211 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
212
213 # 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
240 1;

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