#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Erzeugt _build_info.py mit Build-Zeitstempel, Git-Commit, Branch und Dirty-Status. Aufruf: python aza_build_stamp.py Ergebnis: _build_info.py im selben Verzeichnis """ import datetime import os import subprocess import sys def _run_git(*args: str) -> str: try: r = subprocess.run( ["git"] + list(args), capture_output=True, text=True, timeout=5, cwd=os.path.dirname(os.path.abspath(__file__)), ) return r.stdout.strip() if r.returncode == 0 else "" except Exception: return "" def generate() -> dict: now = datetime.datetime.now() info = { "BUILD_TIME": now.strftime("%Y-%m-%d %H:%M:%S"), "BUILD_TIMESTAMP": now.strftime("%Y%m%d_%H%M%S"), "GIT_COMMIT": _run_git("rev-parse", "--short=8", "HEAD"), "GIT_BRANCH": _run_git("rev-parse", "--abbrev-ref", "HEAD"), "GIT_DIRTY": bool(_run_git("status", "--porcelain")), } return info def write_module(info: dict, path: str | None = None): if path is None: path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "_build_info.py") lines = [ '# Auto-generated by aza_build_stamp.py – DO NOT EDIT', f'BUILD_TIME = "{info["BUILD_TIME"]}"', f'BUILD_TIMESTAMP = "{info["BUILD_TIMESTAMP"]}"', f'GIT_COMMIT = "{info["GIT_COMMIT"]}"', f'GIT_BRANCH = "{info["GIT_BRANCH"]}"', f'GIT_DIRTY = {info["GIT_DIRTY"]}', ] with open(path, "w", encoding="utf-8") as f: f.write("\n".join(lines) + "\n") print(f"[BUILD-STAMP] {path} geschrieben ({info['BUILD_TIME']}, {info['GIT_COMMIT']}, dirty={info['GIT_DIRTY']})") if __name__ == "__main__": info = generate() write_module(info)