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):
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():
68 def __txt_config__(con, conf_num):
69 # Ensure smap existence
70 utils.build_symbol_map()
71 # Write temporally file
81 if 'NONAMEGEN' in utils.smap[s]: # ignore generated names
83 txt += 'CONFIG_' + utils.smap[s] + '='
91 def __write_temp_config_file__(con, conf_num):
92 wfile = tempfile.NamedTemporaryFile(delete=False)
93 txt = __txt_config__(con, conf_num)
94 wfile.write(bytes(txt, sys.getdefaultencoding()))
98 def __load_config_text__(txt):
101 if ln[0] == '#' or not '=' in ln:
104 if (ln[indx + 1] == 'y'):
105 rtn[ln[7:indx]] = True
107 rtn[ln[7:indx]] = False
111 def __load_config_file__(file):
113 rtn = __load_config_text__(f)
117 def __calchash__(con):
118 dt = database.database()
119 csort = dt.get_configsort()
130 for key, val in con.items():
136 dt.add_configsort(key)
140 hsh = hashlib.md5(bytes(cstr, 'UTF-8'))
141 return hsh.hexdigest()
144 def __calchash_file__(file):
145 """Calculates hash from configuration file"""
146 con = __load_config_file__(file)
147 return __calchash__(con)
149 def __register_conf__(con, conf_num, generator):
150 dtb = database.database()
151 # Solution to configuration
152 txtconfig = __txt_config__(con, conf_num)
153 hsh = __calchash__(con)
154 cconf = dtb.get_configration(hsh)
156 print('hash: ' + hsh)
157 if compare_text(cc, txtconfig):
158 print("I: Generated existing configuration.")
161 print("W: Generated configuration with collision hash.")
162 # TODO this might have to be tweaked
164 dtb.add_configuration(hsh, txtconfig, generator)
167 def __generate_single__(var_num, conf_num):
169 if not os.path.isfile(sf(conf.single_generated_file)):
170 with open(sf(conf.measure_file), 'r') as fi:
172 measure_list.add(int(ln))
174 with open(sf(conf.single_generated_file), 'r') as f:
176 measure_list.add(int(ln))
179 tfile = __buildtempcnf__(var_num, (sf(conf.rules_file),
180 sf(conf.fixed_file)), [str(measure_list.pop())])
181 with open(sf(conf.single_generated_file), 'w') as fo:
182 for ln in measure_list:
183 fo.write(str(ln) + '\n')
185 confs = __exec_sat__(tfile, ['-i', '0'])
187 __register_conf__(con, conf_num, 'single-sat')
188 except exceptions.NoSolution:
189 return __generate_single__(var_num, conf_num)
194 def __generate_random__(var_num, conf_num):
195 tfile = __buildtempcnf__(var_num, (sf(conf.rules_file), sf(conf.fixed_file)), set())
197 confs = __exec_sat__(tfile, ['-i', '3'])
199 if not __register_conf__(con, conf_num, 'random-sat'):
200 __generate_random__(var_num, conf_num)
206 """Collect boolean equations from files rules and required
207 And get solution with picosat
209 # Check if rules_file exist. If it was generated.
210 if not os.path.isfile(sf(conf.rules_file)):
211 raise exceptions.MissingFile(conf.rules_file,"Run parse_kconfig.")
212 if not os.path.isfile(sf(conf.fixed_file)):
213 raise exceptions.MissingFile(conf.required_file,"Run allconfig and initialization process.")
215 # Load variable count
216 with open(sf(conf.variable_count_file)) as f:
217 var_num = f.readline().rstrip()
218 conf_num = f.readline().rstrip()
220 if __generate_single__(var_num, conf_num):
222 elif __generate_random__(var_num, conf_num):
225 raise exceptions.NoNewConfiguration()
227 def compare(conf1, conf2):
228 # This is not exactly best comparison method
229 for key, val in conf1.items():
231 if conf2[key] != val:
235 for key, val in conf2.items():
237 if conf1[key] != val:
243 def compare_text(text1, text2):
244 conf1 = __load_config_text__(text1)
245 conf2 = __load_config_text__(text2)
246 return compare_file(conf1, conf2)
248 def compare_file(file1, file2):
249 """Compared two configuration"""
250 conf1 = __load_config_file__(file1)
251 conf2 = __load_config_file__(file2)
252 return compare_file(conf1, conf2)