Skip to content

Commit 16ee3bb

Browse files
version-check was refactored into nox task and its usage changed
1 parent 586d220 commit 16ee3bb

File tree

7 files changed

+163
-7
lines changed

7 files changed

+163
-7
lines changed

.github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
uses: ./.github/actions/python-environment
2020

2121
- name: Check Version(s)
22-
run: poetry run version-check `poetry run -- python -c "from noxconfig import PROJECT_CONFIG; print(PROJECT_CONFIG.version_file)"`
22+
run: poetry run nox -s version:check -- `poetry run -- python -c "from noxconfig import PROJECT_CONFIG; print(PROJECT_CONFIG.version_file)"`
2323

2424
Documentation:
2525
name: Docs
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import argparse
2+
import subprocess
3+
import sys
4+
from argparse import (
5+
ArgumentParser,
6+
Namespace,
7+
)
8+
from collections import namedtuple
9+
from collections.abc import Iterable
10+
from inspect import cleandoc
11+
from pathlib import Path
12+
from shutil import which
13+
from typing import (
14+
Any,
15+
Dict,
16+
Union,
17+
)
18+
19+
import nox
20+
from nox import Session
21+
22+
Version = namedtuple("Version", ["major", "minor", "patch"])
23+
24+
_SUCCESS = 0
25+
_FAILURE = 1
26+
27+
# fmt: off
28+
_VERSION_MODULE_TEMPLATE = cleandoc('''
29+
# ATTENTION:
30+
# This file is generated by exasol/toolbox/nox/_package_version.py when using:
31+
# * either "poetry run -- nox -s project:fix"
32+
# * or "poetry run -- nox version:check <path/version.py> --fix"
33+
# Do not edit this file manually!
34+
# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`.
35+
MAJOR = {major}
36+
MINOR = {minor}
37+
PATCH = {patch}
38+
VERSION = f"{{MAJOR}}.{{MINOR}}.{{PATCH}}"
39+
__version__ = VERSION
40+
''') + "\n"
41+
# fmt: on
42+
43+
44+
def version_from_string(s: str) -> Version:
45+
"""Converts a version string of the following format major.minor.patch to a version object"""
46+
major, minor, patch = (int(number, base=0) for number in s.split("."))
47+
return Version(major, minor, patch)
48+
49+
50+
class CommitHookError(Exception):
51+
"""Indicates that this commit hook encountered an error"""
52+
53+
54+
def version_from_python_module(path: Path) -> Version:
55+
"""Retrieve version information from the `version` module"""
56+
with open(path, encoding="utf-8") as file:
57+
_locals: dict[str, Any] = {}
58+
_globals: dict[str, Any] = {}
59+
exec(file.read(), _locals, _globals)
60+
61+
try:
62+
version = _globals["VERSION"]
63+
except KeyError as ex:
64+
raise CommitHookError("Couldn't find version within module") from ex
65+
66+
return version_from_string(version)
67+
68+
69+
def version_from_poetry() -> Version:
70+
poetry = which("poetry")
71+
if not poetry:
72+
raise CommitHookError("Couldn't find poetry executable")
73+
74+
result = subprocess.run(
75+
[poetry, "version", "--no-ansi"], capture_output=True, check=False
76+
)
77+
version = result.stdout.decode().split()[1]
78+
return version_from_string(version)
79+
80+
81+
def write_version_module(version: Version, path: str, exists_ok: bool = True) -> None:
82+
version_file = Path(path)
83+
if version_file.exists() and not exists_ok:
84+
raise CommitHookError(f"Version file [{version_file}] already exists.")
85+
version_file.unlink(missing_ok=True)
86+
with open(version_file, "w", encoding="utf-8") as f:
87+
f.write(
88+
_VERSION_MODULE_TEMPLATE.format(
89+
major=version.major, minor=version.minor, patch=version.patch
90+
)
91+
)
92+
93+
94+
def _create_parser() -> ArgumentParser:
95+
parser = ArgumentParser(
96+
prog="nox -s version:check",
97+
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
98+
)
99+
parser.add_argument("version_module", help="Path to version module")
100+
parser.add_argument("files", nargs="*")
101+
parser.add_argument(
102+
"-d",
103+
"--debug",
104+
action="store_true",
105+
default=False,
106+
help="enabled debug mode for execution.",
107+
)
108+
parser.add_argument(
109+
"-f",
110+
"--fix",
111+
action="store_true",
112+
default=False,
113+
help="fix instead of check.",
114+
)
115+
return parser
116+
117+
118+
def _main_debug(args: Namespace) -> int:
119+
module_version = version_from_python_module(args.version_module)
120+
poetry_version = version_from_poetry()
121+
122+
if args.fix:
123+
write_version_module(poetry_version, args.version_module)
124+
125+
if not module_version == poetry_version:
126+
print(
127+
f"Version in pyproject.toml {poetry_version} and {args.version_module} {module_version} do not match!"
128+
)
129+
if args.fix:
130+
print(
131+
f"Updating version in file ({args.version_module}) from {module_version} to {poetry_version}"
132+
)
133+
return _SUCCESS
134+
135+
return _FAILURE
136+
137+
return _SUCCESS
138+
139+
140+
def _main(args: Namespace) -> int:
141+
try:
142+
return _main_debug(args)
143+
except Exception as ex:
144+
print(f"Error while executing program, details: {ex}", file=sys.stderr)
145+
return _FAILURE
146+
147+
148+
@nox.session(name="version:check", python=False)
149+
def version_check(session: Session) -> None:
150+
""""""
151+
parser = _create_parser()
152+
args = parser.parse_args(session.posargs)
153+
entry_point = _main if not args.debug else _main_debug
154+
if entry_point(args):
155+
session.error()

