]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Fix wrong input type to compare_text in register_conf
[linux-conf-perf.git] / scripts / utils.py
1 import os
2 import sys
3 import subprocess
4 import time
5 import hashlib
6 import re
7 from conf import conf
8 from conf import sf
9 import exceptions
10
11 def build_symbol_map():
12         """Generates global variable smap from symbol_map_file.
13         When file not exists, MissingFile exception is raised.
14         """
15         global smap
16         try:
17                 smap
18         except NameError:
19                 # Check if symbol_map_file exist
20                 if not os.path.isfile(sf(conf.symbol_map_file)):
21                         raise exceptions.MissingFile(sf(conf.symbol_map_file),
22                                         "Run parse_kconfig to generate it.")
23
24                 smap = dict()
25                 with open(sf(conf.symbol_map_file)) as f:
26                         for lnn in f:
27                                 w = lnn.rstrip().split(sep=':')
28                                 smap[int(w[0])] = w[1]
29
30
31 def callsubprocess(process_name, process, show_output = True,
32                 return_output = False, env=os.environ, allowed_exit_codes = [0],
33                 allow_all_exit_codes = False, stdin = None):
34         sprc = subprocess.Popen(process, stdout = subprocess.PIPE,
35                         stderr = subprocess.STDOUT, stdin = subprocess.PIPE, env = env)
36
37         try:
38                 os.mkdir(os.path.join(sf(conf.log_folder), process_name))
39         except OSError:
40                 pass
41
42         if stdin != None:
43                 for ln in stdin:
44                         sprc.stdin.write(bytes(ln + '\n', sys.getdefaultencoding()))
45                         sprc.stdin.flush()
46                 sprc.stdin.close()
47
48         rtn = []
49         with open(os.path.join(sf(conf.log_folder),
50                         process_name, time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
51                         "a") as f:
52                 f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::\n')
53                 for linen in sprc.stdout:
54                         line = linen.decode(sys.getdefaultencoding())
55                         f.write(line)
56                         if show_output:
57                                 print(line, end="")
58                         if return_output:
59                                 rtn.append(line.rstrip())
60
61         rtncode = sprc.wait()
62         if rtncode not in allowed_exit_codes and not allow_all_exit_codes:
63                 raise exceptions.ProcessFailed(process, rtncode)
64         return rtn
65
66 def get_kernel_env():
67         env = dict(os.environ)
68         env.update(conf.kernel_env)
69         return env
70
71 def __dirty_repo__(path):
72         cwd = os.getcwd()
73         os.chdir(conf.absroot)
74         out = subprocess.check_output(conf.git_describe_cmd)
75         if re.search('dirty', out.decode(sys.getdefaultencoding())):
76                 raise exceptions.DirtyRepository(path)
77
78 def dirtycheck():
79         __dirty_repo__(conf.absroot)
80         __dirty_repo__(conf.linux_sources)