Compare commits

...

7 Commits

Author SHA1 Message Date
cclecle
38cde9d700 reword 2023-03-18 21:13:11 +00:00
cclecle
fff52596b1 update doc 2023-03-18 21:06:39 +00:00
cclecle
3ce09a0194 fix typo 2023-03-18 21:04:03 +00:00
cclecle
961f92d9c6 update doc and docstring 2023-03-18 21:02:49 +00:00
cclecle
a1cf4983ff improve code quality 2023-03-18 20:21:59 +00:00
cclecle
c9e6f4f436 improve detection of wrong case: both pre and post index + test 2023-03-18 20:20:11 +00:00
cclecle
7d9f2a7792 improve coverage and unittest and quality 2023-03-18 20:11:14 +00:00
4 changed files with 229 additions and 79 deletions

View File

@@ -3,46 +3,33 @@
![](docs-static/Library.jpg)
# Python project template
# pyGitVersionHelper
A nice template to start a blank python projet.
This template automate a lot of handy things and allow CI/CD automatic releases generation.
_A tiny library to help versioning management of git python projects_
It is also collectings data to feed Jenkins build.
Because a good developer is a lazy developer and version management in CI/CD can be very time consuming.
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pychachadummyproject/{{branch}}/latest/).
Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/{{branch}}/latest/).
## Features
### Generic pipeline skeleton:
- Prepare
- GetCode
- BuildPackage
- Install
- CheckCode
- PlotMetrics
- RunUnitTests
- GenDOC
- PostRelease
- list tags
- get last tag
- get last version
- get current version (bumped)
- convert / switch from SemVer to PEP440 (both ways)
- automatic version format detection (SemVer by default)
### CI/CD Environment
- Jenkins
- Gitea (with patch for dynamic Readme variables: https://chacha.ddns.net/gitea/chacha/GiteaMarkupVariable)
- Docker
- MkDocsWeb
### CI/CD Helper libs
- VirtualEnv
- Changelog generation based on commits
- copier
- pylint + pylint_json2html
- mypy
- unittest + xmlrunner + junitparser + junit2htmlreport
- mkdocs
## Options
- restrict to same branch
- both SemVer and PEP440 support
- custom output format
- configurable default bump type major, minor, patch or dev
- configurable default bump type: post, pre-patch, pre-minor, pre-major
- ignore non-version tag
- force version format
### Python project
- Full .toml implementation
- .whl automatic generation
- dynamic versionning using git repository
- embedded unit-test
## Process
- full CI/CD developpment: Gitea / Jenkins + few python libs (pytlint, coverage, unittest, mkdocs)
- documentation generated mkdocs and self-hosted
- CI/CD on Linux, manually tested in Windows environnement

View File

@@ -1,16 +1,113 @@
# Usage
## Pulvinar dolor
Donec dapibus est fermentum justo volutpat condimentum. Integer quis nunc neque. Donec dictum vehicula justo, in facilisis ex tincidunt in.
Vivamus sollicitudin sem dui, id mollis orci facilisis ut. Proin sed pulvinar dolor. Donec volutpat commodo urna imperdiet pulvinar. Fusce eget aliquam risus.
Vivamus viverra luctus ex, in finibus mi. Nullam elementum dapibus mollis. Ut suscipit volutpat ex, quis feugiat lacus consectetur eu.
## Installation
## Condimentum faucibus
Quisque auctor egestas sem, luctus suscipit ex maximus vitae. Duis facilisis augue et condimentum faucibus.
Donec cursus, enim a sagittis egestas, lectus lorem eleifend libero, at tincidunt leo magna at libero.
Nunc eros velit, suscipit luctus tempor vel, finibus et est. Curabitur efficitur pretium pulvinar.
Donec urna lectus, vulputate quis turpis sed, placerat congue urna. Phasellus aliquet fermentum quam, non auctor elit porta nec. Morbi eu ligula at nisl ultricies condimentum vitae id ante.
From master repository:
## Aliquam lacinia
In volutpat lorem ex, et fringilla nibh faucibus quis. Mauris et arcu elementum, auctor dui vitae, egestas arcu. Duis sit amet aliquam quam.
Phasellus a odio turpis. Etiam tristique mi eu enim varius, eget facilisis est vestibulum. Aliquam lacinia nec purus sed luctus. Cras at laoreet erat.
python -m pip install git+https://chacha.ddns.net/gitea/chacha/pygitversionhelper.git@master
From local .whl file:
python -m pip install pygitversionhelper-<VERSION>-py3-none-any.whl
From public repository:
TBD
## Import in your project
Add this line on the top of your python script:
#from pygitversionhelper import gitversionhelper
[optionnal] If you need to catch exception from this module:
#from pygitversionhelper import gitversionhelperException
## Basic API
All the API commands are static so it is not needed to create instantiate any object.
They are all executed in the current active directory.
One easy way to change directory:
import os
os.chdir("<YOUR DIRECTORY>")
### sublib: repository
To check if a repository is dirty:
if gitversionhelper.repository.isDirty():
print("repository is dirty")
### sublib: tag
List all tags [default to taggerdate order]:
for tag in gitversionhelper.tag.getTags():
print(f"found tag: {tag}")
List all tags [using git refname order]:
for tag in gitversionhelper.tag.getTags("v:refname"):
print(f"found tag: {tag}")
Get the last tag:
print(f"most recent repository tag: {gitversionhelper.tag.getLastTag()}")
Get the last tag [only on same branch]:
print(f"most recent repository tag: {gitversionhelper.tag.getLastTag(same_branch=True)}")
Get the distance from HEAD to last tag:
print(f"number of commit since last tag: {gitversionhelper.tag.getDistanceFromTag()}")
Get the distance from HEAD to last tag [only on same branch]:
print(f"number of commit since last tag: {gitversionhelper.tag.getDistanceFromTag(same_branch=True)}")
### sublib: version
Get the last found version in the repository [return MetaVersion object]:
print(f"most recent repository version: {gitversionhelper.tag.getLastVersion()}")
Get the last found version in the repository [return formated string]:
print(f"most recent repository version: {gitversionhelper.tag.getLastVersion(formated_output=True)}")
Others kwargs available to this function:
- version_std: string to force a version standard for rendering ["PEP440" or "SemVer"]
- same_branch: boolean to force searching on same branch
- ignore_unknown_tags: boolean to allow unknown tag to be ignored
Get the current version of the repository, automatically bump it if the last one is not tagged [returns MetaVersion object]:
print(f"most recent repository version: {gitversionhelper.tag.getCurrentVersion()}")
kwargs available to this function:
- All same args as getLastVersion()
- bump_type: if version need to be pump, allow to configure next release update type: major, minor, patch, dev
- bump_dev_strategy: if bump_type is dev, allow to choose dev update strategy: post, pre-patch, pre-minor, pre-major
A version object can also be manually formated:
_version = gitversionhelper.tag.getCurrentVersion()
_version.doFormatVersion()
#or
gitversionhelper.version.doFormatVersion(_version)
kwargs available to those function:
- output_format: string to choose a rendering format ["Auto","PEP440" or "SemVer"]
## Limitations
There is unfortunately some technical limitation :
- MultiThreading and async behavior is not tested.
- Multiple tag on the same commit is not supported.
- Branch filter when searching for a version is only tested with -no-ff strategy

View File

@@ -9,19 +9,22 @@
"""
This project try to help doing handy operations with git when
dealing with project versioning and tags on python project -
or at leat for project using PEP440 versionning or SemVer.
at leat for project using PEP440 or SemVer standards.
Goal is to keep it compact and not covering too much other things.
This is the reason it is one single file with nested classes.
=> Design is on purpose poorly expandable to keep the scope maintainable.
One requirement is to keep it compact and to not cover too much fancy features.
This is the reason why it is one single file with nested classes.
=> Design is on-purpose poorly expandable to keep the scope light.
This library is maid for repository using tag as version.
Support for non-version tags is optional and not well tested.
This module is the main gitversionhelper file, containing all the code.
This module is the main project file, containing all the code.
Read the read me for more information.
Check the unittest s for usage samples.
Note:
Other Parameters are **kwargs
"""
from __future__ import annotations
@@ -47,9 +50,9 @@ def _exec(cmd: str, root: str | os.PathLike | None = None) -> list[str]:
p = subprocess.run(cmd.split(), text=True, cwd=root, capture_output=True, check=False, timeout=2)
if re.search("not a git repository",p.stderr):
raise gitversionhelper.repository.notAGitRepository()
if re.search("fatal:",p.stderr):
if re.search("fatal:",p.stderr): #pragma: nocover
raise gitversionhelper.unknownGITFatalError(p.stderr)
if int(p.returncode) < 0:
if int(p.returncode) < 0: #pragma: nocover
raise gitversionhelper.unknownGITError(p.stderr)
lines = p.stdout.splitlines()
return [line.rstrip() for line in lines if line.rstrip()]
@@ -137,10 +140,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
Returns:
the tags list
"""
if sort not in cls.__validGitTagSort:
raise gitversionhelper.wrongArguments("sort option not in allowed list")
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True)):
currentBranch = _exec("git rev-parse --abbrev-ref HEAD")
return list(reversed(_exec(f"git tag --merged {currentBranch[0]} --sort={sort}")))
@@ -164,7 +167,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if len(res)==0:
raise cls.tagNotFound("no tag found in commit history")
if len(res)!=1:
if len(res)!=1: #pragma: nocover
raise cls.moreThanOneTag("multiple tags on same commit is unsupported")
return res[0]
@@ -196,9 +199,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
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-]+)*))?$",
"regex_preversion_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
"regex_build_num" : r"(?:\.)(?P<num>(?:\d+(?!\w))+)"
"regex_preversion_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
"regex_build_num" : r"(?:\.)(?P<num>(?:\d+(?!\w))+)"
},
"PEP440" : { "regex" : packaging_VERSION_PATTERN,
"Auto" : None
@@ -215,6 +217,11 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
no valid version found exception
"""
class PreAndPostVersionUnsupported(versionException):
"""
pre and post release can not be present at the same time
"""
class MetaVersion:
"""
generic version object
@@ -279,7 +286,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
return BumpType
def bump(self,amount:int=1,**kwargs) -> MetaVersion | str : # pylint: disable=R0912
def bump(self,amount:int=1,**kwargs) -> gitversionhelper.version.MetaVersion | str : # pylint: disable=R0912
"""
bump the version to the next one
Keyword Arguments:
@@ -298,7 +305,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
_v.pre_count = _v.pre_count + amount
else:
_v.post_count = _v.post_count + amount
elif BumpDevStrategy in ["pre-patch","pre-minor","pre-major"]:
#elif BumpDevStrategy in ["pre-patch","pre-minor","pre-major"]:
else:
if _v.post_count > 0:
_v.post_count = _v.post_count + amount
else:
@@ -308,7 +316,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
elif BumpDevStrategy == "pre-minor":
_v.minor = _v.minor + 1
_v.patch = 0
elif BumpDevStrategy == "pre-major":
#elif BumpDevStrategy == "pre-major":
else:
_v.major = _v.major + 1
_v.minor = 0
_v.patch = 0
@@ -318,12 +327,13 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
_v.major = _v.major + amount
elif BumpType == "minor":
_v.minor = _v.minor + amount
elif BumpType == "patch":
#elif BumpType == "patch":
else:
_v.patch = _v.patch + amount
_v.pre_count=0
_v.post_count=0
_v.raw=_v.doFormatVersion(**kwargs)
if ((self.__OptDict["formated_output"] in kwargs) and (kwargs[self.__OptDict["formated_output"]] is True)):
return _v.doFormatVersion(**kwargs)
return _v
@@ -331,6 +341,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
def doFormatVersion(self,**kwargs) -> str:
"""
output a formated version string
Keyword Arguments:
output_format: output format to render ("Auto" or "PEP440" or "SemVer")
Returns:
formated version string
"""
@@ -361,6 +373,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
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
bump_type(str): the given bump_type (can be None)
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
Returns:
the last version
"""
@@ -382,7 +396,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
return _v
@classmethod
def _parseTag(cls,tag,**kwargs):
def _parseTag(cls,tag,**kwargs): # pylint: disable=R0914, R0912, R0915
"""get the last version from tags
Arguments:
tag: the tag to be parsed
@@ -444,6 +458,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if not bFound :
raise gitversionhelper.version.noValidVersion("no valid version found in tags")
if pre_count > 0 and post_count > 0:
raise cls.PreAndPostVersionUnsupported("can not parse a version with both pre" \
" and post release number.")
return cls.MetaVersion(VersionStd, major, minor, patch, pre_count, post_count, tag)
@classmethod
@@ -468,18 +486,18 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
_v=None
try:
_v=cls._parseTag(lastTag,**kwargs)
except gitversionhelper.version.noValidVersion:
except gitversionhelper.version.noValidVersion as _ex:
if ((cls.__OptDict["ignore_unknown_tags"] in kwargs) and (kwargs[cls.__OptDict["ignore_unknown_tags"]] is True)):
tags = gitversionhelper.tag.getTags(sort= "taggerdate",**kwargs)
_v=None
_v = None
for _tag in tags:
try:
_v=cls._parseTag(_tag,**kwargs)
break;
except:
break
except gitversionhelper.version.noValidVersion:
continue
if _v is None:
raise gitversionhelper.version.noValidVersion()
raise gitversionhelper.version.noValidVersion() from _ex
if ((cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True)):
return _v.doFormatVersion(**kwargs)
@@ -489,6 +507,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
def doFormatVersion(cls,inputversion:MetaVersion,**kwargs) -> str:
"""
output a formated version string
Keyword Arguments:
output_format: output format to render ("Auto" or "PEP440" or "SemVer")
Args:
inputversion: version to be rendered
Returns:
@@ -512,7 +532,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if OutputFormat is None:
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
if post_count > 0 and pre_count > 0:
raise RuntimeError("pre and post release can not be present at the same time")
raise gitversionhelper.version.PreAndPostVersionUnsupported("cannot output a version with both pre " \
"and post release number.")
if VersionStd == "PEP440":
if post_count > 0:
revpattern=".post"
@@ -520,7 +541,8 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
elif pre_count > 0:
revpattern=".pre"
revcount=f"{pre_count}"
elif VersionStd == "SemVer":
#elif VersionStd == "SemVer":
else:
if post_count > 0:
revpattern="+post"
revcount=f".{post_count}"

View File

@@ -12,6 +12,7 @@ import tempfile
import os
import pathlib
import re
import copy
print(__name__)
print(__package__)
@@ -36,7 +37,7 @@ class Test_gitversionhelper(unittest.TestCase):
os.system("git add .")
os.system("git commit -m \"first commit\"")
os.system(f"git tag {tag}")
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(**kwargs)
self.assertIsInstance(_v, pygitversionhelper.gitversionhelper.version.MetaVersion)
@@ -53,6 +54,7 @@ class Test_gitversionhelper(unittest.TestCase):
self.assertEqual(int(_m.group("MAJ")),_v.major)
self.assertEqual(int(_m.group("MIN")),_v.minor)
self.assertEqual(int(_m.group("PATCH")),_v.patch)
return _v
def _test_version_format(self,_v:pygitversionhelper.gitversionhelper.version.MetaVersion,tag:str,**kwargs):
@@ -63,6 +65,17 @@ class Test_gitversionhelper(unittest.TestCase):
def _test_version_readback_simple(self,tag:str,**kwargs):
_v=self._test_version_readback(tag,**kwargs)
self._test_version_format(_v,tag,**kwargs)
def test_nominal__version__formated_output(self):
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("PEP440",
1,
0,
0,
0,
0,
"1.0.0")
self.assertEqual("1.0.1", _v.bump(formated_output=True))
self.assertEqual("2.0.0", _v.bump(formated_output=True,bump_type="major"))
def test_nominal__version__auto_1(self):
self._test_version_readback_simple("0.0.1")
@@ -636,7 +649,34 @@ class Test_gitversionhelper(unittest.TestCase):
with self.assertRaises(pygitversionhelper.gitversionhelper.repository.repositoryDirty) :
pygitversionhelper.gitversionhelper.version.getCurrentVersion()
def test_defect__version_post_and_pre_output(self):
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("PEP440",
0,
0,
1,
1,
1,
"0.0.1.pre1.post1")
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
_v.doFormatVersion()
_v = pygitversionhelper.gitversionhelper.version.MetaVersion("SemVer",
0,
0,
1,
1,
1,
"0.0.1-pre.1+post.1")
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
_v.doFormatVersion()
def test_defect__version_post_and_pre_parse(self):
with self.assertRaises(pygitversionhelper.gitversionhelper.version.PreAndPostVersionUnsupported) :
pygitversionhelper.gitversionhelper.version._parseTag("0.0.1.pre1.post1")
def test_defect__git__wrongargument_sortargs(self):
with self.assertRaises(pygitversionhelper.gitversionhelper.wrongArguments) :
pygitversionhelper.gitversionhelper.tag.getTags(sort="toto")
@@ -649,7 +689,7 @@ class Test_gitversionhelper(unittest.TestCase):
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.tagNotFound) :
pygitversionhelper.gitversionhelper.tag.getLastTag()
""" This test is impossible to do
""" This test is impossible to do because current implementation can only return one tag
def test_defect__git_multipletagsfound(self):
with open("demofile.txt", "w+t") as tmpFile:
tmpFile.write("testvalue")
@@ -657,8 +697,8 @@ class Test_gitversionhelper(unittest.TestCase):
os.system("git commit -m \"first commit\"")
os.system(f"git tag 0.1.0")
os.system(f"git tag 0.2.0")
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.moreThanOneTag) as context:
pygitversionhelper.gitversionhelper.tag.getLastTag()
with self.assertRaises(pygitversionhelper.gitversionhelper.tag.moreThanOneTag) :
pygitversionhelper.gitversionhelper.tag.getLastTag(same_branch=True)
"""
def test_defect__wrongargument_bump_type(self):
@@ -955,6 +995,10 @@ class Test_gitversionhelper(unittest.TestCase):
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
_v = pygitversionhelper.gitversionhelper.version.getLastVersion()
with self.assertRaises(pygitversionhelper.gitversionhelper.version.noValidVersion) :
_v = pygitversionhelper.gitversionhelper.version.getLastVersion(ignore_unknown_tags=True)
def test_defect__tag__invalidtag_inbetween(self):
with open("demofile.txt", "w+t") as tmpFile: