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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.2 by joko, Thu Feb 20 19:46:33 2003 UTC revision 1.9 by joko, Fri Apr 4 17:23:11 2003 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ----------------------------------------------------------------------  ## ----------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
5    ##  Revision 1.9  2003/04/04 17:23:11  joko
6    ##  minor update: debugging output
7    ##
8    ##  Revision 1.8  2003/03/29 07:11:54  joko
9    ##  modified: sub run_executable
10    ##  new: sub run_script
11    ##
12    ##  Revision 1.7  2003/03/28 07:02:56  joko
13    ##  modified structure around '$wrapper_program'
14    ##
15    ##  Revision 1.6  2003/03/27 15:31:05  joko
16    ##  fixes to modules regarding new namespace(s) below Data::Mungle::*
17    ##
18    ##  Revision 1.5  2003/03/27 15:03:03  joko
19    ##  enhanced 'sub run_executable'
20    ##
21    ##  Revision 1.4  2003/02/22 16:51:21  joko
22    ##  + enhanced run_executable
23    ##  modified logging output
24    ##
25    ##  Revision 1.3  2003/02/21 01:46:17  joko
26    ##  renamed core function
27    ##
28  ##  Revision 1.2  2003/02/20 19:46:33  joko  ##  Revision 1.2  2003/02/20 19:46:33  joko
29  ##  renamed and revamped some of modules  ##  renamed and revamped some of modules
30  ##  renamed methods  ##  renamed methods
# Line 24  use Hash::Merge qw( merge ); Line 47  use Hash::Merge qw( merge );
47  use Iterate;  use Iterate;
48    
49  use shortcuts qw( run_cmd );  use shortcuts qw( run_cmd );
50  use Data::Code::Ref qw( ref_slot );  use Data::Mungle::Code::Ref qw( ref_slot );
51  use Data::Transform::Deep qw( expand );  use Data::Mungle::Transform::Deep qw( expand deep_copy );
52    use File::Temp qw/ tempfile tempdir /;
53    
54    
55  sub performTarget {  sub performTarget {
# Line 38  sub perform_target { Line 62  sub perform_target {
62    my $self = shift;    my $self = shift;
63    my $targetname = shift;    my $targetname = shift;
64    
65      my $header = ("- " x 12) . "   " . $targetname . "   " . ("- " x 6);
66      
67      # V1
68      #$self->log("- " x 35, 'notice');
69      #$self->log("Performing Target '$targetname'.", 'notice');
70    
71      # V2
72      #$self->log($header, 'notice');
73    
74      # V3
75      $self->log("- " x 20, 'info');
76    $self->log("Performing Target '$targetname'.", 'notice');    $self->log("Performing Target '$targetname'.", 'notice');
77    
78    #exit;    #exit;
# Line 81  sub perform_details { Line 116  sub perform_details {
116    foreach my $entry (@{$target->{content}}) {    foreach my $entry (@{$target->{content}}) {
117      my $command = $entry->{name};      my $command = $entry->{name};
118      my $args = $entry->{attrib};      my $args = $entry->{attrib};
119      $self->perform_command($command, $args);      my $content = $entry->{content};
120        $self->perform_command($command, $args, $content);
121      # check recursiveness      # check recursiveness
122      if ($entry->{content} && ref $entry->{content}) {      if ($entry->{content} && ref $entry->{content}) {
123        $self->perform_details($entry);        $self->perform_details($entry);
# Line 97  sub rc { Line 133  sub rc {
133  sub perform_command {  sub perform_command {
134    my $self = shift;    my $self = shift;
135    my $command = shift;    my $command = shift;
136      my $args_list = shift;
137      my $content = shift;
138    
139    if (!$command) {    if (!$command) {
140      $self->log("Command was empty!", 'warning');      $self->log("Command was empty!", 'debug');
141      return;      return;
142    }    }
143    
144    # FIXME: make '__PACKAGE__' go one level deeper properly!    # FIXME: make '__PACKAGE__' go one level deeper properly!
145    $self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug');    $self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug');
146        
   my $args_list = shift;  
147    my $args = {};    my $args = {};
148    #print Dumper($args_list);    #print Dumper($args_list);
149    if ($args_list) {    if ($args_list) {
# Line 126  sub perform_command { Line 163  sub perform_command {
163    $container ||= $self;    $container ||= $self;
164        
165    if ($container->can($command)) {    if ($container->can($command)) {
166      $container->$command($args);      $container->$command($args, $content);
167    } else {    } else {
168      $self->log("Command '$command' not implemented.", "warning");      $self->log("Command '$command' not implemented.", "warning");
169    }    }
# Line 155  sub merge_properties { Line 192  sub merge_properties {
192      #print "DATA NEE! - MERGE!", "\n";      #print "DATA NEE! - MERGE!", "\n";
193      #print Dumper($data_new);      #print Dumper($data_new);
194      #exit;      #exit;
195      #hash2object($self, $data_new);      #merge_to($self, $data_new);
196      $self->set_property( { name => $name, value => $data_new } );      $self->set_property( { name => $name, value => $data_new } );
197            
198    } else {    } else {
# Line 279  sub run_executable { Line 316  sub run_executable {
316    my $self = shift;    my $self = shift;
317    my $opts = shift;    my $opts = shift;
318    
319      my $meta = deep_copy($opts);
320    
321      delete $opts->{caption};
322      delete $opts->{async};
323    
324      #print Dumper($meta);
325    
326    if ($opts->{executable}) {    if ($opts->{executable}) {
327    
328      my $program = $opts->{executable};      my $program = $opts->{executable};
329      delete $opts->{executable};      delete $opts->{executable};
330    
331        # determine execution method
332        my $wrapper_program = '';
333        
334        # check if program is a namespace-string (contains '::') - use 'do' in this case!
335        if ($program =~ /::/) {
336          #$wrapper_program = 'rap.pl';
337          $wrapper_program = $0;
338        }
339    
340        # prepare arguments
341      my @buf;      my @buf;
342      foreach (keys %$opts) {      foreach (keys %$opts) {
343        my $value = $opts->{$_};        my $value = $opts->{$_};
344          if (m/^_/) {
345            if ($_ eq '_switches') {
346              my @switches = split(/,\s|,/, $value);
347              foreach my $switch (@switches) {
348                push @buf, '--' . $switch;
349              }
350            }
351            next;
352          }
353          
354        if ($value =~ /\s/) {        if ($value =~ /\s/) {
355          $value = "\"$value\"";          $value = "\"$value\"";
356        }        }
357        push @buf, "--$_=$value";        push @buf, "--$_=$value";
358      }      }
359      
360      my $cmd = $program . ' ' . join(' ', @buf);      # build {program} & {arguments}  
361        my $cmd = ($wrapper_program ? $wrapper_program . ' ' : '') . $program . ' ' . join(' ', @buf);
362            
363      # trace      # trace
364        #print "command: $cmd", "\n";        #print "command: $cmd", "\n";
# Line 306  sub run_executable { Line 371  sub run_executable {
371        $ENV{PERL5LIB} = $INC[0];        $ENV{PERL5LIB} = $INC[0];
372                
373        #print Dumper(%ENV);        #print Dumper(%ENV);
374        run_cmd($cmd);        
375          #print "command: '$cmd'", "\n";
376    
377          # V1 - basic
378          #run_cmd($cmd);
379    
380          # V1.b - enhanced: variable local method
381          $meta->{caption} ||= '';
382          $meta->{async} ||= 0;
383          my $evalstr = "run_cmd('$cmd', '$meta->{caption}', { async => $meta->{async} });";
384          eval($evalstr);
385          #my $res = do "$cmd";
386          #print $res, "\n" if $res;
387    
388          #$self->log("run_executable: $evalstr", 'info');
389          $self->raiseException("run_executable: $evalstr\n$@") if $@;
390    
391        # V2: via IPC::Run        # V2: via IPC::Run
392        # ....  (TODO)        # ....  (TODO)
393              
394            
395    }    }
396        
397  }  }
398    
399    
400    sub run_script {
401    
402      my $self = shift;
403      my $args = shift;
404      my $code = shift;
405    
406      if ($args->{language} eq 'msdos/bat') {
407        
408        #print "code: $code", "\n";
409        
410        # reporting
411        $self->log("Executing some arbitrary unsigned code (probably unsafe). [language=$args->{language}]", 'info');
412        $self->log("\n<code>\n$code\n</code>", 'info');
413        
414        # create temporary intermediate file to execute code
415        my $tmpdir = '/tmp/rap';
416        mkdir $tmpdir;
417        (my $fh, my $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.bat' );
418        print $fh $code, "\n";
419        run_cmd( $filename, '', { async => 1 } );
420        
421        # FIXME: DELETE code inside temp-files as soon as possible!
422        #print $fh '';
423        
424        # TODO: delete FILE completely!
425        # required for this: wait until execution has finished, then unlink....
426        # but: "how to wait until execution is finished"?
427        # i believe the best is to spawn *another* process directly from here,
428        # let's call it 'watcher-agent'.
429        # This one should monitor a certain running process and delete its
430        # executable file after it has finished execution.
431        # Possible extensions could be:
432        #   keep track of all stuff sent to STDOUT or STDERR and
433        #   send that stuff to the task-owner indirectly (not via shell/console)
434        #   (e.g. via email, by posting report to a newsgroup or publishing on a specified web-page: use mod-dav!)
435        
436      } elsif ($args->{language} eq 'bash') {
437        $self->log("FIXME: - - - - - -- - - --  BASH -  - -    -  - -   -    --   - ", 'error');
438        
439      } else {
440        $self->log("FIXME: Script language '$args->{language}' not implemented.", 'error');
441        
442      }
443    
444    }
445    
446  1;  1;
447  __END__  __END__

Legend:
Removed from v.1.2  
changed lines
  Added in v.1.9

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