Skip to content

Commit 73830ba

Browse files
resolve conversation
1 parent efd22a9 commit 73830ba

File tree

10 files changed

+318
-335
lines changed

10 files changed

+318
-335
lines changed

exasol/toolbox/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from argparse import ArgumentTypeError
22

3-
from exasol.toolbox.release import Version
3+
from exasol.toolbox.version import Version
44

55

66
def version(arg: str) -> Version:

exasol/toolbox/nox/_package_version.py

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
11
import argparse
2-
import subprocess
32
import sys
43
from argparse import (
54
ArgumentParser,
65
Namespace,
76
)
8-
from collections import namedtuple
97
from inspect import cleandoc
108
from pathlib import Path
11-
from shutil import which
12-
from typing import Any
139

1410
import nox
1511
from nox import Session
1612

17-
Version = namedtuple("Version", ["major", "minor", "patch"])
13+
from exasol.toolbox.error import ToolboxError
14+
from exasol.toolbox.version import Version
1815

1916
_SUCCESS = 0
2017
_FAILURE = 1
@@ -36,47 +33,10 @@
3633
# fmt: on
3734

3835

39-
def version_from_string(s: str) -> Version:
40-
"""Converts a version string of the following format major.minor.patch to a version object"""
41-
major, minor, patch = (int(number, base=0) for number in s.split("."))
42-
return Version(major, minor, patch)
43-
44-
45-
class CommitHookError(Exception):
46-
"""Indicates that this commit hook encountered an error"""
47-
48-
49-
def version_from_python_module(path: Path) -> Version:
50-
"""Retrieve version information from the `version` module"""
51-
with open(path, encoding="utf-8") as file:
52-
_locals: dict[str, Any] = {}
53-
_globals: dict[str, Any] = {}
54-
exec(file.read(), _locals, _globals)
55-
56-
try:
57-
version = _globals["VERSION"]
58-
except KeyError as ex:
59-
raise CommitHookError("Couldn't find version within module") from ex
60-
61-
return version_from_string(version)
62-
63-
64-
def version_from_poetry() -> Version:
65-
poetry = which("poetry")
66-
if not poetry:
67-
raise CommitHookError("Couldn't find poetry executable")
68-
69-
result = subprocess.run(
70-
[poetry, "version", "--no-ansi"], capture_output=True, check=False
71-
)
72-
version = result.stdout.decode().split()[1]
73-
return version_from_string(version)
74-
75-
7636
def write_version_module(version: Version, path: str, exists_ok: bool = True) -> None:
7737
version_file = Path(path)
7838
if version_file.exists() and not exists_ok:
79-
raise CommitHookError(f"Version file [{version_file}] already exists.")
39+
raise ToolboxError(f"Version file [{version_file}] already exists.")
8040
version_file.unlink(missing_ok=True)
8141
with open(version_file, "w", encoding="utf-8") as f:
8242
f.write(
@@ -111,8 +71,8 @@ def _create_parser() -> ArgumentParser:
11171

11272

11373
def _main_debug(args: Namespace) -> int:
114-
module_version = version_from_python_module(args.version_module)
115-
poetry_version = version_from_poetry()
74+
module_version = Version.from_python_module(args.version_module)
75+
poetry_version = Version.from_poetry()
11676

11777
if args.fix:
11878
write_version_module(poetry_version, args.version_module)

exasol/toolbox/nox/_release.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
)
1515
from exasol.toolbox.nox.plugin import NoxTasks
1616
from exasol.toolbox.release import (
17-
ReleaseTypes,
18-
Version,
1917
extract_release_notes,
2018
new_changelog,
2119
new_changes,
2220
new_unreleased,
2321
)
22+
from exasol.toolbox.version import (
23+
ReleaseTypes,
24+
Version,
25+
)
2426
from noxconfig import PROJECT_CONFIG
2527

2628

exasol/toolbox/release/__init__.py

Lines changed: 2 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,10 @@
11
from __future__ import annotations
22

