11import argparse
2- import sys
32from argparse import (
43 ArgumentParser ,
54 Namespace ,
109import nox
1110from nox import Session
1211
13- from exasol .toolbox .error import ToolboxError
1412from exasol .toolbox .util .version import Version
13+ from noxconfig import (
14+ PROJECT_CONFIG ,
15+ Config ,
16+ )
1517
1618_SUCCESS = 0
1719_FAILURE = 1
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}
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 )
10491def 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 ()
0 commit comments