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

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.8

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