#!/usr/bin/env python3 """Merge set/count coverage profiles by source block, rejecting mismatched layouts.""" import argparse from pathlib import Path def merge(paths): blocks = {} for path in paths: lines = Path(path).read_text().splitlines() if not lines or lines[0] not in ("mode: set", "mode: count", "mode: atomic"): raise ValueError(f"invalid profile header: {path}") for line in lines[1:]: location, statements, count = line.rsplit(" ", 2) statements, count = int(statements), int(count) previous = blocks.get(location) if previous is not None and previous[0] != statements: raise ValueError(f"source layout changed: {location}") blocks[location] = (statements, max(int(count > 0), previous[1] if previous else 0)) return "mode: set\n" + "".join(f"{loc} {n} {covered}\n" for loc, (n, covered) in sorted(blocks.items())) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("profiles", nargs="+") parser.add_argument("--output", required=True) arguments = parser.parse_args() Path(arguments.output).write_text(merge(arguments.profiles)) if __name__ == "__main__": main()