13 def __buildtempcnf__(variable_count, files, strlines):
14 """ Builds temporally file for cnf formulas
15 variable_count - number of variables in formulas
16 files - list of files with formulas
17 strlines - list of string lines with formulas"""
24 with open(file, 'r') as f:
26 lines.add(ln.rstrip())
28 first_line = "p cnf " + str(variable_count) + " " + str(len(lines))
30 wfile = tempfile.NamedTemporaryFile(delete=False)
31 wfile.write(bytes(first_line + '\n', 'UTF-8'))
33 wfile.write(bytes(ln + ' 0\n', 'UTF-8'))
37 def __exec_sat__(file, args):
38 """Executes SAT solver and returns configuration."""
39 picosat_cmd = [sf(conf.picosat), file]
40 picosat_cmd += conf.picosat_args
41 stdout = utils.callsubprocess('picosat', conf.picosat_cmd, conf.picosat_output, ".")
53 if not line.rstrip() == 's SATISFIABLE':
54 raise exceptions.NoSolution()
56 for sl in line[2:].split():
60 def __write_temp_config_file__(conf):
61 # Ensure smap existence
62 utils.build_symbol_map()
64 with open(sf(conf.variable_count_file)) as f:
66 var_num = int(f,readline())
67 # Write temporally file
68 wfile = tempfile.NamedTemporaryFile(delete=False)
77 if 'NONAMEGEN' in utils.smap[s]: # ignore generated names
79 wfile.write('CONFIG_' + utils.smap[s] + '=')
88 def __load_config_file__(file):
90 with open(file, 'r') as f:
92 if ln[0] == '#' or not '=' in ln:
95 if (ln[indx + 1] == 'y'):
96 rtn[line[7:indx]] = True
98 rtn[line[7:indx]] = True
101 def __calchash__(file):
102 """Calculates hash from configuration file"""
103 # Build hashconfigsort
106 with open(conf.hashconfigsort, 'r') as f:
108 csort.append(ln.rstrip())
109 except FileNotFoundError:
112 conf = __load_config_file__(file)
124 csortfile = open(conf.hashconfigsort, 'a');
125 for key, val in conf:
131 csortfile.write(key + '\n')
138 hsh = hashlib.md5(bytes(cstr, 'UTF-8'))
139 return hsh.hexdigest()
141 def __register_conf__(conf):
142 with open(sf(conf.variable_count_file)) as f:
143 var_num = int(f.readline())
145 dtb = database.database()
146 # Solution to configuration
147 wfile = __write_temp_config_file__(conf)
148 hsh = __calchash__(wfile.name)
149 filen = os.path.join(sf(conf.configuration_folder, hsh))
151 if os.path.isfile(filen):
152 if compare(filen, wfile):
153 print("I: Generated existing configuration.")
156 print("W: Generated configuration with collision hash.")
157 # TODO this might have to be tweaked
159 os.rename(wfile.name, filen)
160 dtb.add_configuration(hsh, hshf)
164 """Collect boolean equations from files rules and required
165 And get solution with picosat
167 # Check if rules_file exist. If it was generated.
168 if not os.path.isfile(sf(conf.rules_file)):
169 raise exceptions.MissingFile(conf.rules_file,"Run parse_kconfig.")
170 if not os.path.isfile(sf(conf.required_file)):
171 raise exceptions.MissingFile(conf.required_file,"Run allconfig.")
173 tfile = __buildtempcnf__(var_num, (conf.rules_file, conf.required_file), ())
175 confs = __exec_sat__(tfile)
178 __register_conf__(conf)
179 except exceptions.NoSolution:
181 raise exceptions.NoSolution()
183 def compare(file1, file2):
184 """Compared two configuration"""
185 conf1 = __load_config_file__(file1)
186 conf2 = __load_config_file__(file2)
188 # This is not exactly best comparison method
189 for key, val in conf1:
191 if conf2[key] != val:
195 for key, val in conf2:
197 if conf1[key] != val: