Refactor version generation and error handling in versioning scripts

This commit is contained in:
Kyler Olsen 2025-12-02 23:18:41 -07:00
parent 4a2ee88328
commit a754cd4df4
4 changed files with 101 additions and 48 deletions

View File

@ -1,17 +1,23 @@
import subprocess
from datetime import datetime, timezone
__result_hash = subprocess.check_output(
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(
).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"
).strip()
commit = __result_hash + " " + __result_date
except:
commit = "unknown"
try:
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
except:
timestamp = "unknown"

View File

@ -1,17 +1,23 @@
import subprocess
from datetime import datetime, timezone
__result_hash = subprocess.check_output(
version = "{version}"
try:
__result_hash = subprocess.check_output(
["git", "describe", "--always", "--dirty", "--abbrev=7"],
cwd=".",
stderr=subprocess.DEVNULL,
text=True
).strip()
__result_date = subprocess.check_output(
).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"
).strip()
commit = __result_hash + " " + __result_date
except:
commit = "unknown"
try:
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
except:
timestamp = "unknown"

View File

@ -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()
generate_version_dev()
return o

View File

@ -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()