-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnox_common.py
More file actions
112 lines (85 loc) · 3.15 KB
/
Copy pathnox_common.py
File metadata and controls
112 lines (85 loc) · 3.15 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# SPDX-FileCopyrightText: Aresys S.r.l. <info@aresys.it>
# SPDX-License-Identifier: MIT
"""Automating python testing, SCT Plugins common NOX sessions"""
from __future__ import annotations
from datetime import datetime
import glob
import os
import shutil
import sys
from pathlib import Path
import nox
nox.options.error_on_missing_interpreters = True
_LICENSE_HEADER_MIT = """# SPDX-FileCopyrightText: Aresys S.r.l. <info@aresys.it>
# SPDX-License-Identifier: MIT
"""
PY_VERSIONS = ["3.11", "3.12", "3.13", "3.14"]
WIN32 = sys.platform == "win32"
PLATFORM = "win" if WIN32 else "linux"
def pytest_executor(session: nox.Session, project: str) -> None:
"""Executor of pytest from nox session.
Parameters
----------
session : nox.Session
nox session
project : str
project name, with "-" as separator for namespace sub-packages
"""
Path("_build").mkdir(exist_ok=True)
project = project.replace("-", "_")
session.install("-e", ".[test]", silent=True)
# Run pytest with coverage and JUnit XML output
session.run(
"python",
"-m",
"pytest",
f"--junitxml=_build/pytest-report-{PLATFORM}-py{session.python}.xml",
f"--cov-report=xml:_build/pytest-coverage-{PLATFORM}-py{session.python}.xml",
)
@nox.session()
def fix_format(session: nox.Session):
"""Reformat code base with ruff"""
session.install("ruff")
session.run("ruff", "check", "--select", "I", "--fix-only")
session.run("ruff", "format")
@nox.session()
def check_format(session: nox.Session):
"""Check proper formatting with ruff. Check presence of license header"""
session.install("ruff")
session.run("ruff", "--version")
session.run("ruff", "format", "--check")
session.run("ruff", "check")
def wrong_license_header(file: str) -> bool:
with open(file, "r", encoding="utf-8") as ifile:
first_line = ifile.readline()
if "# noqa:" in first_line:
first_line = ifile.readline()
header = first_line + ifile.readline() + ifile.readline()
validation = header != _LICENSE_HEADER_MIT
return validation
source_files = glob.glob("src/**/*.py", recursive=True)
no_licensed_files = list(filter(wrong_license_header, source_files))
if len(no_licensed_files) > 0:
for file in no_licensed_files:
session.warn(f"{file} has no license header")
session.error()
@nox.session()
def pylint(session: nox.Session):
"""Analysis of code-base quality with pylint"""
session.install("pylint")
session.run("python", "-m", "pylint", "src")
@nox.session(python=PY_VERSIONS)
def pytest(session: nox.Session) -> None:
"""Module testing with pytest"""
cwd = Path.cwd()
pytest_executor(session, project=cwd.name)
@nox.session()
def build_sdist(session: nox.Session):
"""Build source distribution package"""
session.install("build")
session.run("python", "-m", "build", "--sdist", silent=True)
@nox.session()
def build_wheel(session: nox.Session):
"""Build wheel distribution package"""
session.install("build")
session.run("python", "-m", "build", "--wheel", silent=True)