"""The tree after the whole-file step (wave 0) and waves 1..k of plan.json: the changed files, written under . python3 -I mktree.py prints files changed, files removed, lines removed""" import collections, json, os, sys plan, site, k, out = json.load(open(sys.argv[1])), sys.argv[2], int(sys.argv[3]), sys.argv[4] cut = collections.defaultdict(list) for w in plan["waves"][:k]: for units in w.values(): for m, a, b, _ in units: cut[m.replace(".", "/") + ".lean"].append((a, b)) removed = 0 os.makedirs(out, exist_ok=True) for f, rs in cut.items(): t = open(f"{site}/{f}", encoding="utf-8").read().split("\n"); drop = set() for a, b in rs: drop.update(range(a, b + 1)) if a >= 2 and b < len(t) and not t[a - 2].strip() and not t[b].strip(): drop.add(b + 1) # one blank line between what is left removed += len(drop) os.makedirs(os.path.dirname(f"{out}/{f}"), exist_ok=True) open(f"{out}/{f}", "w", encoding="utf-8").write("\n".join(l for i, l in enumerate(t, 1) if i not in drop)) open(f"{out}/REMOVED", "w").write("\n".join(plan["whole_files"]) + "\n") print(f"wave {k}: {len(cut)} files changed, {len(plan['whole_files'])} files removed, {removed} lines removed from the changed files")