From 0a8de36190a0f097eb2fcb8b74fab351a806a89c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ricks?= Date: Fri, 10 Mar 2023 10:54:06 +0100 Subject: [PATCH] Change: Refactor VersionCommands * Add support for VersioningScheme and therefore different version parsing * Move all VersionCommand classes into a commands package * Mark commands sup modules as "private" --- docs/pontos/version.md | 5 +-- docs/pontos/version/cmake.md | 6 ---- docs/pontos/version/commands.md | 6 ++++ docs/pontos/version/go.md | 6 ---- docs/pontos/version/javascript.md | 6 ---- docs/pontos/version/python.md | 6 ---- .../{commands.py => commands/__init__.py} | 22 ++++++++++--- .../version/{cmake.py => commands/_cmake.py} | 26 ++++++++------- pontos/version/{ => commands}/_command.py | 4 +-- pontos/version/{go.py => commands/_go.py} | 17 ++++++---- .../_javascript.py} | 13 ++++---- .../{python.py => commands/_python.py} | 33 ++++++++++++------- 12 files changed, 80 insertions(+), 70 deletions(-) delete mode 100644 docs/pontos/version/cmake.md create mode 100644 docs/pontos/version/commands.md delete mode 100644 docs/pontos/version/go.md delete mode 100644 docs/pontos/version/javascript.md delete mode 100644 docs/pontos/version/python.md rename pontos/version/{commands.py => commands/__init__.py} (69%) rename pontos/version/{cmake.py => commands/_cmake.py} (90%) rename pontos/version/{ => commands}/_command.py (96%) rename pontos/version/{go.py => commands/_go.py} (87%) rename pontos/version/{javascript.py => commands/_javascript.py} (93%) rename pontos/version/{python.py => commands/_python.py} (85%) diff --git a/docs/pontos/version.md b/docs/pontos/version.md index 05f971d0e..932a42a17 100644 --- a/docs/pontos/version.md +++ b/docs/pontos/version.md @@ -5,11 +5,8 @@ ```{toctree} :maxdepth: 1 -version/cmake -version/go -version/javascript +version/commands version/project -version/python version/schemes version/helper ``` diff --git a/docs/pontos/version/cmake.md b/docs/pontos/version/cmake.md deleted file mode 100644 index 0ff2c840b..000000000 --- a/docs/pontos/version/cmake.md +++ /dev/null @@ -1,6 +0,0 @@ -# pontos.version.cmake package - -```{eval-rst} -.. automodule:: pontos.version.cmake - :members: -``` diff --git a/docs/pontos/version/commands.md b/docs/pontos/version/commands.md new file mode 100644 index 000000000..e9565704c --- /dev/null +++ b/docs/pontos/version/commands.md @@ -0,0 +1,6 @@ +# pontos.version.commands package + +```{eval-rst} +.. automodule:: pontos.version.commands + :members: +``` diff --git a/docs/pontos/version/go.md b/docs/pontos/version/go.md deleted file mode 100644 index 7949ff2ec..000000000 --- a/docs/pontos/version/go.md +++ /dev/null @@ -1,6 +0,0 @@ -# pontos.version.go package - -```{eval-rst} -.. automodule:: pontos.version.go - :members: -``` diff --git a/docs/pontos/version/javascript.md b/docs/pontos/version/javascript.md deleted file mode 100644 index f70d9c5b0..000000000 --- a/docs/pontos/version/javascript.md +++ /dev/null @@ -1,6 +0,0 @@ -# pontos.version.javascript package - -```{eval-rst} -.. automodule:: pontos.version.javascript - :members: -``` diff --git a/docs/pontos/version/python.md b/docs/pontos/version/python.md deleted file mode 100644 index 23976bf1b..000000000 --- a/docs/pontos/version/python.md +++ /dev/null @@ -1,6 +0,0 @@ -# pontos.version.python package - -```{eval-rst} -.. automodule:: pontos.version.python - :members: -``` diff --git a/pontos/version/commands.py b/pontos/version/commands/__init__.py similarity index 69% rename from pontos/version/commands.py rename to pontos/version/commands/__init__.py index 95c28753b..ff6229549 100644 --- a/pontos/version/commands.py +++ b/pontos/version/commands/__init__.py @@ -17,11 +17,20 @@ from typing import Iterable, Tuple, Type -from .cmake import CMakeVersionCommand -from .go import GoVersionCommand -from .javascript import JavaScriptVersionCommand -from .python import PythonVersionCommand -from .version import VersionCommand +from ._cmake import CMakeVersionCommand +from ._command import VersionCommand +from ._go import GoVersionCommand +from ._javascript import JavaScriptVersionCommand +from ._python import PythonVersionCommand + +__all__ = ( + "VersionCommand", + "CMakeVersionCommand", + "GoVersionCommand", + "JavaScriptVersionCommand", + "PythonVersionCommand", + "get_commands", +) __COMMANDS: Tuple[Type[VersionCommand]] = ( CMakeVersionCommand, @@ -32,4 +41,7 @@ def get_commands() -> Iterable[Type[VersionCommand]]: + """ + Returns the available VersionCommands + """ return __COMMANDS diff --git a/pontos/version/cmake.py b/pontos/version/commands/_cmake.py similarity index 90% rename from pontos/version/cmake.py rename to pontos/version/commands/_cmake.py index 38fcb4344..b3faa765c 100644 --- a/pontos/version/cmake.py +++ b/pontos/version/commands/_cmake.py @@ -19,9 +19,10 @@ import re from typing import Generator, Literal, Optional, Tuple, Union -from pontos.version.errors import VersionError - -from .version import Version, VersionCommand, VersionUpdate, parse_version +from ..errors import VersionError +from ..schemes import PEP440VersioningScheme +from ..version import Version, VersionUpdate +from ._command import VersionCommand class CMakeVersionCommand(VersionCommand): @@ -50,14 +51,15 @@ def get_current_version(self) -> Version: raise VersionError(f"{self.project_file_path} not found.") content = self.project_file_path.read_text(encoding="utf-8") - return CMakeVersionParser(content).get_current_version() + current_version = CMakeVersionParser(content).get_current_version() + return self.versioning_scheme.from_version(current_version) def verify_version( - self, version: Union[Literal["current"], Version] + self, version: Union[Literal["current"], Version, None] ) -> None: current_version = self.get_current_version() - if version == "current": + if not version or version == "current": return if current_version != version: @@ -94,17 +96,19 @@ def __init__(self, cmake_content_lines: str): def get_current_version(self) -> Version: return ( - parse_version(f"{self._current_version}.dev1") + PEP440VersioningScheme.parse_version( + f"{self._current_version}.dev1" + ) if self.is_dev_version() - else parse_version(self._current_version) + else PEP440VersioningScheme.parse_version(self._current_version) ) def update_version(self, new_version: Version) -> str: - if new_version.is_devrelease: - new_version = parse_version( + if new_version.is_dev_release: + new_version = PEP440VersioningScheme.parse_version( f"{str(new_version.major)}." f"{str(new_version.minor)}." - f"{str(new_version.micro)}" + f"{str(new_version.patch)}" ) develop = True else: diff --git a/pontos/version/_command.py b/pontos/version/commands/_command.py similarity index 96% rename from pontos/version/_command.py rename to pontos/version/commands/_command.py index 41a525158..706d91403 100644 --- a/pontos/version/_command.py +++ b/pontos/version/commands/_command.py @@ -19,8 +19,8 @@ from pathlib import Path from typing import Literal, Union -from .scheme import VersioningScheme -from .version import Version, VersionUpdate +from ..schemes import VersioningScheme +from ..version import Version, VersionUpdate class VersionCommand(ABC): diff --git a/pontos/version/go.py b/pontos/version/commands/_go.py similarity index 87% rename from pontos/version/go.py rename to pontos/version/commands/_go.py index 250db712c..02c9e7e62 100644 --- a/pontos/version/go.py +++ b/pontos/version/commands/_go.py @@ -19,9 +19,10 @@ from pathlib import Path from typing import Literal, Union -from .errors import VersionError -from .helper import get_last_release_version -from .version import Version, VersionCommand, VersionUpdate, parse_version +from ..errors import VersionError +from ..helper import get_last_release_version +from ..version import Version, VersionUpdate +from ._command import VersionCommand VERSION_MATCH = r'var [Vv]ersion = "(.+)"' TEMPLATE = """package main @@ -60,7 +61,7 @@ def get_current_version(self) -> Version: ) match = re.search(VERSION_MATCH, version_file_text) if match: - return parse_version(match.group(1)) + return self.versioning_scheme.parse_version(match.group(1)) else: raise VersionError( f"No version found in the {self.version_file_path} file." @@ -72,12 +73,12 @@ def get_current_version(self) -> Version: ) def verify_version( - self, version: Union[Literal["current"], Version] + self, version: Union[Literal["current"], Version, None] ) -> None: """Verify the current version of this project""" current_version = self.get_current_version() - if version == "current": + if not version or version == "current": return if current_version != version: @@ -93,7 +94,9 @@ def update_version( try: current_version = self.get_current_version() except VersionError: - current_version = get_last_release_version("v") + current_version = get_last_release_version( + self.versioning_scheme.parse_version, git_tag_prefix="v" + ) if not force and new_version == current_version: return VersionUpdate(previous=current_version, new=new_version) diff --git a/pontos/version/javascript.py b/pontos/version/commands/_javascript.py similarity index 93% rename from pontos/version/javascript.py rename to pontos/version/commands/_javascript.py index ea3ab6763..68e0b104c 100644 --- a/pontos/version/javascript.py +++ b/pontos/version/commands/_javascript.py @@ -20,8 +20,9 @@ from pathlib import Path from typing import Any, Dict, Literal, Optional, Union -from .errors import VersionError -from .version import Version, VersionCommand, VersionUpdate, parse_version +from ..errors import VersionError +from ..version import Version, VersionUpdate +from ._command import VersionCommand # This class is used for JavaScript Version command(s) @@ -70,21 +71,21 @@ def _get_current_file_version( if not match: raise VersionError(f"VERSION variable not found in {version_file}") - return Version(match.group("version")) + return self.versioning_scheme.parse_version(match.group("version")) def get_current_version(self) -> Version: """Get the current version of this project In go the version is only defined within the repository tags, thus we need to check git, what tag is the latest""" - return parse_version(self.package["version"]) + return self.versioning_scheme.parse_version(self.package["version"]) def verify_version( - self, version: Union[Literal["current"], Version] + self, version: Union[Literal["current"], Version, None] ) -> None: """Verify the current version of this project""" current_version = self.get_current_version() - if version == "current": + if not version or version == "current": for version_file in self.version_file_paths: file_version = self._get_current_file_version(version_file) if file_version and file_version != current_version: diff --git a/pontos/version/python.py b/pontos/version/commands/_python.py similarity index 85% rename from pontos/version/python.py rename to pontos/version/commands/_python.py index 1bd25cccb..31f070932 100644 --- a/pontos/version/python.py +++ b/pontos/version/commands/_python.py @@ -21,8 +21,10 @@ import tomlkit -from .errors import VersionError -from .version import Version, VersionCommand, VersionUpdate, parse_version +from ..errors import VersionError +from ..schemes import PEP440VersioningScheme +from ..version import Version, VersionUpdate +from ._command import VersionCommand TEMPLATE = """# pylint: disable=invalid-name @@ -48,7 +50,7 @@ def _get_version_from_pyproject_toml(self) -> Version: and "poetry" in self.pyproject_toml["tool"] # type: ignore and "version" in self.pyproject_toml["tool"]["poetry"] # type: ignore # pylint: disable=line-too-long ): - return parse_version(self.pyproject_toml["tool"]["poetry"]["version"]) # type: ignore # pylint: disable=line-too-long + return self.versioning_scheme.parse_version(self.pyproject_toml["tool"]["poetry"]["version"]) # type: ignore # pylint: disable=line-too-long raise VersionError( "Version information not found in " @@ -145,7 +147,9 @@ def get_current_version(self) -> Version: ) version_module = importlib.util.module_from_spec(spec) spec.loader.exec_module(version_module) - return parse_version(version_module.__version__) + return PEP440VersioningScheme.parse_version( + version_module.__version__ + ) except FileNotFoundError: raise VersionError( f"Could not load version from '{module_name}'. " @@ -157,7 +161,7 @@ def get_current_version(self) -> Version: ) from None def verify_version( - self, version: Union[Literal["current"], Version] + self, version: Union[Literal["current"], Version, None] ) -> None: current_version = self.get_current_version() pyproject_version = self._get_version_from_pyproject_toml() @@ -169,7 +173,7 @@ def verify_version( f"version {current_version}." ) - if version != "current": + if version and version != "current": if version != current_version: raise VersionError( f"Provided version {version} does not match the " @@ -186,15 +190,22 @@ def update_version( # pyproject.toml current_version = self._get_version_from_pyproject_toml() - if not force and new_version == current_version: - return VersionUpdate(previous=current_version, new=new_version) + new_pep440_version = PEP440VersioningScheme.from_version(new_version) + current_converted_version = self.versioning_scheme.from_version( + current_version + ) + + if not force and new_pep440_version == current_version: + return VersionUpdate( + previous=current_converted_version, new=new_version + ) - self._update_pyproject_version(new_version=new_version) + self._update_pyproject_version(new_version=new_pep440_version) - self._update_version_file(new_version=new_version) + self._update_version_file(new_version=new_pep440_version) return VersionUpdate( - previous=current_version, + previous=current_converted_version, new=new_version, changed_files=[self.version_file_path, self.project_file_path], )