|
| 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() |
0 commit comments