Refactor versioning and metadata handling in the Python module

This commit is contained in:
Kyler Olsen 2025-12-02 22:51:34 -07:00
parent 1a11569e9a
commit 4a2ee88328
7 changed files with 50 additions and 30 deletions

View File

@ -1,5 +1,4 @@
__pycache__/ __pycache__/
.venv/ .venv/
sls/_version.py
sls_python.egg-info/ sls_python.egg-info/
dist/ dist/

View File

@ -5,8 +5,8 @@ build-backend = "sls_build_backend"
[project] [project]
name = "sls_python" name = "sls_python"
version = "0.1.0" version = "0.0.1-alpha"
description = "Python reimplementation skeleton of the SLS C project" description = "Python reimplementation of the SLS C project"
authors = [ { name = "Kyler Olsen" } ] authors = [ { name = "Kyler Olsen" } ]
readme = "README.md" readme = "README.md"
license = { text = "MIT" } license = { text = "MIT" }

View File

@ -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"

View File

@ -4,34 +4,15 @@
# November 2025 # November 2025
import sys import sys
from datetime import datetime, timezone
try: try:
from ._version import commit, timestamp # type: ignore from ._version import version, commit, timestamp # type: ignore
except ImportError: except ImportError:
try: version = "unknown"
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" commit = "unknown"
timestamp = "unknown" timestamp = "unknown"
# Equivalent of SLS_NAME and SLS_VER
SLS_NAME = "SLS_PYTHON" SLS_NAME = "SLS_PYTHON"
SLS_VER = "a.0.0"
# Runtime interpreter info (Python equivalent of compiler) # Runtime interpreter info (Python equivalent of compiler)
_impl = sys.implementation _impl = sys.implementation
@ -39,5 +20,5 @@ INTERPRETER_NAME = _impl.name.capitalize()
INTERPRETER_VER = _impl.version.major INTERPRETER_VER = _impl.version.major
def print_version() -> None: 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}") print(f"Running on {INTERPRETER_NAME} {INTERPRETER_VER} at {timestamp}")

View File

@ -1,3 +1,4 @@
# Auto-generated during build # Auto-generated during build
version = "{version}"
commit = "{commit}" commit = "{commit}"
timestamp = "{timestamp}" timestamp = "{timestamp}"

View File

@ -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"

View File

@ -4,6 +4,7 @@ import pathlib
root = pathlib.Path(__file__).resolve().parents[1] root = pathlib.Path(__file__).resolve().parents[1]
template = root / "sls_build_backend" / "_version.py.in" template = root / "sls_build_backend" / "_version.py.in"
template_dev = root / "sls_build_backend" / "_version_dev.py.in"
output = root / "sls" / "_version.py" output = root / "sls" / "_version.py"
def get_commit(): def get_commit():
@ -28,15 +29,19 @@ def get_timestamp():
return datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z" return datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z"
def clean_output(): def clean_output():
if output.exists(): version = "unknown"
output.unlink()
text = template_dev.read_text()
text = text.format(version=version)
output.write_text(text)
def main(): def main():
version = "unknown"
commit = get_commit() commit = get_commit()
timestamp = get_timestamp() timestamp = get_timestamp()
text = template.read_text() text = template.read_text()
text = text.format(commit=commit, timestamp=timestamp) text = text.format(version=version, commit=commit, timestamp=timestamp)
output.write_text(text) output.write_text(text)
if __name__ == "__main__": if __name__ == "__main__":