/[cvs]/nfo/perl/libs/Data/Rap/Engine.pm
ViewVC logotype

Annotation of /nfo/perl/libs/Data/Rap/Engine.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Thu Mar 27 15:03:03 2003 UTC (21 years, 3 months ago) by joko
Branch: MAIN
Changes since 1.4: +41 -4 lines
enhanced 'sub run_executable'

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

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