From a754cd4df4b412e91ce4a5745d79fd4031a1479f Mon Sep 17 00:00:00 2001 From: Kyler Date: Tue, 2 Dec 2025 23:18:41 -0700 Subject: [PATCH] Refactor version generation and error handling in versioning scripts --- SLS_Python/sls/_version.py | 36 +++++----- .../sls_build_backend/_version_dev.py.in | 34 ++++++---- SLS_Python/sls_build_backend/build_hooks.py | 14 ++-- SLS_Python/sls_build_backend/write_version.py | 65 +++++++++++++++---- 4 files changed, 101 insertions(+), 48 deletions(-) diff --git a/SLS_Python/sls/_version.py b/SLS_Python/sls/_version.py index 8acd5f3..0deac39 100644 --- a/SLS_Python/sls/_version.py +++ b/SLS_Python/sls/_version.py @@ -1,17 +1,23 @@ import subprocess from datetime import datetime, timezone -__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() -version = "unknown" -commit = __result_hash + " " + __result_date -timestamp = datetime.now(timezone.utc).isoformat() + "Z" +version = "0.0.1-alpha" +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() + commit = __result_hash + " " + __result_date +except: + commit = "unknown" +try: + timestamp = datetime.now(timezone.utc).isoformat() + "Z" +except: + timestamp = "unknown" diff --git a/SLS_Python/sls_build_backend/_version_dev.py.in b/SLS_Python/sls_build_backend/_version_dev.py.in index 07d667a..b6f0ae9 100644 --- a/SLS_Python/sls_build_backend/_version_dev.py.in +++ b/SLS_Python/sls_build_backend/_version_dev.py.in @@ -1,17 +1,23 @@ import subprocess from datetime import datetime, timezone -__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() version = "{version}" -commit = __result_hash + " " + __result_date -timestamp = datetime.now(timezone.utc).isoformat() + "Z" +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() + commit = __result_hash + " " + __result_date +except: + commit = "unknown" +try: + timestamp = datetime.now(timezone.utc).isoformat() + "Z" +except: + timestamp = "unknown" diff --git a/SLS_Python/sls_build_backend/build_hooks.py b/SLS_Python/sls_build_backend/build_hooks.py index 399c3f8..93f3d79 100644 --- a/SLS_Python/sls_build_backend/build_hooks.py +++ b/SLS_Python/sls_build_backend/build_hooks.py @@ -1,19 +1,17 @@ from setuptools.build_meta import build_wheel as _build_wheel from setuptools.build_meta import build_sdist as _build_sdist -from .write_version import main, clean_output +from .write_version import generate_version, generate_version_dev -def _generate_version(): - main() def build_wheel(*args, **kwargs): - _generate_version() + generate_version() o = _build_wheel(*args, **kwargs) - clean_output() + generate_version_dev() return o def build_sdist(*args, **kwargs): - _generate_version() + generate_version() o = _build_sdist(*args, **kwargs) - clean_output() - return o \ No newline at end of file + generate_version_dev() + return o diff --git a/SLS_Python/sls_build_backend/write_version.py b/SLS_Python/sls_build_backend/write_version.py index 0a198e5..108df93 100644 --- a/SLS_Python/sls_build_backend/write_version.py +++ b/SLS_Python/sls_build_backend/write_version.py @@ -1,48 +1,91 @@ + import subprocess import datetime import pathlib +try: + import tomllib # Python 3.11+ +except Exception: # pragma: no cover - older Pythons + tomllib = None + root = pathlib.Path(__file__).resolve().parents[1] +# templates live inside the backend package template = root / "sls_build_backend" / "_version.py.in" template_dev = root / "sls_build_backend" / "_version_dev.py.in" output = root / "sls" / "_version.py" + def get_commit(): try: result_hash = subprocess.check_output( ["git", "describe", "--always", "--dirty", "--abbrev=7"], - cwd=".", + cwd=str(root), stderr=subprocess.DEVNULL, - text=True + text=True, ).strip() result_date = subprocess.check_output( ["git", "show", "-s", "--format=%ci"], - cwd=".", + cwd=str(root), stderr=subprocess.DEVNULL, - text=True + 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 clean_output(): - version = "unknown" +def _get_version_from_pyproject(root_path: pathlib.Path): + py = root_path / "pyproject.toml" + if not py.exists() or tomllib is None: + return None + try: + data = tomllib.loads(py.read_text()) + except Exception: + return None + # PEP 621 + version = data.get("project", {}).get("version") + if version: + return version + # Some projects put metadata under tool.setuptools + version = data.get("tool", {}).get("setuptools", {}).get("version") + return version + + +def _determine_version(root_path: pathlib.Path): + v = _get_version_from_pyproject(root_path) + if v: + return v + return "unknown" + + +def generate_version_dev(): + version = _determine_version(root) + if not template_dev.exists(): + # write a minimal generated file if dev template missing + output.write_text(f"version = \"{version}\"\ncommit = \"unknown\"\ntimestamp = \"unknown\"\n") + return text = template_dev.read_text() text = text.format(version=version) output.write_text(text) -def main(): - version = "unknown" + +def generate_version(): + version = _determine_version(root) + commit = get_commit() timestamp = get_timestamp() + if not template.exists(): + # fallback: write a minimal file + output.write_text( + f"version = \"{version}\"\ncommit = \"{commit}\"\ntimestamp = \"{timestamp}\"\n" + ) + return + text = template.read_text() text = text.format(version=version, commit=commit, timestamp=timestamp) output.write_text(text) - -if __name__ == "__main__": - main()