Skip to content

Commit 4d33ae9

Browse files
fix
1 parent 832e59d commit 4d33ae9

File tree

13 files changed

+144
-132
lines changed

13 files changed

+144
-132
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.util 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from nox import Session
1212

1313
from exasol.toolbox.error import ToolboxError
14-
from exasol.toolbox.util import Version
14+
from exasol.toolbox.version import Version
1515

1616
_SUCCESS = 0
1717
_FAILURE = 1

exasol/toolbox/nox/_release.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
new_changes,
2020
new_unreleased,
2121
)
22-
from exasol.toolbox.util import ReleaseTypes, Version
22+
from exasol.toolbox.version import (
23+
ReleaseTypes,
24+
Version,
25+
)
2326
from noxconfig import PROJECT_CONFIG
2427

2528

exasol/toolbox/release/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from inspect import cleandoc
55
from pathlib import Path
66

7-
from exasol.toolbox.util import Version
7+
from exasol.toolbox.version import Version
88

99

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

exasol/toolbox/sphinx/multiversion/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
from exasol.toolbox.sphinx.multiversion.main import main
1919
from exasol.toolbox.sphinx.multiversion.sphinx import setup
20-
from exasol.toolbox.util import Version
20+
from exasol.toolbox.util.version import VERSION
2121

22-
__version__ = Version
22+
__version__ = VERSION
2323

2424
__all__ = [
2525
"setup",

exasol/toolbox/sphinx/multiversion/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
git,
2626
sphinx,
2727
)
28-
from exasol.toolbox.util import Version as ExasolVersion
28+
from exasol.toolbox.version import Version as ExasolVersion
2929

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

exasol/toolbox/sphinx/multiversion/sphinx.py

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

12-
from exasol.toolbox.util import Version as ExasolVersion, Version as PLUGIN_VERSION
12+
from exasol.toolbox.util.version import VERSION as PLUGIN_VERSION
13+
from exasol.toolbox.version import Version as ExasolVersion
1314

1415
logger = logging.getLogger(__name__)
1516

exasol/toolbox/util/__init__.py

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

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)

noxconfig.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ class Config:
4444
root: Path = Path(__file__).parent
4545
doc: Path = Path(__file__).parent / "doc"
4646
importlinter: Path = Path(__file__).parent / ".import_linter_config"
47-
version_file: Path = Path(__file__).parent / "exasol" / "toolbox" / "version.py"
47+
version_file: Path = (
48+
Path(__file__).parent / "exasol" / "toolbox" / "util" / "version.py"
49+
)
4850
path_filters: Iterable[str] = (
4951
"dist",
5052
".eggs",

0 commit comments

Comments
 (0)