1
- import subprocess
1
+ import argparse
2
2
import sys
3
3
from argparse import (
4
4
ArgumentParser ,
5
5
Namespace ,
6
6
)
7
- from collections import namedtuple
8
- from collections .abc import Iterable
9
7
from inspect import cleandoc
10
8
from pathlib import Path
11
- from shutil import which
12
- from typing import (
13
- Any ,
14
- Dict ,
15
- Union ,
16
- )
17
9
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
19
15
20
16
_SUCCESS = 0
21
17
_FAILURE = 1
22
18
23
19
# fmt: off
24
20
_VERSION_MODULE_TEMPLATE = cleandoc ('''
25
21
# 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:
27
23
# * 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"
29
25
# 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`.
31
27
MAJOR = {major}
32
28
MINOR = {minor}
33
29
PATCH = {patch}
37
33
# fmt: on
38
34
39
35
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
-
77
36
def write_version_module (version : Version , path : str , exists_ok : bool = True ) -> None :
78
37
version_file = Path (path )
79
38
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." )
81
40
version_file .unlink (missing_ok = True )
82
41
with open (version_file , "w" , encoding = "utf-8" ) as f :
83
42
f .write (
@@ -88,7 +47,10 @@ def write_version_module(version: Version, path: str, exists_ok: bool = True) ->
88
47
89
48
90
49
def _create_parser () -> ArgumentParser :
91
- parser = ArgumentParser ()
50
+ parser = ArgumentParser (
51
+ prog = "nox -s version:check --" ,
52
+ formatter_class = argparse .ArgumentDefaultsHelpFormatter ,
53
+ )
92
54
parser .add_argument ("version_module" , help = "Path to version module" )
93
55
parser .add_argument ("files" , nargs = "*" )
94
56
parser .add_argument (
@@ -109,13 +71,13 @@ def _create_parser() -> ArgumentParser:
109
71
110
72
111
73
def _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 ()
114
76
115
77
if args .fix :
116
78
write_version_module (poetry_version , args .version_module )
117
79
118
- if not module_version = = poetry_version :
80
+ if module_version ! = poetry_version :
119
81
print (
120
82
f"Version in pyproject.toml { poetry_version } and { args .version_module } { module_version } do not match!"
121
83
)
@@ -138,12 +100,11 @@ def _main(args: Namespace) -> int:
138
100
return _FAILURE
139
101
140
102
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
+ """"""
142
106
parser = _create_parser ()
143
- args = parser .parse_args ()
107
+ args = parser .parse_args (session . posargs )
144
108
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