1 |
## ---------------------------------------------------------------------- |
2 |
## $Id: Engine.pm,v 1.6 2003/03/27 15:31:05 joko Exp $ |
3 |
## ---------------------------------------------------------------------- |
4 |
## $Log: Engine.pm,v $ |
5 |
## Revision 1.6 2003/03/27 15:31:05 joko |
6 |
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
7 |
## |
8 |
## Revision 1.5 2003/03/27 15:03:03 joko |
9 |
## enhanced 'sub run_executable' |
10 |
## |
11 |
## Revision 1.4 2003/02/22 16:51:21 joko |
12 |
## + enhanced run_executable |
13 |
## modified logging output |
14 |
## |
15 |
## Revision 1.3 2003/02/21 01:46:17 joko |
16 |
## renamed core function |
17 |
## |
18 |
## Revision 1.2 2003/02/20 19:46:33 joko |
19 |
## renamed and revamped some of modules |
20 |
## renamed methods |
21 |
## + sub run_executable |
22 |
## |
23 |
## Revision 1.1 2003/02/18 15:35:25 joko |
24 |
## + initial commit |
25 |
## |
26 |
## ---------------------------------------------------------------------- |
27 |
|
28 |
|
29 |
package Data::Rap::Engine; |
30 |
|
31 |
use strict; |
32 |
use warnings; |
33 |
|
34 |
|
35 |
use Data::Dumper; |
36 |
use Hash::Merge qw( merge ); |
37 |
use Iterate; |
38 |
|
39 |
use shortcuts qw( run_cmd ); |
40 |
use Data::Mungle::Code::Ref qw( ref_slot ); |
41 |
use Data::Mungle::Transform::Deep qw( expand ); |
42 |
|
43 |
|
44 |
sub performTarget { |
45 |
my $self = shift; |
46 |
my $targetname = shift; |
47 |
$self->perform_target($targetname); |
48 |
} |
49 |
|
50 |
sub perform_target { |
51 |
my $self = shift; |
52 |
my $targetname = shift; |
53 |
|
54 |
my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6); |
55 |
|
56 |
#$self->log("- " x 35, 'notice'); |
57 |
#$self->log("Performing Target '$targetname'.", 'notice'); |
58 |
|
59 |
$self->log($header, 'notice'); |
60 |
|
61 |
#exit; |
62 |
|
63 |
my $target = $self->getTargetDetails($targetname); |
64 |
|
65 |
# trace |
66 |
#print Dumper($target); |
67 |
#exit; |
68 |
|
69 |
$self->perform_dependencies($target); |
70 |
$self->perform_details($target); |
71 |
|
72 |
} |
73 |
|
74 |
sub perform_dependencies { |
75 |
my $self = shift; |
76 |
my $target = shift; |
77 |
# resolve dependencies (just run prior) |
78 |
if (my $targetname_dep = $target->{attrib}->{depends}) { |
79 |
my @targets = split(/,\s|,/, $targetname_dep); |
80 |
#print Dumper(@targets); |
81 |
#$self->perform($targetname_dep); |
82 |
#delete $target->{attrib}->{depends}; |
83 |
foreach (@targets) { |
84 |
if (!$self->{__rap}->{dependencies}->{resolved}->{$_}++) { |
85 |
$self->perform_target($_); |
86 |
} |
87 |
} |
88 |
delete $target->{attrib}->{depends}; |
89 |
} |
90 |
} |
91 |
|
92 |
sub perform_details { |
93 |
my $self = shift; |
94 |
my $target = shift; |
95 |
|
96 |
#print Dumper($target); |
97 |
#exit; |
98 |
|
99 |
foreach my $entry (@{$target->{content}}) { |
100 |
my $command = $entry->{name}; |
101 |
my $args = $entry->{attrib}; |
102 |
$self->perform_command($command, $args); |
103 |
# check recursiveness |
104 |
if ($entry->{content} && ref $entry->{content}) { |
105 |
$self->perform_details($entry); |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
sub rc { |
111 |
my $self = shift; |
112 |
return $self->perform_command(@_); |
113 |
} |
114 |
|
115 |
sub perform_command { |
116 |
my $self = shift; |
117 |
my $command = shift; |
118 |
|
119 |
if (!$command) { |
120 |
$self->log("Command was empty!", 'debug'); |
121 |
return; |
122 |
} |
123 |
|
124 |
# FIXME: make '__PACKAGE__' go one level deeper properly! |
125 |
$self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug'); |
126 |
|
127 |
my $args_list = shift; |
128 |
my $args = {}; |
129 |
#print Dumper($args_list); |
130 |
if ($args_list) { |
131 |
if (ref $args_list eq 'ARRAY') { |
132 |
foreach (@$args_list) { |
133 |
$args = merge($args, $_); |
134 |
} |
135 |
} else { |
136 |
$args = $args_list; |
137 |
} |
138 |
} |
139 |
$command = '_' . $command; |
140 |
|
141 |
|
142 |
# determine container |
143 |
my $container; # = $self->getInstance(); |
144 |
$container ||= $self; |
145 |
|
146 |
if ($container->can($command)) { |
147 |
$container->$command($args); |
148 |
} else { |
149 |
$self->log("Command '$command' not implemented.", "warning"); |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
|
155 |
sub merge_properties { |
156 |
my $self = shift; |
157 |
my $name = shift; |
158 |
my $data = shift; |
159 |
|
160 |
$self->log("merge-name: $name"); |
161 |
#print "name: $name", "\n"; |
162 |
#print Dumper($data); |
163 |
#exit; |
164 |
|
165 |
# check if slot (or childs of it) is/are already occupied |
166 |
#if (my $data_old = ref_slot($self, $name, undef, '.')) { |
167 |
if (my $data_old = $self->get_property($name)) { |
168 |
#print "old:", "\n"; |
169 |
#print Dumper($data_old); |
170 |
|
171 |
# FIXME: review - any options for 'merge' here? |
172 |
my $data_new = merge($data_old, $data); |
173 |
#print "DATA NEE! - MERGE!", "\n"; |
174 |
#print Dumper($data_new); |
175 |
#exit; |
176 |
#merge_to($self, $data_new); |
177 |
$self->set_property( { name => $name, value => $data_new } ); |
178 |
|
179 |
} else { |
180 |
|
181 |
=pod |
182 |
# make up a dummy hash matching the structure of the destination one |
183 |
my $dummy = {}; |
184 |
ref_slot($dummy, $name, $data, '.'); |
185 |
print Dumper($dummy); |
186 |
|
187 |
# mix into destination |
188 |
mixin($self, $dummy, { init => 1 }); |
189 |
=cut |
190 |
|
191 |
ref_slot($self, $name, $data, '.'); |
192 |
|
193 |
} |
194 |
|
195 |
|
196 |
#print Dumper($self); |
197 |
#exit; |
198 |
#$self-> |
199 |
|
200 |
} |
201 |
|
202 |
sub set_property { |
203 |
my $self = shift; |
204 |
my $args = shift; |
205 |
|
206 |
$self->interpolate($args) ; |
207 |
my $name = $args->{name}; |
208 |
my $value = $args->{value}; |
209 |
$name = '__rap.properties.' . $name; |
210 |
|
211 |
$self->log("set-name: $name"); |
212 |
|
213 |
# fill property slot with given value |
214 |
if ($value) { |
215 |
ref_slot($self, $name, $value, '.'); |
216 |
} |
217 |
|
218 |
#print Dumper($self); |
219 |
|
220 |
# FIXME!!! |
221 |
#return if ! ref $args; |
222 |
|
223 |
# fill property slot with (probably bigger) data structure from property-/configuration-style - file |
224 |
if (my $file = $args->{file}) { |
225 |
my $type = $args->{type}; |
226 |
if (!$type) { |
227 |
die("default file (no type specified) is not implemented yet!"); |
228 |
|
229 |
} elsif ($type eq 'perl-eval') { |
230 |
#die($type); |
231 |
$self->loadFromPerlFile($file, $name, $args->{'varnames'}); |
232 |
|
233 |
} elsif ($type eq 'App::Config') { |
234 |
die("not implemented: $type"); |
235 |
|
236 |
} |
237 |
|
238 |
} elsif (my $command = $args->{command}) { |
239 |
$self->perform_command($command, $args); |
240 |
|
241 |
} |
242 |
|
243 |
} |
244 |
|
245 |
sub get_property { |
246 |
my $self = shift; |
247 |
#my $args = shift; |
248 |
my $name = shift; |
249 |
|
250 |
#$self->interpolate($args); |
251 |
|
252 |
#my $name = $args->{name}; |
253 |
my $result; |
254 |
|
255 |
if (!$name) { |
256 |
$self->log( __PACKAGE__ . ": no name!", 'critical'); |
257 |
|
258 |
} elsif ($name eq '/') { |
259 |
$result = expand($self); |
260 |
|
261 |
} elsif ($name eq '.') { |
262 |
if (my $instance = $self->getInstance()) { |
263 |
$result = expand($instance); |
264 |
} else { |
265 |
$result = ref_slot($self, '__rap.properties', undef, '.'); |
266 |
|
267 |
} |
268 |
|
269 |
} else { |
270 |
|
271 |
$name = '__rap.properties.' . $name; |
272 |
|
273 |
$self->log("get-name: $name"); |
274 |
|
275 |
# get property slot and return value |
276 |
$result = ref_slot($self, $name, undef, '.'); |
277 |
|
278 |
# FIXME: Is this okay? It's provided for now in order not |
279 |
# to deliver an 'undef' to the regex below inside 'interpolate'. |
280 |
# revamp this, maybe! |
281 |
#$result ||= ''; # NO!!! |
282 |
} |
283 |
|
284 |
return $result; |
285 |
} |
286 |
|
287 |
sub interpolate { |
288 |
my $self = shift; |
289 |
my $ref = shift; |
290 |
IterHash %$ref, sub { |
291 |
#print $_[1], "\n"; |
292 |
$_[1] =~ s/\${(.+)}/$self->get_property($1)/e; |
293 |
} |
294 |
} |
295 |
|
296 |
sub run_executable { |
297 |
my $self = shift; |
298 |
my $opts = shift; |
299 |
|
300 |
if ($opts->{executable}) { |
301 |
|
302 |
my $program = $opts->{executable}; |
303 |
delete $opts->{executable}; |
304 |
|
305 |
# determine execution method |
306 |
my $wrapper_program = ''; |
307 |
|
308 |
# check if program is a namespace-string (contains '::') - use 'do' in this case! |
309 |
if ($program =~ /::/) { |
310 |
#$wrapper_program = 'rap.pl'; |
311 |
$wrapper_program = $0; |
312 |
} |
313 |
|
314 |
# prepare arguments |
315 |
my @buf; |
316 |
foreach (keys %$opts) { |
317 |
my $value = $opts->{$_}; |
318 |
if (m/^_/) { |
319 |
if ($_ eq '_switches') { |
320 |
my @switches = split(/,\s|,/, $value); |
321 |
foreach my $switch (@switches) { |
322 |
push @buf, '--' . $switch; |
323 |
} |
324 |
} |
325 |
next; |
326 |
} |
327 |
|
328 |
if ($value =~ /\s/) { |
329 |
$value = "\"$value\""; |
330 |
} |
331 |
push @buf, "--$_=$value"; |
332 |
} |
333 |
|
334 |
# build {program} & {arguments} |
335 |
my $cmd = ($wrapper_program ? $wrapper_program . ' ' : '') . $program . ' ' . join(' ', @buf); |
336 |
|
337 |
# trace |
338 |
#print "command: $cmd", "\n"; |
339 |
|
340 |
# start process |
341 |
# V1: via shortcut |
342 |
#$ENV{PERL5LIB} = join(' ', @INC); |
343 |
|
344 |
# FIXME!!! what about the other slots of @INC? |
345 |
$ENV{PERL5LIB} = $INC[0]; |
346 |
|
347 |
#print Dumper(%ENV); |
348 |
|
349 |
#print "command: '$cmd'", "\n"; |
350 |
|
351 |
# V1 - basic |
352 |
#run_cmd($cmd); |
353 |
|
354 |
# V1.b - enhanced: variable local method |
355 |
my $evalstr = "run_cmd('$cmd', 'Some task...', { async => 1 });"; |
356 |
eval($evalstr); |
357 |
#my $res = do "$cmd"; |
358 |
#print $res, "\n" if $res; |
359 |
|
360 |
#$self->log("run_executable: $evalstr", 'info'); |
361 |
$self->raiseException("run_executable: $evalstr\n$@") if $@; |
362 |
|
363 |
# V2: via IPC::Run |
364 |
# .... (TODO) |
365 |
|
366 |
|
367 |
} |
368 |
|
369 |
} |
370 |
|
371 |
|
372 |
1; |
373 |
__END__ |