blob: 4c1ce4528b21fa82189eef0f4842ed7143a74e85 [file] [log] [blame]
mblighf4c35322006-03-13 01:01:10 +00001from autotest_utils import *
apw0865f482006-03-30 18:50:19 +00002import os, sys, kernel, test, pickle, threading
mblighf4c35322006-03-13 01:01:10 +00003
apw0865f482006-03-30 18:50:19 +00004# Parallel run interface.
5class 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.
mblighf4c35322006-03-13 01:01:10 +000014class job:
apwecf41b72006-03-31 14:00:55 +000015 def __init__(self, control, jobtag='default'):
mblighf4c35322006-03-13 01:01:10 +000016 self.autodir = os.environ['AUTODIR']
17 self.tmpdir = self.autodir + '/tmp'
mbligha66742f2006-04-02 19:54:27 +000018 if os.path.exists(self.tmpdir):
19 os.system('rm -rf ' + self.tmpdir)
20 os.mkdir(self.tmpdir)
mblighf4c35322006-03-13 01:01:10 +000021 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
apwecf41b72006-03-31 14:00:55 +000028 self.control = control
mblighf4c35322006-03-13 01:01:10 +000029 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
apw5a78eef2006-04-03 14:16:59 +000037 def runtest(self, tag, testname, *test_args):
38 exec "import %s" % testname
39 exec "mytest = %s.%s(self, testname + '.' + tag)" % (testname, testname)
mblighf4c35322006-03-13 01:01:10 +000040 mytest.run(testname, test_args)
apw0865f482006-03-30 18:50:19 +000041
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:
apwecf41b72006-03-31 14:00:55 +000062 os.unlink(self.control + '.state')
apw0865f482006-03-30 18:50:19 +000063 except:
64 pass
mbligha66742f2006-04-02 19:54:27 +000065 if os.path.exists(self.control):
66 os.rename(self.control, self.control + '.complete')
apw0865f482006-03-30 18:50:19 +000067
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)
apwecf41b72006-03-31 14:00:55 +000076 pickle.dump(self.steps, open(self.control + '.state', 'w'))
apw0865f482006-03-30 18:50:19 +000077
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:
apwecf41b72006-03-31 14:00:55 +000083 self.steps = pickle.load(open(self.control + '.state',
84 'r'))
apw0865f482006-03-30 18:50:19 +000085 except:
86 self.next_step(init)
87
88 # Run the step list.
89 while len(self.steps) > 0:
90 step = self.steps.pop()
apwecf41b72006-03-31 14:00:55 +000091 pickle.dump(self.steps, open(self.control + '.state',
92 'w'))
apw0865f482006-03-30 18:50:19 +000093
94 cmd = step.pop(0)
95 cmd(*step)
96
97 # all done, clean up and exit.
98 self.complete(0)
99