Compare commits

...

121 Commits

Author SHA1 Message Date
cclecle
f32127abd7 revert to static readme file... :-/ 2023-03-24 22:12:08 +00:00
cclecle
b20e5bd868 fix 2023-03-24 21:46:51 +00:00
cclecle
f1b1901f8a format + dynamic readme 2023-03-24 21:37:55 +00:00
cclecle
f63a7b76cd format 2023-03-24 21:08:57 +00:00
cclecle
7ce1a04eb9 formating 2023-03-24 20:28:57 +00:00
cclecle
658eef2df6 update readme 2023-03-24 20:25:16 +00:00
cclecle
3d2b8d3762 adjust colours 2023-03-24 20:02:17 +00:00
cclecle
da9fa475e8 fix csv path 2023-03-24 19:53:01 +00:00
cclecle
93d2046c98 set badge colour 2023-03-24 19:51:24 +00:00
cclecle
fa7eaf0769 update badge_maintainability 2023-03-24 19:47:54 +00:00
cclecle
cad73c4711 add complexity check 2023-03-24 19:26:11 +00:00
cclecle
5621ba99f6 start implementation of complexity measurement 2023-03-24 01:09:10 +00:00
cclecle
c49468e294 fix decimal + colours 2023-03-24 00:19:30 +00:00
cclecle
bec5f9234d fix 2023-03-24 00:11:08 +00:00
cclecle
4e50b8d03f add badge colour
score all /10
2023-03-23 23:56:11 +00:00
cclecle
e74a53e3bf fix tostring() method from BigDecimal 2023-03-23 23:47:46 +00:00
cclecle
ef7469f35b aaaaaaa 2023-03-23 23:28:01 +00:00
cclecle
9e5f0de5c4 add badges in readme
fix float value for quality
2023-03-23 23:23:24 +00:00
cclecle
99f38741fd add code quality badge 2023-03-23 23:13:55 +00:00
cclecle
eee2bf551c bvlabnaal 2023-03-23 23:02:57 +00:00
cclecle
01ce809823 add missing import 2023-03-23 22:57:14 +00:00
cclecle
4ab70409a0 switch to BigDecimal grrrrr 2023-03-23 22:49:08 +00:00
cclecle
7b9752a17a cast to decimalformat 2023-03-23 22:47:52 +00:00
cclecle
879a7ca03d fix wrong method name :-/ 2023-03-23 22:42:07 +00:00
cclecle
bd3389aeb2 cast to double + fix complexity 2023-03-23 22:37:45 +00:00
cclecle
886a22bef2 add missing java lib in groovy 2023-03-23 22:23:57 +00:00
cclecle
e9355471ba cast float to strings 2023-03-23 22:19:48 +00:00
cclecle
3bbbe946a1 switch to readfile 2023-03-23 21:54:21 +00:00
cclecle
cc2f6353ac add coverage results publishing 2023-03-23 21:40:03 +00:00
cclecle
6e1bff135b add missing test 2023-03-23 01:12:51 +00:00
cclecle
f74633cee3 feat: remove coverage pragma because we need to be realistic
test: add missing unit test
2023-03-23 00:07:53 +00:00
cclecle
3bb580689a improve formating
fix kwargs call
2023-03-22 23:32:36 +00:00
cclecle
235af0fd6d apply black 2023-03-22 23:21:39 +00:00
cclecle
8bdc425b3c updates 2023-03-22 23:13:40 +00:00
cclecle
4701015ba8 update name 2023-03-22 23:06:20 +00:00
cclecle
bf9b4f2207 update from pychangelogfactory 2023-03-22 23:05:20 +00:00
cclecle
31df26eb48 fix: twine cmd line 2023-03-22 09:43:00 +00:00
cclecle
7fa5de67a6 remove mermaid caus pyaml is having issue.. 2023-03-22 08:52:17 +00:00
cclecle
99ee668fe0 add mermaid 2023-03-22 01:34:07 +00:00
cclecle
e13561007b add mermaid2 and markdownextradata doc exts 2023-03-22 01:30:17 +00:00
cclecle
fe0bb13d86 revert yo mkdocs-with-pdf 2023-03-22 01:18:28 +00:00
cclecle
4fc9032cd0 test 2023-03-22 01:15:13 +00:00
cclecle
72e43e7c1c disable mermaid/markdown 2023-03-22 00:42:21 +00:00
cclecle
6d7411693e update installation doc
mkdocs options
fix jenkins
2023-03-22 00:31:07 +00:00
cclecle
1338ef5f8e fix usernamePassword in jenkins 2023-03-21 23:56:48 +00:00
cclecle
da411f51e3 add black formating 2023-03-21 23:46:24 +00:00
cclecle
e9559e97fb fix: pypi upload 2023-03-21 23:38:02 +00:00
cclecle
158b85e29c add pypi publish 2023-03-21 23:27:28 +00:00
cclecle
d4545f999b test: fix doc 2023-03-20 09:04:41 +00:00
cclecle
21fc9e7c52 fix: remove tag_regex from toml -> we want the full version 2023-03-20 01:49:01 +00:00
cclecle
13a584c7d9 fix missing packaging 2023-03-20 01:44:37 +00:00
cclecle
6dfe0682d7 fix jenkins script 2023-03-20 01:09:32 +00:00
cclecle
d15b495f0a test local gitversionhelper import 2023-03-20 01:07:14 +00:00
cclecle
c9df0e7409 fix: doc 2023-03-20 00:28:19 +00:00
cclecle
866e8ff7ff test 2023-03-20 00:25:11 +00:00
cclecle
d786d27cd7 fix: ident 2023-03-20 00:19:57 +00:00
cclecle
fb2f3eb412 escape toml 2023-03-20 00:16:11 +00:00
cclecle
d93fd3286c test: tag_regex to extract verison only 2023-03-20 00:03:58 +00:00
cclecle
f361a5189a test doc 2023-03-19 23:09:25 +00:00
cclecle
3751ef1c93 test 2023-03-19 22:55:23 +00:00
cclecle
d5e003c86c update Jenkinsfile 2023-03-19 22:40:38 +00:00
cclecle
8b2b0ca07f update jenkinsfile 2023-03-19 22:31:15 +00:00
cclecle
ab1a69b8f8 cleaning jenkins file
updating documentation in toml
2023-03-19 22:08:22 +00:00
cclecle
a564e04219 fix: remove setuptools-git-versioning stuff 2023-03-19 22:03:09 +00:00
cclecle
a29c1778fc test: fall back to setuptools_scm 2023-03-19 21:57:15 +00:00
cclecle
2ee687959e fix: typo 2023-03-19 21:01:45 +00:00
cclecle
e226f16292 fix: typo 2023-03-19 20:54:02 +00:00
cclecle
8ea6b43a73 fix: git remote credentials 2023-03-19 20:50:50 +00:00
cclecle
cb213df6b6 fix: workaround for setuptools-git-versioning version guess 2023-03-19 20:44:31 +00:00
cclecle
2c87f5488d test: bump dev version 2023-03-19 20:05:32 +00:00
cclecle
348204abb5 fix: project username 2023-03-19 19:59:01 +00:00
cclecle
48114149fb fix: typo in jenkins pipeline file 2023-03-19 19:54:46 +00:00
cclecle
5eb2f1c5cf fix: pipeline 2023-03-19 19:50:23 +00:00
cclecle
65927077aa fix: git credentials for pushing 2023-03-19 19:49:57 +00:00
cclecle
0c02512814 fix: remove branch on git push (useless) 2023-03-19 19:35:37 +00:00
cclecle
9f900ba597 fix git tag cmd 2023-03-19 19:31:37 +00:00
cclecle
97612703b7 fix git username/address in jenkinsfile 2023-03-19 19:26:04 +00:00
cclecle
5bb8b35c73 update jenkinsfile 2023-03-19 19:21:48 +00:00
cclecle
a6c2513d9f fix fstrings 2023-03-19 18:55:58 +00:00
cclecle
70ef90a529 fix jenkinsfile python 2023-03-19 18:53:52 +00:00
cclecle
f728842a5a random fixes 2023-03-19 18:51:35 +00:00
cclecle
4837a99ac6 fix deps 2023-03-19 18:46:52 +00:00
cclecle
3e130e6bdf revert to full setuptools-git-versioning in toml file 2023-03-19 18:42:24 +00:00
cclecle
d7fbf52647 test 2023-03-19 18:23:42 +00:00
cclecle
f5c97757b6 make setuptools-git-versioning use pygitversionhelper
+ add getCurrentFormatedVersion()
2023-03-19 18:22:26 +00:00
cclecle
3c0e5bebc2 update doc 2023-03-19 18:02:02 +00:00
cclecle
c5962d533b fix tag -> version api 2023-03-19 17:59:27 +00:00
cclecle
e9e25793d8 fix doc bullets 2023-03-19 17:57:28 +00:00
cclecle
17f74d6675 test gitversionhelper calls 2023-03-19 17:57:20 +00:00
cclecle
264c51de2e update CI/CD 2023-03-19 17:51:11 +00:00
cclecle
ac942480cb feature: add log-line in pipeline 2023-03-19 10:34:34 +00:00
cclecle
be9ef684e4 update jenkinsFile => no error when master build are skipped because of
no-tag
2023-03-19 10:26:23 +00:00
cclecle
792666c6ec fix: rename hash -> commit_hash to avoid using builtin symbol 2023-03-19 10:17:58 +00:00
cclecle
2951e70c47 fix: escapes git strings options 2023-03-19 10:15:23 +00:00
cclecle
63b3f25b33 fix quality warning + commit messages unittest 2023-03-19 10:03:50 +00:00
cclecle
cefa964a3a add commit class 2023-03-19 09:52:42 +00:00
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
cclecle
a223500509 cleanup code 2023-03-18 19:38:29 +00:00
cclecle
04578b3066 fix typing + add formated_output option to bump
add more unittest
2023-03-18 19:34:26 +00:00
cclecle
2598a41227 fix kwargs 2023-03-18 09:25:43 +00:00
cclecle
4e43629dfd test docstrings 2023-03-18 02:26:12 +00:00
cclecle
05093566d6 fix doc + try **kwargs 2023-03-18 01:59:21 +00:00
cclecle
f3f3459ccf improve quality 2023-03-18 01:42:53 +00:00
cclecle
df74016945 improve quality 2023-03-18 01:39:25 +00:00
cclecle
bce35f0a60 fix quality 2023-03-18 01:32:44 +00:00
cclecle
39e7d8236c update: update: quality column length check 120 -> 140 2023-03-18 01:23:33 +00:00
cclecle
6a1331d1bf improve quality 2023-03-18 01:22:32 +00:00
cclecle
96250682ec fix missing user/mail for git 2023-03-18 00:59:35 +00:00
cclecle
014e8ac0d2 fix + more unittests 2023-03-18 00:49:33 +00:00
cclecle
b74269b39b more tests and features 2023-03-18 00:10:00 +00:00
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
cclecle
2d718d2e4c fix: wrong cls usage in self class 2023-03-17 18:32:38 +00:00
cclecle
525200b1fc trial fix tag_filter 2023-03-17 18:26:11 +00:00
cclecle
70a1c6ef8c start fixing pylint warnings + add docstrings 2023-03-17 18:09:34 +00:00
18 changed files with 2983 additions and 672 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>gitversionhelper</name>
<name>pygitversionhelper</name>
<comment></comment>
<projects>
</projects>

287
Jenkinsfile vendored
View File

