Skip to content

Commit

Permalink
store package version in __version__ constant
Browse files Browse the repository at this point in the history
This allows using the `hatch version` tool to automatically update
versions and makes sure the version metadata of editable installs is
always up to date.
  • Loading branch information
gotmax23 committed Nov 22, 2023
1 parent aa1dadf commit b328ee7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
26 changes: 8 additions & 18 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,17 +289,6 @@ def check_package_files(
)


def _repl_version(session: nox.Session, new_version: str):
with open("pyproject.toml", "r+") as fp:
lines = tuple(fp)
fp.seek(0)
for line in lines:
if line.startswith("version = "):
line = f'version = "{new_version}"\n'
fp.write(line)
fp.truncate()


def check_no_modifications(session: nox.Session) -> None:
modified = session.run(
"git",
Expand Down Expand Up @@ -356,7 +345,7 @@ def bump(session: nox.Session):
"or two positional arguments must be provided."
)
install(session, "antsibull-changelog[toml]", "hatch")
_repl_version(session, version)
session.run("hatch", "version", version)
if len(session.posargs) > 1:
fragment = session.run(
"python",
Expand All @@ -367,18 +356,20 @@ def bump(session: nox.Session):
)
with open(fragment_file, "w") as fp:
print(fragment, file=fp)
session.run("git", "add", "pyproject.toml", str(fragment_file), external=True)
session.run(
"git", "add", "src/antsibull/__init__.py", str(fragment_file), external=True
)
session.run("git", "commit", "-m", f"Prepare {version}.", external=True)
session.run("antsibull-changelog", "release")
session.run("antsibull-changelog", "release", "--version", version)
session.run(
"git",
"add",
"CHANGELOG.rst",
"changelogs/changelog.yaml",
"changelogs/fragments/",
# pyproject.toml is not committed in the last step
# src/antsibull/__init__.py is not committed in the last step
# when the release_summary fragment is created manually
"pyproject.toml",
"src/antsibull/__init__.py",
external=True,
)
install(session, ".") # Smoke test
Expand All @@ -403,8 +394,7 @@ def publish(session: nox.Session):
check_no_modifications(session)
install(session, "hatch")
session.run("hatch", "publish", *session.posargs)
version = session.run("hatch", "version", silent=True).strip()
_repl_version(session, f"{version}.post0")
session.run("hatch", "version", "post")
session.run("git", "add", "pyproject.toml", external=True)
session.run("git", "commit", "-m", "Post-release version bump.", external=True)

Expand Down
7 changes: 6 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ build-backend = "hatchling.build"

[project]
name = "antsibull"
version = "0.59.1.post0"
dynamic = [
"version",
]
description = "Tools for building the Ansible Distribution"
license = "GPL-3.0-or-later"
license-files = {globs=["LICENSES/*.txt"]}
Expand Down Expand Up @@ -101,6 +103,9 @@ dev = [
"nox",
]

[tool.hatch.version]
path = "src/antsibull/__init__.py"

[tool.isort]
profile = "black"
extend_skip = [
Expand Down
8 changes: 8 additions & 0 deletions src/antsibull/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
# https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: Ansible Project, 2020

"""
Tools for building the Ansible Distribution
"""

from __future__ import annotations

__version__ = "0.59.1.post0"
4 changes: 4 additions & 0 deletions src/antsibull/cli/antsibull_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,10 +323,14 @@ def parse_args(program_name: str, args: list[str]) -> argparse.Namespace:
" is $BASENAME_OF_BUILD_FILE-X.Y.Z.yaml",
)

# Delay import to avoid potential import loops
from antsibull import __version__ as _ver # pylint: disable=import-outside-toplevel

parser = get_toplevel_parser(
prog=program_name,
package="antsibull",
description="Script to manage building Ansible",
package_version=_ver,
)

subparsers = parser.add_subparsers(
Expand Down

0 comments on commit b328ee7

Please sign in to comment.