exasol/toolbox/nox/_shared.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ def _deny_filter(files: Iterable[Path], deny_list: Iterable[str]) -> Iterable[Pa
4747

4848

4949
def _version(session: Session, mode: Mode, version_file: Path) -> None:
50-
command = ["version-check"]
51-
command = command if mode == Mode.Check else command + ["--fix"]
50+
command = ["nox", "-s", "version:check"]
51+
command = command if mode == Mode.Check else command + ["--"] + ["--fix"]
5252
session.run(*command, f"{version_file}")
5353

5454

exasol/toolbox/nox/tasks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,7 @@ def check(session: Session) -> None:
8585
audit
8686
)
8787

88+
from exasol.toolbox.nox._package_version import version_check
89+
8890
# isort: on
8991
# fmt: on

exasol/toolbox/templates/github/workflows/checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
run: |
2323
echo "Please enable the version check by replacing this output with shell command bellow:"
2424
echo ""
25-
echo "poetry run -- version-check <<VERSION_PY>>"
25+
echo "poetry run nox -s version:check -- <<VERSION_PY>>"
2626
echo ""
2727
echo "Note: <<VERSION_PY>> needs to point to the version file of the project (version.py)."
2828
exit 1

exasol/toolbox/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ATTENTION:
2-
# This file is generated by exasol/toolbox/pre_commit_hooks/package_version.py when using:
2+
# This file is generated by exasol/toolbox/nox/_package_version.py when using:
33
# * either "poetry run -- nox -s project:fix"
4-
# * or "poetry run -- version-check <path/version.py> --fix"
4+
# * or "poetry run -- nox version:check <path/version.py> --fix"
55
# Do not edit this file manually!
66
# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`.
77
MAJOR = 1

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,5 @@ module = [
110110
ignore_errors = true
111111

112112
[tool.poetry.plugins."console_scripts"]
113-
version-check = "exasol.toolbox.pre_commit_hooks.package_version:main"
114113
tbx = 'exasol.toolbox.tools.tbx:CLI'
115114
sphinx-multiversion = 'exasol.toolbox.sphinx.multiversion:main'

0 commit comments

Comments
 (0)