def update_mask(db_root, mask_db, src_dbs, offset=0): bits = set() mask_db_file = "%s/mask_%s.db" % (db_root, mask_db) if os.path.exists(mask_db_file): with util.OpenSafeFile(mask_db_file, "r") as f: for line in f: line = line.split() assert len(line) == 2 assert line[0] == "bit" bits.add(line[1]) for src_db in src_dbs: seg_db_file = "%s/segbits_%s.db" % (db_root, src_db) if not os.path.exists(seg_db_file): continue with util.OpenSafeFile(seg_db_file, "r") as f: for line in f: line = line.split() for bit in line[1:]: if bit[0] == "!": continue if offset != 0: m = re.match(r"(\d+)_(\d+)", bit) bit = "%02d_%02d" % (int( m.group(1)), int(m.group(2)) + offset) bits.add(bit) if len(bits) > 0: with util.OpenSafeFile(mask_db_file, "w") as f: for bit in sorted(bits): print("bit %s" % bit, file=f)
def load_tag_groups(file_name): """ Loads tag groups from a text file. A tag group is defined by specifying a space separated list of tags within a single line. Lines that are empty or start with '#' are ignored. """ tag_groups = [] # Load tag group specifications with util.OpenSafeFile(file_name, "r") as fp: for line in fp: line = line.strip() if len(line) == 0 or line.startswith("#"): continue group = set(line.split()) if len(group): tag_groups.append(group) # Check if all tag groups are exclusive for tag_group_a, tag_group_b in itertools.combinations(tag_groups, 2): tags = tag_group_a & tag_group_b if len(tags): raise RuntimeError( "Tag(s) {} are present in multiple groups".format( " ".join(tags))) return tag_groups
def load_zero_db(fn): # Remove comments and convert to list of lines ret = [] with util.OpenSafeFile(fn, "r") as f: for l in f: pos = l.find("#") if pos >= 0: l = l[0:pos] l = l.strip() if not l: continue ret.append(l) return ret
def update_seg_fns(fn_inouts, zero_db, tag_groups, clb_int, lazy=False, strict=True, verbose=False): seg_files = 0 seg_lines = 0 for fn_in, fn_out in fn_inouts: verbose and print("zb %s: %s" % (fn_in, os.path.exists(fn_in))) if lazy and not os.path.exists(fn_in): continue lines = read_segbits(fn_in) changes = 0 # Find common bits for tag groups bit_groups = find_common_bits_for_tag_groups(lines, tag_groups) # Group tags new_changes, lines = group_tags(lines, tag_groups, bit_groups) changes += new_changes new_changes, lines = add_zero_bits(fn_in, lines, zero_db, clb_int=clb_int, strict=strict, verbose=verbose) changes += new_changes new_changes, lines = remove_ambiguous_solutions( fn_in, lines, strict=strict, verbose=verbose, ) changes += new_changes with util.OpenSafeFile(fn_out, "w") as f: for line in sorted(lines): print(line, file=f) if changes is not None: seg_files += 1 seg_lines += changes print("Segbit: checked %u files w/ %u changed lines" % (seg_files, seg_lines))
def read_segbits(fn_in): """ Reads a segbits file. Removes duplcated lines. Returns a list of the lines. """ lines = [] llast = None with util.OpenSafeFile(fn_in, "r") as f: for line in f: # Hack: skip duplicate lines # This happens while merging a new multibit entry line = line.strip() if len(line) == 0: continue if line == llast: continue lines.append(line) return lines