-
Notifications
You must be signed in to change notification settings - Fork 1
Refactoring/#392 refactor pre commit hook package version.py into nox task #412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
16ee3bb
9f67bf3
a1527c1
282bca1
efd22a9
73830ba
68ab863
2974ddb
832e59d
4d33ae9
eecf9fe
5fc4cc2
533e9b2
39956f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -2,4 +2,8 @@ | |||||
|
||||||
## ✨ Features | ||||||
|
||||||
* [#378](https://github.com/exasol/python-toolbox/pull/378/files): Add Nox task to trigger a release | ||||||
* [#378](https://github.com/exasol/python-toolbox/pull/378/files): Add Nox task to trigger a release | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad 😞 should have caught last time. These should refer to the issue number.
Suggested change
|
||||||
|
||||||
## ⚒️ Refactorings | ||||||
|
||||||
* [#412](https://github.com/exasol/python-toolbox/pull/412): Refactor pre commit hook package version.py into nox task | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import subprocess | ||
import argparse | ||
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import sys | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the following:
Please update:
|
||
from argparse import ( | ||
ArgumentParser, | ||
Namespace, | ||
) | ||
from collections import namedtuple | ||
from collections.abc import Iterable | ||
from inspect import cleandoc | ||
from pathlib import Path | ||
from shutil import which | ||
from typing import ( | ||
Any, | ||
Dict, | ||
Union, | ||
) | ||
|
||
Version = namedtuple("Version", ["major", "minor", "patch"]) | ||
import nox | ||
ArBridgeman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
from nox import Session | ||
|
||
from exasol.toolbox.error import ToolboxError | ||
from exasol.toolbox.util.version import Version | ||
|
||
_SUCCESS = 0 | ||
_FAILURE = 1 | ||
|
||
# fmt: off | ||
_VERSION_MODULE_TEMPLATE = cleandoc(''' | ||
# ATTENTION: | ||
# This file is generated by exasol/toolbox/pre_commit_hooks/package_version.py when using: | ||
# This file is generated by exasol/toolbox/nox/_package_version.py when using: | ||
# * either "poetry run -- nox -s project:fix" | ||
# * or "poetry run -- version-check <path/version.py> --fix" | ||
# * or "poetry run -- nox version:check -- <path/version.py> --fix" | ||
# Do not edit this file manually! | ||
# If you need to change the version, do so in the project.toml, e.g. by using `poetry version X.Y.Z`. | ||
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`. | ||
MAJOR = {major} | ||
MINOR = {minor} | ||
PATCH = {patch} | ||
|
@@ -37,47 +33,10 @@ | |
# fmt: on | ||
|
||
|
||
def version_from_string(s: str) -> Version: | ||
"""Converts a version string of the following format major.minor.patch to a version object""" | ||
major, minor, patch = (int(number, base=0) for number in s.split(".")) | ||
return Version(major, minor, patch) | ||
|
||
|
||
class CommitHookError(Exception): | ||
"""Indicates that this commit hook encountered an error""" | ||
|
||
|
||
def version_from_python_module(path: Path) -> Version: | ||
"""Retrieve version information from the `version` module""" | ||
with open(path, encoding="utf-8") as file: | ||
_locals: dict[str, Any] = {} | ||
_globals: dict[str, Any] = {} | ||
exec(file.read(), _locals, _globals) | ||
|
||
try: | ||
version = _globals["VERSION"] | ||
except KeyError as ex: | ||
raise CommitHookError("Couldn't find version within module") from ex | ||
|
||
return version_from_string(version) | ||
|
||
|
||
def version_from_poetry() -> Version: | ||
poetry = which("poetry") | ||
if not poetry: | ||
raise CommitHookError("Couldn't find poetry executable") | ||
|
||
result = subprocess.run( | ||
[poetry, "version", "--no-ansi"], capture_output=True, check=False | ||
) | ||
version = result.stdout.decode().split()[1] | ||
return version_from_string(version) | ||
|
||
|
||
def write_version_module(version: Version, path: str, exists_ok: bool = True) -> None: | ||
version_file = Path(path) | ||
if version_file.exists() and not exists_ok: | ||
raise CommitHookError(f"Version file [{version_file}] already exists.") | ||
raise ToolboxError(f"Version file [{version_file}] already exists.") | ||
version_file.unlink(missing_ok=True) | ||
with open(version_file, "w", encoding="utf-8") as f: | ||
f.write( | ||
|
@@ -88,7 +47,10 @@ def write_version_module(version: Version, path: str, exists_ok: bool = True) -> | |
|
||
|
||
def _create_parser() -> ArgumentParser: | ||
parser = ArgumentParser() | ||
parser = ArgumentParser( | ||
prog="nox -s version:check --", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
) | ||
Jannis-Mittenzwei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser.add_argument("version_module", help="Path to version module") | ||
parser.add_argument("files", nargs="*") | ||
parser.add_argument( | ||
|
@@ -109,13 +71,13 @@ def _create_parser() -> ArgumentParser: | |
|
||
|
||
def _main_debug(args: Namespace) -> int: | ||
module_version = version_from_python_module(args.version_module) | ||
poetry_version = version_from_poetry() | ||
module_version = Version.from_python_module(args.version_module) | ||
poetry_version = Version.from_poetry() | ||
|
||
if args.fix: | ||
write_version_module(poetry_version, args.version_module) | ||
|
||
if not module_version == poetry_version: | ||
if module_version != poetry_version: | ||
print( | ||
f"Version in pyproject.toml {poetry_version} and {args.version_module} {module_version} do not match!" | ||
) | ||
|
@@ -138,12 +100,11 @@ def _main(args: Namespace) -> int: | |
return _FAILURE | ||
|
||
|
||
def main(argv: Union[Iterable[str], None] = None) -> int: | ||
@nox.session(name="version:check", python=False) | ||
def version_check(session: Session) -> None: | ||
"""""" | ||
parser = _create_parser() | ||
args = parser.parse_args() | ||
args = parser.parse_args(session.posargs) | ||
entry_point = _main if not args.debug else _main_debug | ||
return entry_point(args) | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) | ||
if entry_point(args): | ||
session.error() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a brief description to #392 answering: