/[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.9 - (hide annotations)
Tue May 13 05:36:24 2003 UTC (20 years, 11 months ago) by joko
Branch: MAIN
Changes since 1.8: +68 -20 lines
heavy modifications to run_cmd
+ sub get_executable
+ sub get_executable_wrapper

1 joko 1.1 ## ---------------------------------------------------------------------------
2 joko 1.9 ## $Id: shortcuts.pm,v 1.8 2003/04/04 17:31:59 joko Exp $
3 joko 1.1 ## ---------------------------------------------------------------------------
4     ## $Log: shortcuts.pm,v $
5 joko 1.9 ## Revision 1.8 2003/04/04 17:31:59 joko
6     ## + sub make_guid
7     ##
8 joko 1.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 joko 1.7 ## Revision 1.6 2003/03/28 06:58:06 joko
12     ## new: 'run_cmd' now asynchronous! (via IPC::Run...)
13     ##
14 joko 1.6 ## Revision 1.5 2003/02/22 17:26:13 joko
15     ## + enhanced unix compatibility fix
16     ##
17 joko 1.5 ## Revision 1.4 2003/02/22 17:19:36 joko
18     ## + unix compatibility fix
19     ##
20 joko 1.4 ## Revision 1.3 2003/02/14 14:17:04 joko
21     ## - shortened seperator
22     ##
23 joko 1.3 ## Revision 1.2 2003/02/11 05:14:28 joko
24     ## + refactored code from libp.pm
25     ##
26 joko 1.2 ## Revision 1.1 2003/02/09 04:49:45 joko
27     ## + shortcuts now refactored to this file
28     ##
29 joko 1.1 ## ---------------------------------------------------------------------------
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 joko 1.2 bool2status
45 joko 1.8 make_guid
46 joko 1.1 );
47    
48    
49     use Data::Dumper;
50 joko 1.6 use POSIX qw( strftime );
51     #use IPC::Run qw( run timeout );
52 joko 1.9 use IPC::Run qw( start pump finish timeout run ) ;
53 joko 1.8 use Carp;
54    
55    
56 joko 1.1
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 joko 1.9 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 joko 1.1 sub run_cmd {
96     my $cmd = shift;
97     my $caption = shift;
98 joko 1.6 my $options = shift;
99 joko 1.1 #$cmd = 'perl ' . $cmd;
100 joko 1.6
101 joko 1.9 #print Dumper($options);
102    
103 joko 1.6 # report - header
104 joko 1.3 my $sep = "-" x 60;
105 joko 1.1 print $sep, "\n";
106 joko 1.6 #print " ", $cmd, "\n";
107     #print " ", " $caption", "\n" if $caption;
108     print " ", $cmd;
109     print " - ", $caption if $caption;
110     print "\n";
111 joko 1.1 print $sep, "\n";
112 joko 1.4
113 joko 1.5 # strip name of executable from full command string
114     $cmd =~ m/(.+?)\s/;
115     my $executable = $1;
116    
117 joko 1.9 =pod
118 joko 1.5 # for unix: check if executable is in local directory, if so - prefix with './'
119 joko 1.4 if (!RUNNING_IN_HELL()) {
120 joko 1.5 #if ($cmd !~ m/\//) {
121     if (-e $executable) {
122 joko 1.4 }
123     }
124 joko 1.9 =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 joko 1.6 # V1 - backticks or qq{}
144 joko 1.1 #`$cmd`;
145 joko 1.6 #qq{$cmd};
146    
147     # V2 - via 'system'
148     #system($cmd);
149    
150     # V3 - using IPC::Run (optional)
151     if ($options->{async}) {
152 joko 1.9 my $application = get_executable_wrapper($cmd);
153 joko 1.7 $cmd = "$application $cmd" if $application;
154    
155 joko 1.9 print "run_cmd: IPC::Run: $cmd", "\n";
156 joko 1.6 #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 joko 1.9 #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 joko 1.6
165     #$IPC::Run::Timer::timeout = 2000;
166     #start $cmd or die("IPC::Run could not start '$cmd'.");
167    
168     } else {
169 joko 1.9 if (!$use_path) {
170     my $application = get_executable($cmd);
171     $cmd = "$application $cmd" if $application;
172     }
173     print "run_cmd: system('$cmd').", "\n";
174 joko 1.6 system($cmd);
175     }
176    
177 joko 1.9 print "run_cmd: ready.", "\n";
178 joko 1.6
179 joko 1.1 }
180    
181     sub run_cmds {
182 joko 1.9 my $options = {};
183     if (ref $_[$#_] eq 'HASH') {
184     #print "YAI", "\n";
185     $options = pop @_;
186     }
187 joko 1.1 foreach (@_) {
188 joko 1.9 run_cmd($_, '', $options);
189 joko 1.1 }
190     }
191    
192     sub get_chomped {
193     my $str = shift;
194     chomp($str);
195     return $str;
196 joko 1.2 }
197    
198     sub bool2status {
199     my $bool = shift;
200     return ($bool ? 'ok' : 'failed');
201 joko 1.1 }
202 joko 1.4
203     sub RUNNING_IN_HELL () { $^O eq 'MSWin32' }
204    
205 joko 1.8 # 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 joko 1.1
232     1;

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