diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index eda3308..833113c 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,6 +4,11 @@ name: Publish Python 🐍 distribution 📦 to PyPI and TestPyPI # yamllint disable-line rule:truthy on: push +# For now always take the "reduced" form of the version, even for those +# actions runs that do not push to a package index. +env: + VERSIONINGIT_FOR_PACKAGE_INDEX: true + jobs: build: name: Build distribution 📦 @@ -22,7 +27,7 @@ jobs: - name: Install pypa/build run: | python3 -m pip install -U pip setuptools - python3 -m pip install build --user + python3 -m pip install build - name: Build a binary wheel and a source tarball run: python3 -m build - name: Store the distribution packages diff --git a/pyproject.toml b/pyproject.toml index 7668046..6d76efa 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -94,6 +94,12 @@ section-order = ["future", "standard-library", "attrs", "third-party", "chemistr "attrs" = ["attr"] "chemistry" = ["psi4", "pyscf"] +[tool.versioningit.format] +method = { module = "versioningit_override", value = "my_format", module-dir = "pyresponse" } + +[tool.versioningit.template-fields.version-tuple] +pep440 = true + [tool.versioningit.vcs] method = "git" match = ["v*"] diff --git a/pyresponse/versioningit_override.py b/pyresponse/versioningit_override.py new file mode 100644 index 0000000..35759cb --- /dev/null +++ b/pyresponse/versioningit_override.py @@ -0,0 +1,35 @@ +from os import getenv +from typing import Any, Dict + +from versioningit import VCSDescription +from versioningit.basics import DEFAULT_FORMATS + +_ENVVARNAME = "VERSIONINGIT_FOR_PACKAGE_INDEX" + + +def my_format( + *, description: VCSDescription, base_version: str, next_version: str, params: Dict[str, Any] +) -> str: + state = description.state + assert state in {"distance", "dirty", "distance-dirty"} + + if getenv(_ENVVARNAME, "False").lower() in ("true", "1", "t"): + fmt_distance = "{base_version}.post{distance}" + if state != "distance": + raise RuntimeError("dirty state doesn't make sense when building for a package index") + else: + # Default but missing {vcs} before {rev} + fmt_distance = "{base_version}.post{distance}+{rev}" + # Default + fmt_dirty = DEFAULT_FORMATS["dirty"] + # Default but missing {vcs} before {rev} + fmt_distance_dirty = "{base_version}.post{distance}+{rev}.d{build_date:%Y%m%d}" + + if state == "distance": + fmt = fmt_distance + elif state == "dirty": + fmt = fmt_dirty + elif state == "distance-dirty": + fmt = fmt_distance_dirty + + return fmt.format_map({**description.fields, "base_version": base_version})