@@ -6,6 +6,14 @@
// 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/>.
import groovy.xml.XmlUtil
import static javax.xml.xpath.XPathConstants.*
import javax.xml.xpath.*
import groovy.xml.DOMBuilder
import groovy.xml.dom.DOMCategory
import java.math.RoundingMode
import java.math.BigDecimal
// configurable settings:
// use to send email if workflow problem
def _MaintainerName = "CHACHA"
@@ -17,12 +25,19 @@ def _bPreRelease = false
// toogle Draft flag on Gitea release system => If False, TAG is not created
def _bDraft = false
// release content / changelog management
def _bAutoChangelog = false //Not supported yet
def _bAutoChangelog = true //Not supported yet
def _ReleaseContent_Title = "_CI/CD Automatic Release_"
def bPushMasterOnPypi = true
// full rebuild toogle
def _bFullRebuilt = true
def _MkDocsWebURL = "dabauto--mkdocs-web.dmz.chacha.home/mkdocs-web/"
def _MkDocsWebCredentials = "2c5b684e-3787-4b37-8aca-b3dd4a383fe2"
def _PypiCredentials = "Pypi"
def badge_coverage = addEmbeddableBadgeConfiguration(id: "coverage", subject: "coverage")
def badge_maintainability = addEmbeddableBadgeConfiguration(id: "maintainability", subject: "maintainability")
def badge_quality = addEmbeddableBadgeConfiguration(id: "quality", subject: "quality score")
// commands Helper: /!\ Made for GITEA /!\
String determineRepoUserName() {
@@ -59,6 +74,46 @@ String ExtractBaseVersion(inVersion) {
return matcher[0][1]
}
int GetCoverageValue(String CoverageFilePath,String XPath)
{
//File file = new File(CoverageFilePath)
//coverageReportRaw = file.getText('UTF-8')
coverageReportRaw = readFile(CoverageFilePath)
coverageReport = DOMBuilder.parse(new StringReader(coverageReportRaw), false, false)
coverageReportRoot = coverageReport.documentElement
def xpath = XPathFactory.newInstance().newXPath()
res = xpath.evaluate(XPath,coverageReportRoot,NUMBER)
return res
}
String getColorScale(BigDecimal value)
{
if( value >9) { return "Goldenrod"}
else if( value >6) { return "seagreen"}
else if( value >4) { return "orange"}
else if( value >2) { return "darkred"}
else { return "dimgrey"}
}
String getColorScale_reversed(BigDecimal value)
{
if( value >9) { return "dimgrey"}
else if( value >6) { return "darkred"}
else if( value >4) { return "orange"}
else if( value >2) { return "seagreen"}
else { return "Goldenrod"}
}
int GetCoverageValue_lines_valid(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@lines-valid") }
int GetCoverageValue_lines_covered(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@lines-covered") }
int GetCoverageValue_line_rate(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@line-rate") }
int GetCoverageValue_branches_valid(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branches-valid") }
int GetCoverageValue_branches_covered(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branches-covered") }
int GetCoverageValue_branch_rate(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@branch-rate") }
int GetCoverageValue_complexity(String CoverageFilePath) { return GetCoverageValue(CoverageFilePath,"/coverage/@complexity") }
pipeline {
// for Docker based build (preferable)
@@ -94,12 +149,12 @@ pipeline {
PY_PROJECT_VERSION = "__NOTSET__"
PY_PROJECT_VERSION_STRIPPED = "__NOTSET__"
}
stages {
stage("Prepare") {
steps {
script{
script {
if (_bFullRebuilt) {
// start by cleaning the workspace (not using cleanWs() because we want to keep the directory itself)
// => this is needed to fetch it again with custom options
@@ -108,8 +163,7 @@ pipeline {
else {
sh("find ~/. -name . ! -path './TEST_ENV/*' ! -path './BUILD_ENV/*' -o -prune -exec rm -rf -- {} +")
}
if(_GIT_BRANCH!="master")
{
if(_GIT_BRANCH!="master") {
_bPreRelease = true
}
}
@@ -120,51 +174,114 @@ pipeline {
echo("_GITEA_BASE_URL: . . . . . . . . $_GITEA_BASE_URL")
echo("GIT_COMMIT:. . . . . . . . . . . $GIT_COMMIT ")
echo("_PROJECT_USER_NAME:. . . . . . . $_PROJECT_USER_NAME")
echo("_GITEA_PROJECT_NAME: . . . . . . $_PROJECT_NAME")
echo("_PROJECT_NAME: . . . . . . . . . $_PROJECT_NAME")
echo("_MaintainerEmail:. . . . . . . . $_MaintainerEmail")
echo("_MaintainerName:. . . . . . . . $_MaintainerName")
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python BUILD_ENV")
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python TEST_ENV")
sh("virtualenv --pip=embed --setuptools=embed --wheel=embed --no-periodic-update --activators bash,python TOOLS_ENV")
sh(". ~/BUILD_ENV/bin/activate && pip install --upgrade setuptools build pip copier jinja2-slug toml \"setuptools-git-versioning<2\"")
sh(". ~/BUILD_ENV/bin/activate && pip install --upgrade setuptools build pip copier jinja2-slug toml")
sh(". ~/TOOLS_ENV/bin/activate && pip install simple_rest_client requests twine")
script {
if(_PROJECT_NAME!="pygitversionhelper") {
sh(". ~/TOOLS_ENV/bin/activate && pip install pygitversionhelper")
}
else
{
//TODO: need to install pygitversionhelper deps from a better way...
sh(". ~/TOOLS_ENV/bin/activate && pip install packaging")
}
}
sh("git config --global user.email $_MaintainerEmail")
sh("git config --global user.name $_MaintainerName")
sh("git config --global init.defaultBranch master")
}
}
stage("GetCode") {
steps {
dir("gitrepo") {
// manually checkout the repository, with All branches and tags
// manually checkout the repository, with All branches and tags
// => because we might need them for version and changelog computing
checkout([ $class: "GitSCM",
branches: [[name: GIT_BRANCH]],
extensions: [[$class: "CloneOption", noTags: false, shallow: false, depth: 0, reference: '']],
userRemoteConfigs: [[credentialsId: _SCMCredentials, url: GIT_URL]]])
script{
if(_GIT_BRANCH=="master")
{
if(sh(returnStdout: true, script: "git tag --points-at HEAD").trim().isEmpty())
{
error("master push/merge must have an explicit tag release number")
script {
withCredentials([usernamePassword(credentialsId: _SCMCredentials, passwordVariable: 'GIT_PASSWORD', usernameVariable: 'GIT_USERNAME')]) {
sh("git remote set-url origin https://${GIT_USERNAME}:${GIT_PASSWORD}@chacha.ddns.net/gitea/${_PROJECT_USER_NAME}/${_PROJECT_NAME}.git")
}
if(_GIT_BRANCH=="master") {
if(sh(returnStdout: true, script: "git tag --points-at HEAD").trim().isEmpty()) {
BUMPED_VERSION = sh(script: """#!/bin/sh -
|. ~/TOOLS_ENV/bin/activate
|exec python - << '__EOWRAPPER__'
|
|import re
|
|try:
| from pygitversionhelper import gitversionhelper
|except ImportError:
| from src.pygitversionhelper import gitversionhelper
|
|lastcommit=gitversionhelper.commit.getLast(same_branch=True)
|msg=gitversionhelper.commit.getMessage(lastcommit)
|
|_match=re.search(r"\\s*(?:#)?\\s*(?<=new-tag:)(?:\\s*)(?P<TAG>\\S*)",msg)
|print(_match.group("TAG"),end="")
|
|__EOWRAPPER__
""".stripMargin(),
returnStdout: true).trim()
if(BUMPED_VERSION.isEmpty()) {
echo "master push/merge must have an explicit tag release number, stopping pipeline"
currentBuild.getRawBuild().getExecutor().doStop()
}
else {
echo "new-tag requested in commit message: $BUMPED_VERSION"
sh("git tag $BUMPED_VERSION")
sh("git push origin --tags")
}
}
}
// using git to get the latest tag on this branch
def latestTag = sh(returnStdout: true, script: "git tag --sort=-creatordate | head -n 1").trim()
latestTag = sh(script: """#!/bin/sh -
|. ~/TOOLS_ENV/bin/activate
|exec python - << '__EOWRAPPER__'
|
|try:
| from pygitversionhelper import gitversionhelper
|except ImportError:
| from src.pygitversionhelper import gitversionhelper
|
|print(gitversionhelper.tag.getLastTag(same_branch=True),end ="")
|
|__EOWRAPPER__
""".stripMargin(),
returnStdout: true)
echo("latestTag:. . . . . . . . . . . . $latestTag ")
// using setuptools_git_versioning to get the generated version number based on Git tags and logs
// TODO: read dev_template from toml
// get current (or bumped) version number from git history
PY_PROJECT_VERSION = sh(script: """#!/bin/sh -
|. ~/BUILD_ENV/bin/activate
|. ~/TOOLS_ENV/bin/activate
|exec python - << '__EOWRAPPER__'
|
|from setuptools_git_versioning import version_from_git
|try:
| from pygitversionhelper import gitversionhelper
|except ImportError:
| from src.pygitversionhelper import gitversionhelper
|
|print(str(version_from_git(tag_filter="^\\d+\\.\\d+\\.\\d+\$",dev_template = "{tag}.post{ccount}")),end ="")
|print(gitversionhelper.version.getCurrentVersion(formated_output=True,version_std="PEP440",bump_type="dev",bump_dev_strategy="post"),end ="")
|
|__EOWRAPPER__
""".stripMargin(),
@@ -172,16 +289,18 @@ pipeline {
echo("PY_PROJECT_VERSION: . . . . . . . . . $PY_PROJECT_VERSION")
PY_PROJECT_VERSION_STRIPPED=ExtractBaseVersion(PY_PROJECT_VERSION)
// Manually pushing a new tag with version string guessed by gitversionhelper
// because setuptools-git-versioning / setuptools_scm cant fing tag on other branches, so will guess a wrong version without this tag.
if(latestTag!=PY_PROJECT_VERSION) {
sh("git tag $PY_PROJECT_VERSION")
sh("git push origin --tags")
}
// specific handling to test the template itself
// => little hacky... creating a new git repo with a commit/tag corresponding to HEAD of the official one
if(_PROJECT_NAME=="pyChaChaDummyProject") //specific case to test the template itself
{
sh("git config --global user.email $_MaintainerEmail")
sh("git config --global user.name $_MaintainerName")
if(_PROJECT_NAME=="pyChaChaDummyProject") { //specific case to test the template itself
sh("rm -Rf ~/_gitrepo || true")
//sh(". ~/BUILD_ENV/bin/activate && python -m copier --vcs-ref HEAD --prereleases --defaults ./ ~/_gitrepo")
sh(script: """#!/bin/sh -
|. ~/BUILD_ENV/bin/activate
|exec python - << '__EOWRAPPER__'
@@ -240,7 +359,7 @@ pipeline {
steps {
// no need for a build-env: setuptools is already creating one
dir("gitrepo") {
script{
script {
// actually doing the package build
sh(". ~/BUILD_ENV/bin/activate && python -m build .")
}
@@ -256,7 +375,7 @@ pipeline {
def wheelPath = findFiles(glob: "**/dist/*.whl")[0]
echo "wheel artifact path: $wheelPath"
// install the package, with *test* optionnal packages, as user
sh(". ~/TEST_ENV/bin/activate && pip install --find-links dist/ ${PY_PROJECT_NAME} .[test,coverage-check,quality-check,type-check,doc-gen]")
sh(". ~/TEST_ENV/bin/activate && pip install --find-links dist/ ${PY_PROJECT_NAME} .[test,coverage-check,quality-check,type-check,doc-gen,complexity-check]")
}
}
}
@@ -266,19 +385,27 @@ pipeline {
steps {
dir("gitrepo") {
sh(". ~/TEST_ENV/bin/activate && python -m helpers --type-check --quality-check")
script {
def jsonObj = readJSON file: "helpers-results/quality_check/metrics.json"
quality_score = new BigDecimal(jsonObj["GlobalScore"])
sz_quality_score = quality_score.setScale(2, RoundingMode.HALF_EVEN).toString()
badge_quality.setStatus(sz_quality_score)
badge_quality.setColor(getColorScale(quality_score))
}
sh(". ~/TEST_ENV/bin/activate && python -m helpers --complexity-check")
}
}
post {
always {
dir("gitrepo") {
publishHTML([
reportDir: "helpers-results/quality_check",
reportFiles: "report.html",
reportName: "quality-report",
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true])
}
always {
dir("gitrepo") {
publishHTML([
reportDir: "helpers-results/quality_check",
reportFiles: "report.html",
reportName: "quality-report",
allowMissing: false,
alwaysLinkToLastBuild: true,
keepAll: true])
}
}
}
}
@@ -320,6 +447,14 @@ pipeline {
style: 'stackedArea',
keepRecords: true,
numBuilds: ''])
plot([ csvFileName: 'plot-4ceb9ee2-ca78-11ed-afa1-0242ac120002.csv',
csvSeries: [[ file: 'gitrepo/helpers-results/complexity_check/MI.csv', inclusionFlag: 'INCLUDE_BY_STRING',exclusionValues: 'MeanMaintainability', url: '']],
group: 'metrics',
title: 'maintainability',
style: 'stackedArea',
keepRecords: true,
numBuilds: ''])
}
}
@@ -332,6 +467,36 @@ pipeline {
println unit_test_full_name__html
unit_test_full_name__xml=findFiles(glob: "helpers-results/unit_test_full/*.xml")[0].getName()
println unit_test_full_name__xml
coverage_report_path = "helpers-results/unit_test_coverage/test_coverage.xml"
println GetCoverageValue_lines_valid(coverage_report_path)
println GetCoverageValue_lines_covered(coverage_report_path)
println GetCoverageValue_line_rate(coverage_report_path)
println GetCoverageValue_branches_valid(coverage_report_path)
println GetCoverageValue_branches_covered(coverage_report_path)
println GetCoverageValue_branch_rate(coverage_report_path)
println GetCoverageValue_complexity(coverage_report_path)
full_rate = new BigDecimal( 10*(GetCoverageValue_line_rate(coverage_report_path) + GetCoverageValue_branch_rate(coverage_report_path)) / 2 )
sz_full_rate = full_rate.setScale(2, RoundingMode.HALF_EVEN).toString()
badge_coverage.setStatus(sz_full_rate)
badge_coverage.setColor(getColorScale(full_rate))
//complexity = new BigDecimal( 10*GetCoverageValue_complexity(coverage_report_path))
//sz_complexity = complexity.setScale(2, RoundingMode.HALF_EVEN).toString()
//badge_complexity.setStatus(sz_complexity)
//badge_quality.setColor(getColorScale_reversed(complexity))
//badge_maintainability
records = readCSV file: 'helpers-results/complexity_check/MI.csv'
maintainability = records[1][1]
badge_maintainability.setStatus(maintainability)
if ( maintainability == 'D') { badge_maintainability.setColor( "dimgrey")}
else if( maintainability == 'C') { badge_maintainability.setColor( "darkred")}
else if( maintainability == 'B') { badge_maintainability.setColor( "orange")}
else if( maintainability == 'A') { badge_maintainability.setColor( "seagreen")}
else if( maintainability == 'A+') { badge_maintainability.setColor( "Goldenrod")}
}
}
}
@@ -388,20 +553,27 @@ pipeline {
def GITEA_LOGIN_TOKEN=credentials("GiteaCHACHAPush")
}
steps {
sh("python3 -m pip install simple_rest_client requests")
dir("gitrepo") {
script {
def CurrentDateTime=java.time.LocalDateTime.now()
withCredentials([string( credentialsId: _MkDocsWebCredentials,variable: 'MKDOCSTOKEN' )])
{
sh(script: """#!/usr/bin/env python3
|from simple_rest_client.api import API
|from simple_rest_client.resource import Resource
withCredentials([string( credentialsId: _MkDocsWebCredentials,variable: 'MKDOCSTOKEN' )]) {
sh(script: """#!/bin/sh -
|. ~/TOOLS_ENV/bin/activate
|exec python - << '__EOWRAPPER__'
|
|import json
|import glob
|import requests
|import shutil
|
|from simple_rest_client.api import API
|from simple_rest_client.resource import Resource
|
|try:
| from pygitversionhelper import gitversionhelper
|except ImportError:
| from src.pygitversionhelper import gitversionhelper
|
|from urllib.parse import urljoin
|
|class GiteaRepoCommits(Resource):
@@ -478,9 +650,18 @@ pipeline {
|response=requests.post("http://${_MkDocsWebURL}/API.php?REQ=pushDoc",data=reqData,files=files)
|if response.status_code != 200:
| raise RuntimeError(f"Wrong server response: {response.status_code}")
|
""".stripMargin())
|
|__EOWRAPPER__
""".stripMargin())
}
if((_GIT_BRANCH=="master") && (bPushMasterOnPypi)) {
withCredentials([usernamePassword( credentialsId: _PypiCredentials, passwordVariable: 'PYPI_PASSWORD', usernameVariable: 'PYPI_USERNAME')]) {
sh(script: """#!/bin/sh -
|. ~/TOOLS_ENV/bin/activate
|exec twine upload -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} --non-interactive --disable-progress-bar dist/*
""".stripMargin())
}
}
}
}
}

View File

@@ -1,48 +1,40 @@
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=status&status=active&color=seagreen)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=doc&status=MkDocs&color=blue)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=jenkins-unittest&job={{repository}}-{{branch}})
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=licence&status=CC%20BY-NC-SA%204.0&color=blue)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=coverage)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=maintainability)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?job={{repository}}-{{branch}}&build=0&config=quality)
![](https://chacha.ddns.net/jenkins/buildStatus/icon?subject=licence&status=CC%20BY-NC-SA%204.0&color=teal)
![](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/{{repository}}/{{branch}}/latest/).
## Features
- 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)
- get commit message history
### Generic pipeline skeleton:
- Prepare
- GetCode
- BuildPackage
- Install
- CheckCode
- PlotMetrics
- RunUnitTests
- GenDOC
- PostRelease
### 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 strategy: 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,131 @@
# 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 pypi repository (prefered):
## 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 pygitversionhelper
From downloaded .whl file:
python -m pip install pygitversionhelper-<VERSION>-py3-none-any.whl
From master git repository:
python -m pip install git+https://chacha.ddns.net/gitea/chacha/pygitversionhelper.git@master
## 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.version.getLastVersion()}")
Get the last found version in the repository [return formated string]:
print(f"most recent repository version: {gitversionhelper.version.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"current repository version: {gitversionhelper.version.getCurrentVersion()}")
Or with formated output:
print(f"current repository version: {gitversionhelper.version.getCurrentVersion(formated_output=True)}")
Typical usage in CI/CD env:
bumped_version = gitversionhelper.version.getCurrentVersion( formated_output=True, \
version_std="PEP440", \
bump_type="dev", \
bump_dev_strategy="post")
print(f"current repository version: {bumped_version}")
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

@@ -16,89 +16,101 @@ import os
import logging
import sys
if __package__=="helpers":
if __package__ == "helpers":
# when calling the module from: > python -m helpers
from .types_check import types_check
from .quality_check import quality_check
from .unit_test import unit_test
from .doc_gen import doc_gen
from .changelog_gen import changelog_gen
else:
from .types_check import types_check
from .quality_check import quality_check
from .unit_test import unit_test
from .doc_gen import doc_gen
from .changelog_gen import changelog_gen
from .complexity_check import complexity_check
else:
# when calling the __main__.py file (from IDE)
from helpers.types_check import types_check
from helpers.quality_check import quality_check
from helpers.unit_test import unit_test
from helpers.doc_gen import doc_gen
from helpers.changelog_gen import changelog_gen
from helpers.types_check import types_check
from helpers.quality_check import quality_check
from helpers.unit_test import unit_test
from helpers.doc_gen import doc_gen
from helpers.changelog_gen import changelog_gen
from helpers.complexity_check import complexity_check
logging.getLogger().setLevel(logging.INFO)
if __name__ == "__main__":
project_rootdir_path=Path(__file__).parent.parent.absolute()
project_rootdir_path = Path(__file__).parent.parent.absolute()
with open(project_rootdir_path / "pyproject.toml", mode="rb") as fp:
pyproject = tomli.load(fp)
parser = argparse.ArgumentParser( prog = 'continuous-integration-helper',
description = 'A tiny set of scripts to help continous integration on python')
parser.add_argument('-tc', '--type-check', dest='typecheck', action='store_true', help='enable static typing check')
parser.add_argument('-ut', '--unit-test', dest='unittest', action='store_true', help='enable unit-test')
parser.add_argument('-cc', '--coverage-check', dest='coveragecheck', action='store_true', help='enable unit-test coverage check (requires unit-test)')
parser.add_argument('-qc', '--quality-check', dest='qualitycheck', action='store_true', help='enable code quality check')
parser.add_argument('-dg', '--doc-gen', dest='docgen', action='store_true', help='enable documentation generation using MkDoc')
parser.add_argument('-pdf', '--doc-gen-pdf', dest='docgenpdf', action='store_true', help='enable pdf documentation export (requires doc-gen)')
parser.add_argument('-clg', '--changelog-gen', dest='changeloggen', action='store_true', help='enable changelog generation')
parser = argparse.ArgumentParser(
prog="continuous-integration-helper", description="A tiny set of scripts to help continous integration on python"
)
parser.add_argument("-tc", "--type-check", dest="typecheck", action="store_true", help="enable static typing check")
parser.add_argument("-ut", "--unit-test", dest="unittest", action="store_true", help="enable unit-test")
parser.add_argument(
"-cc", "--coverage-check", dest="coveragecheck", action="store_true", help="enable unit-test coverage check (requires unit-test)"
)
parser.add_argument("-qc", "--quality-check", dest="qualitycheck", action="store_true", help="enable code quality check")
parser.add_argument("-dg", "--doc-gen", dest="docgen", action="store_true", help="enable documentation generation using MkDoc")
parser.add_argument(
"-pdf", "--doc-gen-pdf", dest="docgenpdf", action="store_true", help="enable pdf documentation export (requires doc-gen)"
)
parser.add_argument("-clg", "--changelog-gen", dest="changeloggen", action="store_true", help="enable changelog generation")
parser.add_argument("-cpc", "--complexity-check", dest="complexitycheck", action="store_true", help="enable complexity check")
args = parser.parse_args()
##################################
# Dev / Debug forced toogles
#
#--------------------------------
# --------------------------------
#
#args.typecheck = True
#args.qualitycheck = True
#args.unittest = True
#args.coveragecheck = True
#args.docgen = True
#args.docgenpdf = True
#args.changeloggen = True
# args.typecheck = True
# args.qualitycheck = True
# args.unittest = True
# args.coveragecheck = True
# args.docgen = True
# args.docgenpdf = True
# args.changeloggen = True
# args.complexitycheck = True
helpers = []
if args.typecheck == True:
if args.typecheck == True:
helpers.append(types_check)
if args.unittest == True:
if args.unittest == True:
helpers.append(unit_test)
if args.coveragecheck == True:
if args.coveragecheck == True:
if args.unittest == True:
unit_test.enable_coverage_check = True
else:
raise RuntimeError("unit-test is required to enable coverage-check")
if args.qualitycheck == True:
if args.qualitycheck == True:
helpers.append(quality_check)
if args.docgen == True:
if args.docgen == True:
helpers.append(doc_gen)
if args.docgenpdf==True:
if args.docgenpdf == True:
if args.docgen == True:
doc_gen.enable_gen_pdf = True
else:
raise RuntimeError("doc-gen is required to enable doc-gen-pdf")
if args.changeloggen == True:
if args.changeloggen == True:
helpers.append(changelog_gen)
if args.complexitycheck == True:
helpers.append(complexity_check)
for helper in helpers:
helper.set_context(project_rootdir_path,pyproject)
helper.set_context(project_rootdir_path, pyproject)
helper.reset_result_dir()
helper.do_job()

View File

@@ -9,16 +9,15 @@
from __future__ import annotations
from typing import TYPE_CHECKING
#from pathlib import Path
#import os
#import datetime
# from pathlib import Path
# import os
# import datetime
from .helper_base import helper_base
class changelog_gen(helper_base):
@classmethod
def do_job(cls):
pass
class changelog_gen(helper_base):
@classmethod
def do_job(cls):
pass

View File

@@ -0,0 +1,70 @@
# pyChaChaDummyProject (c) by chacha
#
# pyChaChaDummyProject 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/>.
from __future__ import annotations
from typing import TYPE_CHECKING
# from pathlib import Path
# import os
import statistics
import csv
from json import loads as JSON_LOADS
from radon.complexity import cc_rank, SCORE
from radon.cli import Config
from radon.cli.harvest import CCHarvester, HCHarvester, MIHarvester
from .helper_base import helper_withresults_base
from pprint import pprint
class complexity_check(helper_withresults_base):
@classmethod
def do_job(cls):
config = Config(
exclude="__init__\.py",
ignore=None,
order=SCORE,
show_closures=False,
no_assert=True,
min="A",
max="F",
multi=False,
)
h = MIHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
res = JSON_LOADS(h)
with open(cls.get_result_dir() / "MI.json", "w", newline="") as oFile:
oFile.write(h)
mean = statistics.mean(_["mi"] for _ in res.values())
if mean >= 65:
rank = "A+"
elif mean >= 20:
rank = "A"
elif mean >= 10:
rank = "B"
else:
rank = "C"
RES_MI = {"MeanMaintainability": mean, "MaintainabilityIndex": rank}
with open(cls.get_result_dir() / "MI.csv", "w", newline="") as oFile:
writer = csv.DictWriter(oFile, fieldnames=RES_MI.keys())
writer.writeheader()
writer.writerow(RES_MI)
config = Config(exclude=None, ignore=None, order=SCORE, show_closures=False, no_assert=True, min="A", max="F", multi=False)
h = CCHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
with open(cls.get_result_dir() / "CC.json", "w", newline="") as oFile:
oFile.write(h)
config = Config(exclude=None, ignore=None, order=SCORE, show_closures=False, no_assert=True, min="A", max="F", by_function=None)
h = HCHarvester([str(_) for _ in sorted((cls.project_rootdir_path / "src").rglob("*.py"))], config).as_json()
with open(cls.get_result_dir() / "HC.json", "w", newline="") as oFile:
oFile.write(h)

View File

@@ -17,6 +17,7 @@ from pathlib import Path
from distutils.dir_util import copy_tree
import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
@@ -24,35 +25,34 @@ except ImportError:
from .helper_base import helper_withresults_base
class doc_gen(helper_withresults_base):
class doc_gen(helper_withresults_base):
enable_gen_pdf: bool = False
@classmethod
def do_job(cls):
print(cls.project_rootdir_path)
print()
@classmethod
def do_job(cls):
# create doc root dir
doc_path = cls.project_rootdir_path/"docs"
doc_path = cls.project_rootdir_path / "docs"
cls._reset_dir(doc_path)
site_path = cls.get_result_dir()/"site"
site_path = cls.get_result_dir() / "site"
cls._reset_dir(site_path)
# copy files from main project dir
shutil.copyfile(str(cls.project_rootdir_path/"README.md"),str(doc_path/"README.md"))
shutil.copyfile(str(cls.project_rootdir_path/"LICENSE.md"),str(doc_path/"LICENSE.md"))
shutil.copyfile(str(cls.project_rootdir_path / "README.md"), str(doc_path / "README.md"))
shutil.copyfile(str(cls.project_rootdir_path / "LICENSE.md"), str(doc_path / "LICENSE.md"))
# copy files from static-doc dir
copy_tree(str(cls.project_rootdir_path/"docs-static"),str(doc_path))
copy_tree(str(cls.project_rootdir_path / "docs-static"), str(doc_path))
# generating API doc + nav from python docstrings
reference_path = doc_path / "reference"
cls._reset_dir(reference_path)
for path in sorted((cls.project_rootdir_path / "src").rglob("*.py")):
module_path = path.relative_to(cls.project_rootdir_path/ "src").with_suffix("")
doc_path = path.relative_to(cls.project_rootdir_path/ "src").with_suffix(".md")
module_path = path.relative_to(cls.project_rootdir_path / "src").with_suffix("")
doc_path = path.relative_to(cls.project_rootdir_path / "src").with_suffix(".md")
full_doc_path = Path(reference_path, doc_path)
parts = list(module_path.parts)
@@ -61,43 +61,44 @@ class doc_gen(helper_withresults_base):
parts = parts[:-1]
elif parts[-1] == "__main__":
continue
cls._reset_dir(os.path.dirname(full_doc_path))
with open(full_doc_path, "w+") as fd:
identifier = "src."+".".join(parts)
identifier = "src." + ".".join(parts)
print("::: " + identifier, file=fd)
cmdopts = [f"{sys.executable}","-m","mkdocs","-v","build","--site-dir",str(site_path),"--clean"]
cmdopts = [f"{sys.executable}", "-m", "mkdocs", "-v", "build", "--site-dir", str(site_path), "--clean"]
# little hack here, to enable / disable pdf generation using own class config
# => reason is mkdocs seems to try loading the plugin even if we disable it, so we need to
# => reason is mkdocs seems to try loading the plugin even if we disable it, so we need to
# manually process the configuration file.
mkdocsCfg=None
with open(cls.project_rootdir_path / "mkdocs.yml",'r') as mkdocsCfgFile:
mkdocsCfg = None
with open(cls.project_rootdir_path / "mkdocs.yml", "r") as mkdocsCfgFile:
mkdocsCfg = yaml.load(mkdocsCfgFile, Loader=yaml.SafeLoader)
if cls.enable_gen_pdf==True:
mkdocsCfg['plugins'].append({ 'with-pdf':{
'cover_subtitle': 'User Manual',
'cover_logo': str(cls.project_rootdir_path / 'docs-static' / 'Library.jpg'),
'verbose': False,
'media_type': 'print',
'exclude_pages': ['LICENSE'],
'output_path': str(site_path / 'pdf' / 'manual.pdf')
}})
if cls.enable_gen_pdf == True:
mkdocsCfg["plugins"].append(
{
"with-pdf": {
"cover_subtitle": "User Manual",
"cover_logo": str(cls.project_rootdir_path / "docs-static" / "Library.jpg"),
"verbose": False,
"media_type": "print",
"exclude_pages": ["LICENSE"],
"output_path": str(site_path / "pdf" / "manual.pdf"),
}
}
)
else:
for subelem in mkdocsCfg['plugins']:
if isinstance(subelem,dict) :
if 'with-pdf' in subelem.keys():
mkdocsCfg['plugins'].remove(subelem)
break
with open(cls.project_rootdir_path / "mkdocs.yml",'w') as mkdocsCfgFile:
mkdocsCfgFile.write(yaml.dump(mkdocsCfg, Dumper=Dumper,default_flow_style=False, sort_keys=False))
res=cls.run_cmd(cmdopts)
for subelem in mkdocsCfg["plugins"]:
if isinstance(subelem, dict):
if "with-pdf" in subelem.keys():
mkdocsCfg["plugins"].remove(subelem)
break
with open(cls.project_rootdir_path / "mkdocs.yml", "w") as mkdocsCfgFile:
mkdocsCfgFile.write(yaml.dump(mkdocsCfg, Dumper=Dumper, default_flow_style=False, sort_keys=False))
res = cls.run_cmd(cmdopts)
print(res.decode())
print(' !! done')
print(" !! done")

View File

@@ -9,64 +9,66 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from abc import ABC,abstractmethod
from abc import ABC, abstractmethod
import os
from pathlib import Path
import subprocess
if TYPE_CHECKING: # Only imports the below statements during type checking
from typing import Union
from typing import Union
class helper_base(ABC):
project_rootdir_path: Union[Path,None] = None
pyproject: Union[dict,None] = None
current_dir: Union[Path,None] = None
@classmethod
def set_context(cls,project_rootdir_path:Path, pyproject:dict):
cls.project_rootdir_path = project_rootdir_path
cls.pyproject = pyproject
cls.current_dir = Path(__file__).parent.absolute()
@classmethod
project_rootdir_path: Union[Path, None] = None
pyproject: Union[dict, None] = None
current_dir: Union[Path, None] = None
@classmethod
def set_context(cls, project_rootdir_path: Path, pyproject: dict):
cls.project_rootdir_path = project_rootdir_path
cls.pyproject = pyproject
cls.current_dir = Path(__file__).parent.absolute()
@classmethod
def get_result_dir(cls):
return None
@staticmethod
def _reset_dir(dirpath:Path):
@staticmethod
def _reset_dir(dirpath: Path):
dirpath = Path(dirpath)
if not os.path.exists(dirpath):
os.makedirs(dirpath)
[f.unlink() for f in Path(dirpath).glob("*") if f.is_file()]
@classmethod
[f.unlink() for f in Path(dirpath).glob("*") if f.is_file()]
@classmethod
def reset_result_dir(cls):
result_dir = cls.get_result_dir()
if result_dir != None:
cls._reset_dir(result_dir)
@classmethod
@classmethod
@abstractmethod
def do_job(cls):
def do_job(cls):
raise NotImplementedError()
@classmethod
@classmethod
def run_cmd_(cls, cmdarray):
process = subprocess.run(cmdarray, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True)
return process.stdout
@classmethod
@classmethod
def run_cmd(cls, cmdarray):
p = subprocess.run(cmdarray, capture_output=True)
print(p.stdout)
print(p.stderr)
return p.stdout
class helper_withresults_base(helper_base):
helper_results_dir: Union[Path,None] = None
@classmethod
helper_results_dir: Union[Path, None] = None
@classmethod
def get_result_dir(cls):
if cls.helper_results_dir==None:
cls.helper_results_dir=cls.__name__
return Path(__file__).parent.parent.absolute() / "helpers-results" / cls.helper_results_dir
if cls.helper_results_dir == None:
cls.helper_results_dir = cls.__name__
return Path(__file__).parent.parent.absolute() / "helpers-results" / cls.helper_results_dir

View File

@@ -25,197 +25,222 @@ import pylint_json2html
from .helper_base import helper_withresults_base
class PyLintMetricNotFound(Warning):
pass
class quality_check(helper_withresults_base):
PylintMessageList=dict()
@classmethod
class quality_check(helper_withresults_base):
PylintMessageList = dict()
@classmethod
def GetPylintMessageList(cls):
Messagelist=dict()
regex = r"^:([a-zA-Z-]+) \(([^\)]+)\)"
for line in cls.run_cmd([sys.executable,"-m","pylint","--list-msgs"]).splitlines():
if res:=re.search(regex,line.decode()):
Messagelist[res.group(1)]=res.group(2)
Messagelist = dict()
regex = r"^:([a-zA-Z-]+) \(([^\)]+)\)"
for line in cls.run_cmd([sys.executable, "-m", "pylint", "--list-msgs"]).splitlines():
if res := re.search(regex, line.decode()):
Messagelist[res.group(1)] = res.group(2)
cls.PylintMessageList = Messagelist
@staticmethod
def TryExtractPYReportMetric(line: str,tag: str):
regex=f"^(?:\|{tag}\s*\|)(\d+)(?=\s*|)"
if res:=re.search(regex,line):
def TryExtractPYReportMetric(line: str, tag: str):
regex = f"^(?:\|{tag}\s*\|)(\d+)(?=\s*|)"
if res := re.search(regex, line):
return float(res.group(1))
raise PyLintMetricNotFound()
@classmethod
def do_job(cls):
@classmethod
def do_job(cls):
print("checking code quality ...")
cls.GetPylintMessageList()
RES_all=dict()
RES_all = dict()
with StringIO() as StdOutput:
JsonContent=""
JsonContent = ""
with redirect_stdout(StdOutput):
pylint_Run(['--output-format=json,parseable',
'--disable=invalid-name',
'--ignore=_version.py',
'--reports=y',
'--score=yes',
'--max-line-length=120',
'src.' + cls.pyproject['project']['name']], exit=False)
with open(cls.get_result_dir()/"report.json","w+", encoding='utf-8') as Outfile:
pylint_Run(
[
"--load-plugins=pylint.extensions.mccabe",
"--output-format=json,parseable",
"--disable=invalid-name",
"--ignore=_version.py",
"--reports=y",
"--score=yes",
"--max-line-length=140",
"src." + cls.pyproject["project"]["name"],
],
exit=False,
)
with open(cls.get_result_dir() / "report.json", "w+", encoding="utf-8") as Outfile:
# hacky way of exctracting json + having overall score...
class TScanState(Enum):
TEXT_REPORT = 1
JSON_REPORT = 2
OTHER_REPORT_START = 3
OTHER_REPORT_STATISTICS = 4
OTHER_REPORT_METRICS = 5
OTHER_REPORT_DUPLICATION = 6
OTHER_REPORT_MESSAGES_CAT = 7
OTHER_REPORT_MESSAGES = 8
OTHER_REPORT_END = 99
RES_all['Statistics'] = dict()
RES_all['RawMetrics'] = dict()
RES_all['RawMetricsPercent'] = dict()
RES_all['Duplication'] = dict()
RES_all['MessagesCat'] = dict()
RES_all['Messages'] = dict()
RES_all['GlobalScore'] = -999
RES_all['NbAnalysedStatments'] = -999
RES_all['NbAnalysedLines'] = -999
TEXT_REPORT = 1
JSON_REPORT = 2
OTHER_REPORT_START = 3
OTHER_REPORT_STATISTICS = 4
OTHER_REPORT_METRICS = 5
OTHER_REPORT_DUPLICATION = 6
OTHER_REPORT_MESSAGES_CAT = 7
OTHER_REPORT_MESSAGES = 8
OTHER_REPORT_END = 99
RES_all["Statistics"] = dict()
RES_all["RawMetrics"] = dict()
RES_all["RawMetricsPercent"] = dict()
RES_all["Duplication"] = dict()
RES_all["MessagesCat"] = dict()
RES_all["Messages"] = dict()
RES_all["GlobalScore"] = -999
RES_all["NbAnalysedStatments"] = -999
RES_all["NbAnalysedLines"] = -999
ScanState = TScanState.TEXT_REPORT
for line in StdOutput.getvalue().split('\n'):
print(line)
if ScanState == TScanState.TEXT_REPORT:
for line in StdOutput.getvalue().split("\n"):
print(line)
if ScanState == TScanState.TEXT_REPORT:
# ignoring this part, we need json
if line=='[':
JsonContent+=line
if line == "[":
JsonContent += line
ScanState = TScanState.JSON_REPORT
elif line=='[]':
JsonContent+=line
elif line == "[]":
JsonContent += line
ScanState = TScanState.OTHER_REPORT_START
elif ScanState == TScanState.JSON_REPORT:
JsonContent+=line
if line==']':
elif ScanState == TScanState.JSON_REPORT:
JsonContent += line
if line == "]":
ScanState = TScanState.OTHER_REPORT_START
elif ScanState == TScanState.OTHER_REPORT_START:
if res:=re.search(r"^(\d+)(?= statements analysed.)",line):
RES_all['NbAnalysedStatments'] = float(res.group(1))
elif ScanState == TScanState.OTHER_REPORT_START:
if res := re.search(r"^(\d+)(?= statements analysed.)", line):
RES_all["NbAnalysedStatments"] = float(res.group(1))
if line == "Statistics by type":
ScanState = TScanState.OTHER_REPORT_STATISTICS
elif ScanState == TScanState.OTHER_REPORT_STATISTICS:
if res:=re.search(r"^(\d+)(?= lines have been analyzed)",line):
RES_all['NbAnalysedLines' ]= float(res.group(1))
elif ScanState == TScanState.OTHER_REPORT_STATISTICS:
if res := re.search(r"^(\d+)(?= lines have been analyzed)", line):
RES_all["NbAnalysedLines"] = float(res.group(1))
elif line == "Raw metrics":
ScanState = TScanState.OTHER_REPORT_METRICS
else:
with suppress(PyLintMetricNotFound): RES_all['Statistics']['module'] = cls.TryExtractPYReportMetric(line,"module")
with suppress(PyLintMetricNotFound): RES_all['Statistics']['class'] = cls.TryExtractPYReportMetric(line,"class")
with suppress(PyLintMetricNotFound): RES_all['Statistics']['method'] = cls.TryExtractPYReportMetric(line,"method")
with suppress(PyLintMetricNotFound): RES_all['Statistics']['function'] = cls.TryExtractPYReportMetric(line,"function")
elif ScanState == TScanState.OTHER_REPORT_METRICS:
with suppress(PyLintMetricNotFound):
RES_all["Statistics"]["module"] = cls.TryExtractPYReportMetric(line, "module")
with suppress(PyLintMetricNotFound):
RES_all["Statistics"]["class"] = cls.TryExtractPYReportMetric(line, "class")
with suppress(PyLintMetricNotFound):
RES_all["Statistics"]["method"] = cls.TryExtractPYReportMetric(line, "method")
with suppress(PyLintMetricNotFound):
RES_all["Statistics"]["function"] = cls.TryExtractPYReportMetric(line, "function")
elif ScanState == TScanState.OTHER_REPORT_METRICS:
if line == "Duplication":
RES_all['RawMetricsPercent']['code'] = RES_all['RawMetrics']['code'] / RES_all['NbAnalysedLines' ]
RES_all['RawMetricsPercent']['docstring'] = RES_all['RawMetrics']['docstring'] / RES_all['NbAnalysedLines' ]
RES_all['RawMetricsPercent']['comment'] = RES_all['RawMetrics']['comment'] / RES_all['NbAnalysedLines' ]
RES_all['RawMetricsPercent']['empty'] = RES_all['RawMetrics']['empty'] / RES_all['NbAnalysedLines' ]
RES_all["RawMetricsPercent"]["code"] = RES_all["RawMetrics"]["code"] / RES_all["NbAnalysedLines"]
RES_all["RawMetricsPercent"]["docstring"] = RES_all["RawMetrics"]["docstring"] / RES_all["NbAnalysedLines"]
RES_all["RawMetricsPercent"]["comment"] = RES_all["RawMetrics"]["comment"] / RES_all["NbAnalysedLines"]
RES_all["RawMetricsPercent"]["empty"] = RES_all["RawMetrics"]["empty"] / RES_all["NbAnalysedLines"]
ScanState = TScanState.OTHER_REPORT_DUPLICATION
else:
with suppress(PyLintMetricNotFound): RES_all['RawMetrics']['code'] = cls.TryExtractPYReportMetric(line,"code")
with suppress(PyLintMetricNotFound): RES_all['RawMetrics']['docstring'] = cls.TryExtractPYReportMetric(line,"docstring")
with suppress(PyLintMetricNotFound): RES_all['RawMetrics']['comment'] = cls.TryExtractPYReportMetric(line,"comment")
with suppress(PyLintMetricNotFound): RES_all['RawMetrics']['empty'] = cls.TryExtractPYReportMetric(line,"empty")
elif ScanState == TScanState.OTHER_REPORT_DUPLICATION:
with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"]["code"] = cls.TryExtractPYReportMetric(line, "code")
with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"]["docstring"] = cls.TryExtractPYReportMetric(line, "docstring")
with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"]["comment"] = cls.TryExtractPYReportMetric(line, "comment")
with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"]["empty"] = cls.TryExtractPYReportMetric(line, "empty")
elif ScanState == TScanState.OTHER_REPORT_DUPLICATION:
if line == "Messages by category":
ScanState = TScanState.OTHER_REPORT_MESSAGES_CAT
else:
with suppress(PyLintMetricNotFound): RES_all['Duplication']['NbDupLines'] = cls.TryExtractPYReportMetric(line,"nb duplicated lines")
with suppress(PyLintMetricNotFound): RES_all['Duplication']['PersentDuplicatedLines'] = cls.TryExtractPYReportMetric(line,"percent duplicated lines")
elif ScanState == TScanState.OTHER_REPORT_MESSAGES_CAT:
with suppress(PyLintMetricNotFound):
RES_all["Duplication"]["NbDupLines"] = cls.TryExtractPYReportMetric(line, "nb duplicated lines")
with suppress(PyLintMetricNotFound):
RES_all["Duplication"]["PersentDuplicatedLines"] = cls.TryExtractPYReportMetric(
line, "percent duplicated lines"
)
elif ScanState == TScanState.OTHER_REPORT_MESSAGES_CAT:
if line == "Messages":
ScanState = TScanState.OTHER_REPORT_MESSAGES
else:
with suppress(PyLintMetricNotFound): RES_all['MessagesCat']['Convention'] = cls.TryExtractPYReportMetric(line,"convention")
with suppress(PyLintMetricNotFound): RES_all['MessagesCat']['Refactor'] = cls.TryExtractPYReportMetric(line,"refactor")
with suppress(PyLintMetricNotFound): RES_all['MessagesCat']['Warning'] = cls.TryExtractPYReportMetric(line,"warning")
with suppress(PyLintMetricNotFound): RES_all['MessagesCat']['Error'] = cls.TryExtractPYReportMetric(line,"error")
elif ScanState == TScanState.OTHER_REPORT_MESSAGES:
with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"]["Convention"] = cls.TryExtractPYReportMetric(line, "convention")
with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"]["Refactor"] = cls.TryExtractPYReportMetric(line, "refactor")
with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"]["Warning"] = cls.TryExtractPYReportMetric(line, "warning")
with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"]["Error"] = cls.TryExtractPYReportMetric(line, "error")
elif ScanState == TScanState.OTHER_REPORT_MESSAGES:
# approx match because the number of '-' depend on screen width..
if line.startswith("--------"):
ScanState = TScanState.OTHER_REPORT_END
else:
for PylintMessage in cls.PylintMessageList.keys():
with suppress(PyLintMetricNotFound): RES_all['Messages'][PylintMessage] = cls.TryExtractPYReportMetric(line,PylintMessage)
elif ScanState == TScanState.OTHER_REPORT_END:
if res:=re.search(r"(?<=Your code has been rated at )(\d+(?:\.\d+)?)/10",line):
RES_all['GlobalScore']=float(res.group(1))
print(RES_all['GlobalScore'])
with suppress(PyLintMetricNotFound):
RES_all["Messages"][PylintMessage] = cls.TryExtractPYReportMetric(line, PylintMessage)
elif ScanState == TScanState.OTHER_REPORT_END:
if res := re.search(r"(?<=Your code has been rated at )(\d+(?:\.\d+)?)/10", line):
RES_all["GlobalScore"] = float(res.group(1))
print(RES_all["GlobalScore"])
else:
raise RuntimeError("Invalid ScanState")
Outfile.write(JsonContent)
with open(cls.get_result_dir()/"metrics.json", 'w') as json_file:
with open(cls.get_result_dir() / "metrics.json", "w") as json_file:
json.dump(RES_all, json_file)
# exporting all Data in one csv, unused atm because jenkins seems not able to select columns from csv an keep displaying all...
# exporting all Data in one csv, unused atm because jenkins seems not able to select columns from csv an keep displaying all...
# => to export a working full csv we need to a 'flat' dict (no more nested dict)
RES_all_trim = copy.deepcopy(RES_all)
del RES_all_trim["Messages"]
flat_RES_all=pandas.json_normalize(RES_all_trim, sep='_').to_dict(orient='records')[0]
with open(cls.get_result_dir()/"metrics.csv", 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file,fieldnames=flat_RES_all.keys())
flat_RES_all = pandas.json_normalize(RES_all_trim, sep="_").to_dict(orient="records")[0]
with open(cls.get_result_dir() / "metrics.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=flat_RES_all.keys())
writer.writeheader()
writer.writerow(flat_RES_all)
# splited csv exports for jenkins plots: RawMetricsPercent
RES_all_percent = RES_all["RawMetricsPercent"]
with open(cls.get_result_dir()/"metrics_rawpercent.csv", 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file,fieldnames=RES_all_percent.keys())
with open(cls.get_result_dir() / "metrics_rawpercent.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_percent.keys())
writer.writeheader()
writer.writerow(RES_all_percent)
# splited csv exports for jenkins plots: Statistics + Duplication + NbAnalysedStatments + NbAnalysedLines
RES_all_stats = copy.deepcopy(RES_all["Statistics"])
RES_all_stats["NbDupLines"] = RES_all['Duplication']['NbDupLines']
RES_all_stats["PersentDuplicatedLines"] = RES_all['Duplication']['PersentDuplicatedLines']
RES_all_stats["NbAnalysedStatments"] = RES_all['NbAnalysedStatments' ]
RES_all_stats["NbAnalysedLines"] = RES_all['NbAnalysedLines' ]
with open(cls.get_result_dir()/"metrics_Statistics.csv", 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file,fieldnames=RES_all_stats.keys())
RES_all_stats["NbDupLines"] = RES_all["Duplication"]["NbDupLines"]
RES_all_stats["PersentDuplicatedLines"] = RES_all["Duplication"]["PersentDuplicatedLines"]
RES_all_stats["NbAnalysedStatments"] = RES_all["NbAnalysedStatments"]
RES_all_stats["NbAnalysedLines"] = RES_all["NbAnalysedLines"]
with open(cls.get_result_dir() / "metrics_Statistics.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_stats.keys())
writer.writeheader()
writer.writerow(RES_all_stats)
# splited csv exports for jenkins plots: Statistics + Duplication
RES_all_MessagesCat = RES_all['MessagesCat']
with open(cls.get_result_dir()/"metrics_MessagesCat.csv", 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file,fieldnames=RES_all_MessagesCat.keys())
RES_all_MessagesCat = RES_all["MessagesCat"]
with open(cls.get_result_dir() / "metrics_MessagesCat.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_MessagesCat.keys())
writer.writeheader()
writer.writerow(RES_all_MessagesCat)
# splited csv exports for jenkins plots: GlobalScore
RES_GlobalScore = {'GlobalScore': RES_all['GlobalScore']}
with open(cls.get_result_dir()/"metrics_GlobalScore.csv", 'w', newline='') as csv_file:
writer = csv.DictWriter(csv_file,fieldnames=RES_GlobalScore.keys())
RES_GlobalScore = {"GlobalScore": RES_all["GlobalScore"]}
with open(cls.get_result_dir() / "metrics_GlobalScore.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_GlobalScore.keys())
writer.writeheader()
writer.writerow(RES_GlobalScore)
# converting the report using pylint_json2html (/!\ internal API, but as their is no leading '_' ...)
with open(cls.get_result_dir()/"report.html","w+", encoding='utf-8') as Outfile:
with open(cls.get_result_dir() / "report.html", "w+", encoding="utf-8") as Outfile:
raw_data = json.loads(JsonContent)
report=pylint_json2html.Report( raw_data)
report = pylint_json2html.Report(raw_data)
Outfile.write(report.render())
print("Done")

View File

@@ -16,36 +16,46 @@ from mypy import api
from .helper_base import helper_withresults_base
class types_check(helper_withresults_base):
class types_check(helper_withresults_base):
JUnitReportName = "junit.xml"
@classmethod
def do_job(cls):
@classmethod
def do_job(cls):
print("checking code typing ...")
result = api.run([ # project path
"-m",
"src." + str(cls.pyproject['project']['name']),
# analysis configuration
"--ignore-missing-imports",
"--strict-equality",
# reports generation
"--cobertura-xml-report", str(cls.get_result_dir()),
"--html-report", str(cls.get_result_dir()),
"--linecount-report", str(cls.get_result_dir()),
"--linecoverage-report", str(cls.get_result_dir()),
"--lineprecision-report", str(cls.get_result_dir()),
"--txt-report", str(cls.get_result_dir()),
"--xml-report", str(cls.get_result_dir()),
"--junit-xml", str(cls.get_result_dir()) + "/" + cls.JUnitReportName
])
result = api.run(
[ # project path
"-m",
"src." + str(cls.pyproject["project"]["name"]),
# analysis configuration
"--ignore-missing-imports",
"--strict-equality",
# reports generation
"--cobertura-xml-report",
str(cls.get_result_dir()),
"--html-report",
str(cls.get_result_dir()),
"--linecount-report",
str(cls.get_result_dir()),
"--linecoverage-report",
str(cls.get_result_dir()),
"--lineprecision-report",
str(cls.get_result_dir()),
"--txt-report",
str(cls.get_result_dir()),
"--xml-report",
str(cls.get_result_dir()),
"--junit-xml",
str(cls.get_result_dir()) + "/" + cls.JUnitReportName,
]
)
if result[0]:
print('\nType checking report:\n')
print("\nType checking report:\n")
print(result[0]) # stdout
if result[1]:
print('\nError report:\n')
print("\nError report:\n")
print(result[1]) # stderr
print('\nExit status:', result[2])
print("Done")
print("\nExit status:", result[2])
print("Done")

View File

@@ -21,34 +21,36 @@ from junit2htmlreport import parser as junit2html_parser
from .helper_base import helper_withresults_base
class unit_test(helper_withresults_base):
enable_coverage_check: bool = False
enable_xml_export: bool = True
class unit_test(helper_withresults_base):
enable_coverage_check: bool = False
enable_xml_export: bool = True
enable_full_xml_export: bool = True
FullReportName: str = "full_report"
CoverageReportName: str = "test_coverage"
@classmethod
def do_job(cls):
if cls.enable_coverage_check==True:
FullReportName: str = "full_report"
CoverageReportName: str = "test_coverage"
@classmethod
def do_job(cls):
if cls.enable_coverage_check == True:
import coverage
# preparing unittest framework
test_loader = unittest.TestLoader()
if cls.enable_coverage_check == True:
#we start coverage now because module files discovery is part of the coverage measurement
CoverageReportPath = Path(str(cls.get_result_dir())+"_coverage")
# we start coverage now because module files discovery is part of the coverage measurement
CoverageReportPath = Path(str(cls.get_result_dir()) + "_coverage")
cls._reset_dir(CoverageReportPath)
cov = coverage.Coverage(cover_pylib=False,branch=True,source_pkgs=["src."+cls.pyproject['project']['name']])
cov = coverage.Coverage(cover_pylib=False, branch=True, source_pkgs=["src." + cls.pyproject["project"]["name"]])
cov.start()
package_tests = test_loader.discover(start_dir=str(cls.project_rootdir_path / "test"),top_level_dir=str(cls.project_rootdir_path / "test"))
package_tests = test_loader.discover(
start_dir=str(cls.project_rootdir_path / "test"), top_level_dir=str(cls.project_rootdir_path / "test")
)
if cls.enable_xml_export:
testRunner = xmlrunner.XMLTestRunner(output=str(str(cls.get_result_dir())))
else:
testRunner = unittest.TextTestRunner()
# running the test
testRunner.run(package_tests)
@@ -57,23 +59,23 @@ class unit_test(helper_withresults_base):
cov.stop()
cov.save()
cov.html_report(directory=str(CoverageReportPath))
cov.xml_report(outfile=(CoverageReportPath/f"{cls.CoverageReportName}.xml"))
cov.xml_report(outfile=(CoverageReportPath / f"{cls.CoverageReportName}.xml"))
# computing results (Only if xml available)
if cls.enable_full_xml_export == True:
print("Full reports generation...")
FullReportPath = Path(str(cls.get_result_dir())+"_full")
FullReportPath = Path(str(cls.get_result_dir()) + "_full")
cls._reset_dir(FullReportPath)
FullJUnitReport = JUnitXml()
for fname in [fname for fname in os.listdir(cls.get_result_dir()) if fname.endswith('.xml')]:
FullJUnitReport+=JUnitXml.fromfile(str(cls.get_result_dir() / fname))
for fname in [fname for fname in os.listdir(cls.get_result_dir()) if fname.endswith(".xml")]:
FullJUnitReport += JUnitXml.fromfile(str(cls.get_result_dir() / fname))
current_datetime = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ")
full_report_base_name = f'{cls.pyproject["project"]["name"]}-{cls.FullReportName}-{current_datetime}'
FullJUnitReport.write(str(FullReportPath / f'{full_report_base_name}.xml'))
report = junit2html_parser.Junit(FullReportPath/ f'{full_report_base_name}.xml')
FullJUnitReport.write(str(FullReportPath / f"{full_report_base_name}.xml"))
report = junit2html_parser.Junit(FullReportPath / f"{full_report_base_name}.xml")
html = report.html()
with open(FullReportPath/ f'{full_report_base_name}.html', "wb") as outfile:
outfile.write(html.encode('utf-8'))
print("Done")
with open(FullReportPath / f"{full_report_base_name}.html", "wb") as outfile:
outfile.write(html.encode("utf-8"))
print("Done")

View File

@@ -6,57 +6,87 @@
# 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/>.
docs_dir: docs
site_name: pygitversionhelper
site_url: https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/
site_url: 'https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/latest/'
site_description: pygitversionhelper
site_author: chacha
repo_url: https://chacha.ddns.net/gitea/chacha/pygitversionhelper
site_author: ChaCha
repo_url: 'https://chacha.ddns.net/gitea/chacha/pygitversionhelper'
use_directory_urls: false
copyright: CC BY-NC-SA 4.0
theme:
name: material
features:
- navigation.instant
- navigation.tracking
- navigation.tabs
- navigation.tabs.sticky
- toc.integrate
- navigation.top
- navigation.instant
- navigation.tracking
- navigation.tabs
- navigation.tabs.sticky
- toc.integrate
- navigation.top
palette:
- media: '(prefers-color-scheme: dark)'
scheme: slate
toggle:
icon: material/brightness-4
name: Switch to system preference
- media: (prefers-color-scheme)
toggle:
icon: material/brightness-auto
name: Switch to light mode
- media: '(prefers-color-scheme: light)'
scheme: default
toggle:
icon: material/brightness-7
name: Switch to dark mode
- media: '(prefers-color-scheme: dark)'
scheme: slate
toggle:
icon: material/brightness-4
name: Switch to system preference
- media: (prefers-color-scheme)
toggle:
icon: material/brightness-auto
name: Switch to light mode
- media: '(prefers-color-scheme: light)'
scheme: default
toggle:
icon: material/brightness-7
name: Switch to dark mode
plugins:
- search
- localsearch
- autorefs
- mkdocstrings:
default_handler: python
handlers:
python:
selection:
filters:
- '!^_(?!_init__)'
inherited_members: true
rendering:
show_root_heading: false
show_root_toc_entry: false
show_root_full_path: false
show_if_no_docstring: true
show_signature_annotations: true
show_source: false
heading_level: 2
group_by_category: true
show_category_heading: true
- search
- markdownextradata
- mermaid2
- localsearch
- autorefs
- mkdocstrings:
default_handler: python
handlers:
python:
selection:
filters:
- '!^_(?!_init__)'
inherited_members: true
rendering:
show_root_heading: false
show_root_toc_entry: false
show_root_full_path: false
show_if_no_docstring: true
show_signature_annotations: true
show_source: false
heading_level: 2
group_by_category: true
show_category_heading: true
markdown_extensions:
- def_list
- tables
- attr_list
- abbr
- pymdownx.betterem:
smart_enable: all
- pymdownx.caret
- pymdownx.critic
- pymdownx.details
- pymdownx.inlinehilite
- pymdownx.snippets
- pymdownx.highlight:
anchor_linenums: true
line_spans: __span
pygments_lang_class: true
- pymdownx.keys
- pymdownx.mark
- pymdownx.progressbar
- pymdownx.smartsymbols
- pymdownx.tasklist:
custom_checkbox: true
- pymdownx.tilde
- footnotes
extra:
branch: master
repository: pygitversionhelper

View File

@@ -7,18 +7,15 @@
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.
[build-system]
requires = ["setuptools>=63", "wheel", "setuptools-git-versioning<2"]
requires = ["setuptools>=63", "wheel", "setuptools_scm"]
build-backend = "setuptools.build_meta"
[tool.setuptools-git-versioning]
enabled = true
dev_template = "{tag}.post{ccount}"
#tag_filter = "^\\d+\\.\\d+\\.\\d+$"
[tool.setuptools_scm]
version_scheme= "post-release"
[project]
name = "pygitversionhelper"
description = "pygitversionhelper"
readme = "README.md"
requires-python = ">=3.9"
keywords = ["chacha","chacha","template","pygitversionhelper"]
license = { file = "LICENSE.md" }
@@ -36,6 +33,7 @@ classifiers = [
]
dependencies = [
'importlib-metadata; python_version<"3.9"',
'packaging'
]
dynamic = ["version"]
@@ -49,21 +47,18 @@ where = ["src"]
[tool.setuptools.package-data]
"pygitversionhelper.data" = ["*.*"]
#[tool.setuptools_scm]
#write_to = "src/pygitversionhelper/_version.py"
#version_scheme = "python-simplified-semver"
[project.urls]
Homepage = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper"
Documentation = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/wiki"
Homepage = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/"
Documentation = "https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/master/latest/"
Tracker = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/issues"
[project.optional-dependencies]
test = ["junitparser>=2.8","junit2html>=30.1","xmlrunner>=1.7","mypy>=0.99" ]
coverage-check = ["coverage>=7.0"]
complexity-check = ["radon>=5.1"]
quality-check = ["pylint>=2.15","pylint-json2html>=0.4","pandas>=1.5"]
type-check = ["mypy[reports]>=0.99" ]
doc-gen = ["mkdocs>=1.4.0", "mkdocs-material>=8.5", "mkdocs-localsearch>=0.9.0", "mkdocstrings[python]>=0.19", "mkdocs-with-pdf>=0.9.3","pyyaml>=6.0"]
doc-gen = ["mkdocs>=1.4.0", "mkdocs-material>=8.5", "mkdocs-localsearch>=0.9.0", "mkdocstrings[python]>=0.19", "mkdocs-with-pdf>=0.9.3","pyyaml>=6.0","pymdown-extensions>=9","mkdocs-markdownextradata-plugin","mkdocs-mermaid2-plugin"]
#[project.scripts]
#my-script = "my_package.module:function"

View File

@@ -14,9 +14,10 @@ from importlib.metadata import version, PackageNotFoundError
try: # pragma: no cover
__version__ = version("pygitversionhelper")
except PackageNotFoundError: # pragma: no cover
except PackageNotFoundError: # pragma: no cover
import warnings
warnings.warn("can not read __version__, assuming local test context, setting it to ?.?.?")
__version__ = "?.?.?"
from .gitversionhelper import gitversionhelper
from .gitversionhelper import gitversionhelper, gitversionhelperException

View File

@@ -1,244 +1,712 @@
# 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 project try to help doing handy operations with git when
dealing with project versioning and tags on python project -
at leat for project using PEP440 or SemVer standards.
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 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
import os
import subprocess
from packaging.version import VERSION_PATTERN as packaging_VERSION_PATTERN
import re
from dataclasses import dataclass
from copy import copy
import logging
def _exec(cmd: str, root: str | os.PathLike | None = None) -> list[str]:
try:
stdout = subprocess.check_output(cmd, shell=True, text=True, cwd=root)
except subprocess.CalledProcessError as e:
stdout = e.output
lines = stdout.splitlines()
from packaging.version import VERSION_PATTERN as packaging_VERSION_PATTERN
def _exec(cmd: str, root: str | os.PathLike | None = None, raw: bool = False) -> list[str]:
"""
helper function to handle system cmd execution
Args:
cmd: command line to be executed
root: root directory where the command need to be executed
Returns:
a list of command's return lines
"""
p = subprocess.run(
cmd,
text=True,
cwd=root,
capture_output=True,
check=False,
timeout=2,
shell=True,
)
if re.search("not a git repository", p.stderr):
raise gitversionhelper.repository.notAGitRepository()
if re.search("fatal:", p.stderr):
raise gitversionhelper.unknownGITFatalError(p.stderr)
if int(p.returncode) < 0:
raise gitversionhelper.unknownGITError(p.stderr)
if raw:
return p.stdout
lines = p.stdout.splitlines()
return [line.rstrip() for line in lines if line.rstrip()]
class gitversionhelper:
class head:
class gitversionhelperException(Exception):
"""
general Module Exception
"""
class gitversionhelper: # pylint: disable=too-few-public-methods
"""
main gitversionhelper class
"""
class wrongArguments(gitversionhelperException):
"""
wrong argument generic exception
"""
class unknownGITError(gitversionhelperException):
"""
unknown git error generic exception
"""
class unknownGITFatalError(unknownGITError):
"""
unknown fatal git error generic exception
"""
class repository:
"""
class containing methods focusing on repository
"""
class repositoryException(gitversionhelperException):
"""
generic repository exeption
"""
class notAGitRepository(repositoryException):
"""
not a git repository exception
"""
class repositoryDirty(repositoryException):
"""
dirty repository exception
"""
@classmethod
def isDirty(cls):
return True if _exec(f"git status --short") else False
def isDirty(cls) -> bool:
"""
check if the repository is in dirty state
Returns:
True if it is dirty
"""
return bool(_exec("git status --short"))
class commit:
"""
class containing methods focusing on commits
"""
__OptDict = {"same_branch": "same_branch", "merged_output": "merged_output"}
class commitException(gitversionhelperException):
"""
generic commit exception
"""
class commitNotFound(commitException):
"""
tag not found exception
"""
@classmethod
def getMessagesSinceTag(cls, tag: str, **kwargs) -> str:
"""
retrieve a commits message history from repository
from Latest commit to the given tag
Keyword Arguments:
merged_output: output one single merged string
same_branch(bool): force searching only in the same branch
Returns:
the commit message
"""
current_commit_id = cls.getLast(**kwargs)
tag_commit_id = cls.getFromTag(tag)
if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}")
else:
commits = _exec(f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}")
result = []
for commit in commits:
result.append(cls.getMessage(commit))
if (cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True):
print("JOIN")
return os.linesep.join(result)
return result
@classmethod
def getMessage(cls, commit_hash: str) -> str:
"""
retrieve a commit message from repository
Args:
commit_hash: id of the commit
Returns:
the commit message
"""
try:
res = _exec(
f'git log -z --pretty="tformat:%B%-C()" -n 1 {commit_hash}',
None,
True,
).rstrip("\x00")
except gitversionhelper.unknownGITFatalError as _e:
raise cls.commitNotFound("no commit found in commit history") from _e
return res.replace("\r\n", "\n").replace("\n", "\r\n")
@classmethod
def getFromTag(cls, tag: str) -> str:
"""
retrieve a commit from repository associated to a tag
Args:
tag: tag of the commit
Returns:
the commit Id
"""
try:
res = _exec(f"git rev-list -n 1 {tag}")
except gitversionhelper.unknownGITFatalError as _e:
raise cls.commitNotFound("no commit found in commit history") from _e
if len(res) == 0:
raise cls.commitNotFound("no commit found in commit history")
return res[0]
@classmethod
def getLast(cls, **kwargs) -> str:
"""
retrieve last commit from repository
Keyword Arguments:
same_branch(bool): force searching only in the same branch
Returns:
the commit Id
"""
if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
try:
res = _exec("git rev-parse HEAD")
except gitversionhelper.unknownGITFatalError as _e:
raise cls.commitNotFound("no commit found in commit history") from _e
else:
res = _exec('git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format="%(objectname)"')
if len(res) == 0:
raise cls.commitNotFound("no commit found in commit history")
return res[0]
class tag:
"""
class containing methods focusing on tags
"""
__OptDict = {"same_branch": "same_branch"}
@classmethod
def getTags(cls,sort:str = "version:refname"):
return _exec(f"git tag -l --sort={sort}")
__validGitTagSort = [
"",
"v:refname",
"-v:refname",
"taggerdate",
"committerdate",
"-taggerdate",
"-committerdate",
]
class tagException(gitversionhelperException):
"""
generic tag exception
"""
class tagNotFound(tagException):
"""
tag not found exception
"""
class moreThanOneTag(tagException):
"""
more than one tag exception
"""
@classmethod
def getLastTag(cls,**kwargs):
if ((cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]]==True)):
return _exec(f"git describe --tags --abbrev=0")[0]
else:
tag = _exec("git rev-list --tags --max-count=1")
return _exec(f"git describe --tags {tag[0]}")[0]
def getTags(cls, sort: str = "taggerdate", **kwargs) -> list[str | None]:
"""
retrieve all tags from a repository
Args:
sort: sorting constraints (git format)
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}")))
return list(reversed(_exec(f"git tag -l --sort={sort}")))
@classmethod
def getDistanceFromLastTag(cls,tag=None,**kwargs):
if (tag == None):
def getLastTag(cls, **kwargs) -> str | None:
"""
retrieve the Latest tag from a repository
Keyword Arguments:
same_branch(bool): force searching only in the same branch
Returns:
the tag
"""
if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
res = _exec("git describe --tags --first-parent --abbrev=0")
else:
res = _exec("git rev-list --tags --date-order --max-count=1")
if len(res) == 1:
res = _exec(f"git describe --tags {res[0]}")
if len(res) == 0:
raise cls.tagNotFound("no tag found in commit history")
if len(res) != 1:
raise cls.moreThanOneTag("multiple tags on same commit is unsupported")
return res[0]
@classmethod
def getDistanceFromTag(cls, tag: str = None, **kwargs) -> int:
"""
retrieve the distance between Latest commit and tag in the repository
Arguments:
tag: reference tag, if None the most recent one will be used
Keyword Arguments:
same_branch(bool): force searching only in the same branch
Returns:
the tag
"""
if tag is None:
tag = cls.getLastTag(**kwargs)
return _exec(f"git rev-list {tag}..HEAD --count")[0]
return int(_exec(f"git rev-list {tag}..HEAD --count")[0])
class version:
__OptDict = { "version_std": "version_std",
"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-]+)*))?$"},
"PEP440" : { "regex" : packaging_VERSION_PATTERN }
"""
class containing methods focusing on versions
"""
__OptDict = {
"version_std": "version_std",
"formated_output": "formated_output",
"output_format": "output_format",
"ignore_unknown_tags": "ignore_unknown_tags",
}
DefaultInputFormat = "Auto"
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-]+)*))?$",
"regex_preversion_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
"regex_build_num": r"(?:\.)(?P<num>(?:\d+(?!\w))+)",
},
"PEP440": {"regex": packaging_VERSION_PATTERN, "Auto": None},
}
__versionReseted = False
class versionException(gitversionhelperException):
"""
generic version exception
"""
class noValidVersion(versionException):
"""
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
"""
__OptDict = { "bump_type": "bump_type",
"bump_dev_strategy": "bump_dev_strategy"}
DefaultBumpType = "patch"
BumpTypes = ["major","minor","patch","dev"]
DefaultBumpDevStrategy = "post"
BumpDevStrategys = ["post","pre","force_post","force_pre"]
major: int = 0
minor: int = 1
patch: int = 0
pre_count:int = 0
post_count:int = 0
raw:str = "0.1.0"
def __init__(self,major,minor,patch,pre_count,post_count,raw):
self.major,self.minor,self.patch,self.pre_count,self.post_count,self.raw = major,minor,patch,pre_count,post_count,raw
__OptDict = {
"bump_type": "bump_type",
"bump_dev_strategy": "bump_dev_strategy",
"formated_output": "formated_output",
}
DefaultBumpType = "patch"
BumpTypes = ["major", "minor", "patch", "dev"]
DefaultBumpDevStrategy = "post"
BumpDevStrategys = ["post", "pre-patch", "pre-minor", "pre-major"]
version_std: str = "None"
major: int = 0
minor: int = 1
patch: int = 0
pre_count: int = 0
post_count: int = 0
raw: str = "0.1.0"
def __init__(
self,
version_std,
major=0,
minor=1,
patch=0,
pre_count=0,
post_count=0,
raw="0.1.0",
): # pylint: disable=R0913
self.version_std = version_std
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):
def _getBumpDevStrategy(cls, **kwargs) -> str:
"""
get selected bump_dev_strategy
Keyword Arguments:
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
Returns:
Kwargs given bump_dev_strategy or the default one.
"""
BumpDevStrategy = cls.DefaultBumpDevStrategy
if (cls.__OptDict["bump_dev_strategy"] in kwargs):
if cls.__OptDict["bump_dev_strategy"] in kwargs:
if kwargs[cls.__OptDict["bump_dev_strategy"]] in cls.BumpDevStrategys:
BumpDevStrategy = kwargs[cls.__OptDict["bump_dev_strategy"]]
else:
raise RuntimeError(f"invalid {cls.__OptDict['bump_type']} requested")
raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
return BumpDevStrategy
@classmethod
def _getBumpType(cls,**kwargs):
def _getBumpType(cls, **kwargs) -> str:
"""
get selected bump_type
Keyword Arguments:
bump_type(str): the given bump_type (can be None)
Returns:
Kwargs given bump_type or the default one.
"""
BumpType = cls.DefaultBumpType
if (cls.__OptDict["bump_type"] in kwargs):
if cls.__OptDict["bump_type"] in kwargs:
if kwargs[cls.__OptDict["bump_type"]] in cls.BumpTypes:
BumpType = kwargs[cls.__OptDict["bump_type"]]
else:
raise RuntimeError(f"invalid {cls.__OptDict['bump_type']} requested")
raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
return BumpType
def bump(self,**kwargs):
def bump(self, amount: int = 1, **kwargs) -> gitversionhelper.version.MetaVersion | str: # pylint: disable=R0912
"""
bump the version to the next one
Keyword Arguments:
bump_type(str): the given bump_type (can be None)
bump_dev_strategy(str): the given bump_dev_strategy (can be None)
Returns:
the bumped version
"""
BumpType = self._getBumpType(**kwargs)
BumpDevStrategy=self._getBumpDevStrategy(**kwargs)
_v=copy(self)
BumpDevStrategy = self._getBumpDevStrategy(**kwargs)
_v = copy(self)
if BumpType == "dev":
if BumpDevStrategy=="post":
if BumpDevStrategy == "post":
if _v.pre_count > 0:
_v.pre_count = _v.pre_count + 1
_v.pre_count = _v.pre_count + amount
else:
_v.post_count = _v.post_count + 1
elif BumpDevStrategy=="pre":
_v.post_count = _v.post_count + amount
# elif BumpDevStrategy in ["pre-patch","pre-minor","pre-major"]:
else:
if _v.post_count > 0:
_v.post_count = _v.post_count + 1
_v.post_count = _v.post_count + amount
else:
_v.pre_count = _v.pre_count + 1
elif BumpDevStrategy=="force_post":
pass
elif BumpDevStrategy=="force_pre":
pass
if _v.pre_count == 0:
if BumpDevStrategy == "pre-patch":
_v.patch = _v.patch + 1
elif BumpDevStrategy == "pre-minor":
_v.minor = _v.minor + 1
_v.patch = 0
# elif BumpDevStrategy == "pre-major":
else:
_v.major = _v.major + 1
_v.minor = 0
_v.patch = 0
_v.pre_count = _v.pre_count + amount
else:
if BumpType == "major":
_v.major = _v.major + 1
_v.major = _v.major + amount
elif BumpType == "minor":
_v.minor = _v.minor + 1
elif BumpType == "patch":
_v.patch = _v.patch + 1
_v.pre_count=0
_v.post_count=0
_v.raw="{major}.{minor}.{patch}".format(major=_v.major,minor=_v.minor,patch=_v.patch,revpattern="",revcount="")
_v.minor = _v.minor + amount
# 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
def doFormatVersion(self,**kwargs):
return gitversionhelper.version.doFormatVersion(self,**kwargs)
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
"""
return gitversionhelper.version.doFormatVersion(self, **kwargs)
@classmethod
def _getVersionStd(cls,**kwargs):
def _getVersionStd(cls, **kwargs) -> str:
"""
get selected version_std
Keyword Arguments:
version_std(str): the given version_std (can be None)
Returns:
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:
raise RuntimeError(f"invalid {cls.__OptDict['version_std']} requested")
raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['version_std']} requested")
return VersionStd
@classmethod
def getLastVersion(cls,**kwargs):
def getCurrentVersion(cls, **kwargs) -> MetaVersion | str:
"""
get last version from git tags
get the current version or bump depending of repository state
Keyword Arguments:
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
"""
if gitversionhelper.repository.isDirty() is not False:
raise gitversionhelper.repository.repositoryDirty("The repository is dirty and a current version can not be generated.")
saved_kwargs = copy(kwargs)
if "formated_output" in kwargs:
del saved_kwargs["formated_output"]
_v = cls.getLastVersion(**saved_kwargs)
if not cls.__versionReseted:
amount = gitversionhelper.tag.getDistanceFromTag(_v.raw, **kwargs)
_v = _v.bump(amount, **saved_kwargs)
if (cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True):
return _v.doFormatVersion(**kwargs)
return _v
@classmethod
def getCurrentFormatedVersion(cls, **kwargs) -> str:
"""
Same as getCurrentVersion() with formated_output kwarg activated
"""
kwargs["formated_output"] = True
return cls.getCurrentVersion(**kwargs)
@classmethod
def _parseTag(cls, tag, **kwargs): # pylint: disable=R0914, R0912, R0915
"""get the last version from tags
Arguments:
tag: the tag to be parsed
Keyword Arguments:
version_std(str): the given version_std (can be None)
ignore_unknown_tags(bool): skip tags with not decoded versions (default to False)
Returns:
the last version
"""
VersionStd = cls._getVersionStd(**kwargs)
_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 == "PEP440":
ver=_m.group("release").split(".")
ver += ["0"] * (3 - len(ver))
ver[0]=int(ver[0])
ver[1]=int(ver[1])
ver[2]=int(ver[2])
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 == "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
_v = cls.MetaVersion(major, minor, patch, pre_count, post_count, lastTag)
if ((cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]]==True)):
return cls.doFormatVersion(_v,**kwargs)
else:
return _v
bAutoVersionStd = False
if VersionStd == "Auto":
bAutoVersionStd = True
bFound = False
if VersionStd == "SemVer" or (bAutoVersionStd is True):
_r = re.compile(
r"^\s*" + cls.VersionStds["SemVer"]["regex"] + r"\s*$",
re.VERBOSE | re.IGNORECASE,
)
_m = re.match(_r, tag)
if not _m:
pass
else:
major, minor, patch = (
int(_m.group("major")),
int(_m.group("minor")),
int(_m.group("patch")),
)
pre_count = 0
if _pre := _m.group("prerelease"):
if (_match := re.search(cls.VersionStds["SemVer"]["regex_preversion_num"], _pre)) is not None:
pre_count = int(_match.group("num"))
else:
pre_count = 1
post_count = 0
if _post := _m.group("buildmetadata"):
if (_match := re.search(cls.VersionStds["SemVer"]["regex_build_num"], _post)) is not None:
post_count = int(_match.group("num"))
else:
post_count = 1
bFound = True
VersionStd = "SemVer"
if VersionStd == "PEP440" or ((bAutoVersionStd is True) and (bFound is not True)):
_r = re.compile(
r"^\s*" + cls.VersionStds["PEP440"]["regex"] + r"\s*$",
re.VERBOSE | re.IGNORECASE,
)
_m = re.match(_r, tag)
if not _m:
pass
else:
ver = _m.group("release").split(".")
ver += ["0"] * (3 - len(ver))
ver[0] = int(ver[0])
ver[1] = int(ver[1])
ver[2] = int(ver[2])
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
bFound = True
VersionStd = "PEP440"
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
def doFormatVersion(cls,inputversion:MetaVersion,**kwargs):
def getLastVersion(cls, **kwargs) -> MetaVersion | str: # pylint: disable=R0914, R0912, R0915
"""get the last version from tags
Keyword Arguments:
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
ignore_unknown_tags(bool): skip tags with not decoded versions (default to False)
Returns:
the last version
"""
output a formated version
lastTag = cls.MetaVersion.raw
cls.__versionReseted = False
try:
lastTag = gitversionhelper.tag.getLastTag(**kwargs)
except gitversionhelper.tag.tagNotFound:
logging.warning("tag not found, reseting versionning")
cls.__versionReseted = True
_v = None
try:
_v = cls._parseTag(lastTag, **kwargs)
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
for _tag in tags:
try:
_v = cls._parseTag(_tag, **kwargs)
break
except gitversionhelper.version.noValidVersion:
continue
if _v is None:
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)
return _v
@classmethod
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:
formated version string
"""
VersionStd = cls._getVersionStd(**kwargs)
if VersionStd == "Auto":
VersionStd = inputversion.version_std
OutputFormat = None
revpattern=""
revcount=""
revpattern = ""
revcount = ""
post_count = inputversion.post_count
pre_count = inputversion.pre_count
patch = inputversion.patch
if (cls.__OptDict["output_format"] in kwargs):
OutputFormat=kwargs[cls.__OptDict["output_format"]]
if OutputFormat == None:
if VersionStd == "PEP440":
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
pre_count = inputversion.pre_count
patch = inputversion.patch
if cls.__OptDict["output_format"] in kwargs:
OutputFormat = kwargs[cls.__OptDict["output_format"]]
if OutputFormat is None:
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
if post_count > 0 and pre_count > 0:
raise gitversionhelper.version.PreAndPostVersionUnsupported(
"cannot output a version with both pre and post release number."
)
if VersionStd == "PEP440":
if post_count > 0:
revpattern=".post"
revcount=f"{post_count}"
elif VersionStd == "SemVer":
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
revpattern = ".post"
revcount = f"{post_count}"
elif pre_count > 0:
revpattern = ".pre"
revcount = f"{pre_count}"
# elif VersionStd == "SemVer":
else:
if post_count > 0:
pre_count = pre_count + post_count
post_count = 0
patch = patch + 1
if pre_count > 0:
revpattern="-pre"
revcount=f"{pre_count}"
return OutputFormat.format(major=inputversion.major,minor=inputversion.minor,patch=patch,revpattern=revpattern,revcount=revcount)
#print(gitversionhelper.head.isDirty())
#print(gitversionhelper.tag.getTags())
#print(gitversionhelper.tag.getLastTag())
#print(gitversionhelper.tag.getLastTag(same_branch=True))
#print(gitversionhelper.tag.getDistanceFromLastTag())
#print(gitversionhelper.tag.getDistanceFromLastTag(same_branch=True))
#print(gitversionhelper.version.getLastVersion())
#print(gitversionhelper.version.getLastVersion(same_branch=True,version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=True,version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=True,version_std="PEP440",formated_output=True))
#print(gitversionhelper.version.getLastVersion(same_branch=True,version_std="PEP440",formated_output=True,output_format="v{major}.{minor}.{patch}"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump())
#print("")
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="dev").doFormatVersion(version_std="SemVer"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="patch").doFormatVersion(version_std="SemVer"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="minor").doFormatVersion(version_std="SemVer"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="major").doFormatVersion(version_std="SemVer"))
#print("")
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="dev").doFormatVersion(version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="patch").doFormatVersion(version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="minor").doFormatVersion(version_std="PEP440"))
#print(gitversionhelper.version.getLastVersion(same_branch=False,version_std="PEP440").bump(bump_type="major").doFormatVersion(version_std="PEP440"))
#from setuptools_git_versioning import version_from_git
#print(str(version_from_git(tag_filter="^\d+\.\d+\.\d+$",dev_template = "{tag}.post{ccount}",sort_by="v:refname")),end ="")
revpattern = "+post"
revcount = f".{post_count}"
elif pre_count > 0:
revpattern = "-pre"
revcount = f".{pre_count}"
return OutputFormat.format(
major=inputversion.major,
minor=inputversion.minor,
patch=patch,
revpattern=revpattern,
revcount=revcount,
)

View File

@@ -4,4 +4,4 @@
# 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/>.
# work. If not, see <https://creativecommons.org/licenses/by-nc-sa/4.0/>.

File diff suppressed because it is too large Load Diff