-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebug_python_toolbox.py
78 lines (68 loc) · 2.12 KB
/
debug_python_toolbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import datetime
import json
# new imports
import subprocess
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
# imports from PTB
from typing import (
Optional,
Union,
)
import nox
from exasol.toolbox.metrics import (
Report,
maintainability,
reliability,
security,
technical_debt,
)
from nox import Session
def total_coverage(file: Union[str, Path]) -> float:
with TemporaryDirectory() as tmpdir:
tmp_dir = Path(tmpdir)
report = tmp_dir / "coverage.json"
p = subprocess.run(
["coverage", "json", f"--data-file={file}", "-o", f"{report}"],
capture_output=True,
check=False,
encoding="utf-8",
)
stdout = p.stdout.strip()
if (p.returncode == 1) and (stdout == "No data to report."):
print(
f"The following command"
f" returned non-zero exit status {p.returncode}:\n"
f' {" ".join(p.args)}\n'
f"{stdout}\n"
"Returning total coverage 100 %.",
file=sys.stderr,
)
return 100.0
with open(report, encoding="utf-8") as r:
data = json.load(r)
total: float = data["totals"]["percent_covered"]
return total
def create_report(
commit: str,
date: Optional[datetime.datetime] = None,
coverage_report: Union[str, Path] = ".coverage",
pylint_report: Union[str, Path] = ".lint.txt",
bandit_report: Union[str, Path] = ".security.json",
) -> Report:
return Report(
commit=commit,
date=date if date is not None else datetime.datetime.now(),
coverage=total_coverage(coverage_report),
maintainability=maintainability(pylint_report),
reliability=reliability(),
security=security(bandit_report),
technical_debt=technical_debt(),
)
@nox.session(name="project:report", python=False)
def report(session: Session) -> None:
sha1 = str(
session.run("git", "rev-parse", "HEAD", external=True, silent=True)
).strip()
project_report = create_report(commit=sha1)