1import os 2import logging 3 4 5class PidFileManager(object): 6 def __init__(self, label, results_dir): 7 self.path = os.path.join(results_dir, ".%s_execute" % label) 8 self.pid_file = None 9 self.num_tests_failed = 0 10 11 12 def open_file(self): 13 self.pid_file = open(self.path, "w") 14 self.pid_file.write("%s\n" % os.getpid()) 15 self.pid_file.flush() 16 logging.info("Logged pid %s to %s", os.getpid(), self.path) 17 18 19 def close_file(self, exit_code, signal_code=0): 20 if not self.pid_file: 21 return 22 pid_file = self.pid_file 23 self.pid_file = None 24 encoded_exit_code = ((exit_code & 0xFF) << 8) | (signal_code & 0xFF) 25 pid_file.write("%s\n" % encoded_exit_code) 26 pid_file.write("%s\n" % self.num_tests_failed) 27 pid_file.close() 28