import subprocess
from pathlib import Path

try:
    revision = (
        '"'
        + (
            subprocess.check_output(
                ["git", "rev-parse", "HEAD"],
                cwd=Path(".git").resolve(),
            )
            .decode()
            .strip()
        )
        + '"'
    )

except Exception:
    revision = "None # Build system failed to determine the revision"

with open(Path("src") / "loom" / "_revision.py", "w") as f:
    f.write(f'"""Records Loom source revision as seen by the distribution system.\n')
    f.write(f'(AUTOGENERATED by ./scripts/update-revision.py) """\n\n')
    f.write(f"__revision__ = {revision}\n")