3-
import subprocess
4-
from dataclasses import dataclass
53
from datetime import datetime
6-
from enum import Enum
7-
from functools import (
8-
total_ordering,
9-
wraps,
10-
)
114
from inspect import cleandoc
125
from pathlib import Path
13-
from shutil import which
14-
15-
from exasol.toolbox.error import ToolboxError
16-
17-
18-
def _index_or(container, index, default):
19-
try:
20-
return container[index]
21-
except IndexError:
22-
return default
23-
24-
25-
class ReleaseTypes(Enum):
26-
Major = "major"
27-
Minor = "minor"
28-
Patch = "patch"
29-
30-
def __str__(self):
31-
return self.name.lower()
32-
33-
34-
def poetry_command(func):
35-
@wraps(func)
36-
def wrapper(*args, **kwargs):
37-
cmd = which("poetry")
38-
if not cmd:
39-
raise ToolboxError("Couldn't find poetry executable")
40-
try:
41-
return func(*args, **kwargs)
42-
except subprocess.CalledProcessError as ex:
43-
raise ToolboxError(f"Failed to execute: {ex.cmd}") from ex
44-
45-
return wrapper
46-
47-
48-
@total_ordering
49-
@dataclass(frozen=True)
50-
class Version:
51-
major: int
52-
minor: int
53-
patch: int
54-
55-
def __str__(self):
56-
return f"{self.major}.{self.minor}.{self.patch}"
57-
58-
def __lt__(self, other: object):
59-
if not isinstance(other, Version):
60-
return NotImplemented
61-
return (
62-
self.major < other.major
63-
or (self.major <= other.major and self.minor < other.minor)
64-
or (
65-
self.major <= other.major
66-
and self.minor <= other.minor
67-
and self.patch < other.patch
68-
)
69-
)
70-
71-
def __eq__(self, other: object):
72-
if not isinstance(other, Version):
73-
return NotImplemented
74-
return (
75-
self.major == other.major
76-
and self.minor == other.minor
77-
and self.patch == other.patch
78-
)
79-
80-
@staticmethod
81-
def from_string(version):
82-
parts = [int(number, base=0) for number in version.split(".")]
83-
if len(parts) > 3:
84-
raise ValueError(
85-
"Version has an invalid format, "
86-
f"expected: '<major>.<minor>.<patch>', actual: '{version}'"
87-
)
88-
version = [_index_or(parts, i, 0) for i in range(3)]
89-
return Version(*version)
90-
91-
@staticmethod
92-
@poetry_command
93-
def from_poetry():
94-
output = subprocess.run(
95-
["poetry", "version", "--no-ansi", "--short"],
96-
capture_output=True,
97-
text=True,
98-
)
99-
return Version.from_string(output.stdout.strip())
100-
101-
@staticmethod
102-
@poetry_command
103-
def upgrade_version_from_poetry(t: ReleaseTypes):
104-
output = subprocess.run(
105-
["poetry", "version", str(t), "--dry-run", "--no-ansi", "--short"],
106-
capture_output=True,
107-
text=True,
108-
)
109-
return Version.from_string(output.stdout.strip())
6+
7+
from exasol.toolbox.version import Version
1108

1119

11210
def extract_release_notes(file: str | Path) -> str:

exasol/toolbox/sphinx/multiversion/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
from sphinx import config as sphinx_config
2222
from sphinx import project as sphinx_project
2323

24-
from exasol.toolbox.release import Version as ExasolVersion
2524
from exasol.toolbox.sphinx.multiversion import (
2625
git,
2726
sphinx,
2827
)
28+
from exasol.toolbox.version import Version as ExasolVersion
2929

