Compare commits

...

2 Commits

Author SHA1 Message Date
cclecle
46ee6dec57 fix: quality score 2023-03-17 18:54:36 +00:00
cclecle
c0d5e4480c fix: code quality 2023-03-17 18:48:18 +00:00

View File

@@ -1,3 +1,17 @@
# 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 +42,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
"""
@@ -43,8 +57,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
Return:
True if it is dirty
"""
return True if _exec(f"git status --short") else False
return True if _exec("git status --short") else False
class tag:
"""
class containing methods focusing on tags
@@ -66,12 +80,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,16 +94,16 @@ 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
"""
if (tag is None):
if tag is None:
tag = cls.getLastTag(**kwargs)
return int(_exec(f"git rev-list {tag}..HEAD --count")[0])
@@ -103,7 +115,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 }
}
@@ -125,16 +140,21 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
raw:str = "0.1.0"
def __init__(self,major=0,minor=1,patch=0,pre_count=0,post_count=0,raw="0.1.0"):
self.major,self.minor,self.patch,self.pre_count,self.post_count,self.raw = major,minor,patch,pre_count,post_count,raw
self.major = major
self.minor = minor
self.patch = patch
self.pre_count = pre_count
self.post_count = post_count
self.raw = raw
@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 +166,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,43 +182,47 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
return BumpType
def bump(self,**kwargs):
f"""
"""
bump the version to the next one
Kwargs:
{self.__OptDict["bump_type"]}: the given bump_type (can be None)
{self.__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
_v.raw="{major}.{minor}.{patch}{revpattern}{revcount}".format(major=_v.major,minor=_v.minor,patch=_v.patch,revpattern="",revcount="")
_v.raw="{major}.{minor}.{patch}{revpattern}{revcount}".format(major=_v.major,\
minor=_v.minor,\
patch=_v.patch,\
revpattern="",\
revcount="")
return _v
def doFormatVersion(self,**kwargs):
@@ -208,9 +232,13 @@ 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):
if cls.__OptDict["version_std"] in kwargs:
if kwargs[cls.__OptDict["version_std"]] in cls.VersionStds:
VersionStd = kwargs[cls.__OptDict["version_std"]]
else:
@@ -218,19 +246,26 @@ 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)
_r=re.compile(r"^\s*" + cls.VersionStds[VersionStd]["regex"] + r"\s*$", re.VERBOSE | \
re.IGNORECASE)
lastTag = gitversionhelper.tag.getLastTag(**kwargs)
_m = re.match(_r,lastTag)
if not _m:
raise RuntimeError("no valid version found in tags")
if VersionStd is "PEP440":
if VersionStd == "PEP440":
ver=_m.group("release").split(".")
ver += ["0"] * (3 - len(ver))
ver[0]=int(ver[0])
@@ -239,8 +274,11 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
major, minor, patch = tuple(ver)
pre_count = int(_m.group("pre_n")) if _m.group("pre_n") else 0
post_count = int(_m.group("post_n2")) if _m.group("post_n2") else 0
elif VersionStd is "SemVer":
major, minor, patch = int(_m.group("major")),int(_m.group("minor")),int(_m.group("patch")),(_m.group("prerelease") if _m.group("prerelease") else ""), ""
elif VersionStd == "SemVer":
major, minor, patch = int(_m.group("major")),\
int(_m.group("minor")),\
int(_m.group("patch")),\
(_m.group("prerelease") if _m.group("prerelease") else ""), ""
pre_count = 0
post_count = 0
@@ -248,13 +286,16 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if ((cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True)):
return cls.doFormatVersion(_v,**kwargs)
else:
return _v
return _v
@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)
@@ -267,12 +308,12 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if (cls.__OptDict["output_format"] in kwargs):
OutputFormat=kwargs[cls.__OptDict["output_format"]]
if OutputFormat is None:
if VersionStd is "PEP440":
if VersionStd == "PEP440":
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
if post_count > 0:
revpattern=".post"
revcount=f"{post_count}"
elif VersionStd is "SemVer":
elif VersionStd == "SemVer":
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
if post_count > 0:
pre_count = pre_count + post_count