import subprocess import datetime import pathlib root = pathlib.Path(__file__).resolve().parents[1] template = root / "scripts" / "_version.py.in" output = root / "sls" / "_version.py" def get_commit(): try: result_hash = subprocess.check_output( ["git", "describe", "--always", "--dirty", "--abbrev=7"], cwd=".", stderr=subprocess.DEVNULL, text=True ).strip() result_date = subprocess.check_output( ["git", "show", "-s", "--format=%ci"], cwd=".", stderr=subprocess.DEVNULL, text=True ).strip() return f"{result_hash} {result_date}" except Exception: return "unknown" def get_timestamp(): return datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" def main(): commit = get_commit() timestamp = get_timestamp() text = template.read_text() text = text.format(commit=commit, timestamp=timestamp) output.write_text(text) if __name__ == "__main__": main()