#!/usr/bin/perl # # constructs a job sequence into a queue, with dependencies. # # use Getopt::Std; use FileHandle; getopts('m:l'); #$skeleton = $ENV{HOME} . '/.skeleton/' . $ARGV[0] ; #if ( ! -f $skeleton ) { # $skeleton = '/home/inverse/.skeleton/' . $ARGV[0] ; #} $skeleton = get_template $ARGV[0]; open(SKELETON, "$skeleton") or die "Could not open $skeleton"; @skeleton = ; chomp @skeleton; close(SKELETON); #print @skeleton; @jobs=(); foreach (@skeleton) { if ( $_ =~ /^#/ ) { print STDERR "foo"; next; } #An example of a line: # 22:1,5,3: foo $1 $2 -lh bar $job = {}; @parts = split(/:/,$_); $job->{Id} = $parts[0]; # assemble the dependency list. $job->{deplist} = $parts[1]; @deps = split(/,/,$job->{deplist}); if (@deps) { $job->{deplist} = [@deps]; } # print "deps $deps ... @{$job->{deplist}}\n"; # replace the arguments with the requested arguments in this incantation. $job->{command} = $parts[2]; $job->{command} =~ s/\$(\d+)/$ARGV[$1]/g; # push(@jobs,$job); # we've built an anonymous hash. Now refer to it. @jobs[$job->{Id}] = $job; } foreach $job (@jobs) { print "Job $job->{Id}, deps @{$job->{deplist}} \n"; } if ($opt_l) { $taskfile = &get_taskfile_number; print "Logging the task to $taskfile.\n"; open(TASK, ">$taskfile") or die "Could not open $taskfile.\n"; foreach (@jobs) { print TASK "$_->{Id}\n"; } close TASK; } foreach $job (@jobs) { if (!$job->{Id} ) { next ; } $pbsubmit = "pbsubmit -c \"$job->{command}\" " ; # handle dependencies. add them to $pbsubmit $foo = @{$job->{deplist}} ; if ( $foo > 0 ) { $pbsubmit .= '-o "-W depend='; foreach (@{$job->{deplist}}) { $foo = $jobs[$_]; print " . $foo . %$foo . $foo->{qsubid} \n" ; print "__ $_\n"; $pbsubmit .= "afterok:$foo->{qsubid},"; } $pbsubmit =~ s/,$/\"/; } # TODO: handle any pbsubmit options being passed down? print "$pbsubmit\n"; open(PBSUBMIT, "$pbsubmit 2>/dev/null |"); @results = ; close PBSUBMIT; # print "res @results\n"; # @result = split(/\n/,$result) ; chomp @results; $job->{qsubid} = $results[1]; print "qsubid $job->{qsubid} \n"; } sub get_template { my @dirs; my $template_name = shift; my $path_to_template = ''; # Yeah, I know what you're saying, "as if" if ($ENV{"PBS_TEMPLATE_PATH"}) { @dirs = split(':', $ENV{PBS_TEMPLATE_PATH}) ; } push @dirs, ("$ENV{HOME}/.pbs_templates",'/usr/share/pbs_templates'); foreach (@paths) { if (-f "$_/$template_name") { return "$_/$template_name"; } } die "Could not find the template $template_name anywhere.\n"; } sub get_taskfile_number { my $homedir = $ENV{"HOME"} ; my ($last_job,$new_job,@jobs_dir); unless(opendir(JOBSDIR, "$homedir/.pbsrc")) { print STDERR "no .pbsrc directory. Making one. \n"; mkdir("$homedir/.pbsrc",0744) || die "Could not make .pbsrc. Something is wrong.\n\n" ; } @jobs_dir = grep /pbstask_[1234567890]+$/, readdir JOBSDIR; closedir JOBSDIR; foreach $last_job (@jobs_dir) { $last_job =~ s/^pbsjob_// ; if ($last_job > $new_job) { $new_job = $last_job ; } } $new_job ++; $new_job = "$homedir/pbstask_$new_job"; return $new_job; }