15 def __buildtempcnf__(variable_count, files, strlines):
16 """ Builds temporally file for cnf formulas
17 variable_count - number of variables in formulas
18 files - list of files with formulas
19 strlines - list of string lines with formulas"""
26 with open(file, 'r') as f:
28 lines.add(ln.rstrip())
30 first_line = "p cnf " + str(variable_count) + " " + str(len(lines))
32 wfile = tempfile.NamedTemporaryFile(delete=False)
33 wfile.write(bytes(first_line + '\n', 'UTF-8'))
35 wfile.write(bytes(ln + ' 0\n', 'UTF-8'))
39 def __exec_sat__(file, args, conf_num):
40 """Executes SAT solver and returns configuration."""
41 picosat_cmd = [sf(conf.picosat), file]
42 picosat_cmd += conf.picosat_args
43 stdout = utils.callsubprocess('picosat', picosat_cmd, conf.picosat_output,
44 True, allow_all_exit_codes = True)
56 if not line.rstrip() == 's SATISFIABLE':
57 raise exceptions.NoSolution()
59 for sl in line[2:].split():
66 # Ensure smap existence
67 utils.build_symbol_map()
80 if 'NONAMEGEN' in utils.smap[r]: # ignore generated names
82 cond[utils.smap[r]] = val
86 def __txt_config__(con):
88 for key, val in con.items():
89 txt += 'CONFIG_' + key + '='
97 def __write_temp_config_file__(con):
98 wfile = tempfile.NamedTemporaryFile(delete=False)
99 txt = __txt_config__(con)
100 wfile.write(bytes(txt, sys.getdefaultencoding()))
104 def __load_config_text__(txt):
107 if ln[0] == '#' or not '=' in ln:
110 if (ln[indx + 1] == 'y'):
111 rtn[ln[7:indx]] = True
113 rtn[ln[7:indx]] = False
117 def __load_config_file__(file):
119 rtn = __load_config_text__(f)
123 def __calchash__(con):
124 dt = database.database()
125 csort = dt.get_configsort()
136 for key, val in con.items():
142 dt.add_configsort(key)
146 hsh = hashlib.md5(bytes(cstr, 'UTF-8'))
147 return hsh.hexdigest()
150 def __calchash_file__(file):
151 """Calculates hash from configuration file"""
152 con = __load_config_file__(file)
153 return __calchash__(con)
155 def __register_conf__(con, conf_num, generator):
156 dtb = database.database()
157 # Solution to configuration
158 txtconfig = __txt_config__(con)
159 hsh = __calchash__(con)
160 cconf = dtb.get_configration(hsh)
162 print('hash: ' + hsh)
163 if compare_text(cc, txtconfig):
164 print("I: Generated existing configuration.")
167 print("W: Generated configuration with collision hash.")
168 # TODO this might have to be tweaked
170 dtb.add_configuration(hsh, txtconfig, generator)
173 def __generate_single__(var_num, conf_num):
175 if not os.path.isfile(sf(conf.single_generated_file)):
176 with open(sf(conf.measure_file), 'r') as fi:
178 measure_list.add(int(ln))
180 with open(sf(conf.single_generated_file), 'r') as f:
182 measure_list.add(int(ln))
185 tfile = __buildtempcnf__(var_num, (sf(conf.rules_file),
186 sf(conf.fixed_file)), [str(measure_list.pop())])
187 with open(sf(conf.single_generated_file), 'w') as fo:
188 for ln in measure_list:
189 fo.write(str(ln) + '\n')
191 confs = __exec_sat__(tfile, ['-i', '0'], conf_num)
193 __register_conf__(con, conf_num, 'single-sat')
194 except exceptions.NoSolution:
195 return __generate_single__(var_num, conf_num)
200 def __generate_random__(var_num, conf_num):
201 tfile = __buildtempcnf__(var_num, (sf(conf.rules_file), sf(conf.fixed_file)), set())
203 confs = __exec_sat__(tfile, ['-i', '3'], conf_num)
205 if not __register_conf__(con, conf_num, 'random-sat'):
206 __generate_random__(var_num, conf_num)
212 """Collect boolean equations from files rules and required
213 And get solution with picosat
215 # Check if rules_file exist. If it was generated.
216 if not os.path.isfile(sf(conf.rules_file)):
217 raise exceptions.MissingFile(conf.rules_file,"Run parse_kconfig.")
218 if not os.path.isfile(sf(conf.fixed_file)):
219 raise exceptions.MissingFile(conf.required_file,"Run allconfig and initialization process.")
221 # Load variable count
222 with open(sf(conf.variable_count_file)) as f:
223 var_num = f.readline().rstrip()
224 conf_num = f.readline().rstrip()
226 if __generate_single__(var_num, conf_num):
228 elif __generate_random__(var_num, conf_num):
231 raise exceptions.NoNewConfiguration()
233 def compare(conf1, conf2):
234 # This is not exactly best comparison method
235 for key, val in conf1.items():
237 if conf2[key] != val:
241 for key, val in conf2.items():
243 if conf1[key] != val:
249 def compare_text(text1, text2):
250 conf1 = __load_config_text__(text1)
251 conf2 = __load_config_text__(text2)
252 return compare_file(conf1, conf2)
254 def compare_file(file1, file2):
255 """Compared two configuration"""
256 conf1 = __load_config_file__(file1)
257 conf2 = __load_config_file__(file2)
258 return compare_file(conf1, conf2)