Skip to content

Commit 5fc4cc2

Browse files
fix
1 parent eecf9fe commit 5fc4cc2

File tree

12 files changed

+144
-30
lines changed

12 files changed

+144
-30
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.version import Version
3+
from exasol.toolbox.util.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.version import Version
14+
from exasol.toolbox.util.version import Version
1515

1616
_SUCCESS = 0
1717
_FAILURE = 1

exasol/toolbox/nox/_release.py

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

2825

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.version import Version
7+
from exasol.toolbox.util.version import Version
88

99

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

exasol/toolbox/sphinx/multiversion/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

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

2222
__version__ = VERSION
2323

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.version import Version as ExasolVersion
28+
from exasol.toolbox.util.version import Version as ExasolVersion
2929

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

exasol/toolbox/sphinx/multiversion/sphinx.py

Lines changed: 2 additions & 2 deletions
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.util.version import VERSION as PLUGIN_VERSION
13-
from exasol.toolbox.version import Version as ExasolVersion
12+
from exasol.toolbox.version import VERSION as PLUGIN_VERSION
13+
from exasol.toolbox.util.version import Version as ExasolVersion
1414

1515
logger = logging.getLogger(__name__)
1616

exasol/toolbox/util/version.py

Lines changed: 120 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,120 @@
1-
# ATTENTION:
2-
# This file is generated by exasol/toolbox/nox/_package_version.py when using:
3-
# * either "poetry run -- nox -s project:fix"
4-
# * or "poetry run -- nox version:check -- <path/version.py> --fix"
5-
# Do not edit this file manually!
6-
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`.
7-
MAJOR = 1
8-
MINOR = 0
9-
PATCH = 1
10-
VERSION = f"{MAJOR}.{MINOR}.{PATCH}"
11-
__version__ = VERSION
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.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# ATTENTION:
2+
# This file is generated by exasol/toolbox/nox/_package_version.py when using:
3+
# * either "poetry run -- nox -s project:fix"
4+
# * or "poetry run -- nox version:check -- <path/version.py> --fix"
5+
# Do not edit this file manually!
6+
# If you need to change the version, do so in the pyproject.toml, e.g. by using `poetry version X.Y.Z`.
7+
MAJOR = 1
8+
MINOR = 0
9+
PATCH = 1
10+
VERSION = f"{MAJOR}.{MINOR}.{PATCH}"
11+
__version__ = 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.version import Version
6+
from exasol.toolbox.util.version import Version
77

88

99
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)