| 2 |
## $Id$ |
## $Id$ |
| 3 |
## ---------------------------------------------------------------------- |
## ---------------------------------------------------------------------- |
| 4 |
## $Log$ |
## $Log$ |
| 5 |
|
## Revision 1.16 2004/06/21 14:15:06 jonen |
| 6 |
|
## handle path-modifications in a generic way now(fix for BSD) |
| 7 |
|
## |
| 8 |
|
## Revision 1.15 2004/06/16 16:37:59 joko |
| 9 |
|
## attempt to get things going in a generic way (Linux/FreeBSD/Win32) |
| 10 |
|
## |
| 11 |
|
## Revision 1.14 2004/05/12 14:23:31 jonen |
| 12 |
|
## add comment/code related to PERL5LIB var at different OS's |
| 13 |
|
## |
| 14 |
|
## Revision 1.13 2003/12/05 05:02:08 joko |
| 15 |
|
## + minor update: disabled some unnecessary loggers or changed to debug-level |
| 16 |
|
## |
| 17 |
|
## Revision 1.12 2003/06/24 20:59:51 jonen |
| 18 |
|
## added option 'detach' |
| 19 |
|
## |
| 20 |
|
## Revision 1.11 2003/06/23 17:54:32 joko |
| 21 |
|
## prepared execution of in-process perl-code via eval (not activated yet!) |
| 22 |
|
## |
| 23 |
|
## Revision 1.10 2003/05/13 07:56:12 joko |
| 24 |
|
## enhanced: *hierarchical* containers for context handling |
| 25 |
|
## fixes: some pre-flight checks |
| 26 |
|
## new: propagate "end-tag" event to e.g. close containers |
| 27 |
|
## |
| 28 |
|
## Revision 1.9 2003/04/04 17:23:11 joko |
| 29 |
|
## minor update: debugging output |
| 30 |
|
## |
| 31 |
|
## Revision 1.8 2003/03/29 07:11:54 joko |
| 32 |
|
## modified: sub run_executable |
| 33 |
|
## new: sub run_script |
| 34 |
|
## |
| 35 |
|
## Revision 1.7 2003/03/28 07:02:56 joko |
| 36 |
|
## modified structure around '$wrapper_program' |
| 37 |
|
## |
| 38 |
## Revision 1.6 2003/03/27 15:31:05 joko |
## Revision 1.6 2003/03/27 15:31:05 joko |
| 39 |
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
## fixes to modules regarding new namespace(s) below Data::Mungle::* |
| 40 |
## |
## |
| 69 |
use Hash::Merge qw( merge ); |
use Hash::Merge qw( merge ); |
| 70 |
use Iterate; |
use Iterate; |
| 71 |
|
|
| 72 |
use shortcuts qw( run_cmd ); |
use shortcuts qw( run_cmd RUNNING_IN_HELL RUNNING_IN_HEAVEN ); |
| 73 |
use Data::Mungle::Code::Ref qw( ref_slot ); |
use Data::Mungle::Code::Ref qw( ref_slot ); |
| 74 |
use Data::Mungle::Transform::Deep qw( expand ); |
use Data::Mungle::Transform::Deep qw( expand deep_copy ); |
| 75 |
|
use File::Temp qw/ tempfile tempdir /; |
| 76 |
|
|
| 77 |
|
my $DEBUG = 0; |
| 78 |
|
|
| 79 |
sub performTarget { |
sub performTarget { |
| 80 |
my $self = shift; |
my $self = shift; |
| 86 |
my $self = shift; |
my $self = shift; |
| 87 |
my $targetname = shift; |
my $targetname = shift; |
| 88 |
|
|
| 89 |
|
# pre-flight checks |
| 90 |
|
if (!$targetname) { |
| 91 |
|
$self->log("Target name empty. Please try to specify (e.g.) on the command line.", 'critical'); |
| 92 |
|
return; |
| 93 |
|
} |
| 94 |
|
|
| 95 |
my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6); |
my $header = ("- " x 12) . " " . $targetname . " " . ("- " x 6); |
| 96 |
|
|
| 97 |
|
# V1 |
| 98 |
#$self->log("- " x 35, 'notice'); |
#$self->log("- " x 35, 'notice'); |
| 99 |
#$self->log("Performing Target '$targetname'.", 'notice'); |
#$self->log("Performing Target '$targetname'.", 'notice'); |
| 100 |
|
|
| 101 |
$self->log($header, 'notice'); |
# V2 |
| 102 |
|
#$self->log($header, 'notice'); |
| 103 |
|
|
| 104 |
|
# V3 |
| 105 |
|
#$self->log("- " x 20, 'info'); |
| 106 |
|
$self->log("Performing Target '$targetname'.", 'notice'); |
| 107 |
|
|
| 108 |
#exit; |
#exit; |
| 109 |
|
|
| 115 |
|
|
| 116 |
$self->perform_dependencies($target); |
$self->perform_dependencies($target); |
| 117 |
$self->perform_details($target); |
$self->perform_details($target); |
| 118 |
|
|
| 119 |
|
return 1; |
| 120 |
|
|
| 121 |
} |
} |
| 122 |
|
|
| 148 |
foreach my $entry (@{$target->{content}}) { |
foreach my $entry (@{$target->{content}}) { |
| 149 |
my $command = $entry->{name}; |
my $command = $entry->{name}; |
| 150 |
my $args = $entry->{attrib}; |
my $args = $entry->{attrib}; |
| 151 |
$self->perform_command($command, $args); |
my $content = $entry->{content}; |
| 152 |
|
$self->perform_command($command, $args, $content, { warn => 1 } ); |
| 153 |
# check recursiveness |
# check recursiveness |
| 154 |
if ($entry->{content} && ref $entry->{content}) { |
# new condition: don't recurse if node is flagged to have inline-args (2003-04-17) |
| 155 |
|
my $has_inline_args = ($entry->{attrib}->{_args} && $entry->{attrib}->{_args} eq 'inline'); |
| 156 |
|
if ($entry->{content} && ref $entry->{content} && !$has_inline_args) { |
| 157 |
$self->perform_details($entry); |
$self->perform_details($entry); |
| 158 |
} |
} |
| 159 |
|
# new of 2003-05-08 |
| 160 |
|
$command ||= ''; |
| 161 |
|
$self->perform_command($command . '_end', undef, undef, { warn => 0 } ); |
| 162 |
} |
} |
| 163 |
} |
} |
| 164 |
|
|
| 170 |
sub perform_command { |
sub perform_command { |
| 171 |
my $self = shift; |
my $self = shift; |
| 172 |
my $command = shift; |
my $command = shift; |
| 173 |
|
my $args_list = shift; |
| 174 |
|
my $content = shift; |
| 175 |
|
my $options = shift; |
| 176 |
|
|
| 177 |
if (!$command) { |
if (!$command) { |
| 178 |
$self->log("Command was empty!", 'debug'); |
$self->log("Command was empty!", 'debug') if $DEBUG; |
| 179 |
return; |
return; |
| 180 |
} |
} |
| 181 |
|
|
| 182 |
# FIXME: make '__PACKAGE__' go one level deeper properly! |
# FIXME: make '__PACKAGE__' go one level deeper properly! |
| 183 |
$self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug'); |
$self->log( __PACKAGE__ . "->perform_command: " . $command, 'debug') if $DEBUG; |
| 184 |
|
|
| 185 |
my $args_list = shift; |
|
| 186 |
|
# 1. make arguments from list of arguments(?) |
| 187 |
|
|
| 188 |
my $args = {}; |
my $args = {}; |
| 189 |
#print Dumper($args_list); |
#print Dumper($args_list); |
| 190 |
if ($args_list) { |
if ($args_list) { |
| 196 |
$args = $args_list; |
$args = $args_list; |
| 197 |
} |
} |
| 198 |
} |
} |
|
$command = '_' . $command; |
|
| 199 |
|
|
| 200 |
|
|
| 201 |
# determine container |
# 2. prepare command |
| 202 |
|
|
| 203 |
|
# default setting for internal rap commands |
| 204 |
|
my $method_prefix_default = '_'; |
| 205 |
|
# setting from property database |
| 206 |
|
my $method_prefix_setting = $self->get_property('core.method_prefix'); |
| 207 |
|
#print "setting: ", $method_prefix_setting, "\n"; |
| 208 |
|
my $prefix = $method_prefix_setting; |
| 209 |
|
if (not defined $prefix) { |
| 210 |
|
$prefix = $method_prefix_default; |
| 211 |
|
} |
| 212 |
|
$command = $prefix . $command; |
| 213 |
|
|
| 214 |
|
|
| 215 |
|
# 3. determine container |
| 216 |
my $container; # = $self->getInstance(); |
my $container; # = $self->getInstance(); |
| 217 |
|
#$container ||= $self->getInstance(); |
| 218 |
$container ||= $self; |
$container ||= $self; |
| 219 |
|
|
| 220 |
|
# 4. run method |
| 221 |
if ($container->can($command)) { |
if ($container->can($command)) { |
| 222 |
$container->$command($args); |
$container->$command($args, $content); |
| 223 |
} else { |
} else { |
| 224 |
$self->log("Command '$command' not implemented.", "warning"); |
my $level = "debug"; |
| 225 |
|
$level = "warning" if $options->{warn}; |
| 226 |
|
$self->log("Command '$command' not implemented.", $level) if $DEBUG; |
| 227 |
} |
} |
| 228 |
|
|
| 229 |
} |
} |
| 287 |
|
|
| 288 |
$self->log("set-name: $name"); |
$self->log("set-name: $name"); |
| 289 |
|
|
| 290 |
|
#print Dumper($name, $value, '.'); |
| 291 |
|
|
| 292 |
# fill property slot with given value |
# fill property slot with given value |
| 293 |
if ($value) { |
# fix (2003-04-17): always do fill if value is *defined*!!! |
| 294 |
|
if (defined $value) { |
| 295 |
ref_slot($self, $name, $value, '.'); |
ref_slot($self, $name, $value, '.'); |
| 296 |
} |
} |
| 297 |
|
|
| 350 |
|
|
| 351 |
$name = '__rap.properties.' . $name; |
$name = '__rap.properties.' . $name; |
| 352 |
|
|
| 353 |
$self->log("get-name: $name"); |
$self->log("get-name: $name") if $DEBUG; |
| 354 |
|
|
| 355 |
# get property slot and return value |
# get property slot and return value |
| 356 |
$result = ref_slot($self, $name, undef, '.'); |
$result = ref_slot($self, $name, undef, '.'); |
| 377 |
my $self = shift; |
my $self = shift; |
| 378 |
my $opts = shift; |
my $opts = shift; |
| 379 |
|
|
| 380 |
|
my $meta = deep_copy($opts); |
| 381 |
|
|
| 382 |
|
delete $opts->{caption}; |
| 383 |
|
delete $opts->{async}; |
| 384 |
|
delete $opts->{detach}; |
| 385 |
|
|
| 386 |
|
#print Dumper($meta); |
| 387 |
|
|
| 388 |
if ($opts->{executable}) { |
if ($opts->{executable}) { |
| 389 |
|
|
| 390 |
my $program = $opts->{executable}; |
my $program = $opts->{executable}; |
| 391 |
delete $opts->{executable}; |
delete $opts->{executable}; |
| 392 |
|
|
| 393 |
# determine execution method |
# determine execution method |
|
my $method = 'run_cmd'; |
|
| 394 |
my $wrapper_program = ''; |
my $wrapper_program = ''; |
| 395 |
|
|
| 396 |
# check if program is a namespace-string (contains '::') - use 'do' in this case! |
# check if program is a namespace-string (contains '::') - use 'do' in this case! |
| 397 |
if ($program =~ /::/) { |
if ($program =~ /::/) { |
| 398 |
#if ($program =~ s/::/\\/g) { |
#$wrapper_program = 'rap.pl'; |
| 399 |
#$program = '.\\' . $program; |
$wrapper_program = $0; |
|
#$method = 'require'; |
|
|
#do "$program"; |
|
|
#return; |
|
|
#$method = 'rap.'; |
|
|
|
|
|
$wrapper_program = 'rap.pl'; |
|
|
|
|
| 400 |
} |
} |
| 401 |
|
|
| 402 |
# prepare arguments |
# prepare arguments |
| 420 |
} |
} |
| 421 |
|
|
| 422 |
# build {program} & {arguments} |
# build {program} & {arguments} |
| 423 |
my $cmd = join(' ', $wrapper_program, $program) . ' ' . join(' ', @buf); |
my $cmd = ($wrapper_program ? $wrapper_program . ' ' : '') . $program . ' ' . join(' ', @buf); |
| 424 |
|
|
| 425 |
# trace |
# trace |
| 426 |
#print "command: $cmd", "\n"; |
#print "command: $cmd", "\n"; |
| 427 |
|
|
| 428 |
# start process |
# start process |
| 429 |
# V1: via shortcut |
# 2004-05-11 - seems like only ONE args is valid at PERL5LIB, so we use V2! |
| 430 |
#$ENV{PERL5LIB} = join(' ', @INC); |
# 2004-06-16 - found out delimiter required for PERL5LIB, reverting back to V1! |
| 431 |
|
|
| 432 |
# FIXME!!! what about the other slots of @INC? |
# V1: join all args |
| 433 |
$ENV{PERL5LIB} = $INC[0]; |
#my $delimiter = ':'; |
| 434 |
|
#$delimiter = ';' if RUNNING_IN_HELL(); |
| 435 |
|
#$ENV{PERL5LIB} = join($delimiter, @INC); |
| 436 |
|
# V2: insert only FIRST arg |
| 437 |
|
#$ENV{PERL5LIB} = $INC[0]; |
| 438 |
|
# WARNING: at (free)BSD our var is the SECOND, NOT FIRST!! |
| 439 |
|
# FIXME!! Do this in an abstract way!! |
| 440 |
|
#$ENV{PERL5LIB} = $INC[1]; |
| 441 |
|
# V3: mix V1+V2 (because V1 doesn't fit at freeBSD..) |
| 442 |
|
if(RUNNING_IN_HEAVEN()) { |
| 443 |
|
$ENV{PERL5LIB} = $INC[1]; |
| 444 |
|
} else { |
| 445 |
|
my $delimiter = ':'; |
| 446 |
|
$delimiter = ';' if RUNNING_IN_HELL(); |
| 447 |
|
$ENV{PERL5LIB} = join($delimiter, @INC); |
| 448 |
|
} |
| 449 |
|
|
| 450 |
#print Dumper(%ENV); |
#print Dumper(%ENV); |
| 451 |
|
|
| 452 |
print "command: '$cmd'", "\n"; |
#print "command: '$cmd'", "\n"; |
| 453 |
|
|
| 454 |
# V1 - basic |
# V1 - basic |
| 455 |
#run_cmd($cmd); |
#run_cmd($cmd); |
| 456 |
|
|
| 457 |
# V1.b - enhanced: variable local method |
# V1.b - enhanced: variable local method |
| 458 |
my $evalstr = "$method('$cmd');"; |
$meta->{caption} ||= ''; |
| 459 |
|
$meta->{async} ||= 0; |
| 460 |
|
$meta->{detach} ||= 0; |
| 461 |
|
# new of 2003-05-08: USE_PATH! |
| 462 |
|
$meta->{USE_PATH} ||= 0; |
| 463 |
|
my $evalstr = "run_cmd('$cmd', '$meta->{caption}', { async => $meta->{async}, detach => $meta->{detach}, USE_PATH => $meta->{USE_PATH} });"; |
| 464 |
eval($evalstr); |
eval($evalstr); |
| 465 |
#my $res = do "$cmd"; |
#my $res = do "$cmd"; |
| 466 |
#print $res, "\n" if $res; |
#print $res, "\n" if $res; |
| 470 |
|
|
| 471 |
# V2: via IPC::Run |
# V2: via IPC::Run |
| 472 |
# .... (TODO) |
# .... (TODO) |
| 473 |
|
|
| 474 |
|
|
| 475 |
} |
} |
| 476 |
|
|
| 477 |
} |
} |
| 478 |
|
|
| 479 |
|
|
| 480 |
|
sub run_script { |
| 481 |
|
|
| 482 |
|
my $self = shift; |
| 483 |
|
my $args = shift; |
| 484 |
|
my $code = shift; |
| 485 |
|
|
| 486 |
|
if ($args->{language} eq 'msdos/bat') { |
| 487 |
|
|
| 488 |
|
#print "code: $code", "\n"; |
| 489 |
|
|
| 490 |
|
# reporting |
| 491 |
|
$self->log("Executing some arbitrary unsigned code (probably unsafe). [language=$args->{language}]", 'info'); |
| 492 |
|
$self->log("\n<code>\n$code\n</code>", 'info'); |
| 493 |
|
|
| 494 |
|
# create temporary intermediate file to execute code |
| 495 |
|
my $tmpdir = '/tmp/rap'; |
| 496 |
|
mkdir $tmpdir; |
| 497 |
|
(my $fh, my $filename) = tempfile( DIR => $tmpdir, SUFFIX => '.bat' ); |
| 498 |
|
print $fh $code, "\n"; |
| 499 |
|
run_cmd( $filename, '', { async => 1 } ); |
| 500 |
|
|
| 501 |
|
# FIXME: DELETE code inside temp-files as soon as possible! |
| 502 |
|
#print $fh ''; |
| 503 |
|
|
| 504 |
|
# TODO: delete FILE completely! |
| 505 |
|
# required for this: wait until execution has finished, then unlink.... |
| 506 |
|
# but: "how to wait until execution is finished"? |
| 507 |
|
# i believe the best is to spawn *another* process directly from here, |
| 508 |
|
# let's call it 'watcher-agent'. |
| 509 |
|
# This one should monitor a certain running process and delete its |
| 510 |
|
# executable file after it has finished execution. |
| 511 |
|
# Possible extensions could be: |
| 512 |
|
# keep track of all stuff sent to STDOUT or STDERR and |
| 513 |
|
# send that stuff to the task-owner indirectly (not via shell/console) |
| 514 |
|
# (e.g. via email, by posting report to a newsgroup or publishing on a specified web-page: use mod-dav!) |
| 515 |
|
|
| 516 |
|
} elsif ($args->{language} eq 'bash') { |
| 517 |
|
$self->log("FIXME: - - - - - -- - - -- BASH - - - - - - - -- - ", 'error'); |
| 518 |
|
|
| 519 |
|
} elsif ($args->{language} eq 'perl') { |
| 520 |
|
|
| 521 |
|
# reporting |
| 522 |
|
#$self->log("Executing some arbitrary unsigned code (probably unsafe). [language=$args->{language}]", 'info'); |
| 523 |
|
#$self->log("\n<code>\n$code\n</code>", 'info'); |
| 524 |
|
|
| 525 |
|
# do it |
| 526 |
|
#eval($code); |
| 527 |
|
#$self->log("\n<code>\n$code\n</code>", 'error') if $@; |
| 528 |
|
|
| 529 |
|
} else { |
| 530 |
|
$self->log("FIXME: Script language '$args->{language}' not implemented.", 'error'); |
| 531 |
|
|
| 532 |
|
} |
| 533 |
|
|
| 534 |
|
} |
| 535 |
|
|
| 536 |
1; |
1; |
| 537 |
__END__ |
__END__ |