|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +update-defs: change a set of Bazel variable declarations in a particular file. |
| 4 | +
|
| 5 | +For example, if a file named `debian-iso/deps.bzl` had the following lines in the middle: |
| 6 | +
|
| 7 | + VERSION = '20190702+deb10u2' |
| 8 | + RELEASE = 'buster' |
| 9 | + MINI_ISO_HASH = 'fa713fb9ab7853de6aefe6b83c58bcdba19ef79a0310de84d40dd2754a9539d7' |
| 10 | +
|
| 11 | +And you ran the following command with the following input: |
| 12 | +
|
| 13 | + $ ./update-defs.py debian-iso/deps.bzl |
| 14 | + VERSION = '20190702+deb10u3' |
| 15 | + RELEASE = 'buster' |
| 16 | + MINI_ISO_HASH = '26b6f6f6bcb24c4e59b965d4a2a6c44af5d79381b9230d69a7d4db415ddcb4cd' |
| 17 | +
|
| 18 | +Then those three lines (and only those lines) would be updated in `deps.bzl`. |
| 19 | +""" |
| 20 | + |
| 21 | +import re |
| 22 | +import sys |
| 23 | + |
| 24 | + |
| 25 | +def is_valid_identifier(text): |
| 26 | + return bool(re.match("^[A-Z0-9_]+$", text)) |
| 27 | + |
| 28 | + |
| 29 | +def get_match(line): |
| 30 | + if "=" in line: |
| 31 | + match = line.split("=",1)[0].strip() |
| 32 | + if is_valid_identifier(match): |
| 33 | + return match |
| 34 | + |
| 35 | + |
| 36 | +def parse_changes(changes): |
| 37 | + mapping = {} |
| 38 | + for line in changes.split("\n"): |
| 39 | + if not line: continue |
| 40 | + match = get_match(line) |
| 41 | + if match is None: |
| 42 | + raise ValueError("could not parse change line: %s" % repr(line)) |
| 43 | + if match in mapping: |
| 44 | + raise KeyError("duplicate input mapping: %s" % match) |
| 45 | + mapping[match] = line |
| 46 | + return mapping |
| 47 | + |
| 48 | + |
| 49 | +def apply_changes(line, lookup, used): |
| 50 | + match = get_match(line) |
| 51 | + if match is not None and match in lookup: |
| 52 | + used[match] += 1 |
| 53 | + line = lookup[match] |
| 54 | + print("=>", line) |
| 55 | + return line |
| 56 | + |
| 57 | + |
| 58 | +def perform_changes(filename, changes): |
| 59 | + with open(filename, "r") as f: |
| 60 | + lines = f.read().split("\n") |
| 61 | + lookup = parse_changes(changes) |
| 62 | + used = {var: 0 for var in lookup} |
| 63 | + lines = [apply_changes(line, lookup, used) for line in lines] |
| 64 | + for var, usedcount in used.items(): |
| 65 | + if usedcount < 1: |
| 66 | + raise ValueError("did not use requested change: %s was not used" % (repr(var))) |
| 67 | + elif usedcount > 1: |
| 68 | + raise ValueError("used requested change multiple times: %s was used %d times" % (repr(var), usedcount)) |
| 69 | + with open(filename, "w") as f: |
| 70 | + f.write("\n".join(lines)) |
| 71 | + print() |
| 72 | + print("updated", len(lookup), "entries") |
| 73 | + |
| 74 | + |
| 75 | +def main(argv): |
| 76 | + if len(argv) != 2: |
| 77 | + print("usage: %s <target file>", file=sys.stderr) |
| 78 | + sys.exit(1) |
| 79 | + changes = sys.stdin.read() |
| 80 | + perform_changes(sys.argv[1], changes) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + main(sys.argv) |
0 commit comments