]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Implement database using Django framework
[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         print("Running: " + " ".join(process))
60         sprc = subprocess.Popen(process, stdout = subprocess.PIPE,
61                         stderr = subprocess.STDOUT, stdin = subprocess.PIPE, env = env)
62
63
64         try:
65                 os.mkdir(os.path.join(sf(conf.log_folder), process_name))
66         except OSError:
67                 pass
68
69         if stdin != None:
70                 for ln in stdin:
71                         sprc.stdin.write(bytes(ln + '\n', sys.getdefaultencoding()))
72                         sprc.stdin.flush()
73                 sprc.stdin.close()
74
75         rtn = []
76         timerout = __subprocess_timer__(sprc, timeout)
77         with open(os.path.join(sf(conf.log_folder),
78                         process_name, time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
79                         "a") as f:
80                 f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::\n')
81                 for linen in sprc.stdout:
82                         timerout.output()
83                         try:
84                                 line = linen.decode(sys.getdefaultencoding())
85                                 f.write(line)
86                                 if show_output:
87                                         print(line, end="")
88                                 if return_output:
89                                         rtn.append(line.rstrip())
90                         except UnicodeDecodeError:
91                                 if return_output:
92                                         rtn.append('DecodeError')
93         if timerout.exit():
94                 raise exceptions.ProcessTimeout(process_name, rtn)
95         rtncode = sprc.wait()
96         if rtncode not in allowed_exit_codes and not allow_all_exit_codes:
97                 raise exceptions.ProcessFailed(process, rtncode, rtn)
98         return rtn
99
100 def get_kernel_env():
101         env = dict(os.environ)
102         env.update(conf.kernel_env)
103         return env
104
105 def __dirty_repo__(path):
106         cwd = os.getcwd()
107         os.chdir(conf.absroot)
108         out = subprocess.check_output(conf.git_describe_cmd)
109         if re.search('dirty', out.decode(sys.getdefaultencoding())):
110                 raise exceptions.DirtyRepository(path)
111
112 def dirtycheck():
113         __dirty_repo__(conf.absroot)
114         __dirty_repo__(conf.linux_sources)