Refactor version generation and error handling in versioning scripts
This commit is contained in:
parent
4a2ee88328
commit
a754cd4df4
|
|
@ -1,17 +1,23 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime, timezone
|
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"],
|
["git", "describe", "--always", "--dirty", "--abbrev=7"],
|
||||||
cwd=".",
|
cwd=".",
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True
|
||||||
).strip()
|
).strip()
|
||||||
__result_date = subprocess.check_output(
|
__result_date = subprocess.check_output(
|
||||||
["git", "show", "-s", "--format=%ci"],
|
["git", "show", "-s", "--format=%ci"],
|
||||||
cwd=".",
|
cwd=".",
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True
|
||||||
).strip()
|
).strip()
|
||||||
version = "unknown"
|
commit = __result_hash + " " + __result_date
|
||||||
commit = __result_hash + " " + __result_date
|
except:
|
||||||
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
|
commit = "unknown"
|
||||||
|
try:
|
||||||
|
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
|
||||||
|
except:
|
||||||
|
timestamp = "unknown"
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,23 @@
|
||||||
import subprocess
|
import subprocess
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
__result_hash = subprocess.check_output(
|
version = "{version}"
|
||||||
|
try:
|
||||||
|
__result_hash = subprocess.check_output(
|
||||||
["git", "describe", "--always", "--dirty", "--abbrev=7"],
|
["git", "describe", "--always", "--dirty", "--abbrev=7"],
|
||||||
cwd=".",
|
cwd=".",
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True
|
||||||
).strip()
|
).strip()
|
||||||
__result_date = subprocess.check_output(
|
__result_date = subprocess.check_output(
|
||||||
["git", "show", "-s", "--format=%ci"],
|
["git", "show", "-s", "--format=%ci"],
|
||||||
cwd=".",
|
cwd=".",
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True
|
||||||
).strip()
|
).strip()
|
||||||
version = "{version}"
|
commit = __result_hash + " " + __result_date
|
||||||
commit = __result_hash + " " + __result_date
|
except:
|
||||||
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
|
commit = "unknown"
|
||||||
|
try:
|
||||||
|
timestamp = datetime.now(timezone.utc).isoformat() + "Z"
|
||||||
|
except:
|
||||||
|
timestamp = "unknown"
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,17 @@
|
||||||
from setuptools.build_meta import build_wheel as _build_wheel
|
from setuptools.build_meta import build_wheel as _build_wheel
|
||||||
from setuptools.build_meta import build_sdist as _build_sdist
|
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):
|
def build_wheel(*args, **kwargs):
|
||||||
_generate_version()
|
generate_version()
|
||||||
o = _build_wheel(*args, **kwargs)
|
o = _build_wheel(*args, **kwargs)
|
||||||
clean_output()
|
generate_version_dev()
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
|
||||||
def build_sdist(*args, **kwargs):
|
def build_sdist(*args, **kwargs):
|
||||||
_generate_version()
|
generate_version()
|
||||||
o = _build_sdist(*args, **kwargs)
|
o = _build_sdist(*args, **kwargs)
|
||||||
clean_output()
|
generate_version_dev()
|
||||||
return o
|
return o
|
||||||
|
|
@ -1,48 +1,91 @@
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import datetime
|
import datetime
|
||||||
import pathlib
|
import pathlib
|
||||||
|
|
||||||
|
try:
|
||||||
|
import tomllib # Python 3.11+
|
||||||
|
except Exception: # pragma: no cover - older Pythons
|
||||||
|
tomllib = None
|
||||||
|
|
||||||
root = pathlib.Path(__file__).resolve().parents[1]
|
root = pathlib.Path(__file__).resolve().parents[1]
|
||||||
|
# templates live inside the backend package
|
||||||
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"
|
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():
|
||||||
try:
|
try:
|
||||||
result_hash = subprocess.check_output(
|
result_hash = subprocess.check_output(
|
||||||
["git", "describe", "--always", "--dirty", "--abbrev=7"],
|
["git", "describe", "--always", "--dirty", "--abbrev=7"],
|
||||||
cwd=".",
|
cwd=str(root),
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True,
|
||||||
).strip()
|
).strip()
|
||||||
result_date = subprocess.check_output(
|
result_date = subprocess.check_output(
|
||||||
["git", "show", "-s", "--format=%ci"],
|
["git", "show", "-s", "--format=%ci"],
|
||||||
cwd=".",
|
cwd=str(root),
|
||||||
stderr=subprocess.DEVNULL,
|
stderr=subprocess.DEVNULL,
|
||||||
text=True
|
text=True,
|
||||||
).strip()
|
).strip()
|
||||||
return f"{result_hash} {result_date}"
|
return f"{result_hash} {result_date}"
|
||||||
except Exception:
|
except Exception:
|
||||||
return "unknown"
|
return "unknown"
|
||||||
|
|
||||||
|
|
||||||
def get_timestamp():
|
def get_timestamp():
|
||||||
return datetime.datetime.now(datetime.timezone.utc).isoformat() + "Z"
|
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 = template_dev.read_text()
|
||||||
text = text.format(version=version)
|
text = text.format(version=version)
|
||||||
output.write_text(text)
|
output.write_text(text)
|
||||||
|
|
||||||
def main():
|
|
||||||
version = "unknown"
|
def generate_version():
|
||||||
|
version = _determine_version(root)
|
||||||
|
|
||||||
commit = get_commit()
|
commit = get_commit()
|
||||||
timestamp = get_timestamp()
|
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 = template.read_text()
|
||||||
text = text.format(version=version, commit=commit, timestamp=timestamp)
|
text = text.format(version=version, commit=commit, timestamp=timestamp)
|
||||||
output.write_text(text)
|
output.write_text(text)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue