Skip to content

Commit 097f0be

Browse files
authored
Feature/441 remove arg for version check (#443)
* Use config.version_file for version:check * Add tests for version:check * Update workflow & template with version:check changes
1 parent 5feb414 commit 097f0be

File tree

11 files changed

+114
-54
lines changed

11 files changed

+114
-54
lines changed

.github/workflows/checks.yml

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

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

2525
Documentation:
2626
name: Docs

doc/changes/unreleased.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
11
# Unreleased
22

3+
## Summary
4+
5+
With #441, please ensure that the location of the `version.py` is given for `Config.version_file`,
6+
which is specified in the `noxconfig.py`
7+
38
## 📚 Documentation
4-
* Updated getting_started.rst for allowing tag-based releases
9+
* Updated getting_started.rst for allowing tag-based releases
10+
11+
## ✨ Features
12+
13+
* [#441](https://github.com/exasol/python-toolbox/issues/441): Switched nox task for `version:check` to use the config value of `version_file` to specify the location of the `version.py`

exasol/toolbox/nox/_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _pyupgrade(session: Session, files: Iterable[str]) -> None:
3636
def fix(session: Session) -> None:
3737
"""Runs all automated fixes on the code base"""
3838
py_files = [f"{file}" for file in python_files(PROJECT_CONFIG.root)]
39-
_version(session, Mode.Fix, PROJECT_CONFIG.version_file)
39+
_version(session, Mode.Fix)
4040
_pyupgrade(session, py_files)
4141
_code_format(session, Mode.Fix, py_files)
4242

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import argparse
2-
import sys
32
from argparse import (
43
ArgumentParser,
54
Namespace,
@@ -10,8 +9,11 @@
109
import nox
1110
from nox import Session
1211

13-
from exasol.toolbox.error import ToolboxError
1412
from exasol.toolbox.util.version import Version
13+
from noxconfig import (
14+
PROJECT_CONFIG,
15+
Config,
16+
)
1517

1618
_SUCCESS = 0
1719
_FAILURE = 1
@@ -21,7 +23,7 @@
2123
# ATTENTION:
2224
# This file is generated by exasol/toolbox/nox/_package_version.py when using:
2325
# * either "poetry run -- nox -s project:fix"
24-
# * or "poetry run -- nox version:check -- <path/version.py> --fix"
26+
# * or "poetry run -- nox version:check -- --fix"
2527
# Do not edit this file manually!
2628
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`.
2729
MAJOR = {major}
@@ -33,12 +35,8 @@
3335
# fmt: on
3436

3537

36-
def write_version_module(version: Version, path: str, exists_ok: bool = True) -> None:
37-
version_file = Path(path)
38-
if version_file.exists() and not exists_ok:
39-
raise ToolboxError(f"Version file [{version_file}] already exists.")
40-
version_file.unlink(missing_ok=True)
41-
with open(version_file, "w", encoding="utf-8") as f:
38+
def write_version_module(version: Version, version_file: Path) -> None:
39+
with version_file.open(mode="w", encoding="utf-8") as f:
4240
f.write(
4341
_VERSION_MODULE_TEMPLATE.format(
4442
major=version.major, minor=version.minor, patch=version.patch
@@ -51,8 +49,6 @@ def _create_parser() -> ArgumentParser:
5149
prog="nox -s version:check --",
5250
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
5351
)
54-
parser.add_argument("version_module", help="Path to version module")
55-
parser.add_argument("files", nargs="*")
5652
parser.add_argument(
5753
"-d",
5854
"--debug",
@@ -65,46 +61,39 @@ def _create_parser() -> ArgumentParser:
6561
"--fix",
6662
action="store_true",
6763
default=False,
68-
help="fix instead of check.",
64+
help="updates the `version.py`, instead of performing a check.",
6965
)
7066
return parser
7167

7268

73-
def _main_debug(args: Namespace) -> int:
74-
module_version = Version.from_python_module(args.version_module)
75-
poetry_version = Version.from_poetry()
69+
def _version_check(args: Namespace, config: Config) -> int:
70+
version_file = config.version_file
7671

72+
module_version = Version.from_python_module(version_file)
73+
poetry_version = Version.from_poetry()
7774
if args.fix:
78-
write_version_module(poetry_version, args.version_module)
75+
print(
76+
f"Updating version in {version_file} from {module_version} to {poetry_version}"
77+
)
78+
write_version_module(version=poetry_version, version_file=version_file)
79+
module_version = Version.from_python_module(version_file)
7980

8081
if module_version != poetry_version:
8182
print(
82-
f"Version in pyproject.toml {poetry_version} and {args.version_module} {module_version} do not match!"
83+
f"Version in pyproject.toml ({poetry_version}) and {version_file} ({module_version}) do not match!"
8384
)
84-
if args.fix:
85-
print(
86-
f"Updating version in file ({args.version_module}) from {module_version} to {poetry_version}"
87-
)
88-
return _SUCCESS
89-
9085
return _FAILURE
9186

9287
return _SUCCESS
9388

9489

95-
def _main(args: Namespace) -> int:
96-
try:
97-
return _main_debug(args)
98-
except Exception as ex:
99-
print(f"Error while executing program, details: {ex}", file=sys.stderr)
100-
return _FAILURE
101-
102-
10390
@nox.session(name="version:check", python=False)
10491
def version_check(session: Session) -> None:
105-
""""""
92+
"""
93+
Compare the version in the `version.py` to that
94+
declared in the `pyproject.toml`.
95+
"""
10696
parser = _create_parser()
10797
args = parser.parse_args(session.posargs)
108-
entry_point = _main if not args.debug else _main_debug
109-
if entry_point(args):
98+
if _version_check(args=args, config=PROJECT_CONFIG):
11099
session.error()

exasol/toolbox/nox/_release.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ def _create_parser() -> argparse.ArgumentParser:
4444
"--no-add",
4545
default=False,
4646
action="store_true",
47-
help=("Neither add nor commit the changes"),
47+
help="Neither add nor commit the changes",
4848
)
4949
parser.add_argument(
5050
"--no-branch",
5151
default=False,
5252
action="store_true",
53-
help=("Do not create a branch to commit the changes on"),
53+
help="Do not create a branch to commit the changes on",
5454
)
5555
parser.add_argument(
5656
"--no-pr",
5757
default=False,
5858
action="store_true",
59-
help=("Do not create a pull request for the changes"),
59+
help="Do not create a pull request for the changes",
6060
)
6161
return parser
6262

@@ -67,7 +67,7 @@ def _is_valid_version(old: Version, new: Version) -> bool:
6767

6868
def _update_project_version(session: Session, version: Version) -> Version:
6969
session.run("poetry", "version", f"{version}")
70-
_version(session, Mode.Fix, PROJECT_CONFIG.version_file)
70+
_version(session, Mode.Fix)
7171
return version
7272

7373

@@ -129,7 +129,7 @@ def run(*args: str):
129129

130130

131131
@nox.session(name="release:prepare", python=False)
132-
def prepare_release(session: Session, python=False) -> None:
132+
def prepare_release(session: Session) -> None:
133133
"""
134134
Prepares the project for a new release.
135135
"""

exasol/toolbox/nox/_shared.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ def python_files(project_root: Path) -> Iterable[Path]:
3636
return [path for path in files if not set(path.parts).intersection(deny_list)]
3737

3838

39-
def _version(session: Session, mode: Mode, version_file: Path) -> None:
39+
def _version(session: Session, mode: Mode) -> None:
4040
command = ["nox", "-s", "version:check", "--"]
4141
command = command if mode == Mode.Check else command + ["--fix"]
42-
session.run(*command, f"{version_file}")
42+
session.run(*command)
4343

4444

4545
def _context_parser() -> argparse.ArgumentParser:

exasol/toolbox/nox/tasks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def check(session: Session) -> None:
3535
"""Runs all available checks on the project"""
3636
context = _context(session, coverage=True)
3737
py_files = [f"{file}" for file in python_files(PROJECT_CONFIG.root)]
38-
_version(session, Mode.Check, PROJECT_CONFIG.version_file)
38+
_version(session, Mode.Check)
3939
_code_format(session, Mode.Check, py_files)
4040
_pylint(session, py_files)
4141
_type_check(session, py_files)

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

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

2121
- name: Check Version(s)
22-
run: |
23-
echo "Please enable the version check by replacing this output with shell command bellow:"
24-
echo ""
25-
echo "poetry run -- nox -s version:check -- <<VERSION_PY>>"
26-
echo ""
27-
echo "Note: <<VERSION_PY>> needs to point to the version file of the project (version.py)."
28-
exit 1
22+
run: poetry run -- nox -s version:check
2923

3024
Documentation:
3125
name: Docs

exasol/toolbox/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ATTENTION:
22
# 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 -- nox version:check -- <path/version.py> --fix"
4+
# * or "poetry run -- nox version:check -- --fix"
55
# Do not edit this file manually!
66
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`.
77
MAJOR = 1

project-template/{{cookiecutter.repo_name}}/exasol/{{cookiecutter.package_name}}/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# ATTENTION:
22
# 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 -- nox -s version:check -- <path/version.py> --fix"
4+
# * or "poetry run -- nox -s version:check -- --fix"
55
# Do not edit this file manually!
66
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`.
77
MAJOR = 0

0 commit comments

Comments
 (0)