[Melbourne-pm] Runner framework

Timothy S. Nelson wayland at wayland.id.au
Thu Jan 22 19:11:56 PST 2009


On Fri, 23 Jan 2009, Mathew Robertson wrote:

>> I need to run a set of 'scripts' on various application directories.
>> These scripts can be anything. SQL DDL, XML configuration files to be
>> run via a custom program(s), templates to be updated etc...
>> 
>> Some scripts will have pre-requisite  scripts
>> 
>> I need to produce customised logging and error reporting. In most
>> cases some attempt should be made at error recovery or roll-back, but
>> it varies.
>> 
>> I'd like a to be able to provide a bunch of files containing these
>> scripts and some meta-data and have a framework do the heavy lifting
> I'll venture out to say "yes and no" - basically what you just described was 
> shell-programming.

 	This was exactly the thought that prompted me to ask him to clarify. 
Question for Alec; what platform are you on?  Because us Unix types take the 
"bash" shell for granted, which is better than the Windows command/cmd shell. 
But on the other hand, Perl is more flexible than bash :).

 	I'd like to point out that none of the lifting here sounds 
particularly heavy, if you're familiar with the appropriate parts of perl. 
Have you looked at open3 (aka IPC::open3), opendir, and the like?

use	File::Basename;
use	IPC::open3;

$path = "/path/to/scriptdir";
@suffixes = qw(sql pl);
opendir(DIR, $path);
foreach(grep { !/^\./ and -f } readdir(DIR)) {
 	($count) = split(/-/, $_, 2);
 	($name, $path, $suffix) = fileparse($_, @suffixes);
 	push @{ $scripts{$count} }, {
 		count => $count,
 		filename => $_,
 		type => $suffix,
 	};
}
closedir(DIR);

foreach $count (sort { $a <=> $b } keys %scripts) {
 	foreach $filehash ($scripts{$count}) {
 		($filename, $type) = map { $filehash->{$_} } qw(filename type);
 		$text = join '', getfile($filename);
 		&$type($text, $filename);
 	}
}

exit;

sub	sql {
 	my($text) = @_;
 	my($wtr, $rdr, $err, $pid);

 	$pid = open3($wtr, $rdr, $err,
 		'mysql -u username');
 	print $wtr $text;
 	# read from $rdr here
}

sub	getfile {
 	my($filename) = @_;
 	my(@lines);

 	open(FILE, $filename) or die "Error opening file '$filename': $!";
 	@lines = <FILE>;
 	close(FILE) or die "Error closing file '$filename': $!";

 	return(@lines);
}

 	Anyway, I haven't tested this or anything, but it should give you 
something to go on.  You'll presumably need to read the IPC::open3 man page 
fairly carefully.  The assumption in the script is that all your test files 
have names like 01-foo.sql and things like that.  The call to
&$type($text, $filename) will (if the $type variable contains 'sql') call the 
sql() function.

 	You could choose a different method of file type identification.  If 
you use Unix, my file type identification would probably do something like:

sub	get_type {
 	my($filename) = @_;
 	my(@lines);
 	local($_);

 	@lines = getfile("file $filename |"); # Note that the thing with the |
 		# on the end gets passed to the "open" function, which runs it
 		# as a command
 	@lines > 1 and die "What's going on?\n";
 	@lines < 1 and die "Unknown type for file '$filename'\n";
 	$_ = $lines[0];
 	/SQL/ and return('sql');
 	chomp;
 	die "Found type '$lines[0]', but we don't recognise it";
}

 	That's a more reliable ID method, but unfortunately, doesn't work on 
Windows.

 	Hope all this helps.

 	:)


---------------------------------------------------------------------
| Name: Tim Nelson                 | Because the Creator is,        |
| E-mail: wayland at wayland.id.au    | I am                           |
---------------------------------------------------------------------

----BEGIN GEEK CODE BLOCK----
Version 3.12
GCS d+++ s+: a- C++$ U+++$ P+++$ L+++ E- W+ N+ w--- V- 
PE(+) Y+>++ PGP->+++ R(+) !tv b++ DI++++ D G+ e++>++++ h! y-
-----END GEEK CODE BLOCK-----



More information about the Melbourne-pm mailing list