]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Add boot timeout
[linux-conf-perf.git] / scripts / utils.py
1 import os
2 import sys
3 import subprocess
4 import time
5 import hashlib
6 import signal
7 import re
8 from threading import Thread
9 from conf import conf
10 from conf import sf
11 import exceptions
12
13 def build_symbol_map():
14         """Generates global variable smap from symbol_map_file.
15         When file not exists, MissingFile exception is raised.
16         """
17         global smap
18         try:
19                 smap
20         except NameError:
21                 # Check if symbol_map_file exist
22                 if not os.path.isfile(sf(conf.symbol_map_file)):
23                         raise exceptions.MissingFile(sf(conf.symbol_map_file),
24                                         "Run parse_kconfig to generate it.")
25
26                 smap = dict()
27                 with open(sf(conf.symbol_map_file)) as f:
28                         for lnn in f:
29                                 w = lnn.rstrip().split(sep=':')
30                                 smap[int(w[0])] = w[1]
31
32 class __subprocess_timer__(Thread):
33         def __init__(self, sprc, timeout):
34                 Thread.__init__(self, name='subprocess_timer')
35                 self.sprc = sprc
36                 self.last = time.time()
37                 self.exitit = False
38                 self.timeout = timeout
39                 self.timeouted = False
40                 if timeout > 0:
41                         self.start()
42         def output(self):
43                 self.last = time.time()
44         def exit(self):
45                 self.exitit = True
46                 return self.timeouted
47         def run(self):
48                 while not self.exitit:
49                         now = time.time()
50                         if (now - self.last) >= self.timeout:
51                                 self.timeouted = True
52                                 os.kill(self.sprc.pid, signal.SIGTERM)
53                                 return
54                         time.sleep(1)
55
56 def callsubprocess(process_name, process, show_output = True,
57                 return_output = False, env=os.environ, allowed_exit_codes = [0],
58                 allow_all_exit_codes = False, stdin = None, timeout = -1):
59         sprc = subprocess.Popen(process, stdout = subprocess.PIPE,
60                         stderr = subprocess.STDOUT, stdin = subprocess.PIPE, env = env)
61
62
63         try:
64                 os.mkdir(os.path.join(sf(conf.log_folder), process_name))
65         except OSError:
66                 pass
67
68         if stdin != None:
69                 for ln in stdin:
70                         sprc.stdin.write(bytes(ln + '\n', sys.getdefaultencoding()))
71                         sprc.stdin.flush()
72                 sprc.stdin.close()
73
74         rtn = []
75         timerout = __subprocess_timer__(sprc, timeout)
76         with open(os.path.join(sf(conf.log_folder),
77                         process_name, time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
78                         "a") as f:
79                 f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::\n')
80                 for linen in sprc.stdout:
81                         timerout.output()
82                         line = linen.decode(sys.getdefaultencoding())
83                         f.write(line)
84                         if show_output:
85                                 print(line, end="")
86                         if return_output:
87                                 rtn.append(line.rstrip())
88         if timerout.exit():
89                 raise exceptions.ProcessTimeout(process_name, rtn)
90         rtncode = sprc.wait()
91         if rtncode not in allowed_exit_codes and not allow_all_exit_codes:
92                 raise exceptions.ProcessFailed(process, rtncode, rtn)
93         return rtn
94
95 def get_kernel_env():
96         env = dict(os.environ)
97         env.update(conf.kernel_env)
98         return env
99
100 def __dirty_repo__(path):
101         cwd = os.getcwd()
102         os.chdir(conf.absroot)
103         out = subprocess.check_output(conf.git_describe_cmd)
104         if re.search('dirty', out.decode(sys.getdefaultencoding())):
105                 raise exceptions.DirtyRepository(path)
106
107 def dirtycheck():
108         __dirty_repo__(conf.absroot)
109         __dirty_repo__(conf.linux_sources)