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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide 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 joko 1.1 ## ---------------------------------------------------------------------------
2 joko 1.12 ## $Id: shortcuts.pm,v 1.11 2003/06/23 17:41:50 jonen Exp $
3 joko 1.1 ## ---------------------------------------------------------------------------
4     ## $Log: shortcuts.pm,v $
5 joko 1.12 ## 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 jonen 1.11 ## Revision 1.10 2003/06/23 15:59:16 joko
9     ## major/minor fixes?
10     ##
11 joko 1.10 ## 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 joko 1.9 ## Revision 1.8 2003/04/04 17:31:59 joko
17     ## + sub make_guid
18     ##
19 joko 1.8 ## 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 joko 1.7 ## Revision 1.6 2003/03/28 06:58:06 joko
23     ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
24     ##
25 joko 1.6 ## Revision 1.5 2003/02/22 17:26:13 joko
26     ## + enhanced unix compatibility fix
27     ##
28 joko 1.5 ## Revision 1.4 2003/02/22 17:19:36 joko
29     ## + unix compatibility fix
30     ##
31 joko 1.4 ## Revision 1.3 2003/02/14 14:17:04 joko
32     ## - shortened seperator
33     ##
34 joko 1.3 ## Revision 1.2 2003/02/11 05:14:28 joko
35     ## + refactored code from libp.pm
36     ##
37 joko 1.2 ## Revision 1.1 2003/02/09 04:49:45 joko
38     ## + shortcuts now refactored to this file
39     ##
40 joko 1.1 ## ---------------------------------------------------------------------------
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 joko 1.2 bool2status
56 joko 1.8 make_guid
57 joko 1.1 );
58    
59    
60     use Data::Dumper;
61 joko 1.6 use POSIX qw( strftime );
62     #use IPC::Run qw( run timeout );
63 joko 1.9 use IPC::Run qw( start pump finish timeout run ) ;
64 joko 1.8 use Carp;
65    
66 jonen 1.11 # NEW - 2003-06-23
67 joko 1.12 use IPC::Session::NoShell;
68 joko 1.8
69 joko 1.1
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 joko 1.12 sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
85    
86    
87 joko 1.9 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 jonen 1.11 # NEW 2003-06-23 - needed if used with IPC::Session (at Linux)
95     # whats about Win32?
96 joko 1.10 $application = 'perl ';
97     } else {
98     $application = './';
99 joko 1.9 }
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 jonen 1.11 #if (RUNNING_IN_HELL()) {
108 joko 1.9 #$application = 'cmd.exe /C';
109 jonen 1.11 #}
110 joko 1.9 return $application;
111     }
112    
113    
114 joko 1.1 sub run_cmd {
115     my $cmd = shift;
116     my $caption = shift;
117 joko 1.6 my $options = shift;
118    
119 joko 1.9 #print Dumper($options);
120    
121 joko 1.6 # report - header
122 joko 1.3 my $sep = "-" x 60;
123 joko 1.12 print STDERR $sep, "\n";
124     print STDERR " ", $cmd;
125     print STDERR " - ", $caption if $caption;
126     print STDERR "\n", $sep, "\n";
127 joko 1.4
128 joko 1.5 # strip name of executable from full command string
129     $cmd =~ m/(.+?)\s/;
130     my $executable = $1;
131    
132 joko 1.9 =pod
133 joko 1.5 # for unix: check if executable is in local directory, if so - prefix with './'
134 joko 1.4 if (!RUNNING_IN_HELL()) {
135 joko 1.5 #if ($cmd !~ m/\//) {
136     if (-e $executable) {
137 joko 1.4 }
138     }
139 joko 1.9 =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 joko 1.10 #$basedir .= './';
155 joko 1.12 $basedir = "";
156 joko 1.9 }
157     $cmd = "$basedir$cmd";
158    
159 joko 1.6 # V1 - backticks or qq{}
160 joko 1.1 #`$cmd`;
161 joko 1.6 #qq{$cmd};
162    
163     # V2 - via 'system'
164     #system($cmd);
165    
166 joko 1.10 if (not $use_path) {
167     my $application = get_executable($cmd);
168     $cmd = "$application$cmd" if $application;
169     }
170    
171 jonen 1.11 # V3 - using IPC (optional)
172 joko 1.6 if ($options->{async}) {
173 jonen 1.11
174     #$cmd = "$application $cmd" if $application;
175     #my $application = get_executable_wrapper($cmd);
176 joko 1.7
177 joko 1.12 print STDERR "run_cmd: IPC::Session::NoShell: $cmd", "\n";
178 joko 1.6 #run \@cmd, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
179 jonen 1.11
180     my @cmd = split(' ', $cmd);
181 joko 1.6
182    
183 jonen 1.11 # V3.1 - using IPC::Run
184     #
185     # tests:
186     #my $in; my $out; my $err;
187 joko 1.9 #print "IPC::Run: $cmd", "\n";
188     #start \@cmd, timeout(0) or croak("run_cmd: IPC::Run could not start '$cmd'.");
189 jonen 1.11 #
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 joko 1.6 #$IPC::Run::Timer::timeout = 2000;
195     #start $cmd or die("IPC::Run could not start '$cmd'.");
196 jonen 1.11
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 joko 1.12 my $session_command = $cmd;
207 jonen 1.11 # create session:
208 joko 1.12 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 jonen 1.11 # optional switch case:
214     #for ($session->stdout()) {
215     #}
216     # optional get error:
217     #my $err = session->stderr();
218    
219 joko 1.6
220     } else {
221 joko 1.12 print STDERR "run_cmd: system('$cmd').", "\n";
222 joko 1.6 system($cmd);
223     }
224    
225 joko 1.12 print STDERR "run_cmd: ready.", "\n";
226 joko 1.6
227 joko 1.1 }
228    
229     sub run_cmds {
230 joko 1.9 my $options = {};
231     if (ref $_[$#_] eq 'HASH') {
232     #print "YAI", "\n";
233     $options = pop @_;
234     }
235 joko 1.1 foreach (@_) {
236 joko 1.9 run_cmd($_, '', $options);
237 joko 1.1 }
238     }
239    
240     sub get_chomped {
241     my $str = shift;
242     chomp($str);
243     return $str;
244 joko 1.2 }
245    
246     sub bool2status {
247     my $bool = shift;
248     return ($bool ? 'ok' : 'failed');
249 joko 1.1 }
250 joko 1.4
251 joko 1.8 # 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 jonen 1.11
278 joko 1.1 1;
279 joko 1.12 __END__

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