def exit_status(self): """Returns the string exit status of this job.""" # Find the .autoserv_execute path. top_dir = tko_utils.find_toplevel_job_dir(self.dir) if not top_dir: return 'ABORT' execute_path = os.path.join(top_dir, '.autoserv_execute') # If for some reason we can't read the status code, assume disaster. if not os.path.exists(execute_path): return 'ABORT' lines = open(execute_path).readlines() if len(lines) < 2: return 'ABORT' try: status_code = int(lines[1]) except ValueError: return 'ABORT' if not os.WIFEXITED(status_code): # Looks like a signal - an ABORT. return 'ABORT' elif os.WEXITSTATUS(status_code) != 0: # Looks like a non-zero exit - a failure. return 'FAIL' else: # Looks like exit code == 0. return 'GOOD'
def test_parent_is_toplevel(self): jobdir = "/results/job2" os.path.exists.expect_call( jobdir + "/sub/.autoserv_execute").and_return(False) os.path.exists.expect_call( jobdir + "/.autoserv_execute").and_return(True) self.assertEqual(utils.find_toplevel_job_dir(jobdir + "/sub"), jobdir)
def read_keyval(dir): """ Read job keyval files. @param dir: String name of directory containing job keyval files. @return A dictionary containing job keyvals. """ dir = os.path.normpath(dir) top_dir = tko_utils.find_toplevel_job_dir(dir) if not top_dir: top_dir = dir assert (dir.startswith(top_dir)) # Pull in and merge all the keyval files, with higher-level # overriding values in the lower-level ones. keyval = {} while True: try: upper_keyval = utils.read_keyval(dir) # HACK: exclude hostname from the override - this is a special # case where we want lower to override higher. if 'hostname' in upper_keyval and 'hostname' in keyval: del upper_keyval['hostname'] keyval.update(upper_keyval) except IOError: pass # If the keyval can't be read just move on to the next. if dir == top_dir: break else: assert (dir != '/') dir = os.path.dirname(dir) return keyval
def read_keyval(dir): dir = os.path.normpath(dir) top_dir = tko_utils.find_toplevel_job_dir(dir) if not top_dir: top_dir = dir assert(dir.startswith(top_dir)) # pull in and merge all the keyval files, with higher-level # overriding values in the lower-level ones keyval = {} while True: try: upper_keyval = utils.read_keyval(dir) # HACK: exclude hostname from the override - this is a special # case where we want lower to override higher if "hostname" in upper_keyval and "hostname" in keyval: del upper_keyval["hostname"] keyval.update(upper_keyval) except IOError: pass # if the keyval can't be read just move on to the next if dir == top_dir: break else: assert(dir != "/") dir = os.path.dirname(dir) return keyval
def exit_status(self): # find the .autoserv_execute path top_dir = tko_utils.find_toplevel_job_dir(self.dir) if not top_dir: return "ABORT" execute_path = os.path.join(top_dir, ".autoserv_execute") # if for some reason we can't read the status code, assume disaster if not os.path.exists(execute_path): return "ABORT" lines = open(execute_path).readlines() if len(lines) < 2: return "ABORT" try: status_code = int(lines[1]) except ValueError: return "ABORT" if not os.WIFEXITED(status_code): # looks like a signal - an ABORT return "ABORT" elif os.WEXITSTATUS(status_code) != 0: # looks like a non-zero exit - a failure return "FAIL" else: # looks like exit code == 0 return "GOOD"
def read_keyval(dir): dir = os.path.normpath(dir) top_dir = tko_utils.find_toplevel_job_dir(dir) if not top_dir: top_dir = dir assert (dir.startswith(top_dir)) # pull in and merge all the keyval files, with higher-level # overriding values in the lower-level ones keyval = {} while True: try: upper_keyval = utils.read_keyval(dir) # HACK: exclude hostname from the override - this is a special # case where we want lower to override higher if "hostname" in upper_keyval and "hostname" in keyval: del upper_keyval["hostname"] keyval.update(upper_keyval) except IOError: pass # if the keyval can't be read just move on to the next if dir == top_dir: break else: assert (dir != "/") dir = os.path.dirname(dir) return keyval
def test_no_toplevel(self): jobdir = "/results/job5" os.path.exists.expect_call( jobdir + "/.autoserv_execute").and_return(False) os.path.exists.expect_call( "/results/.autoserv_execute").and_return(False) os.path.exists.expect_call("/.autoserv_execute").and_return(False) self.assertEqual(utils.find_toplevel_job_dir(jobdir), None)
def parse_host_keyval(job_dir, hostname): # the "real" job dir may be higher up in the directory tree job_dir = tko_utils.find_toplevel_job_dir(job_dir) if not job_dir: return {} # we can't find a top-level job dir with host keyvals # the keyval is <job_dir>/host_keyvals/<hostname> if it exists keyval_path = os.path.join(job_dir, "host_keyvals", hostname) if os.path.isfile(keyval_path): return utils.read_keyval(keyval_path) else: return {}
def _parse_keyval(job_dir, sub_keyval_path): """ Parse a file of keyvals. @param job_dir: The string directory name of the associated job. @param sub_keyval_path: Path to a keyval file relative to job_dir. @return A dictionary representing the keyvals. """ # The "real" job dir may be higher up in the directory tree. job_dir = tko_utils.find_toplevel_job_dir(job_dir) if not job_dir: return {} # We can't find a top-level job dir with job keyvals. # The keyval is <job_dir>/`sub_keyval_path` if it exists. keyval_path = os.path.join(job_dir, sub_keyval_path) if os.path.isfile(keyval_path): return utils.read_keyval(keyval_path) else: return {}
def parse_host_keyval(job_dir, hostname): """ Parse host keyvals. @param job_dir: The string directory name of the associated job. @param hostname: The string hostname. @return A dictionary representing the host keyvals. """ # The "real" job dir may be higher up in the directory tree. job_dir = tko_utils.find_toplevel_job_dir(job_dir) if not job_dir: return {} # We can't find a top-level job dir with host keyvals. # The keyval is <job_dir>/host_keyvals/<hostname> if it exists. keyval_path = os.path.join(job_dir, 'host_keyvals', hostname) if os.path.isfile(keyval_path): return utils.read_keyval(keyval_path) else: return {}