mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 1 | from autotest_utils import * |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 2 | import os, sys, kernel, test, pickle, threading |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 3 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 4 | # Parallel run interface. |
| 5 | class AsyncRun(threading.Thread): |
| 6 | def __init__(self, cmd): |
| 7 | threading.Thread.__init__(self) |
| 8 | self.cmd = cmd |
| 9 | def run(self): |
| 10 | x = self.cmd.pop(0) |
| 11 | x(*self.cmd) |
| 12 | |
| 13 | # JOB: the actual job against which we do everything. |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 14 | class job: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 15 | def __init__(self, control, jobtag='default'): |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 16 | self.autodir = os.environ['AUTODIR'] |
| 17 | self.tmpdir = self.autodir + '/tmp' |
mbligh | a66742f | 2006-04-02 19:54:27 +0000 | [diff] [blame] | 18 | if os.path.exists(self.tmpdir): |
| 19 | os.system('rm -rf ' + self.tmpdir) |
| 20 | os.mkdir(self.tmpdir) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 21 | self.resultdir = self.autodir + '/results' + jobtag |
| 22 | if os.path.exists(self.resultdir): |
| 23 | os.system('rm -rf ' + self.resultdir) |
| 24 | os.mkdir(self.resultdir) |
| 25 | os.mkdir(self.resultdir + "/debug") |
| 26 | os.mkdir(self.resultdir + "/analysis") |
| 27 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 28 | self.control = control |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 29 | self.jobtab = jobtag |
| 30 | |
| 31 | self.stdout = fd_stack(1, sys.stdout) |
| 32 | self.stderr = fd_stack(2, sys.stderr) |
| 33 | |
| 34 | def kernel(self, topdir, base_tree): |
| 35 | return kernel.kernel(self, topdir, base_tree) |
| 36 | |
apw | 5a78eef | 2006-04-03 14:16:59 +0000 | [diff] [blame^] | 37 | def runtest(self, tag, testname, *test_args): |
| 38 | exec "import %s" % testname |
| 39 | exec "mytest = %s.%s(self, testname + '.' + tag)" % (testname, testname) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 40 | mytest.run(testname, test_args) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 41 | |
| 42 | def noop(self, text): |
| 43 | print "job: noop: " + text |
| 44 | |
| 45 | # Job control primatives. |
| 46 | def parallel(self, *l): |
| 47 | tasks = [] |
| 48 | for t in l: |
| 49 | task = AsyncRun(t) |
| 50 | tasks.append(task) |
| 51 | task.start() |
| 52 | for t in tasks: |
| 53 | t.join() |
| 54 | |
| 55 | # XXX: should have a better name. |
| 56 | def quit(self): |
| 57 | sys.exit(5) |
| 58 | |
| 59 | def complete(self, status): |
| 60 | # We are about to exit 'complete' so clean up the control file. |
| 61 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 62 | os.unlink(self.control + '.state') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 63 | except: |
| 64 | pass |
mbligh | a66742f | 2006-04-02 19:54:27 +0000 | [diff] [blame] | 65 | if os.path.exists(self.control): |
| 66 | os.rename(self.control, self.control + '.complete') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 67 | |
| 68 | sys.exit(0) |
| 69 | |
| 70 | # STEPS: the stepping engine -- if the control file defines |
| 71 | # step_init we will be using this engine to drive multiple |
| 72 | # runs. |
| 73 | steps = [] |
| 74 | def next_step(self, step): |
| 75 | self.steps.append(step) |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 76 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 77 | |
| 78 | def step_engine(self, init): |
| 79 | # If there is a mid-job state file load that in and continue |
| 80 | # where it indicates. Otherwise start stepping at the passed |
| 81 | # entry. |
| 82 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 83 | self.steps = pickle.load(open(self.control + '.state', |
| 84 | 'r')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 85 | except: |
| 86 | self.next_step(init) |
| 87 | |
| 88 | # Run the step list. |
| 89 | while len(self.steps) > 0: |
| 90 | step = self.steps.pop() |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 91 | pickle.dump(self.steps, open(self.control + '.state', |
| 92 | 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 93 | |
| 94 | cmd = step.pop(0) |
| 95 | cmd(*step) |
| 96 | |
| 97 | # all done, clean up and exit. |
| 98 | self.complete(0) |
| 99 | |