Repository navigation
Expand file tree
/
Copy path_tasks.py
More file actions
179 lines (149 loc) · 5.55 KB
/
Copy path_tasks.py
File metadata and controls
179 lines (149 loc) · 5.55 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
"""Cross-platform task helpers for pixi."""
import glob
import os
import shutil
import subprocess
import sys
import webbrowser
from pathlib import Path
def clean_build():
for d in ["build", "dist", ".eggs"]:
shutil.rmtree(d, ignore_errors=True)
for p in glob.glob("**/*.egg-info", recursive=True):
shutil.rmtree(p, ignore_errors=True)
for p in glob.glob("**/*.egg", recursive=True):
os.remove(p)
def clean_pyc():
for pat in ["**/*.pyc", "**/*.pyo", "**/*~"]:
for p in glob.glob(pat, recursive=True):
os.remove(p)
for p in glob.glob("**/__pycache__", recursive=True):
shutil.rmtree(p, ignore_errors=True)
def clean_test():
for f in [".coverage", "report.html", "report.xml", "coverage.xml"]:
if os.path.exists(f):
os.remove(f)
for d in ["htmlcov", ".pytest_cache"]:
shutil.rmtree(d, ignore_errors=True)
for p in glob.glob(".coverage.*"):
os.remove(p)
def docs(pkg):
for f in [os.path.join("docs", pkg + ".rst"), os.path.join("docs", "modules.rst")]:
if os.path.exists(f):
os.remove(f)
subprocess.run(
["sphinx-apidoc", pkg, "-o", "docs/", "--private",
"--doc-project", "Python API reference"],
check=True,
)
shutil.rmtree(os.path.join("docs", "_build"), ignore_errors=True)
subprocess.run(
["sphinx-build", "-b", "html", "docs", os.path.join("docs", "_build", "html")],
check=True,
)
webbrowser.open(os.path.join("docs", "_build", "html", "index.html"))
def dist():
subprocess.run([sys.executable, "-m", "build"], check=True)
for f in os.listdir("dist"):
print(f)
def pytest(pkg):
"""Run pytest.
- puts the coverage results in the folder 'htmlcov'
- generates cobertura 'coverage.xml' (needed to show coverage in GitLab MR changes)
- generates 'report.html' based on pytest-reporter-html1
- generates JUnit 'report.xml' to show the test report as a new tab in a GitLab MR
NOTE: Additional options pytest and coverage (plugin pytest-cov) are defined in .pytest.ini and .coveragerc.
"""
import pytest
return pytest.main(
[
"tests",
"--verbosity=3",
"--color=yes",
"--tb=short",
f"--cov={pkg}",
"--cov-report=html:htmlcov",
"--cov-report=term-missing",
"--cov-report=xml:coverage.xml",
"--template=html1/index.html",
"--report=report.html",
"--junitxml=report.xml",
]
)
def lint(pkg):
def _run_check(command):
result = subprocess.run(command)
if result.returncode != 0:
sys.exit(result.returncode)
_run_check(["flake8", "--max-line-length=120", pkg])
_run_check(["pycodestyle", pkg, "--max-line-length=120"])
# Report docstring violations, but don't fail the CI job.
subprocess.run(
["pydocstyle", pkg],
check=False,
)
def urlcheck():
try:
result = subprocess.run(
[
"lychee",
"**/*.md",
"**/*.rst",
"**/*.py",
"**/*.json",
"--no-progress",
"--timeout", "2",
"--verbose",
"--exclude-path", ".pixi",
"--exclude-path", ".git",
# "forbidden" websites
"--exclude", "https://www.gnu.org/licenses/",
"--exclude", "https://www.mdpi.com/2072-4292/9/7/676",
"--exclude", "https://doi.org/10.3390/s21124125",
"--exclude", "https://stackoverflow.com/a/43357954/2952871",
"--exclude", "https://stackoverflow.com/questions/24978052/interpolation-over-regular-grid-in-python",
"--exclude", "https://stackoverflow.com/questions/2302315/",
],
timeout=120,
)
except subprocess.TimeoutExpired:
print("ERROR: lychee timed out after 120 seconds.")
raise SystemExit(1)
if result.returncode != 0:
raise SystemExit(result.returncode)
def create_env_info(env_name: str):
"""Capture information about the current Pixi environment."""
out = Path(f"env_info__{env_name}")
out.mkdir(exist_ok=True)
(out / "pixi-list.txt").write_text(
subprocess.check_output(["pixi", "list", "-e", env_name], text=True)
)
(out / "pixi-info.txt").write_text(
subprocess.check_output(["pixi", "info"], text=True)
)
(out / "pixi-list.json").write_text(
subprocess.check_output(
["pixi", "list", "-e", env_name, "--json"], text=True
)
)
if __name__ == "__main__":
task = sys.argv[1] if len(sys.argv) > 1 else None
extra = sys.argv[2] if len(sys.argv) > 2 else None
tasks = {
"clean-build": clean_build,
"clean-pyc": clean_pyc,
"clean-test": clean_test,
"docs": lambda: docs(extra) if extra else sys.exit("docs requires package name"),
"dist": dist,
"pytest": lambda: pytest(extra) if extra else sys.exit("pytest requires package name"),
"lint": lambda: lint(extra) if extra else sys.exit("lint requires package name"),
"urlcheck": urlcheck,
"create-env-info": lambda: (create_env_info(extra) if extra
else sys.exit("env-info requires environment name"))
}
if task not in tasks:
print("Unknown task: {}. Available: {}".format(task, ", ".join(tasks)))
sys.exit(1)
result = tasks[task]()
if isinstance(result, int):
sys.exit(result)