Compare commits

...

36 Commits

Author SHA1 Message Date
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
15 changed files with 1035 additions and 973 deletions

View File

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

105
Jenkinsfile vendored
View File

@@ -6,7 +6,13 @@
// You should have received a copy of the license along with this // 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/>.
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: // configurable settings:
// use to send email if workflow problem // use to send email if workflow problem
@@ -28,6 +34,11 @@ def _MkDocsWebURL = "dabauto--mkdocs-web.dmz.chacha.home/mkdocs-web/"
def _MkDocsWebCredentials = "2c5b684e-3787-4b37-8aca-b3dd4a383fe2" def _MkDocsWebCredentials = "2c5b684e-3787-4b37-8aca-b3dd4a383fe2"
def _PypiCredentials = "Pypi" 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 /!\ // commands Helper: /!\ Made for GITEA /!\
String determineRepoUserName() { String determineRepoUserName() {
return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\\.")[0] return scm.getUserRemoteConfigs()[0].getUrl().tokenize('/')[3].split("\\.")[0]
@@ -63,6 +74,46 @@ String ExtractBaseVersion(inVersion) {
return matcher[0][1] 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 { pipeline {
// for Docker based build (preferable) // for Docker based build (preferable)
@@ -137,7 +188,7 @@ pipeline {
script { script {
if(_PROJECT_NAME!="pygitversionhelper") { if(_PROJECT_NAME!="pygitversionhelper") {
sh(". ~/TOOLS_ENV/bin/activate && pip install git+https://chacha.ddns.net/gitea/chacha/pygitversionhelper.git@master") sh(". ~/TOOLS_ENV/bin/activate && pip install pygitversionhelper")
} }
else else
{ {
@@ -324,7 +375,7 @@ pipeline {
def wheelPath = findFiles(glob: "**/dist/*.whl")[0] def wheelPath = findFiles(glob: "**/dist/*.whl")[0]
echo "wheel artifact path: $wheelPath" echo "wheel artifact path: $wheelPath"
// install the package, with *test* optionnal packages, as user // 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]")
} }
} }
} }
@@ -334,6 +385,14 @@ pipeline {
steps { steps {
dir("gitrepo") { dir("gitrepo") {
sh(". ~/TEST_ENV/bin/activate && python -m helpers --type-check --quality-check") 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 { post {
@@ -388,6 +447,14 @@ pipeline {
style: 'stackedArea', style: 'stackedArea',
keepRecords: true, keepRecords: true,
numBuilds: '']) 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: ''])
} }
} }
@@ -400,6 +467,36 @@ pipeline {
println unit_test_full_name__html println unit_test_full_name__html
unit_test_full_name__xml=findFiles(glob: "helpers-results/unit_test_full/*.xml")[0].getName() unit_test_full_name__xml=findFiles(glob: "helpers-results/unit_test_full/*.xml")[0].getName()
println unit_test_full_name__xml 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")}
} }
} }
} }
@@ -561,7 +658,7 @@ pipeline {
withCredentials([usernamePassword( credentialsId: _PypiCredentials, passwordVariable: 'PYPI_PASSWORD', usernameVariable: 'PYPI_USERNAME')]) { withCredentials([usernamePassword( credentialsId: _PypiCredentials, passwordVariable: 'PYPI_PASSWORD', usernameVariable: 'PYPI_USERNAME')]) {
sh(script: """#!/bin/sh - sh(script: """#!/bin/sh -
|. ~/TOOLS_ENV/bin/activate |. ~/TOOLS_ENV/bin/activate
|exec twine upload -r ${PY_PROJECT_NAME} dist/* -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} --non-interactive --disable-progress-bar |exec twine upload -u ${PYPI_USERNAME} -p ${PYPI_PASSWORD} --non-interactive --disable-progress-bar dist/*
""".stripMargin()) """.stripMargin())
} }
} }

View File

@@ -1,5 +1,10 @@
![](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=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) ![](docs-static/Library.jpg)
@@ -9,7 +14,6 @@ _A tiny library to help versioning management of git python projects_
Because a good developer is a lazy developer and version management in CI/CD can be very time consuming. 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/pygitversionhelper/{{branch}}/latest/). Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitversionhelper/{{branch}}/latest/).
## Features ## Features
@@ -19,7 +23,8 @@ Checkout [Latest Documentation](https://chacha.ddns.net/mkdocs-web/chacha/pygitv
- get current version (bumped) - get current version (bumped)
- convert / switch from SemVer to PEP440 (both ways) - convert / switch from SemVer to PEP440 (both ways)
- automatic version format detection (SemVer by default) - automatic version format detection (SemVer by default)
- Get commit message history
## Options ## Options
- restrict to same branch - restrict to same branch
- both SemVer and PEP440 support - both SemVer and PEP440 support

View File

@@ -4,4 +4,4 @@
# Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Unported License. # Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International Unported License.
# #
# You should have received a copy of the license along with this # 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/>.

View File

@@ -23,6 +23,7 @@ if __package__ == "helpers":
from .unit_test import unit_test from .unit_test import unit_test
from .doc_gen import doc_gen from .doc_gen import doc_gen
from .changelog_gen import changelog_gen from .changelog_gen import changelog_gen
from .complexity_check import complexity_check
else: else:
# when calling the __main__.py file (from IDE) # when calling the __main__.py file (from IDE)
from helpers.types_check import types_check from helpers.types_check import types_check
@@ -30,6 +31,7 @@ else:
from helpers.unit_test import unit_test from helpers.unit_test import unit_test
from helpers.doc_gen import doc_gen from helpers.doc_gen import doc_gen
from helpers.changelog_gen import changelog_gen from helpers.changelog_gen import changelog_gen
from helpers.complexity_check import complexity_check
logging.getLogger().setLevel(logging.INFO) logging.getLogger().setLevel(logging.INFO)
@@ -40,63 +42,26 @@ if __name__ == "__main__":
pyproject = tomli.load(fp) pyproject = tomli.load(fp)
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog="continuous-integration-helper", prog="continuous-integration-helper", description="A tiny set of scripts to help continous integration on python"
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( parser.add_argument(
"-tc", "-cc", "--coverage-check", dest="coveragecheck", action="store_true", help="enable unit-test coverage check (requires unit-test)"
"--type-check",
dest="typecheck",
action="store_true",
help="enable static typing check",
) )
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( parser.add_argument(
"-ut", "-pdf", "--doc-gen-pdf", dest="docgenpdf", action="store_true", help="enable pdf documentation export (requires doc-gen)"
"--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( parser.add_argument("-clg", "--changelog-gen", dest="changeloggen", action="store_true", help="enable changelog generation")
"-qc",
"--quality-check",
dest="qualitycheck",
action="store_true",
help="enable code quality check",
)
parser.add_argument( parser.add_argument("-cpc", "--complexity-check", dest="complexitycheck", action="store_true", help="enable complexity check")
"-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",
)
args = parser.parse_args() args = parser.parse_args()
@@ -112,6 +77,7 @@ if __name__ == "__main__":
# args.docgen = True # args.docgen = True
# args.docgenpdf = True # args.docgenpdf = True
# args.changeloggen = True # args.changeloggen = True
# args.complexitycheck = True
helpers = [] helpers = []
if args.typecheck == True: if args.typecheck == True:
@@ -141,6 +107,9 @@ if __name__ == "__main__":
if args.changeloggen == True: if args.changeloggen == True:
helpers.append(changelog_gen) helpers.append(changelog_gen)
if args.complexitycheck == True:
helpers.append(complexity_check)
for helper in helpers: for helper in helpers:
helper.set_context(project_rootdir_path, pyproject) helper.set_context(project_rootdir_path, pyproject)
helper.reset_result_dir() helper.reset_result_dir()

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

@@ -31,8 +31,6 @@ class doc_gen(helper_withresults_base):
@classmethod @classmethod
def do_job(cls): def do_job(cls):
print(cls.project_rootdir_path)
print()
# create doc root dir # create doc root dir
doc_path = cls.project_rootdir_path / "docs" doc_path = cls.project_rootdir_path / "docs"
@@ -42,12 +40,8 @@ class doc_gen(helper_withresults_base):
cls._reset_dir(site_path) cls._reset_dir(site_path)
# copy files from main project dir # copy files from main project dir
shutil.copyfile( shutil.copyfile(str(cls.project_rootdir_path / "README.md"), str(doc_path / "README.md"))
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 / "LICENSE.md"), str(doc_path / "LICENSE.md")
)
# copy files from static-doc dir # 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))
@@ -57,12 +51,8 @@ class doc_gen(helper_withresults_base):
cls._reset_dir(reference_path) cls._reset_dir(reference_path)
for path in sorted((cls.project_rootdir_path / "src").rglob("*.py")): for path in sorted((cls.project_rootdir_path / "src").rglob("*.py")):
module_path = path.relative_to( module_path = path.relative_to(cls.project_rootdir_path / "src").with_suffix("")
cls.project_rootdir_path / "src" doc_path = path.relative_to(cls.project_rootdir_path / "src").with_suffix(".md")
).with_suffix("")
doc_path = path.relative_to(cls.project_rootdir_path / "src").with_suffix(
".md"
)
full_doc_path = Path(reference_path, doc_path) full_doc_path = Path(reference_path, doc_path)
parts = list(module_path.parts) parts = list(module_path.parts)
@@ -77,16 +67,7 @@ class doc_gen(helper_withresults_base):
identifier = "src." + ".".join(parts) identifier = "src." + ".".join(parts)
print("::: " + identifier, file=fd) print("::: " + identifier, file=fd)
cmdopts = [ cmdopts = [f"{sys.executable}", "-m", "mkdocs", "-v", "build", "--site-dir", str(site_path), "--clean"]
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 # 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
@@ -100,12 +81,9 @@ class doc_gen(helper_withresults_base):
{ {
"with-pdf": { "with-pdf": {
"cover_subtitle": "User Manual", "cover_subtitle": "User Manual",
"cover_logo": str( "cover_logo": str(cls.project_rootdir_path / "docs-static" / "Library.jpg"),
cls.project_rootdir_path / "docs-static" / "Library.jpg"
),
"verbose": False, "verbose": False,
"media_type": "print", "media_type": "print",
"headless_chrome_path": "chromium",
"exclude_pages": ["LICENSE"], "exclude_pages": ["LICENSE"],
"output_path": str(site_path / "pdf" / "manual.pdf"), "output_path": str(site_path / "pdf" / "manual.pdf"),
} }
@@ -119,11 +97,7 @@ class doc_gen(helper_withresults_base):
break break
with open(cls.project_rootdir_path / "mkdocs.yml", "w") as mkdocsCfgFile: with open(cls.project_rootdir_path / "mkdocs.yml", "w") as mkdocsCfgFile:
mkdocsCfgFile.write( mkdocsCfgFile.write(yaml.dump(mkdocsCfg, Dumper=Dumper, default_flow_style=False, sort_keys=False))
yaml.dump(
mkdocsCfg, Dumper=Dumper, default_flow_style=False, sort_keys=False
)
)
res = cls.run_cmd(cmdopts) res = cls.run_cmd(cmdopts)
print(res.decode()) print(res.decode())

View File

@@ -53,13 +53,7 @@ class helper_base(ABC):
@classmethod @classmethod
def run_cmd_(cls, cmdarray): def run_cmd_(cls, cmdarray):
process = subprocess.run( process = subprocess.run(cmdarray, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, check=True)
cmdarray,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
check=True,
)
return process.stdout return process.stdout
@classmethod @classmethod
@@ -77,8 +71,4 @@ class helper_withresults_base(helper_base):
def get_result_dir(cls): def get_result_dir(cls):
if cls.helper_results_dir == None: if cls.helper_results_dir == None:
cls.helper_results_dir = cls.__name__ cls.helper_results_dir = cls.__name__
return ( return Path(__file__).parent.parent.absolute() / "helpers-results" / cls.helper_results_dir
Path(__file__).parent.parent.absolute()
/ "helpers-results"
/ cls.helper_results_dir
)

View File

@@ -37,9 +37,7 @@ class quality_check(helper_withresults_base):
def GetPylintMessageList(cls): def GetPylintMessageList(cls):
Messagelist = dict() Messagelist = dict()
regex = r"^:([a-zA-Z-]+) \(([^\)]+)\)" regex = r"^:([a-zA-Z-]+) \(([^\)]+)\)"
for line in cls.run_cmd( for line in cls.run_cmd([sys.executable, "-m", "pylint", "--list-msgs"]).splitlines():
[sys.executable, "-m", "pylint", "--list-msgs"]
).splitlines():
if res := re.search(regex, line.decode()): if res := re.search(regex, line.decode()):
Messagelist[res.group(1)] = res.group(2) Messagelist[res.group(1)] = res.group(2)
cls.PylintMessageList = Messagelist cls.PylintMessageList = Messagelist
@@ -62,6 +60,7 @@ class quality_check(helper_withresults_base):
with redirect_stdout(StdOutput): with redirect_stdout(StdOutput):
pylint_Run( pylint_Run(
[ [
"--load-plugins=pylint.extensions.mccabe",
"--output-format=json,parseable", "--output-format=json,parseable",
"--disable=invalid-name", "--disable=invalid-name",
"--ignore=_version.py", "--ignore=_version.py",
@@ -73,9 +72,7 @@ class quality_check(helper_withresults_base):
exit=False, exit=False,
) )
with open( with open(cls.get_result_dir() / "report.json", "w+", encoding="utf-8") as Outfile:
cls.get_result_dir() / "report.json", "w+", encoding="utf-8"
) as Outfile:
# hacky way of exctracting json + having overall score... # hacky way of exctracting json + having overall score...
class TScanState(Enum): class TScanState(Enum):
TEXT_REPORT = 1 TEXT_REPORT = 1
@@ -122,81 +119,45 @@ class quality_check(helper_withresults_base):
ScanState = TScanState.OTHER_REPORT_STATISTICS ScanState = TScanState.OTHER_REPORT_STATISTICS
elif ScanState == TScanState.OTHER_REPORT_STATISTICS: elif ScanState == TScanState.OTHER_REPORT_STATISTICS:
if res := re.search( if res := re.search(r"^(\d+)(?= lines have been analyzed)", line):
r"^(\d+)(?= lines have been analyzed)", line
):
RES_all["NbAnalysedLines"] = float(res.group(1)) RES_all["NbAnalysedLines"] = float(res.group(1))
elif line == "Raw metrics": elif line == "Raw metrics":
ScanState = TScanState.OTHER_REPORT_METRICS ScanState = TScanState.OTHER_REPORT_METRICS
else: else:
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Statistics"][ RES_all["Statistics"]["module"] = cls.TryExtractPYReportMetric(line, "module")
"module"
] = cls.TryExtractPYReportMetric(line, "module")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Statistics"][ RES_all["Statistics"]["class"] = cls.TryExtractPYReportMetric(line, "class")
"class"
] = cls.TryExtractPYReportMetric(line, "class")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Statistics"][ RES_all["Statistics"]["method"] = cls.TryExtractPYReportMetric(line, "method")
"method"
] = cls.TryExtractPYReportMetric(line, "method")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Statistics"][ RES_all["Statistics"]["function"] = cls.TryExtractPYReportMetric(line, "function")
"function"
] = cls.TryExtractPYReportMetric(line, "function")
elif ScanState == TScanState.OTHER_REPORT_METRICS: elif ScanState == TScanState.OTHER_REPORT_METRICS:
if line == "Duplication": if line == "Duplication":
RES_all["RawMetricsPercent"]["code"] = ( RES_all["RawMetricsPercent"]["code"] = RES_all["RawMetrics"]["code"] / RES_all["NbAnalysedLines"]
RES_all["RawMetrics"]["code"] RES_all["RawMetricsPercent"]["docstring"] = RES_all["RawMetrics"]["docstring"] / RES_all["NbAnalysedLines"]
/ 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"]["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 ScanState = TScanState.OTHER_REPORT_DUPLICATION
else: else:
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"][ RES_all["RawMetrics"]["code"] = cls.TryExtractPYReportMetric(line, "code")
"code"
] = cls.TryExtractPYReportMetric(line, "code")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"][ RES_all["RawMetrics"]["docstring"] = cls.TryExtractPYReportMetric(line, "docstring")
"docstring"
] = cls.TryExtractPYReportMetric(line, "docstring")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"][ RES_all["RawMetrics"]["comment"] = cls.TryExtractPYReportMetric(line, "comment")
"comment"
] = cls.TryExtractPYReportMetric(line, "comment")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["RawMetrics"][ RES_all["RawMetrics"]["empty"] = cls.TryExtractPYReportMetric(line, "empty")
"empty"
] = cls.TryExtractPYReportMetric(line, "empty")
elif ScanState == TScanState.OTHER_REPORT_DUPLICATION: elif ScanState == TScanState.OTHER_REPORT_DUPLICATION:
if line == "Messages by category": if line == "Messages by category":
ScanState = TScanState.OTHER_REPORT_MESSAGES_CAT ScanState = TScanState.OTHER_REPORT_MESSAGES_CAT
else: else:
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Duplication"][ RES_all["Duplication"]["NbDupLines"] = cls.TryExtractPYReportMetric(line, "nb duplicated lines")
"NbDupLines"
] = cls.TryExtractPYReportMetric(
line, "nb duplicated lines"
)
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Duplication"][ RES_all["Duplication"]["PersentDuplicatedLines"] = cls.TryExtractPYReportMetric(
"PersentDuplicatedLines"
] = cls.TryExtractPYReportMetric(
line, "percent duplicated lines" line, "percent duplicated lines"
) )
@@ -205,21 +166,13 @@ class quality_check(helper_withresults_base):
ScanState = TScanState.OTHER_REPORT_MESSAGES ScanState = TScanState.OTHER_REPORT_MESSAGES
else: else:
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"][ RES_all["MessagesCat"]["Convention"] = cls.TryExtractPYReportMetric(line, "convention")
"Convention"
] = cls.TryExtractPYReportMetric(line, "convention")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"][ RES_all["MessagesCat"]["Refactor"] = cls.TryExtractPYReportMetric(line, "refactor")
"Refactor"
] = cls.TryExtractPYReportMetric(line, "refactor")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"][ RES_all["MessagesCat"]["Warning"] = cls.TryExtractPYReportMetric(line, "warning")
"Warning"
] = cls.TryExtractPYReportMetric(line, "warning")
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["MessagesCat"][ RES_all["MessagesCat"]["Error"] = cls.TryExtractPYReportMetric(line, "error")
"Error"
] = cls.TryExtractPYReportMetric(line, "error")
elif ScanState == TScanState.OTHER_REPORT_MESSAGES: elif ScanState == TScanState.OTHER_REPORT_MESSAGES:
# approx match because the number of '-' depend on screen width.. # approx match because the number of '-' depend on screen width..
@@ -228,16 +181,10 @@ class quality_check(helper_withresults_base):
else: else:
for PylintMessage in cls.PylintMessageList.keys(): for PylintMessage in cls.PylintMessageList.keys():
with suppress(PyLintMetricNotFound): with suppress(PyLintMetricNotFound):
RES_all["Messages"][ RES_all["Messages"][PylintMessage] = cls.TryExtractPYReportMetric(line, PylintMessage)
PylintMessage
] = cls.TryExtractPYReportMetric(
line, PylintMessage
)
elif ScanState == TScanState.OTHER_REPORT_END: elif ScanState == TScanState.OTHER_REPORT_END:
if res := re.search( if res := re.search(r"(?<=Your code has been rated at )(\d+(?:\.\d+)?)/10", line):
r"(?<=Your code has been rated at )(\d+(?:\.\d+)?)/10", line
):
RES_all["GlobalScore"] = float(res.group(1)) RES_all["GlobalScore"] = float(res.group(1))
print(RES_all["GlobalScore"]) print(RES_all["GlobalScore"])
else: else:
@@ -251,9 +198,7 @@ class quality_check(helper_withresults_base):
# => to export a working full csv we need to a 'flat' dict (no more nested dict) # => to export a working full csv we need to a 'flat' dict (no more nested dict)
RES_all_trim = copy.deepcopy(RES_all) RES_all_trim = copy.deepcopy(RES_all)
del RES_all_trim["Messages"] del RES_all_trim["Messages"]
flat_RES_all = pandas.json_normalize(RES_all_trim, sep="_").to_dict( flat_RES_all = pandas.json_normalize(RES_all_trim, sep="_").to_dict(orient="records")[0]
orient="records"
)[0]
with open(cls.get_result_dir() / "metrics.csv", "w", newline="") as csv_file: with open(cls.get_result_dir() / "metrics.csv", "w", newline="") as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=flat_RES_all.keys()) writer = csv.DictWriter(csv_file, fieldnames=flat_RES_all.keys())
@@ -262,9 +207,7 @@ class quality_check(helper_withresults_base):
# splited csv exports for jenkins plots: RawMetricsPercent # splited csv exports for jenkins plots: RawMetricsPercent
RES_all_percent = RES_all["RawMetricsPercent"] RES_all_percent = RES_all["RawMetricsPercent"]
with open( with open(cls.get_result_dir() / "metrics_rawpercent.csv", "w", newline="") as csv_file:
cls.get_result_dir() / "metrics_rawpercent.csv", "w", newline=""
) as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_percent.keys()) writer = csv.DictWriter(csv_file, fieldnames=RES_all_percent.keys())
writer.writeheader() writer.writeheader()
writer.writerow(RES_all_percent) writer.writerow(RES_all_percent)
@@ -272,40 +215,30 @@ class quality_check(helper_withresults_base):
# splited csv exports for jenkins plots: Statistics + Duplication + NbAnalysedStatments + NbAnalysedLines # splited csv exports for jenkins plots: Statistics + Duplication + NbAnalysedStatments + NbAnalysedLines
RES_all_stats = copy.deepcopy(RES_all["Statistics"]) RES_all_stats = copy.deepcopy(RES_all["Statistics"])
RES_all_stats["NbDupLines"] = RES_all["Duplication"]["NbDupLines"] RES_all_stats["NbDupLines"] = RES_all["Duplication"]["NbDupLines"]
RES_all_stats["PersentDuplicatedLines"] = RES_all["Duplication"][ RES_all_stats["PersentDuplicatedLines"] = RES_all["Duplication"]["PersentDuplicatedLines"]
"PersentDuplicatedLines"
]
RES_all_stats["NbAnalysedStatments"] = RES_all["NbAnalysedStatments"] RES_all_stats["NbAnalysedStatments"] = RES_all["NbAnalysedStatments"]
RES_all_stats["NbAnalysedLines"] = RES_all["NbAnalysedLines"] RES_all_stats["NbAnalysedLines"] = RES_all["NbAnalysedLines"]
with open( with open(cls.get_result_dir() / "metrics_Statistics.csv", "w", newline="") as csv_file:
cls.get_result_dir() / "metrics_Statistics.csv", "w", newline=""
) as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_stats.keys()) writer = csv.DictWriter(csv_file, fieldnames=RES_all_stats.keys())
writer.writeheader() writer.writeheader()
writer.writerow(RES_all_stats) writer.writerow(RES_all_stats)
# splited csv exports for jenkins plots: Statistics + Duplication # splited csv exports for jenkins plots: Statistics + Duplication
RES_all_MessagesCat = RES_all["MessagesCat"] RES_all_MessagesCat = RES_all["MessagesCat"]
with open( with open(cls.get_result_dir() / "metrics_MessagesCat.csv", "w", newline="") as csv_file:
cls.get_result_dir() / "metrics_MessagesCat.csv", "w", newline=""
) as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_all_MessagesCat.keys()) writer = csv.DictWriter(csv_file, fieldnames=RES_all_MessagesCat.keys())
writer.writeheader() writer.writeheader()
writer.writerow(RES_all_MessagesCat) writer.writerow(RES_all_MessagesCat)
# splited csv exports for jenkins plots: GlobalScore # splited csv exports for jenkins plots: GlobalScore
RES_GlobalScore = {"GlobalScore": RES_all["GlobalScore"]} RES_GlobalScore = {"GlobalScore": RES_all["GlobalScore"]}
with open( with open(cls.get_result_dir() / "metrics_GlobalScore.csv", "w", newline="") as csv_file:
cls.get_result_dir() / "metrics_GlobalScore.csv", "w", newline=""
) as csv_file:
writer = csv.DictWriter(csv_file, fieldnames=RES_GlobalScore.keys()) writer = csv.DictWriter(csv_file, fieldnames=RES_GlobalScore.keys())
writer.writeheader() writer.writeheader()
writer.writerow(RES_GlobalScore) writer.writerow(RES_GlobalScore)
# converting the report using pylint_json2html (/!\ internal API, but as their is no leading '_' ...) # converting the report using pylint_json2html (/!\ internal API, but as their is no leading '_' ...)
with open( with open(cls.get_result_dir() / "report.html", "w+", encoding="utf-8") as Outfile:
cls.get_result_dir() / "report.html", "w+", encoding="utf-8"
) as Outfile:
raw_data = json.loads(JsonContent) raw_data = json.loads(JsonContent)
report = pylint_json2html.Report(raw_data) report = pylint_json2html.Report(raw_data)
Outfile.write(report.render()) Outfile.write(report.render())

View File

@@ -40,16 +40,11 @@ class unit_test(helper_withresults_base):
# we start coverage now because module files discovery is part of the coverage measurement # we start coverage now because module files discovery is part of the coverage measurement
CoverageReportPath = Path(str(cls.get_result_dir()) + "_coverage") CoverageReportPath = Path(str(cls.get_result_dir()) + "_coverage")
cls._reset_dir(CoverageReportPath) cls._reset_dir(CoverageReportPath)
cov = coverage.Coverage( cov = coverage.Coverage(cover_pylib=False, branch=True, source_pkgs=["src." + cls.pyproject["project"]["name"]])
cover_pylib=False,
branch=True,
source_pkgs=["src." + cls.pyproject["project"]["name"]],
)
cov.start() cov.start()
package_tests = test_loader.discover( package_tests = test_loader.discover(
start_dir=str(cls.project_rootdir_path / "test"), start_dir=str(cls.project_rootdir_path / "test"), top_level_dir=str(cls.project_rootdir_path / "test")
top_level_dir=str(cls.project_rootdir_path / "test"),
) )
if cls.enable_xml_export: if cls.enable_xml_export:
testRunner = xmlrunner.XMLTestRunner(output=str(str(cls.get_result_dir()))) testRunner = xmlrunner.XMLTestRunner(output=str(str(cls.get_result_dir())))
@@ -64,9 +59,7 @@ class unit_test(helper_withresults_base):
cov.stop() cov.stop()
cov.save() cov.save()
cov.html_report(directory=str(CoverageReportPath)) cov.html_report(directory=str(CoverageReportPath))
cov.xml_report( cov.xml_report(outfile=(CoverageReportPath / f"{cls.CoverageReportName}.xml"))
outfile=(CoverageReportPath / f"{cls.CoverageReportName}.xml")
)
# computing results (Only if xml available) # computing results (Only if xml available)
if cls.enable_full_xml_export == True: if cls.enable_full_xml_export == True:
@@ -75,22 +68,14 @@ class unit_test(helper_withresults_base):
cls._reset_dir(FullReportPath) cls._reset_dir(FullReportPath)
FullJUnitReport = JUnitXml() FullJUnitReport = JUnitXml()
for fname in [ for fname in [fname for fname in os.listdir(cls.get_result_dir()) if fname.endswith(".xml")]:
fname
for fname in os.listdir(cls.get_result_dir())
if fname.endswith(".xml")
]:
FullJUnitReport += JUnitXml.fromfile(str(cls.get_result_dir() / fname)) FullJUnitReport += JUnitXml.fromfile(str(cls.get_result_dir() / fname))
current_datetime = datetime.datetime.utcnow().strftime("%Y%m%dT%H%M%SZ") 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}' full_report_base_name = f'{cls.pyproject["project"]["name"]}-{cls.FullReportName}-{current_datetime}'
FullJUnitReport.write(str(FullReportPath / f"{full_report_base_name}.xml")) FullJUnitReport.write(str(FullReportPath / f"{full_report_base_name}.xml"))
report = junit2html_parser.Junit( report = junit2html_parser.Junit(FullReportPath / f"{full_report_base_name}.xml")
FullReportPath / f"{full_report_base_name}.xml"
)
html = report.html() html = report.html()
with open( with open(FullReportPath / f"{full_report_base_name}.html", "wb") as outfile:
FullReportPath / f"{full_report_base_name}.html", "wb"
) as outfile:
outfile.write(html.encode("utf-8")) outfile.write(html.encode("utf-8"))
print("Done") print("Done")

View File

@@ -1,3 +1,11 @@
# 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/>.
docs_dir: docs docs_dir: docs
site_name: pygitversionhelper 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/'

View File

@@ -12,7 +12,6 @@ build-backend = "setuptools.build_meta"
[tool.setuptools_scm] [tool.setuptools_scm]
version_scheme= "post-release" version_scheme= "post-release"
# tag_regex="^(?:v)?(?P<version>\\d+\\.\\d+\\.\\d+)([\\.\\-\\+])?(?:.*)?"
[project] [project]
name = "pygitversionhelper" name = "pygitversionhelper"
@@ -57,6 +56,7 @@ Tracker = "https://chacha.ddns.net/gitea/chacha/pygitversionhelper/issue
[project.optional-dependencies] [project.optional-dependencies]
test = ["junitparser>=2.8","junit2html>=30.1","xmlrunner>=1.7","mypy>=0.99" ] test = ["junitparser>=2.8","junit2html>=30.1","xmlrunner>=1.7","mypy>=0.99" ]
coverage-check = ["coverage>=7.0"] coverage-check = ["coverage>=7.0"]
complexity-check = ["radon>=5.1"]
quality-check = ["pylint>=2.15","pylint-json2html>=0.4","pandas>=1.5"] quality-check = ["pylint>=2.15","pylint-json2html>=0.4","pandas>=1.5"]
type-check = ["mypy[reports]>=0.99" ] 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","pymdown-extensions>=9","mkdocs-markdownextradata-plugin","mkdocs-mermaid2-plugin"] 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"]

View File

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

View File

@@ -37,9 +37,7 @@ import logging
from packaging.version import VERSION_PATTERN as packaging_VERSION_PATTERN from packaging.version import VERSION_PATTERN as packaging_VERSION_PATTERN
def _exec( def _exec(cmd: str, root: str | os.PathLike | None = None, raw: bool = False) -> list[str]:
cmd: str, root: str | os.PathLike | None = None, raw: bool = False
) -> list[str]:
""" """
helper function to handle system cmd execution helper function to handle system cmd execution
Args: Args:
@@ -60,9 +58,9 @@ def _exec(
) )
if re.search("not a git repository", p.stderr): if re.search("not a git repository", p.stderr):
raise gitversionhelper.repository.notAGitRepository() raise gitversionhelper.repository.notAGitRepository()
if re.search("fatal:", p.stderr): # pragma: nocover if re.search("fatal:", p.stderr):
raise gitversionhelper.unknownGITFatalError(p.stderr) raise gitversionhelper.unknownGITFatalError(p.stderr)
if int(p.returncode) < 0: # pragma: nocover if int(p.returncode) < 0:
raise gitversionhelper.unknownGITError(p.stderr) raise gitversionhelper.unknownGITError(p.stderr)
if raw: if raw:
@@ -147,7 +145,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
def getMessagesSinceTag(cls, tag: str, **kwargs) -> str: def getMessagesSinceTag(cls, tag: str, **kwargs) -> str:
""" """
retrieve a commits message history from repository retrieve a commits message history from repository
from LastCommit to the given tag from Latest commit to the given tag
Keyword Arguments: Keyword Arguments:
merged_output: output one single merged string merged_output: output one single merged string
same_branch(bool): force searching only in the same branch same_branch(bool): force searching only in the same branch
@@ -157,23 +155,15 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
current_commit_id = cls.getLast(**kwargs) current_commit_id = cls.getLast(**kwargs)
tag_commit_id = cls.getFromTag(tag) tag_commit_id = cls.getFromTag(tag)
if (cls.__OptDict["same_branch"] in kwargs) and ( if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
kwargs[cls.__OptDict["same_branch"]] is True commits = _exec(f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}")
):
commits = _exec(
f"git rev-list --first-parent --ancestry-path {tag_commit_id}..{current_commit_id}"
)
else: else:
commits = _exec( commits = _exec(f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}")
f"git rev-list --ancestry-path {tag_commit_id}..{current_commit_id}"
)
result = [] result = []
for commit in commits: for commit in commits:
result.append(cls.getMessage(commit)) result.append(cls.getMessage(commit))
if (cls.__OptDict["merged_output"] in kwargs) and ( if (cls.__OptDict["merged_output"] in kwargs) and (kwargs[cls.__OptDict["merged_output"]] is True):
kwargs[cls.__OptDict["merged_output"]] is True
):
print("JOIN") print("JOIN")
return os.linesep.join(result) return os.linesep.join(result)
return result return result
@@ -224,19 +214,13 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
Returns: Returns:
the commit Id the commit Id
""" """
if (cls.__OptDict["same_branch"] in kwargs) and ( if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
kwargs[cls.__OptDict["same_branch"]] is True
):
try: try:
res = _exec("git rev-parse HEAD") res = _exec("git rev-parse HEAD")
except gitversionhelper.unknownGITFatalError as _e: except gitversionhelper.unknownGITFatalError as _e:
raise cls.commitNotFound( raise cls.commitNotFound("no commit found in commit history") from _e
"no commit found in commit history"
) from _e
else: else:
res = _exec( res = _exec('git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format="%(objectname)"')
'git for-each-ref --sort=-committerdate refs/heads/ --count 1 --format="%(objectname)"'
)
if len(res) == 0: if len(res) == 0:
raise cls.commitNotFound("no commit found in commit history") raise cls.commitNotFound("no commit found in commit history")
@@ -286,29 +270,21 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if sort not in cls.__validGitTagSort: if sort not in cls.__validGitTagSort:
raise gitversionhelper.wrongArguments("sort option not in allowed list") raise gitversionhelper.wrongArguments("sort option not in allowed list")
if (cls.__OptDict["same_branch"] in kwargs) and ( if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
kwargs[cls.__OptDict["same_branch"]] is True
):
currentBranch = _exec("git rev-parse --abbrev-ref HEAD") currentBranch = _exec("git rev-parse --abbrev-ref HEAD")
return list( return list(reversed(_exec(f"git tag --merged {currentBranch[0]} --sort={sort}")))
reversed(
_exec(f"git tag --merged {currentBranch[0]} --sort={sort}")
)
)
return list(reversed(_exec(f"git tag -l --sort={sort}"))) return list(reversed(_exec(f"git tag -l --sort={sort}")))
@classmethod @classmethod
def getLastTag(cls, **kwargs) -> str | None: def getLastTag(cls, **kwargs) -> str | None:
""" """
retrieve the last tag from a repository retrieve the Latest tag from a repository
Keyword Arguments: Keyword Arguments:
same_branch(bool): force searching only in the same branch same_branch(bool): force searching only in the same branch
Returns: Returns:
the tag the tag
""" """
if (cls.__OptDict["same_branch"] in kwargs) and ( if (cls.__OptDict["same_branch"] in kwargs) and (kwargs[cls.__OptDict["same_branch"]] is True):
kwargs[cls.__OptDict["same_branch"]] is True
):
res = _exec("git describe --tags --first-parent --abbrev=0") res = _exec("git describe --tags --first-parent --abbrev=0")
else: else:
res = _exec("git rev-list --tags --date-order --max-count=1") res = _exec("git rev-list --tags --date-order --max-count=1")
@@ -317,14 +293,14 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if len(res) == 0: if len(res) == 0:
raise cls.tagNotFound("no tag found in commit history") raise cls.tagNotFound("no tag found in commit history")
if len(res) != 1: # pragma: nocover if len(res) != 1:
raise cls.moreThanOneTag("multiple tags on same commit is unsupported") raise cls.moreThanOneTag("multiple tags on same commit is unsupported")
return res[0] return res[0]
@classmethod @classmethod
def getDistanceFromTag(cls, tag: str = None, **kwargs) -> int: def getDistanceFromTag(cls, tag: str = None, **kwargs) -> int:
""" """
retrieve the distance between HEAD and tag in the repository retrieve the distance between Latest commit and tag in the repository
Arguments: Arguments:
tag: reference tag, if None the most recent one will be used tag: reference tag, if None the most recent one will be used
Keyword Arguments: Keyword Arguments:
@@ -428,15 +404,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
""" """
BumpDevStrategy = cls.DefaultBumpDevStrategy BumpDevStrategy = cls.DefaultBumpDevStrategy
if cls.__OptDict["bump_dev_strategy"] in kwargs: if cls.__OptDict["bump_dev_strategy"] in kwargs:
if ( if kwargs[cls.__OptDict["bump_dev_strategy"]] in cls.BumpDevStrategys:
kwargs[cls.__OptDict["bump_dev_strategy"]]
in cls.BumpDevStrategys
):
BumpDevStrategy = kwargs[cls.__OptDict["bump_dev_strategy"]] BumpDevStrategy = kwargs[cls.__OptDict["bump_dev_strategy"]]
else: else:
raise gitversionhelper.wrongArguments( raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
f"invalid {cls.__OptDict['bump_type']} requested"
)
return BumpDevStrategy return BumpDevStrategy
@classmethod @classmethod
@@ -453,14 +424,10 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if kwargs[cls.__OptDict["bump_type"]] in cls.BumpTypes: if kwargs[cls.__OptDict["bump_type"]] in cls.BumpTypes:
BumpType = kwargs[cls.__OptDict["bump_type"]] BumpType = kwargs[cls.__OptDict["bump_type"]]
else: else:
raise gitversionhelper.wrongArguments( raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['bump_type']} requested")
f"invalid {cls.__OptDict['bump_type']} requested"
)
return BumpType return BumpType
def bump( def bump(self, amount: int = 1, **kwargs) -> gitversionhelper.version.MetaVersion | str: # pylint: disable=R0912
self, amount: int = 1, **kwargs
) -> gitversionhelper.version.MetaVersion | str: # pylint: disable=R0912
""" """
bump the version to the next one bump the version to the next one
Keyword Arguments: Keyword Arguments:
@@ -508,9 +475,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
_v.post_count = 0 _v.post_count = 0
_v.raw = _v.doFormatVersion(**kwargs) _v.raw = _v.doFormatVersion(**kwargs)
if (self.__OptDict["formated_output"] in kwargs) and ( if (self.__OptDict["formated_output"] in kwargs) and (kwargs[self.__OptDict["formated_output"]] is True):
kwargs[self.__OptDict["formated_output"]] is True
):
return _v.doFormatVersion(**kwargs) return _v.doFormatVersion(**kwargs)
return _v return _v
@@ -538,9 +503,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if kwargs[cls.__OptDict["version_std"]] in cls.VersionStds: if kwargs[cls.__OptDict["version_std"]] in cls.VersionStds:
VersionStd = kwargs[cls.__OptDict["version_std"]] VersionStd = kwargs[cls.__OptDict["version_std"]]
else: else:
raise gitversionhelper.wrongArguments( raise gitversionhelper.wrongArguments(f"invalid {cls.__OptDict['version_std']} requested")
f"invalid {cls.__OptDict['version_std']} requested"
)
return VersionStd return VersionStd
@classmethod @classmethod
@@ -557,10 +520,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
the last version the last version
""" """
if gitversionhelper.repository.isDirty() is not False: if gitversionhelper.repository.isDirty() is not False:
raise gitversionhelper.repository.repositoryDirty( raise gitversionhelper.repository.repositoryDirty("The repository is dirty and a current version can not be generated.")
"The repository is dirty and a current version"
" can not be generated."
)
saved_kwargs = copy(kwargs) saved_kwargs = copy(kwargs)
if "formated_output" in kwargs: if "formated_output" in kwargs:
del saved_kwargs["formated_output"] del saved_kwargs["formated_output"]
@@ -571,9 +531,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
amount = gitversionhelper.tag.getDistanceFromTag(_v.raw, **kwargs) amount = gitversionhelper.tag.getDistanceFromTag(_v.raw, **kwargs)
_v = _v.bump(amount, **saved_kwargs) _v = _v.bump(amount, **saved_kwargs)
if (cls.__OptDict["formated_output"] in kwargs) and ( if (cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True):
kwargs[cls.__OptDict["formated_output"]] is True
):
return _v.doFormatVersion(**kwargs) return _v.doFormatVersion(**kwargs)
return _v return _v
@@ -583,7 +541,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
Same as getCurrentVersion() with formated_output kwarg activated Same as getCurrentVersion() with formated_output kwarg activated
""" """
kwargs["formated_output"] = True kwargs["formated_output"] = True
return cls.getCurrentVersion(kwargs) return cls.getCurrentVersion(**kwargs)
@classmethod @classmethod
def _parseTag(cls, tag, **kwargs): # pylint: disable=R0914, R0912, R0915 def _parseTag(cls, tag, **kwargs): # pylint: disable=R0914, R0912, R0915
@@ -618,31 +576,21 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
pre_count = 0 pre_count = 0
if _pre := _m.group("prerelease"): if _pre := _m.group("prerelease"):
if ( if (_match := re.search(cls.VersionStds["SemVer"]["regex_preversion_num"], _pre)) is not None:
_match := re.search(
cls.VersionStds["SemVer"]["regex_preversion_num"], _pre
)
) is not None:
pre_count = int(_match.group("num")) pre_count = int(_match.group("num"))
else: else:
pre_count = 1 pre_count = 1
post_count = 0 post_count = 0
if _post := _m.group("buildmetadata"): if _post := _m.group("buildmetadata"):
if ( if (_match := re.search(cls.VersionStds["SemVer"]["regex_build_num"], _post)) is not None:
_match := re.search(
cls.VersionStds["SemVer"]["regex_build_num"], _post
)
) is not None:
post_count = int(_match.group("num")) post_count = int(_match.group("num"))
else: else:
post_count = 1 post_count = 1
bFound = True bFound = True
VersionStd = "SemVer" VersionStd = "SemVer"
if VersionStd == "PEP440" or ( if VersionStd == "PEP440" or ((bAutoVersionStd is True) and (bFound is not True)):
(bAutoVersionStd is True) and (bFound is not True)
):
_r = re.compile( _r = re.compile(
r"^\s*" + cls.VersionStds["PEP440"]["regex"] + r"\s*$", r"^\s*" + cls.VersionStds["PEP440"]["regex"] + r"\s*$",
re.VERBOSE | re.IGNORECASE, re.VERBOSE | re.IGNORECASE,
@@ -663,22 +611,14 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
VersionStd = "PEP440" VersionStd = "PEP440"
if not bFound: if not bFound:
raise gitversionhelper.version.noValidVersion( raise gitversionhelper.version.noValidVersion("no valid version found in tags")
"no valid version found in tags"
)
if pre_count > 0 and post_count > 0: if pre_count > 0 and post_count > 0:
raise cls.PreAndPostVersionUnsupported( raise cls.PreAndPostVersionUnsupported("can not parse a version with both pre and post release number.")
"can not parse a version with both pre" " and post release number." return cls.MetaVersion(VersionStd, major, minor, patch, pre_count, post_count, tag)
)
return cls.MetaVersion(
VersionStd, major, minor, patch, pre_count, post_count, tag
)
@classmethod @classmethod
def getLastVersion( def getLastVersion(cls, **kwargs) -> MetaVersion | str: # pylint: disable=R0914, R0912, R0915
cls, **kwargs
) -> MetaVersion | str: # pylint: disable=R0914, R0912, R0915
"""get the last version from tags """get the last version from tags
Keyword Arguments: Keyword Arguments:
version_std(str): the given version_std (can be None) version_std(str): the given version_std (can be None)
@@ -700,9 +640,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
try: try:
_v = cls._parseTag(lastTag, **kwargs) _v = cls._parseTag(lastTag, **kwargs)
except gitversionhelper.version.noValidVersion as _ex: except gitversionhelper.version.noValidVersion as _ex:
if (cls.__OptDict["ignore_unknown_tags"] in kwargs) and ( if (cls.__OptDict["ignore_unknown_tags"] in kwargs) and (kwargs[cls.__OptDict["ignore_unknown_tags"]] is True):
kwargs[cls.__OptDict["ignore_unknown_tags"]] is True
):
tags = gitversionhelper.tag.getTags(sort="taggerdate", **kwargs) tags = gitversionhelper.tag.getTags(sort="taggerdate", **kwargs)
_v = None _v = None
for _tag in tags: for _tag in tags:
@@ -714,9 +652,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
if _v is None: if _v is None:
raise gitversionhelper.version.noValidVersion() from _ex raise gitversionhelper.version.noValidVersion() from _ex
if (cls.__OptDict["formated_output"] in kwargs) and ( if (cls.__OptDict["formated_output"] in kwargs) and (kwargs[cls.__OptDict["formated_output"]] is True):
kwargs[cls.__OptDict["formated_output"]] is True
):
return _v.doFormatVersion(**kwargs) return _v.doFormatVersion(**kwargs)
return _v return _v
@@ -750,8 +686,7 @@ class gitversionhelper: # pylint: disable=too-few-public-methods
OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}" OutputFormat = "{major}.{minor}.{patch}{revpattern}{revcount}"
if post_count > 0 and pre_count > 0: if post_count > 0 and pre_count > 0:
raise gitversionhelper.version.PreAndPostVersionUnsupported( raise gitversionhelper.version.PreAndPostVersionUnsupported(
"cannot output a version with both pre " "cannot output a version with both pre and post release number."
"and post release number."
) )
if VersionStd == "PEP440": if VersionStd == "PEP440":
if post_count > 0: if post_count > 0:

File diff suppressed because it is too large Load Diff