3030
logging.basicConfig(
3131
level="INFO",

exasol/toolbox/sphinx/multiversion/sphinx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from sphinx.locale import _
1010
from sphinx.util import i18n as sphinx_i18n
1111

12-
from exasol.toolbox.release import Version as ExasolVersion
1312
from exasol.toolbox.version import VERSION as PLUGIN_VERSION
13+
from exasol.toolbox.version import Version as ExasolVersion
1414

1515
logger = logging.getLogger(__name__)
1616

exasol/toolbox/version/__init__.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
from __future__ import annotations
2+
3+
import subprocess
4+
from dataclasses import dataclass
5+
from enum import Enum
6+
from functools import (
7+
total_ordering,
8+
wraps,
9+
)
10+
from pathlib import Path
11+
from shutil import which
12+
from typing import Any
13+
14+
from exasol.toolbox.error import ToolboxError
15+
16+
17+
def _index_or(container, index, default):
18+
try:
19+
return container[index]
20+
except IndexError:
21+
return default
22+
23+
24+
class ReleaseTypes(Enum):
25+
Major = "major"
26+
Minor = "minor"
27+
Patch = "patch"
28+
29+
def __str__(self):
30+
return self.name.lower()
31+
32+
33+
def poetry_command(func):
34+
@wraps(func)
35+
def wrapper(*args, **kwargs):
36+
cmd = which("poetry")
37+
if not cmd:
38+
raise ToolboxError("Couldn't find poetry executable")
39+
try:
40+
return func(*args, **kwargs)
41+
except subprocess.CalledProcessError as ex:
42+
raise ToolboxError(f"Failed to execute: {ex.cmd}") from ex
43+
44+
return wrapper
45+
46+
47+
@total_ordering
48+
@dataclass(frozen=True)
49+
class Version:
50+
major: int
51+
minor: int
52+
patch: int
53+
54+
def __str__(self):
55+
return f"{self.major}.{self.minor}.{self.patch}"
56+
57+
def __lt__(self, other: object):
58+
if not isinstance(other, Version):
59+
return NotImplemented
60+
return (
61+
self.major < other.major
62+
or (self.major <= other.major and self.minor < other.minor)
63+
or (
64+
self.major <= other.major
65+
and self.minor <= other.minor
66+
and self.patch < other.patch
67+
)
68+
)
69+
70+
def __eq__(self, other: object):
71+
if not isinstance(other, Version):
72+
return NotImplemented
73+
return (
74+
self.major == other.major
75+
and self.minor == other.minor
76+
and self.patch == other.patch
77+
)
78+
79+
@staticmethod
80+
def from_string(version):
81+
parts = [int(number, base=0) for number in version.split(".")]
82+
if len(parts) > 3:
83+
raise ValueError(
84+
"Version has an invalid format, "
85+
f"expected: '<major>.<minor>.<patch>', actual: '{version}'"
86+
)
87+
version = [_index_or(parts, i, 0) for i in range(3)]
88+
return Version(*version)
89+
90+
@staticmethod
91+
@poetry_command
92+
def from_poetry():
93+
output = subprocess.run(
94+
["poetry", "version", "--no-ansi", "--short"],
95+
capture_output=True,
96+
text=True,
97+
)
98+
return Version.from_string(output.stdout.strip())
99+
100+
@staticmethod
101+
@poetry_command
102+
def upgrade_version_from_poetry(t: ReleaseTypes):
103+
output = subprocess.run(
104+
["poetry", "version", str(t), "--dry-run", "--no-ansi", "--short"],
105+
capture_output=True,
106+
text=True,
107+
)
108+
return Version.from_string(output.stdout.strip())
109+
110+
@staticmethod
111+
def from_python_module(path: Path) -> Version:
112+
"""Retrieve version information from the `version` module"""
113+
with open(path, encoding="utf-8") as file:
114+
_locals: dict[str, Any] = {}
115+
_globals: dict[str, Any] = {}
116+
exec(file.read(), _locals, _globals)
117+
118+
try:
119+
version = _globals["VERSION"]
120+
except KeyError as ex:
121+
raise ToolboxError("Couldn't find version within module") from ex
122+
123+
return Version.from_string(version)

test/unit/cli_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import pytest
44

55
from exasol.toolbox.cli import version
6-
from exasol.toolbox.release import Version
6+
from exasol.toolbox.version import Version
77

88

99
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)