Add setuptools_scm for dynamic zero-config Python versioning (#1690)

* Add `setuptools_scm` for dynamic zero-config Python versioning

This removes the need for manually bumping versions in the Python
bindings.

For the wheel uploads, the correct semver version is inferred in the case
of tagged commits, which is exactly the case in GitHub CI.

The docs were updated to reflect the changes in the release workflow.

* Add separate version variable and module, use PEP484-compliant exports

This is the best practice mentioned in the `setuptools_scm` docs, see
https://setuptools-scm.readthedocs.io/en/latest/usage/#version-at-runtime.
This commit is contained in:
Nicholas Junge 2023-11-01 10:48:01 +01:00 committed by GitHub
parent bce46fb413
commit 3623765dd3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 57 deletions

View file

@ -32,44 +32,23 @@ from absl import app
from google_benchmark import _benchmark from google_benchmark import _benchmark
from google_benchmark._benchmark import ( from google_benchmark._benchmark import (
Counter, Counter as Counter,
State, State as State,
kMicrosecond, kMicrosecond as kMicrosecond,
kMillisecond, kMillisecond as kMillisecond,
kNanosecond, kNanosecond as kNanosecond,
kSecond, kSecond as kSecond,
o1, o1 as o1,
oAuto, oAuto as oAuto,
oLambda, oLambda as oLambda,
oLogN, oLogN as oLogN,
oN, oN as oN,
oNCubed, oNCubed as oNCubed,
oNLogN, oNLogN as oNLogN,
oNone, oNone as oNone,
oNSquared, oNSquared as oNSquared,
) )
from google_benchmark.version import __version__ as __version__
__all__ = [
"register",
"main",
"Counter",
"kNanosecond",
"kMicrosecond",
"kMillisecond",
"kSecond",
"oNone",
"o1",
"oN",
"oNSquared",
"oNCubed",
"oLogN",
"oNLogN",
"oAuto",
"oLambda",
"State",
]
__version__ = "1.8.3"
class __OptionMaker: class __OptionMaker:

View file

@ -0,0 +1,7 @@
from importlib.metadata import PackageNotFoundError, version
try:
__version__ = version("google-benchmark")
except PackageNotFoundError:
# package is not installed
pass

View file

@ -3,7 +3,7 @@
Python bindings are available as wheels on [PyPI](https://pypi.org/project/google-benchmark/) for importing and Python bindings are available as wheels on [PyPI](https://pypi.org/project/google-benchmark/) for importing and
using Google Benchmark directly in Python. using Google Benchmark directly in Python.
Currently, pre-built wheels exist for macOS (both ARM64 and Intel x86), Linux x86-64 and 64-bit Windows. Currently, pre-built wheels exist for macOS (both ARM64 and Intel x86), Linux x86-64 and 64-bit Windows.
Supported Python versions are Python 3.7 - 3.10. Supported Python versions are Python 3.8 - 3.12.
To install Google Benchmark's Python bindings, run: To install Google Benchmark's Python bindings, run:
@ -25,9 +25,9 @@ python3 -m venv venv --system-site-packages
source venv/bin/activate # .\venv\Scripts\Activate.ps1 on Windows source venv/bin/activate # .\venv\Scripts\Activate.ps1 on Windows
# upgrade Python's system-wide packages # upgrade Python's system-wide packages
python -m pip install --upgrade pip setuptools wheel python -m pip install --upgrade pip build
# builds the wheel and stores it in the directory "wheelhouse". # builds the wheel and stores it in the directory "dist".
python -m pip wheel . -w wheelhouse python -m build
``` ```
NB: Building wheels from source requires Bazel. For platform-specific instructions on how to install Bazel, NB: Building wheels from source requires Bazel. For platform-specific instructions on how to install Bazel,

View file

@ -8,9 +8,8 @@
* `git log $(git describe --abbrev=0 --tags)..HEAD` gives you the list of * `git log $(git describe --abbrev=0 --tags)..HEAD` gives you the list of
commits between the last annotated tag and HEAD commits between the last annotated tag and HEAD
* Pick the most interesting. * Pick the most interesting.
* Create one last commit that updates the version saved in `CMakeLists.txt`, `MODULE.bazel` * Create one last commit that updates the version saved in `CMakeLists.txt` and `MODULE.bazel`
and the `__version__` variable in `bindings/python/google_benchmark/__init__.py`to the to the release version you're creating. (This version will be used if benchmark is installed
release version you're creating. (This version will be used if benchmark is installed
from the archive you'll be creating in the next step.) from the archive you'll be creating in the next step.)
``` ```
@ -21,16 +20,6 @@ project (benchmark VERSION 1.8.0 LANGUAGES CXX)
module(name = "com_github_google_benchmark", version="1.8.0") module(name = "com_github_google_benchmark", version="1.8.0")
``` ```
```python
# bindings/python/google_benchmark/__init__.py
# ...
__version__ = "1.8.0" # <-- change this to the release version you are creating
# ...
```
* Create a release through github's interface * Create a release through github's interface
* Note this will create a lightweight tag. * Note this will create a lightweight tag.
* Update this to an annotated tag: * Update this to an annotated tag:
@ -38,4 +27,5 @@ __version__ = "1.8.0" # <-- change this to the release version you are creating
* `git tag -a -f <tag> <tag>` * `git tag -a -f <tag> <tag>`
* `git push --force --tags origin` * `git push --force --tags origin`
* Confirm that the "Build and upload Python wheels" action runs to completion * Confirm that the "Build and upload Python wheels" action runs to completion
* run it manually if it hasn't run * Run it manually if it hasn't run.
* IMPORTANT: When re-running manually, make sure to select the newly created `<tag>` as the workflow version in the "Run workflow" tab on the GitHub Actions page.

View file

@ -1,5 +1,5 @@
[build-system] [build-system]
requires = ["setuptools", "wheel"] requires = ["setuptools>=64", "setuptools-scm[toml]>=8"]
build-backend = "setuptools.build_meta" build-backend = "setuptools.build_meta"
[project] [project]
@ -52,9 +52,10 @@ zip-safe = false
where = ["bindings/python"] where = ["bindings/python"]
[tool.setuptools.dynamic] [tool.setuptools.dynamic]
version = { attr = "google_benchmark.__version__" }
readme = { file = "README.md", content-type = "text/markdown" } readme = { file = "README.md", content-type = "text/markdown" }
[tool.setuptools_scm]
[tool.black] [tool.black]
# Source https://github.com/psf/black#configuration-format # Source https://github.com/psf/black#configuration-format
include = "\\.pyi?$" include = "\\.pyi?$"
@ -85,3 +86,6 @@ ignore = [
# line too long, rely on black for formatting. # line too long, rely on black for formatting.
"E501", "E501",
] ]
[tool.ruff.isort]
combine-as-imports = true