]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/utils.py
Add implementation of hash indexing of configurations
[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         try:
61                 os.mkdir(sf(conf.log_folder))
62         except OSError:
63                 pass
64
65         sprc = subprocess.Popen(process, stdout = subprocess.PIPE, env = env)
66
67         rtn = ""
68         with open(os.path.join(sf(conf.log_folder), process_name + ".log"), "a") as f:
69                 f.write("::" + time.strftime("%y-%m-%d-%H-%M-%S") + "::\n")
70                 for linen in sprc.stdout:
71                         line = linen.decode(sys.getdefaultencoding())
72                         f.write(line)
73                         if show_output:
74                                 print(line, end="")
75                         if re.search(regular, line):
76                                 rtn += line
77
78         rtncode = sprc.wait()
79         if rtncode != 0:
80                 raise exceptions.ProcessFailed(process, rtncode)
81
82         return rtn
83
84 def get_kernel_env():
85         env = dict(os.environ)
86         env['SRCARCH'] = conf.SRCARCH
87         env['ARCH'] = conf.ARCH
88         env['KERNELVERSION'] = 'KERNELVERSION' # hides error
89         return env
90
91
92 def hash_config(cf):
93         """Hashes configuration using MD5 hash.
94         """
95         try:
96                 cf.remove(0)
97         except ValueError:
98                 pass
99         str = ""
100         for c in cf:
101                 if c < 0:
102                         str += '-'
103                 else:
104                         str += '+'
105         hsh = hashlib.md5(bytes(str, sys.getdefaultencoding()))
106         return hsh.hexdigest()
107
108 def config_strtoint(str):
109         """Reads list of configured symbols from string
110         """
111         rtn = []
112         for s in str.split(sep=' '):
113                 rtn.append(int(s))
114         try:
115                 rtn.remove(0)
116         except ValueError:
117                 pass
118         return rtn