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 __write_temp_config_file__(con, conf_num):
69 # Ensure smap existence
70 utils.build_symbol_map()
71 # Write temporally file
72 wfile = tempfile.NamedTemporaryFile(delete=False)
81 if 'NONAMEGEN' in utils.smap[s]: # ignore generated names
83 wfile.write(bytes('CONFIG_' + utils.smap[s] + '=',
84 sys.getdefaultencoding()))
86 wfile.write(bytes('y', sys.getdefaultencoding()))
88 wfile.write(bytes('n', sys.getdefaultencoding()))
89 wfile.write(bytes('\n', sys.getdefaultencoding()))
93 def __load_config_file__(file):
95 with open(file, 'r') as f:
97 if ln[0] == '#' or not '=' in ln:
100 if (ln[indx + 1] == 'y'):
101 rtn[ln[7:indx]] = True
103 rtn[ln[7:indx]] = True
106 def __calchash__(file):
107 """Calculates hash from configuration file"""
108 # Build hashconfigsort
111 with open(conf.hashconfigsort, 'r') as f:
113 csort.append(ln.rstrip())
114 except FileNotFoundError:
117 con = __load_config_file__(file)
129 csortfile = open(sf(conf.hashconfigsort), 'a');
130 for key, val in con.items():
136 csortfile.write(key + '\n')
143 hsh = hashlib.md5(bytes(cstr, 'UTF-8'))
144 return hsh.hexdigest()
146 def __register_conf__(con, conf_num):
147 dtb = database.database()
148 # Solution to configuration
149 wfile = __write_temp_config_file__(con, conf_num)
150 hsh = __calchash__(wfile)
151 filen = os.path.join(sf(conf.configurations_folder), hsh)
153 if os.path.isfile(filen):
154 if compare(filen, wfile):
155 print("I: Generated existing configuration.")
157 print("W: Generated configuration with collision hash.")
158 # TODO this might have to be tweaked
160 shutil.move(wfile, filen)
161 dtb.add_configuration(hsh, hshf)
163 def __generate_single__(var_num, conf_num):
164 if os.path.isfile(sf(conf.single_generated_file)):
167 with open(sf(conf.measure_file), 'r') as f:
169 measure_list.append(int(ln))
170 for measure in measure_list:
171 tfile = __buildtempcnf__(var_num, (sf(conf.rules_file),
172 sf(conf.fixed_file)), (str(measure)))
174 confs = __exec_sat__(tfile, ['-i', '0'])
176 __register_conf__(con, conf_num)
177 except exceptions.NoSolution:
181 with open(sf(conf.single_generated_file), 'w') as f:
182 f.write("This file informs scripts, that all single selected configurations are already generated.\n")
183 f.write("Remove this file if you want run generating process again.")
186 def __generate_random__(var_num, conf_num):
191 """Collect boolean equations from files rules and required
192 And get solution with picosat
194 # Check if rules_file exist. If it was generated.
195 if not os.path.isfile(sf(conf.rules_file)):
196 raise exceptions.MissingFile(conf.rules_file,"Run parse_kconfig.")
197 if not os.path.isfile(sf(conf.fixed_file)):
198 raise exceptions.MissingFile(conf.required_file,"Run allconfig and initialization process.")
200 # Load variable count
201 with open(sf(conf.variable_count_file)) as f:
202 var_num = f.readline()
203 conf_num = f.readline()
205 if __generate_single__(var_num, conf_num):
208 #tfile = __buildtempcnf__(var_num, (sf(conf.rules_file), sf(conf.fixed_file)), ())
210 #confs = __exec_sat__(tfile, [])
212 #__register_conf__(con, conf_num)
216 def compare(file1, file2):
217 """Compared two configuration"""
218 conf1 = __load_config_file__(file1)
219 conf2 = __load_config_file__(file2)
221 # This is not exactly best comparison method
222 for key, val in conf1.items():
224 if conf2[key] != val:
228 for key, val in conf2.items():
230 if conf1[key] != val: