sascha@3157: #!/usr/bin/env python sascha@3157: sascha@3157: """ Add unique numbers in front of properties values sascha@3157: to identfy the key without knowing the real key. sascha@3157: """ sascha@3157: sascha@3157: import sys sascha@3157: import re sascha@3157: import os sascha@3157: sascha@3157: BLACK_LISTED_KEYS = [ sascha@3157: re.compile(r".*\.file$") sascha@3157: ] sascha@3157: sascha@3157: BLACK_LISTED_VALUES = [ sascha@3157: re.compile(r"^http.*$") sascha@3157: ] sascha@3157: sascha@3157: NUMBERED = re.compile(r"^\s*([^\s]+)\s*=\s*\[([0-9a-zA-Z]+)\]\s*(.+)$") sascha@3157: UNUMBERED = re.compile(r"^\s*([^\s]+)\s*=\s*(.+)$") sascha@3157: sascha@3157: ALPHA = "0123456789" \ sascha@3157: "abcdefghijklmnopqrstuvwxyz" \ sascha@3157: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" sascha@3157: sascha@3157: def decode_ibase62(s): sascha@3157: t, c = 0, 1 sascha@3157: for x in s[::-1]: sascha@3157: i = ALPHA.find(x) sascha@3157: t += i*c sascha@3157: c *= len(ALPHA) sascha@3157: return t sascha@3157: sascha@3157: def ibase62(i): sascha@3157: if i == 0: sascha@3157: return "0" sascha@3157: out = [] sascha@3157: if i < 0: sascha@3157: out.append("-") sascha@3157: i = -1 sascha@3157: while i > 0: sascha@3157: out.append(ALPHA[i % len(ALPHA)]) sascha@3157: i //= len(ALPHA) sascha@3157: out.reverse() sascha@3157: return ''.join(out) sascha@3157: sascha@3157: def is_blacklisted(key, value): sascha@3157: sascha@3157: for bl in BLACK_LISTED_KEYS: sascha@3157: if bl.match(key): sascha@3157: return True sascha@3157: sascha@3157: for bl in BLACK_LISTED_VALUES: sascha@3157: if bl.match(value): sascha@3157: return True sascha@3157: sascha@3157: return False sascha@3157: sascha@3157: def find_key(already_numbered, value): sascha@3157: for k, v in already_numbered.iteritems(): sascha@3157: if v == value: sascha@3157: return k sascha@3157: return None sascha@3157: sascha@3157: def decorated_content(infile, outfile, already_numbered): sascha@3157: sascha@3157: for line in infile: sascha@3157: line = line.strip() sascha@3157: m = NUMBERED.match(line) sascha@3157: if m: sascha@3157: key, num, value = m.groups() sascha@3157: decoded_num = decode_ibase62(num) sascha@3157: last = find_key(already_numbered, decoded_num) sascha@3157: if last is None: sascha@3157: already_numbered[key] = decoded_num sascha@3157: elif last != key: sascha@3157: print >> sys.stderr, "WARN: Number clash: " \ sascha@3157: "%s leeds to '%s' and '%s'" % (num, key, last) sascha@3157: print >> outfile, line sascha@3157: continue sascha@3157: sascha@3157: m = UNUMBERED.match(line) sascha@3157: if m: sascha@3157: key, value = m.groups(1) sascha@3157: if is_blacklisted(key, value): sascha@3157: print >> outfile, line sascha@3157: else: sascha@3157: num = already_numbered.setdefault(key, len(already_numbered)) sascha@3157: print >> outfile, "%s=[%s] %s" % (key, ibase62(num), m.group(2)) sascha@3157: continue sascha@3157: print >> outfile, line sascha@3157: sascha@3157: def tmp_fname(fname): sascha@3157: name = fname + ".tmp" sascha@3157: i = 0 sascha@3157: while os.path.exists(name): sascha@3157: name = "%s.tmp%d" % (fname, i) sascha@3157: i += 1 sascha@3157: return name sascha@3157: sascha@3157: def decorate_file(fname, already_numbered): sascha@3157: sascha@3157: tmp = tmp_fname(fname) sascha@3157: sascha@3157: with open(fname, "r") as infile: sascha@3157: with open(tmp, "w") as outfile: sascha@3157: decorated_content(infile, outfile, already_numbered) sascha@3157: sascha@3157: os.rename(tmp, fname) sascha@3157: sascha@3157: def main(): sascha@3157: already_numbered = {} sascha@3157: for fname in sys.argv[1:]: sascha@3157: print >> sys.stderr, "checking %s" % fname sascha@3157: decorate_file(fname, already_numbered) sascha@3157: sascha@3157: if __name__ == "__main__": sascha@3157: main()