Daniel Dunbar | 378530c | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 1 | import Arguments |
| 2 | import Util |
| 3 | |
| 4 | class Job(object): |
| 5 | """Job - A set of commands to execute as a single task.""" |
| 6 | |
| 7 | def iterjobs(self): |
| 8 | abstract |
| 9 | |
| 10 | class Command(Job): |
| 11 | """Command - Represent the information needed to execute a single |
| 12 | process.""" |
| 13 | |
| 14 | def __init__(self, executable, args): |
| 15 | assert Util.all_true(args, lambda x: isinstance(x, Arguments.Arg)) |
| 16 | self.executable = executable |
| 17 | self.args = args |
| 18 | |
| 19 | def __repr__(self): |
| 20 | return Util.prefixAndPPrint(self.__class__.__name__, |
| 21 | (self.executable, self.args)) |
| 22 | |
| 23 | def render(self, args): |
| 24 | argv = [self.executable] |
Daniel Dunbar | 7472787 | 2009-01-06 01:35:44 +0000 | [diff] [blame] | 25 | for arg in self.args: |
Daniel Dunbar | 9d32bcd | 2009-01-07 01:29:28 +0000 | [diff] [blame] | 26 | argv.extend(args.render(arg)) |
Daniel Dunbar | 378530c | 2009-01-05 19:53:30 +0000 | [diff] [blame] | 27 | return argv |
| 28 | |
| 29 | def iterjobs(self): |
| 30 | yield self |
| 31 | |
| 32 | class PipedJob(Job): |
| 33 | """PipedJob - A sequence of piped commands.""" |
| 34 | |
| 35 | def __init__(self, commands): |
| 36 | assert all_true(args, lambda x: isinstance(x, Arguments.Command)) |
| 37 | self.commands = list(commands) |
| 38 | |
| 39 | def addJob(self, job): |
| 40 | assert isinstance(job, Command) |
| 41 | self.commands.append(job) |
| 42 | |
| 43 | def __repr__(self): |
| 44 | return Util.prefixAndPPrint(self.__class__.__name__, (self.commands,)) |
| 45 | |
| 46 | class JobList(Job): |
| 47 | """JobList - A sequence of jobs to perform.""" |
| 48 | |
| 49 | def __init__(self, jobs=[]): |
| 50 | self.jobs = list(jobs) |
| 51 | |
| 52 | def addJob(self, job): |
| 53 | self.jobs.append(job) |
| 54 | |
| 55 | def __repr__(self): |
| 56 | return Util.prefixAndPPrint(self.__class__.__name__, (self.jobs,)) |
| 57 | |
| 58 | def iterjobs(self): |
| 59 | for j in self.jobs: |
| 60 | yield j |