]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Return timestamp to subprocess execution log
[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 build_conf_map():
32         """Generates global variable cmap from config_map_file and config_solved_file.
33         cmap is dictionary containing list ([configuration], bool solved)
34         cmap is rebuild every time this function is called.
35         """
36         global cmap
37         cmap = dict()
38         if os.path.isfile(sf(conf.config_map_file)):
39                 with open(sf(conf.config_map_file)) as f:
40                         for ln in f:
41                                 w = ln.rstrip().split(sep=':')
42                                 cf = list()
43                                 for vr in w[1].split(sep=" "):
44                                         if vf[0] == '-':
45                                                 cf.append(-1 * int(vf[1:]))
46                                         cf.append(int(vf))
47                                 cmap[w[0]] = [w[1], False]
48
49                 if os.path.isfile(sf(conf.config_solved_file)):
50                         with open(sf(conf.config_solved_file)) as f:
51                                 for ln in f:
52                                         try:
53                                                 cmap[ln.rstrip()][1] = True
54                                         except KeyError:
55                                                 pass
56
57
58 def callsubprocess(process_name, process, show_output = True, regular = "",
59                 env=os.environ):
60         sprc = subprocess.Popen(process, stdout = subprocess.PIPE, env = env)
61
62         rtn = ""
63         with open(os.path.join(sf(conf.log_folder),
64                         process_name + '-' + time.strftime("%y-%m-%d-%H-%M-%S") + ".log"),
65                         "a") as f:
66                 f.write('::' + time.strftime("%y-%m-%d-%H-%M-%S-%f") + '::')
67                 for linen in sprc.stdout:
68                         line = linen.decode(sys.getdefaultencoding())
69                         f.write(line)
70                         if show_output:
71                                 print(line, end="")
72                         if re.search(regular, line):
73                                 rtn += line
74
75         rtncode = sprc.wait()
76         if rtncode != 0:
77                 raise exceptions.ProcessFailed(process, rtncode)
78
79         return rtn
80
81 def get_kernel_env():
82         env = dict(os.environ)
83         env.update(conf.kernel_env)
84         return env
85
86
87 def hash_config(cf):
88         """Hashes configuration using MD5 hash.
89         """
90         try:
91                 cf.remove(0)
92         except ValueError:
93                 pass
94         str = ""
95         for c in cf:
96                 if c < 0:
97                         str += '-'
98                 else:
99                         str += '+'
100         hsh = hashlib.md5(bytes(str, sys.getdefaultencoding()))
101         return hsh.hexdigest()
102
103 def config_strtoint(str, full):
104         """Reads list of configured symbols from string
105         """
106         rtn = []
107         if full:
108                 for s in str.rstrip().split(sep=' '):
109                         rtn.append(int(s))
110         else:
111                 count = 0
112                 with open(sf(conf.variable_count_file)) as f:
113                         f.readline()
114                         count = int(f.readline())
115                 for s in str.rstrip().split(sep=' '):
116                         val = int(s)
117                         if abs(val) <= count:
118                                 rtn.append(val)
119                         else:
120                                 break;
121         try:
122                 rtn.remove(0)
123         except ValueError:
124                 pass
125         return rtn
126
127 def get_config_from_hash(hash):
128         with open(sf(conf.config_map_file), "r") as f:
129                 for line in f:
130                         w = line.rstrip().split(sep=':')
131                         if w[0] == hash:
132                                 return config_strtoint(w[1], True)
133         return None
134
135 def get_last_configuration():
136         hsh = ""
137         try:
138                 with open(sf(conf.config_solved_file), "r") as f:
139                         for line in f:
140                                 sline = line.rstrip()
141                                 if sline != '':
142                                         hsh = sline
143         except FileNotFoundError:
144                 try:
145                         with open(sf(conf.config_map_file), "r") as f:
146                                 w = f.readline().split(sep=':')
147                                 hsh = w[0]
148                 except FileNotFoundError:
149                         pass
150
151         if hsh != '':
152                 return hsh
153         else:
154                 return 'NoConfig'