]> rtime.felk.cvut.cz Git - linux-conf-perf.git/blob - scripts/initialize.py
1847a04174e136a66ef1e2c370dfc26ba2e23743
[linux-conf-perf.git] / scripts / initialize.py
1 #!/usr/bin/env python3
2 import os
3 import sys
4 import subprocess
5 import shutil
6
7 import utils
8 import database
9 from conf import conf
10 from conf import sf
11 import exceptions
12 import configurations
13
14 def all():
15         try:
16                 utils.dirtycheck()
17         except exceptions.DirtyRepository as e:
18                 print("Warning: " + str(e))
19         parse_kconfig()
20         gen_fixed()
21         checkmeasure()
22         database.database() # check if database is initialized
23
24 def parse_kconfig():
25         "Execute parse_kconfig in linux_sources directory."
26         if os.path.isfile(sf(conf.symbol_map_file)) and \
27                         os.path.isfile(sf(conf.rules_file)) and \
28                         os.path.isfile(sf(conf.variable_count_file)):
29                 print('Warning: parse_kconfig not executed. Files already exists.')
30                 return
31         print('Executing parse_kconfig...')
32         wd = os.getcwd()
33         os.chdir(sf(conf.linux_sources))
34         parse_kconfig_cmd = [sf(conf.parse_kconfig)]
35         parse_kconfig_cmd += ['Kconfig', sf(conf.build_folder)]
36         parse_kconfig_cmd += ['-v', '-v']
37         utils.callsubprocess("parse_kconfig", parse_kconfig_cmd,
38                         conf.parse_kconfig_output, env=utils.get_kernel_env())
39         os.chdir(wd)
40
41
42 def __gen_allconfig_fixed__():
43         wd = os.getcwd()
44         os.chdir(sf(conf.linux_sources))
45         allconfig_cmd = [sf(conf.allconfig)]
46         allconfig_cmd += ['Kconfig', sf(conf.dot_config), sf(conf.dot_measure_file)]
47         allconfig_cmd += ['--inv']
48         utils.callsubprocess("allconfig_fixed", allconfig_cmd, False,
49                         env = utils.get_kernel_env())
50         os.chdir(wd)
51
52 def gen_fixed():
53         "Generates fixed depenpency from dot_config file."
54         print('Generating required configuration...')
55
56         if not os.path.isfile(sf(conf.dot_config)):
57                 raise exceptions.MissingFile(sf(conf.dot_config),
58                                 'Generate fixed configuration. Use make dot_config.')
59
60         utils.build_symbol_map() # Ensure smap existence
61         srmap = {value:key for key, value in utils.smap.items()} # swap dictionary
62
63         shutil.copy(sf(conf.dot_config), sf(conf.dot_config_back_file))
64         __gen_allconfig_fixed__()
65
66         with open(sf(conf.dot_config), 'r') as f:
67                 with open(sf(conf.fixed_file), 'w') as ffix:
68                         for line in f:
69                                 if (line[0] == '#') or (not '=' in line):
70                                         continue
71                                 indx = line.index('=')
72                                 if (line[indx + 1] == 'y'):
73                                         if line[7:indx] == "MODULES": # exception if modules set
74                                                 raise exceptions.ConfigurationError("Fixed kernel configuration must have MODULES disabled.")
75                                         ffix.write(str(srmap[line[7:indx]]) + "\n")
76                                 elif (line[indx + 1] == 'n' or line[indx + 1] == 'm'):
77                                         ffix.write("-" + str(srmap[line[7:indx]]) + "\n")
78         with open(sf(conf.dot_measure_file), 'r') as f:
79                 with open(sf(conf.measure_file), 'w') as fmes:
80                         for line in f:
81                                 if (line[0] == '#') or (not '=' in line):
82                                         continue
83                                 indx = line.index('=')
84                                 if line[7:indx] == "MODULES":
85                                         raise exceptions.ConfigurationError("Can't measure configuraion option MODULES. Not supported.")
86                                 fmes.write(str(srmap[line[7:indx]]) + "\n")
87
88 def checkmeasure():
89         if os.path.isfile(sf(conf.measurechecked_file)):
90                 print("Checking if all configurations can be measured skiped.")
91                 print("  For new check remove file " + sf(conf.measurechecked_file))
92                 return
93         print("Checking if all configurations can be measured...")
94         utils.build_symbol_map()
95         measure_list = set()
96         with open(sf(conf.variable_count_file)) as f:
97                 var_num = f.readline().rstrip()
98                 conf_num = f.readline().rstrip()
99         with open(sf(conf.measure_file), 'r') as fi:
100                 for ln in fi:
101                         measure_list.add(int(ln))
102         for measure in measure_list:
103                 tfile1 = configurations.__buildtempcnf__(var_num, (sf(conf.rules_file),
104                         sf(conf.fixed_file)), [str(measure)])
105                 tfile2 = configurations.__buildtempcnf__(var_num, (sf(conf.rules_file),
106                         sf(conf.fixed_file)), [str(-1 * measure)])
107                 try:
108                         configurations.__exec_sat__(tfile1, [], conf_num)
109                 except exceptions.NoSolution:
110                         print("W: " + utils.smap[measure] + " won't be measured! Can't select.")
111                 try:
112                         configurations.__exec_sat__(tfile2, [], conf_num)
113                 except exceptions.NoSolution:
114                         print("W: " + utils.smap[measure] + " won't be measured! Can't unselect.")
115         with open(sf(conf.measurechecked_file), 'w') as f:
116                 f.write("Remove this file if you wanna execute check if all configurations can be measured once again.\n")
117
118 #################################################################################
119
120 if __name__ == '__main__':
121         all()