From 4a2ee8832838870b661bd1f38dac204bbe8285e9 Mon Sep 17 00:00:00 2001 From: Kyler Date: Tue, 2 Dec 2025 22:51:34 -0700 Subject: [PATCH] Refactor versioning and metadata handling in the Python module --- SLS_Python/.gitignore | 1 - SLS_Python/pyproject.toml | 4 +-- SLS_Python/sls/_version.py | 17 +++++++++++ SLS_Python/sls/meta.py | 29 ++++--------------- SLS_Python/sls_build_backend/_version.py.in | 1 + .../sls_build_backend/_version_dev.py.in | 17 +++++++++++ SLS_Python/sls_build_backend/write_version.py | 11 +++++-- 7 files changed, 50 insertions(+), 30 deletions(-) create mode 100644 SLS_Python/sls/_version.py create mode 100644 SLS_Python/sls_build_backend/_version_dev.py.in diff --git a/SLS_Python/.gitignore b/SLS_Python/.gitignore index 39b71fb..adee7d8 100644 --- a/SLS_Python/.gitignore +++ b/SLS_Python/.gitignore @@ -1,5 +1,4 @@ __pycache__/ .venv/ -sls/_version.py sls_python.egg-info/ dist/ diff --git a/SLS_Python/pyproject.toml b/SLS_Python/pyproject.toml index faba41c..adf20da 100644 --- a/SLS_Python/pyproject.toml +++ b/SLS_Python/pyproject.toml @@ -5,8 +5,8 @@ build-backend = "sls_build_backend" [project] name = "sls_python" -version = "0.1.0" -description = "Python reimplementation skeleton of the SLS C project" +version = "0.0.1-alpha" +description = "Python reimplementation of the SLS C project" authors = [ { name = "Kyler Olsen" } ] readme = "README.md" license = { text = "MIT" } diff --git a/SLS_Python/sls/_version.py b/SLS_Python/sls/_version.py new file mode 100644 index 0000000..8acd5f3 --- /dev/null +++ b/SLS_Python/sls/_version.py @@ -0,0 +1,17 @@ +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" diff --git a/SLS_Python/sls/meta.py b/SLS_Python/sls/meta.py index e9f01e9..bbc95ce 100644 --- a/SLS_Python/sls/meta.py +++ b/SLS_Python/sls/meta.py @@ -4,34 +4,15 @@ # November 2025 import sys -from datetime import datetime, timezone try: - from ._version import commit, timestamp # type: ignore + from ._version import version, commit, timestamp # type: ignore except ImportError: - try: - import subprocess - 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 = f"{result_hash} {result_date}" - timestamp = datetime.now(timezone.utc).isoformat() + "Z" - except Exception: - commit = "unknown" - timestamp = "unknown" + version = "unknown" + commit = "unknown" + timestamp = "unknown" -# Equivalent of SLS_NAME and SLS_VER SLS_NAME = "SLS_PYTHON" -SLS_VER = "a.0.0" # Runtime interpreter info (Python equivalent of compiler) _impl = sys.implementation @@ -39,5 +20,5 @@ INTERPRETER_NAME = _impl.name.capitalize() INTERPRETER_VER = _impl.version.major def print_version() -> None: - print(f"YREA SLS ({SLS_NAME}) {SLS_VER} ({commit})") + print(f"YREA SLS ({SLS_NAME}) {version} ({commit})") print(f"Running on {INTERPRETER_NAME} {INTERPRETER_VER} at {timestamp}") diff --git a/SLS_Python/sls_build_backend/_version.py.in b/SLS_Python/sls_build_backend/_version.py.in index 6a2b4f6..0511cae 100644 --- a/SLS_Python/sls_build_backend/_version.py.in +++ b/SLS_Python/sls_build_backend/_version.py.in @@ -1,3 +1,4 @@ # Auto-generated during build +version = "{version}" commit = "{commit}" timestamp = "{timestamp}" diff --git a/SLS_Python/sls_build_backend/_version_dev.py.in b/SLS_Python/sls_build_backend/_version_dev.py.in new file mode 100644 index 0000000..07d667a --- /dev/null +++ b/SLS_Python/sls_build_backend/_version_dev.py.in @@ -0,0 +1,17 @@ +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" diff --git a/SLS_Python/sls_build_backend/write_version.py b/SLS_Python/sls_build_backend/write_version.py index 79cb061..0a198e5 100644 --- a/SLS_Python/sls_build_backend/write_version.py +++ b/SLS_Python/sls_build_backend/write_version.py @@ -4,6 +4,7 @@ import pathlib root = pathlib.Path(__file__).resolve().parents[1] 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(): @@ -28,15 +29,19 @@ def get_timestamp(): return datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" def clean_output(): - if output.exists(): - output.unlink() + version = "unknown" + + text = template_dev.read_text() + text = text.format(version=version) + output.write_text(text) def main(): + version = "unknown" commit = get_commit() timestamp = get_timestamp() text = template.read_text() - text = text.format(commit=commit, timestamp=timestamp) + text = text.format(version=version, commit=commit, timestamp=timestamp) output.write_text(text) if __name__ == "__main__":