Skip to content

Commit

Permalink
set custom version based upon if destined for package index
Browse files Browse the repository at this point in the history
  • Loading branch information
berquist committed Feb 18, 2024
1 parent d4dadce commit 989db6e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
7 changes: 6 additions & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 📦
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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*"]
Expand Down
35 changes: 35 additions & 0 deletions pyresponse/versioningit_override.py
Original file line number Diff line number Diff line change
@@ -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})

0 comments on commit 989db6e

Please sign in to comment.