56 lines
2.2 KiB
Python
56 lines
2.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Generate committed Go from the server's Ziran sources, or verify it."""
|
|
|
|
import argparse
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import tempfile
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--check", action="store_true")
|
|
parser.add_argument("--compiler", default=os.environ.get("ZI2GO", "../ziran/build/bin/zi2go"))
|
|
parser.add_argument("--std", default=os.environ.get("ZIRAN_STD", "../ziran/std"))
|
|
args = parser.parse_args()
|
|
repo = Path(__file__).resolve().parent.parent
|
|
sources = sorted(path.name for path in repo.glob("*.zi"))
|
|
if not sources:
|
|
parser.error("no Ziran server sources found")
|
|
env = os.environ.copy()
|
|
env.pop("DISPLAY", None)
|
|
env.pop("WAYLAND_DISPLAY", None)
|
|
build = repo / "build"
|
|
build.mkdir(exist_ok=True)
|
|
with tempfile.TemporaryDirectory(prefix="zi-go-", dir=build) as output:
|
|
subprocess.run([
|
|
args.compiler, "--no-main", "--pkg", "main", "--root", ".",
|
|
"--module-path", args.std, "-o", output, *sources,
|
|
], cwd=repo, env=env, check=True)
|
|
generated = sorted(Path(output).glob("*.go"))
|
|
subprocess.run(["gofmt", "-w", *(str(path) for path in generated)],
|
|
cwd=repo, env=env, check=True)
|
|
expected = {path.name for path in generated}
|
|
stale = [path for path in repo.glob("*.go")
|
|
if path.read_bytes().startswith(b"// Code generated by zi2go from ")
|
|
and path.name not in expected]
|
|
if stale:
|
|
parser.error("obsolete generated files: " + ", ".join(path.name for path in stale))
|
|
different = []
|
|
for path in generated:
|
|
target = repo / path.name
|
|
if target.exists() and target.read_bytes() == path.read_bytes():
|
|
continue
|
|
different.append(path.name)
|
|
if not args.check:
|
|
shutil.copyfile(path, target)
|
|
if args.check and different:
|
|
parser.error("generated Go differs; run make generate: " + ", ".join(different))
|
|
print(f"Ziran Go {'checked' if args.check else 'generated'}: {len(generated)} files")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|