def run(globaldb, localdb, verbose=False): local_db_files = list() work_db_files = list() # get DB files global_entries = {} local_entries = {} final_entries = {} verbose and print("removing %s from %s" % (localdb, globaldb)) # parse global db for line, (tag, bits, mode) in util.parse_db_lines(globaldb): global_entries[tag] = bits # parse local db for line, (tag, bits, mode) in util.parse_db_lines(localdb): local_entries[tag] = bits for entry in global_entries: if entry not in local_entries: final_entries[entry] = global_entries[entry] else: verbose and print("Removing entry %s" % entry) util.write_db_lines(globaldb, final_entries)
def run(fn_ins, fn_out, strict=False, verbose=False): # tag to bits entries = {} # tag to (bits, line) tags = dict() # bits to (tag, line) bitss = dict() for fn_in in fn_ins: for line, (tag, bits, mode) in util.parse_db_lines(fn_in): line = line.strip() assert mode is not None or mode != "always", "strict: got ill defined line: %s" % ( line, ) if tag in tags: orig_bits, orig_line = tags[tag] if orig_bits != bits: print("WARNING: got duplicate tag %s" % (tag, )) print(" Orig line: %s" % orig_line) print(" New line : %s" % line) assert not strict, "strict: got duplicate tag" if bits in bitss: orig_tag, orig_line = bitss[bits] if orig_tag != tag: print("WARNING: got duplicate bits %s" % (bits, )) print(" Orig line: %s" % orig_line) print(" New line : %s" % line) assert not strict, "strict: got duplicate bits" entries[tag] = bits tags[tag] = (bits, line) if bits != None: bitss[bits] = (tag, line) util.write_db_lines(fn_out, entries)
def apply_masks(fn_in, groups): """Add 0 entries ("!") to .db entries based on groups definition""" new_db = {} for line, (tag, bits, mode) in util.parse_db_lines(fn_in): assert not mode, "Unresolved tag: %s" % (line, ) prefix = tag[0:tag.rfind(".")] group = groups.get(prefix, None) if group: bits = set(bits) for bit in group: if bit not in bits: bits.add("!" + bit) bits = frozenset(bits) new_db[tag] = bits return new_db
def run(fn_ins, fn_out, strict=False, track_origin=False, verbose=False): # tag to bits entries = {} # tag to (bits, line) tags = dict() # bits to (tag, line) bitss = dict() for fn_in in fn_ins: for line, (tag, bits, mode, origin) in util.parse_db_lines(fn_in): line = line.strip() assert mode is not None or mode != "always", "strict: got ill defined line: %s" % ( line, ) if tag in tags: orig_bits, orig_line, orig_origin = tags[tag] if orig_bits != bits: print( "WARNING: got duplicate tag %s" % (tag, ), file=sys.stderr) print(" Orig line: %s" % orig_line, file=sys.stderr) print(" New line : %s" % line, file=sys.stderr) assert not strict, "strict: got duplicate tag" origin = os.path.basename(os.getcwd()) if track_origin and orig_origin != origin: origin = orig_origin + "," + origin if bits in bitss: orig_tag, orig_line = bitss[bits] if orig_tag != tag: print( "WARNING: got duplicate bits %s" % (bits, ), file=sys.stderr) print(" Orig line: %s" % orig_line, file=sys.stderr) print(" New line : %s" % line, file=sys.stderr) assert not strict, "strict: got duplicate bits" if track_origin and origin is None: origin = os.path.basename(os.getcwd()) entries[tag] = (bits, origin) tags[tag] = (bits, line, origin) if bits != None: bitss[bits] = (tag, line) util.write_db_lines(fn_out, entries, track_origin)
def index_masks(fn_in, groups_in): """Return a dictionary with the bits active in each group for the specified list of groups""" # Only analyze the given groups groups = {} for group in groups_in: groups[group] = set() # Index bits for line, (tag, bits, mode) in util.parse_db_lines(fn_in): assert not mode, "Unresolved tag: %s" % (line, ) prefix = tag[0:tag.rfind(".")] group = groups.get(prefix, None) # Drop groups we aren't interested in if group is None: continue for bit in bits: bit = bit.replace("!", "") group.add(bit) # Verify we were able to find all groups for groupk, groupv in groups.items(): assert len(groupv), "Bad group %s" % groupk return groups