1- import subprocess
1+ import argparse
22import sys
33from argparse import (
44 ArgumentParser ,
55 Namespace ,
66)
7- from collections import namedtuple
8- from collections .abc import Iterable
97from inspect import cleandoc
108from pathlib import Path
11- from shutil import which
12- from typing import (
13- Any ,
14- Dict ,
15- Union ,
16- )
179
18- Version = namedtuple ("Version" , ["major" , "minor" , "patch" ])
10+ import nox
11+ from nox import Session
12+
13+ from exasol .toolbox .error import ToolboxError
14+ from exasol .toolbox .util .version import Version
1915
2016_SUCCESS = 0
2117_FAILURE = 1
2218
2319# fmt: off
2420_VERSION_MODULE_TEMPLATE = cleandoc ('''
2521 # ATTENTION:
26- # This file is generated by exasol/toolbox/pre_commit_hooks/package_version .py when using:
22+ # This file is generated by exasol/toolbox/nox/_package_version .py when using:
2723 # * either "poetry run -- nox -s project:fix"
28- # * or "poetry run -- version- check <path/version.py> --fix"
24+ # * or "poetry run -- nox version: check -- <path/version.py> --fix"
2925 # Do not edit this file manually!
30- # If you need to change the version, do so in the project .toml, e.g. by using `poetry version X.Y.Z`.
26+ # If you need to change the version, do so in the pyproject .toml, e.g. by using `poetry version X.Y.Z`.
3127 MAJOR = {major}
3228 MINOR = {minor}
3329 PATCH = {patch}
3733# fmt: on
3834
3935
40- def version_from_string (s : str ) -> Version :
41- """Converts a version string of the following format major.minor.patch to a version object"""
42- major , minor , patch = (int (number , base = 0 ) for number in s .split ("." ))
43- return Version (major , minor , patch )
44-
45-
46- class CommitHookError (Exception ):
47- """Indicates that this commit hook encountered an error"""
48-
49-
50- def version_from_python_module (path : Path ) -> Version :
51- """Retrieve version information from the `version` module"""
52- with open (path , encoding = "utf-8" ) as file :
53- _locals : dict [str , Any ] = {}
54- _globals : dict [str , Any ] = {}
55- exec (file .read (), _locals , _globals )
56-
57- try :
58- version = _globals ["VERSION" ]
59- except KeyError as ex :
60- raise CommitHookError ("Couldn't find version within module" ) from ex
61-
62- return version_from_string (version )
63-
64-
65- def version_from_poetry () -> Version :
66- poetry = which ("poetry" )
67- if not poetry :
68- raise CommitHookError ("Couldn't find poetry executable" )
69-
70- result = subprocess .run (
71- [poetry , "version" , "--no-ansi" ], capture_output = True , check = False
72- )
73- version = result .stdout .decode ().split ()[1 ]
74- return version_from_string (version )
75-
76-
7736def write_version_module (version : Version , path : str , exists_ok : bool = True ) -> None :
7837 version_file = Path (path )
7938 if version_file .exists () and not exists_ok :
80- raise CommitHookError (f"Version file [{ version_file } ] already exists." )
39+ raise ToolboxError (f"Version file [{ version_file } ] already exists." )
8140 version_file .unlink (missing_ok = True )
8241 with open (version_file , "w" , encoding = "utf-8" ) as f :
8342 f .write (
@@ -88,7 +47,10 @@ def write_version_module(version: Version, path: str, exists_ok: bool = True) ->
8847
8948
9049def _create_parser () -> ArgumentParser :
91- parser = ArgumentParser ()
50+ parser = ArgumentParser (
51+ prog = "nox -s version:check --" ,
52+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
53+ )
9254 parser .add_argument ("version_module" , help = "Path to version module" )
9355 parser .add_argument ("files" , nargs = "*" )
9456 parser .add_argument (
@@ -109,13 +71,13 @@ def _create_parser() -> ArgumentParser:
10971
11072
11173def _main_debug (args : Namespace ) -> int :
112- module_version = version_from_python_module (args .version_module )
113- poetry_version = version_from_poetry ()
74+ module_version = Version . from_python_module (args .version_module )
75+ poetry_version = Version . from_poetry ()
11476
11577 if args .fix :
11678 write_version_module (poetry_version , args .version_module )
11779
118- if not module_version = = poetry_version :
80+ if module_version ! = poetry_version :
11981 print (
12082 f"Version in pyproject.toml { poetry_version } and { args .version_module } { module_version } do not match!"
12183 )
@@ -138,12 +100,11 @@ def _main(args: Namespace) -> int:
138100 return _FAILURE
139101
140102
141- def main (argv : Union [Iterable [str ], None ] = None ) -> int :
103+ @nox .session (name = "version:check" , python = False )
104+ def version_check (session : Session ) -> None :
105+ """"""
142106 parser = _create_parser ()
143- args = parser .parse_args ()
107+ args = parser .parse_args (session . posargs )
144108 entry_point = _main if not args .debug else _main_debug
145- return entry_point (args )
146-
147-
148- if __name__ == "__main__" :
149- sys .exit (main ())
109+ if entry_point (args ):
110+ session .error ()
0 commit comments