mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 1 | """The main job wrapper |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 2 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 3 | This is the core infrastructure. |
| 4 | """ |
| 5 | |
| 6 | __author__ = """Copyright Andy Whitcroft, Martin J. Bligh 2006""" |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 7 | |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 8 | # standard stuff |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 9 | import os, sys, re, pickle, shutil |
mbligh | 8f243ec | 2006-10-10 05:55:49 +0000 | [diff] [blame] | 10 | # autotest stuff |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 11 | from autotest_utils import * |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 12 | from parallel import * |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 13 | import kernel, xen, test, profilers, barrier, filesystem, fd_stack, boottool |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 14 | import harness, config |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 15 | |
| 16 | class job: |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 17 | """The actual job against which we do everything. |
| 18 | |
| 19 | Properties: |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 20 | autodir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 21 | The top level autotest directory (/usr/local/autotest). |
| 22 | Comes from os.environ['AUTODIR']. |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 23 | bindir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 24 | <autodir>/bin/ |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 25 | testdir |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 26 | <autodir>/tests/ |
| 27 | profdir |
| 28 | <autodir>/profilers/ |
| 29 | tmpdir |
| 30 | <autodir>/tmp/ |
| 31 | resultdir |
| 32 | <autodir>/results/<jobtag> |
| 33 | stdout |
| 34 | fd_stack object for stdout |
| 35 | stderr |
| 36 | fd_stack object for stderr |
| 37 | profilers |
| 38 | the profilers object for this job |
apw | 504a7dd | 2006-10-12 17:18:37 +0000 | [diff] [blame] | 39 | harness |
| 40 | the server harness object for this job |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 41 | config |
| 42 | the job configuration for this job |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 43 | """ |
| 44 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 45 | def __init__(self, control, jobtag, cont, harness_type=None): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 46 | """ |
| 47 | control |
| 48 | The control file (pathname of) |
| 49 | jobtag |
| 50 | The job tag string (eg "default") |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 51 | cont |
| 52 | If this is the continuation of this job |
apw | e68a713 | 2006-12-01 11:21:37 +0000 | [diff] [blame] | 53 | harness_type |
| 54 | An alternative server harness |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 55 | """ |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 56 | self.autodir = os.environ['AUTODIR'] |
mbligh | 0674377 | 2006-05-18 21:30:19 +0000 | [diff] [blame] | 57 | self.bindir = self.autodir + '/bin' |
mbligh | 8264186 | 2006-04-23 06:21:36 +0000 | [diff] [blame] | 58 | self.testdir = self.autodir + '/tests' |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 59 | self.profdir = self.autodir + '/profilers' |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 60 | self.tmpdir = self.autodir + '/tmp' |
mbligh | 24f7da0 | 2006-04-23 06:32:18 +0000 | [diff] [blame] | 61 | self.resultdir = self.autodir + '/results/' + jobtag |
mbligh | a250805 | 2006-05-28 21:29:53 +0000 | [diff] [blame] | 62 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 63 | if not cont: |
| 64 | if os.path.exists(self.tmpdir): |
| 65 | system('rm -rf ' + self.tmpdir) |
| 66 | os.mkdir(self.tmpdir) |
| 67 | |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 68 | if not os.path.exists(self.autodir + '/results'): |
| 69 | os.mkdir(self.autodir + '/results') |
| 70 | |
apw | 96da1a4 | 2006-11-02 00:23:18 +0000 | [diff] [blame] | 71 | if os.path.exists(self.resultdir): |
| 72 | system('rm -rf ' + self.resultdir) |
| 73 | os.mkdir(self.resultdir) |
| 74 | |
| 75 | os.mkdir(self.resultdir + "/debug") |
| 76 | os.mkdir(self.resultdir + "/analysis") |
| 77 | os.mkdir(self.resultdir + "/sysinfo") |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 78 | shutil.copyfile(control, self.resultdir + "/control") |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 79 | |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 80 | self.control = control |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 81 | self.jobtab = jobtag |
| 82 | |
mbligh | 56f1fbb | 2006-10-01 15:10:56 +0000 | [diff] [blame] | 83 | self.stdout = fd_stack.fd_stack(1, sys.stdout) |
| 84 | self.stderr = fd_stack.fd_stack(2, sys.stderr) |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 85 | self.record_prefix = '' |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 86 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 87 | self.config = config.config(self) |
| 88 | |
apw | d27e55f | 2006-12-01 11:22:08 +0000 | [diff] [blame] | 89 | self.harness = harness.select(harness_type, self) |
| 90 | |
mbligh | a35553b | 2006-04-23 15:52:25 +0000 | [diff] [blame] | 91 | self.profilers = profilers.profilers(self) |
mbligh | 7290556 | 2006-05-25 01:30:49 +0000 | [diff] [blame] | 92 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 93 | try: |
apw | 90154af | 2006-12-01 11:23:36 +0000 | [diff] [blame] | 94 | tool = self.config_get('boottool.executable') |
| 95 | self.bootloader = boottool.boottool(tool) |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 96 | except: |
| 97 | pass |
| 98 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 99 | pwd = os.getcwd() |
mbligh | 0674377 | 2006-05-18 21:30:19 +0000 | [diff] [blame] | 100 | os.chdir(self.resultdir + "/sysinfo") |
| 101 | system(self.bindir + '/sysinfo.py') |
mbligh | e21abaf | 2007-07-26 16:17:30 +0000 | [diff] [blame] | 102 | system('dmesg -c > dmesg', ignorestatus=1) |
mbligh | 0674377 | 2006-05-18 21:30:19 +0000 | [diff] [blame] | 103 | os.chdir(pwd) |
mbligh | 3a6d6ca | 2006-04-23 15:50:24 +0000 | [diff] [blame] | 104 | |
apw | 357f50f | 2006-12-01 11:22:39 +0000 | [diff] [blame] | 105 | self.harness.run_start() |
| 106 | |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 107 | |
| 108 | def relative_path(self, path): |
| 109 | """\ |
| 110 | Return a patch relative to the job results directory |
| 111 | """ |
mbligh | 1c250ca | 2007-08-30 16:31:38 +0000 | [diff] [blame^] | 112 | head = len(self.resultdir) + 1 # remove the / inbetween |
| 113 | return path[head:] |
mbligh | 0692e47 | 2007-08-30 16:07:53 +0000 | [diff] [blame] | 114 | |
| 115 | |
mbligh | 362ab3d | 2007-08-30 11:24:04 +0000 | [diff] [blame] | 116 | def control_get(self): |
| 117 | return self.control |
| 118 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 119 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 120 | def harness_select(self, which): |
| 121 | self.harness = harness.select(which, self) |
| 122 | |
| 123 | |
apw | 059e1b1 | 2006-10-12 17:18:26 +0000 | [diff] [blame] | 124 | def config_set(self, name, value): |
| 125 | self.config.set(name, value) |
| 126 | |
| 127 | |
| 128 | def config_get(self, name): |
| 129 | return self.config.get(name) |
| 130 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 131 | def setup_dirs(self, results_dir, tmp_dir): |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 132 | if not tmp_dir: |
| 133 | tmp_dir = self.tmpdir + '/build' |
| 134 | if not os.path.exists(tmp_dir): |
| 135 | os.mkdir(tmp_dir) |
| 136 | if not os.path.isdir(tmp_dir): |
| 137 | raise "Temp dir (%s) is not a dir - args backwards?" \ |
| 138 | % self.tmpdir |
| 139 | |
| 140 | # We label the first build "build" and then subsequent ones |
| 141 | # as "build.2", "build.3", etc. Whilst this is a little bit |
| 142 | # inconsistent, 99.9% of jobs will only have one build |
| 143 | # (that's not done as kernbench, sparse, or buildtest), |
| 144 | # so it works out much cleaner. One of life's comprimises. |
| 145 | if not results_dir: |
| 146 | results_dir = os.path.join(self.resultdir, 'build') |
| 147 | i = 2 |
| 148 | while os.path.exists(results_dir): |
| 149 | results_dir = os.path.join(self.resultdir, 'build.%d' % i) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 150 | i += 1 |
mbligh | 1e8858e | 2006-11-24 22:18:35 +0000 | [diff] [blame] | 151 | if not os.path.exists(results_dir): |
| 152 | os.mkdir(results_dir) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 153 | |
mbligh | 8baa2ea | 2006-12-17 23:01:24 +0000 | [diff] [blame] | 154 | return (results_dir, tmp_dir) |
| 155 | |
| 156 | |
| 157 | def xen(self, base_tree, results_dir = '', tmp_dir = '', leave = False, \ |
| 158 | kjob = None ): |
| 159 | """Summon a xen object""" |
| 160 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 161 | build_dir = 'xen' |
| 162 | return xen.xen(self, base_tree, results_dir, tmp_dir, build_dir, leave, kjob) |
| 163 | |
| 164 | |
| 165 | def kernel(self, base_tree, results_dir = '', tmp_dir = '', leave = False): |
| 166 | """Summon a kernel object""" |
| 167 | (results_dir, tmp_dir) = self.setup_dirs(results_dir, tmp_dir) |
| 168 | build_dir = 'linux' |
| 169 | return kernel.kernel(self, base_tree, results_dir, tmp_dir, build_dir, leave) |
mbligh | f4c3532 | 2006-03-13 01:01:10 +0000 | [diff] [blame] | 170 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 171 | |
mbligh | fadca20 | 2006-09-23 04:40:01 +0000 | [diff] [blame] | 172 | def barrier(self, *args): |
| 173 | """Create a barrier object""" |
| 174 | return barrier.barrier(*args) |
| 175 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 176 | |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 177 | def setup_dep(self, deps): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 178 | """Set up the dependencies for this test. |
| 179 | |
| 180 | deps is a list of libraries required for this test. |
| 181 | """ |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 182 | for dep in deps: |
| 183 | try: |
| 184 | os.chdir(self.autodir + '/deps/' + dep) |
| 185 | system('./' + dep + '.py') |
| 186 | except: |
| 187 | error = "setting up dependency " + dep + "\n" |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 188 | raise UnhandledError(error) |
mbligh | 4b08966 | 2006-06-14 22:34:58 +0000 | [diff] [blame] | 189 | |
| 190 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 191 | def __runtest(self, url, tag, args, dargs): |
| 192 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 193 | test.runtest(self, url, tag, args, dargs) |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 194 | except AutotestError: |
| 195 | raise |
| 196 | except: |
| 197 | raise UnhandledError('running test ' + \ |
| 198 | self.__class__.__name__ + "\n") |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 199 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 200 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 201 | def runtest(self, tag, url, *args): |
| 202 | raise "Deprecated call to job.runtest. Use run_test instead" |
| 203 | |
| 204 | |
| 205 | def run_test(self, url, *args, **dargs): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 206 | """Summon a test object and run it. |
| 207 | |
| 208 | tag |
| 209 | tag to add to testname |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 210 | url |
| 211 | url of the test to run |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 212 | """ |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 213 | |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 214 | if not url: |
| 215 | raise "Test name is invalid. Switched arguments?" |
mbligh | 12a7df7 | 2006-10-06 03:54:33 +0000 | [diff] [blame] | 216 | (group, name) = test.testname(url) |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 217 | tag = None |
| 218 | if dargs.has_key('tag'): |
| 219 | tag = dargs['tag'] |
| 220 | del dargs['tag'] |
apw | 5a7335c | 2007-03-12 20:32:40 +0000 | [diff] [blame] | 221 | if tag: |
| 222 | name += '.' + tag |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 223 | try: |
| 224 | try: |
mbligh | d016ecc | 2006-11-25 21:41:07 +0000 | [diff] [blame] | 225 | self.__runtest(url, tag, args, dargs) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 226 | except Exception, detail: |
| 227 | self.record("FAIL " + name + " " + \ |
| 228 | detail.__str__() + "\n") |
| 229 | |
| 230 | raise |
| 231 | else: |
| 232 | self.record("GOOD " + name + \ |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 233 | " completed successfully\n") |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 234 | except TestError: |
| 235 | return 0 |
| 236 | except: |
| 237 | raise |
| 238 | else: |
| 239 | return 1 |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 240 | |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 241 | |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 242 | def run_group(self, function, *args): |
| 243 | """\ |
| 244 | function: |
| 245 | subroutine to run |
| 246 | *args: |
| 247 | arguments for the function |
| 248 | """ |
| 249 | |
| 250 | name = function.__name__ |
| 251 | # if tag: |
| 252 | # name += '.' + tag |
| 253 | old_record_prefix = self.record_prefix |
| 254 | try: |
| 255 | try: |
| 256 | self.record("START " + name) |
| 257 | self.record_prefix += '\t' |
| 258 | function(*args) |
| 259 | self.record_prefix = old_record_prefix |
| 260 | self.record("END %s GOOD" % name) |
| 261 | except: |
| 262 | self.record_prefix = old_record_prefix |
| 263 | self.record("END %s FAIL" % name) |
| 264 | # We don't want to raise up an error higher if it's just |
| 265 | # a TestError - we want to carry on to other tests. Hence |
| 266 | # this outer try/except block. |
| 267 | except TestError: |
| 268 | pass |
| 269 | except: |
| 270 | raise TestError(name + ' failed\n' + format_error()) |
| 271 | |
| 272 | |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 273 | def filesystem(self, device, mountpoint = None, loop_size = 0): |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 274 | if not mountpoint: |
| 275 | mountpoint = self.tmpdir |
mbligh | c235985 | 2007-08-28 18:11:48 +0000 | [diff] [blame] | 276 | return filesystem.filesystem(self, device, mountpoint,loop_size) |
mbligh | d7fb4a6 | 2006-10-01 00:57:53 +0000 | [diff] [blame] | 277 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 278 | |
| 279 | def reboot(self, tag='autotest'): |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 280 | self.harness.run_reboot() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 281 | self.bootloader.boot_once(tag) |
| 282 | system("reboot") |
apw | 0778a2f | 2006-10-06 03:11:40 +0000 | [diff] [blame] | 283 | self.quit() |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 284 | |
| 285 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 286 | def noop(self, text): |
| 287 | print "job: noop: " + text |
| 288 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 289 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 290 | # Job control primatives. |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 291 | |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 292 | def __parallel_execute(self, func, *args): |
| 293 | func(*args) |
| 294 | |
| 295 | |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 296 | def parallel(self, *tasklist): |
| 297 | """Run tasks in parallel""" |
apw | 8fef4ac | 2006-10-10 22:53:37 +0000 | [diff] [blame] | 298 | |
| 299 | pids = [] |
| 300 | for task in tasklist: |
| 301 | pids.append(fork_start(self.resultdir, |
| 302 | lambda: self.__parallel_execute(*task))) |
| 303 | for pid in pids: |
| 304 | fork_waitfor(self.resultdir, pid) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 305 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 306 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 307 | def quit(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 308 | # XXX: should have a better name. |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 309 | self.harness.run_pause() |
apw | f2c6660 | 2006-04-27 14:11:25 +0000 | [diff] [blame] | 310 | raise JobContinue("more to come") |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 311 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 312 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 313 | def complete(self, status): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 314 | """Clean up and exit""" |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 315 | # We are about to exit 'complete' so clean up the control file. |
| 316 | try: |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 317 | os.unlink(self.control + '.state') |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 318 | except: |
| 319 | pass |
mbligh | 61a6c1a | 2006-12-25 01:26:38 +0000 | [diff] [blame] | 320 | self.harness.run_complete() |
apw | 1b02190 | 2006-04-03 17:02:56 +0000 | [diff] [blame] | 321 | sys.exit(status) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 322 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 323 | |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 324 | steps = [] |
| 325 | def next_step(self, step): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 326 | """Define the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 327 | step[0] = step[0].__name__ |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 328 | self.steps.append(step) |
apw | ecf41b7 | 2006-03-31 14:00:55 +0000 | [diff] [blame] | 329 | pickle.dump(self.steps, open(self.control + '.state', 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 330 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 331 | |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 332 | def step_engine(self): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 333 | """the stepping engine -- if the control file defines |
| 334 | step_init we will be using this engine to drive multiple runs. |
| 335 | """ |
| 336 | """Do the next step""" |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 337 | lcl = dict({'job': self}) |
| 338 | |
| 339 | str = """ |
| 340 | from error import * |
| 341 | from autotest_utils import * |
| 342 | """ |
| 343 | exec(str, lcl, lcl) |
| 344 | execfile(self.control, lcl, lcl) |
| 345 | |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 346 | state = self.control + '.state' |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 347 | # If there is a mid-job state file load that in and continue |
| 348 | # where it indicates. Otherwise start stepping at the passed |
| 349 | # entry. |
| 350 | try: |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 351 | self.steps = pickle.load(open(state, 'r')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 352 | except: |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 353 | if lcl.has_key('step_init'): |
| 354 | self.next_step([lcl['step_init']]) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 355 | |
| 356 | # Run the step list. |
| 357 | while len(self.steps) > 0: |
apw | fd922bb | 2006-04-04 07:47:00 +0000 | [diff] [blame] | 358 | step = self.steps.pop(0) |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 359 | pickle.dump(self.steps, open(state, 'w')) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 360 | |
| 361 | cmd = step.pop(0) |
apw | 83f8d77 | 2006-04-27 14:12:56 +0000 | [diff] [blame] | 362 | cmd = lcl[cmd] |
| 363 | lcl['__cmd'] = cmd |
| 364 | lcl['__args'] = step |
| 365 | exec("__cmd(*__args)", lcl, lcl) |
apw | 0865f48 | 2006-03-30 18:50:19 +0000 | [diff] [blame] | 366 | |
mbligh | caa605c | 2006-10-02 00:37:35 +0000 | [diff] [blame] | 367 | |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 368 | def record(self, msg): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 369 | """Record job-level status""" |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 370 | |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 371 | msg = msg.rstrip() |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 372 | # Ensure any continuation lines are marked so we can |
| 373 | # detect them in the status file to ensure it is parsable. |
mbligh | 88ab90f | 2007-08-29 15:52:49 +0000 | [diff] [blame] | 374 | msg = re.sub(r"\n", "\n" + self.record_prefix + " ", msg) |
| 375 | msg = self.record_prefix + msg |
apw | 7db8d0b | 2006-10-09 08:10:25 +0000 | [diff] [blame] | 376 | |
apw | de1503a | 2006-10-10 08:34:21 +0000 | [diff] [blame] | 377 | self.harness.test_status(msg) |
apw | f1a8116 | 2006-04-25 10:10:29 +0000 | [diff] [blame] | 378 | print msg |
| 379 | status = self.resultdir + "/status" |
mbligh | d9223fc | 2006-11-26 17:19:54 +0000 | [diff] [blame] | 380 | file(status, "a").write(msg + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 381 | |
| 382 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 383 | def runjob(control, cont = False, tag = "default", harness_type = ''): |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 384 | """The main interface to this module |
| 385 | |
mbligh | 72b88fc | 2006-12-16 18:41:35 +0000 | [diff] [blame] | 386 | control |
mbligh | c86b0b4 | 2006-07-28 17:35:28 +0000 | [diff] [blame] | 387 | The control file to use for this job. |
| 388 | cont |
| 389 | Whether this is the continuation of a previously started job |
| 390 | """ |
mbligh | b4eef24 | 2007-07-23 18:22:49 +0000 | [diff] [blame] | 391 | control = os.path.abspath(control) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 392 | state = control + '.state' |
| 393 | |
| 394 | # instantiate the job object ready for the control file. |
| 395 | myjob = None |
| 396 | try: |
| 397 | # Check that the control file is valid |
| 398 | if not os.path.exists(control): |
| 399 | raise JobError(control + ": control file not found") |
| 400 | |
| 401 | # When continuing, the job is complete when there is no |
| 402 | # state file, ensure we don't try and continue. |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 403 | if cont and not os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 404 | sys.exit(1) |
mbligh | f3fef46 | 2006-09-13 16:05:05 +0000 | [diff] [blame] | 405 | if cont == False and os.path.exists(state): |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 406 | os.unlink(state) |
| 407 | |
mbligh | 570e93e | 2006-11-26 05:15:56 +0000 | [diff] [blame] | 408 | myjob = job(control, tag, cont, harness_type) |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 409 | |
| 410 | # Load in the users control file, may do any one of: |
| 411 | # 1) execute in toto |
| 412 | # 2) define steps, and select the first via next_step() |
| 413 | myjob.step_engine() |
| 414 | |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 415 | except JobContinue: |
| 416 | sys.exit(5) |
| 417 | |
| 418 | except JobError, instance: |
| 419 | print "JOB ERROR: " + instance.args[0] |
| 420 | if myjob != None: |
apw | 510398f | 2007-03-06 19:19:05 +0000 | [diff] [blame] | 421 | myjob.record("ABORT " + instance.args[0] + "\n") |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 422 | myjob.complete(1) |
| 423 | |
| 424 | except: |
mbligh | fbfb77d | 2007-02-15 18:54:03 +0000 | [diff] [blame] | 425 | if myjob: |
| 426 | myjob.harness.run_abort() |
apw | ce9abe9 | 2006-04-27 14:14:04 +0000 | [diff] [blame] | 427 | # Ensure we cannot continue this job, it is in rictus. |
| 428 | if os.path.exists(state): |
| 429 | os.unlink(state) |
| 430 | raise |
mbligh | 892d37f | 2007-03-01 17:03:25 +0000 | [diff] [blame] | 431 | |
| 432 | # If we get here, then we assume the job is complete and good. |
| 433 | myjob.complete(0) |
| 434 | |