Compare commits

...

2 Commits

Author SHA1 Message Date
cclecle
c0d5e4480c fix: code quality 2023-03-17 18:48:18 +00:00
cclecle
2d718d2e4c fix: wrong cls usage in self class 2023-03-17 18:32:38 +00:00

View File

@@ -1,3 +1,16 @@
# pygitversionhelper (c) by chacha
#
# pygitversionhelper is licensed under a
# Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Unported License.
#
# You should have received a copy of the license along with this
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
"""
This module is the main gitversionhelper file, containing all the code.
This project aim to be kept compact and focused on helping doing handy things with git when it deal with project versions/tags.
"""
from __future__ import annotations
from typing import TYPE_CHECKING
@@ -28,7 +41,7 @@ def _exec(cmd: str, root: str | os.PathLike | None = None) -> list[str]:
lines = stdout.splitlines()
return [line.rstrip() for line in lines if line.rstrip()]
class gitversionhelper: # pylint: disable=too-few-public-methods
class gitversionhelper: # pylint: disable=too-few-public-methods
"""
Main gitversionhelper class
"""
@@ -66,12 +79,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def getLastTag(cls,**kwargs) -> Union[str,None]:
f"""
"""
retrieve the last tag from a repository
Arguments:
sort: sorting constraints (git format)
Kwargs:
{cls.__OptDict["same_branch"]}: force searching only in the same branch
same_branch(bool): force searching only in the same branch
Return:
the tag
"""
@@ -82,12 +93,12 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def getDistanceFromTag(cls,tag=None,**kwargs) -> int:
f"""
"""
retrieve the distance from tag in the repository
Arguments:
tag: reference tag, if None the most recent one will be used
Kwargs:
{cls.__OptDict["same_branch"]}: force searching only in the same branch
same_branch(bool): force searching only in the same branch
Return:
the tag
"""
@@ -103,7 +114,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
"formated_output": "formated_output",
"output_format": "output_format"}
DefaultInputFormat = "PEP440"
VersionStds = { "SemVer" : { "regex" : r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"},
VersionStds = { "SemVer" : { "regex" : r"^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)"\
r"(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)"\
r"(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?"\
r"(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$"},
"PEP440" : { "regex" : packaging_VERSION_PATTERN }
}
@@ -129,12 +143,12 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def _getBumpDevStrategy(cls,**kwargs) -> str:
f"""
"""
get selected bump_dev_strategy
Kwargs:
{cls.__OptDict["bump_dev_strategy"]}: the given bump_dev_strategy (can be None)
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
Return:
Kwargs given {cls.__OptDict["bump_dev_strategy"]} or the default one.
Kwargs given bump_dev_strategy or the default one.
"""
BumpDevStrategy = cls.DefaultBumpDevStrategy
if (cls.__OptDict["bump_dev_strategy"] in kwargs):
@@ -146,12 +160,12 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def _getBumpType(cls,**kwargs) -> str:
f"""
"""
get selected bump_type
Kwargs:
{cls.__OptDict["bump_type"]}: the given bump_type (can be None)
bump_type(str): the given bump_type (can be None)
Return:
Kwargs given {cls.__OptDict["bump_type"]} or the default one.
Kwargs given bump_type or the default one.
"""
BumpType = cls.DefaultBumpType
if (cls.__OptDict["bump_type"] in kwargs):
@@ -162,38 +176,38 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
return BumpType
def bump(self,**kwargs):
f"""
"""
bump the version to the next one
Kwargs:
{cls.__OptDict["bump_type"]}: the given bump_type (can be None)
{cls.__OptDict["bump_dev_strategy"]}: the given bump_dev_strategy (can be None)
bump_type(str): the given bump_type (can be None)
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
Return:
the bumped version
"""
BumpType = self._getBumpType(**kwargs)
BumpDevStrategy=self._getBumpDevStrategy(**kwargs)
_v=copy(self)
if BumpType is "dev":
if BumpDevStrategy is "post":
if BumpType == "dev":
if BumpDevStrategy == "post":
if _v.pre_count > 0:
_v.pre_count = _v.pre_count + 1
else:
_v.post_count = _v.post_count + 1
elif BumpDevStrategy is "pre":
elif BumpDevStrategy == "pre":
if _v.post_count > 0:
_v.post_count = _v.post_count + 1
else:
_v.pre_count = _v.pre_count + 1
elif BumpDevStrategy is "force_post":
elif BumpDevStrategy == "force_post":
pass
elif BumpDevStrategy is "force_pre":
elif BumpDevStrategy == "force_pre":
pass
else:
if BumpType is "major":
if BumpType == "major":
_v.major = _v.major + 1
elif BumpType is "minor":
elif BumpType == "minor":
_v.minor = _v.minor + 1
elif BumpType is "patch":
elif BumpType == "patch":
_v.patch = _v.patch + 1
_v.pre_count=0
_v.post_count=0
@@ -208,6 +222,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
def _getVersionStd(cls,**kwargs):
"""
get selected version_std
Kwargs:
version_std(str): the given version_std (can be None)
Return:
Kwargs given version_std or the default one.
"""
VersionStd = cls.DefaultInputFormat
if (cls.__OptDict["version_std"] in kwargs):
@@ -218,9 +236,15 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
return VersionStd
@classmethod
def getLastVersion(cls,**kwargs):
def getLastVersion(cls,**kwargs) -> Union[str,MetaVersion]:
"""
get last version from git tags
bump the last version from tags
Kwargs:
version_std(str): the given version_std (can be None)
same_branch(bool): force searching only in the same branch
formated_output(bool) : output a formated version string
Return:
the last version
"""
VersionStd = cls._getVersionStd(**kwargs)
_r=re.compile(r"^\s*" + cls.VersionStds[VersionStd]["regex"] + r"\s*$", re.VERBOSE | re.IGNORECASE)
@@ -254,7 +278,11 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
@classmethod
def doFormatVersion(cls,inputversion:MetaVersion,**kwargs):
"""
output a formated version
output a formated version string
Args:
inputversion: version to be rendered
Return:
formated version string
"""
VersionStd = cls._getVersionStd(**kwargs)