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