forked from networktocode/netutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
310 lines (248 loc) · 9.46 KB
/
tasks.py
File metadata and controls
310 lines (248 loc) · 9.46 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
"""Tasks for use with Invoke."""
import re
from pathlib import Path
from invoke import Collection, Exit
from invoke import task as invoke_task
def is_truthy(arg):
"""Convert "truthy" strings into Booleans.
Examples
--------
>>> is_truthy('yes')
True
Args:
arg (str): Truthy string (True values are y, yes, t, true, on and 1; false values are n, no,
f, false, off and 0. Raises ValueError if val is anything else.
"""
if isinstance(arg, bool):
return arg
val = str(arg).lower()
if val in ("y", "yes", "t", "true", "on", "1"):
return True
if val in ("n", "no", "f", "false", "off", "0"):
return False
raise ValueError(f"Invalid truthy value: `{arg}`")
# Use pyinvoke configuration for default values, see http://docs.pyinvoke.org/en/stable/concepts/configuration.html
# Variables may be overwritten in invoke.yml or by the environment variables INVOKE_PYNTC_xxx
namespace = Collection("netutils")
namespace.configure(
{
"netutils": {
"project_name": "netutils",
"python_ver": "3.13",
"local": False,
"image_name": "netutils",
"image_ver": "latest",
"pwd": ".",
}
}
)
# pylint: disable=keyword-arg-before-vararg
def task(function=None, *args, **kwargs):
"""Task decorator to override the default Invoke task decorator and add each task to the invoke namespace."""
def task_wrapper(function=None):
"""Wrapper around invoke.task to add the task to the namespace as well."""
if args or kwargs:
task_func = invoke_task(*args, **kwargs)(function)
else:
task_func = invoke_task(function)
namespace.add_task(task_func)
return task_func
if function:
# The decorator was called with no arguments
return task_wrapper(function)
# The decorator was called with arguments
return task_wrapper
def run_command(context, exec_cmd, port=None):
"""Wrapper to run the invoke task commands.
Args:
context ([invoke.task]): Invoke task object.
exec_cmd ([str]): Command to run.
port (int): Used to serve local docs.
Returns:
result (obj): Contains Invoke result from running task.
"""
if is_truthy(context.netutils.local):
print(f"LOCAL - Running command {exec_cmd}")
result = context.run(exec_cmd, pty=True)
else:
print(
f"DOCKER - Running command: {exec_cmd} container: {context.netutils.image_name}:{context.netutils.image_ver}"
)
if port:
result = context.run(
f"docker run -it -p {port} -v {context.netutils.pwd}:/local {context.netutils.image_name}:{context.netutils.image_ver} sh -c '{exec_cmd}'",
pty=True,
)
else:
result = context.run(
f"docker run -it -v {context.netutils.pwd}:/local {context.netutils.image_name}:{context.netutils.image_ver} sh -c '{exec_cmd}'",
pty=True,
)
return result
@task(
help={
"cache": "Whether to use Docker's cache when building images (default enabled)",
"force_rm": "Always remove intermediate images",
"hide": "Suppress output from Docker",
}
)
def build(context, cache=True, force_rm=False, hide=False):
"""Build a Docker image."""
print(f"Building image {context.netutils.image_name}:{context.netutils.image_ver}")
command = f"docker build --tag {context.netutils.image_name}:{context.netutils.image_ver} --build-arg PYTHON_VER={context.netutils.python_ver} -f Dockerfile ."
if not cache:
command += " --no-cache"
if force_rm:
command += " --force-rm"
result = context.run(command, hide=hide)
if result.exited != 0:
print(
f"Failed to build image {context.netutils.image_name}:{context.netutils.image_ver}\nError: {result.stderr}"
)
@task
def clean(context):
"""Remove the project specific image."""
print(f"Attempting to forcefully remove image {context.netutils.image_name}:{context.netutils.image_ver}")
context.run(f"docker rmi {context.netutils.image_name}:{context.netutils.image_ver} --force")
print(f"Successfully removed image {context.netutils.image_name}:{context.netutils.image_ver}")
@task
def rebuild(context):
"""Clean the Docker image and then rebuild without using cache."""
clean(context)
build(context, cache=False)
@task
def coverage(context):
"""Run the coverage report against pytest.
Args:
context (obj): Used to run specific commands
"""
exec_cmd = "coverage run --source=netutils -m pytest"
run_command(context, exec_cmd)
run_command(context, "coverage report")
run_command(context, "coverage html")
@task
def pytest(context):
"""Run pytest for the specified name and Python version.
Args:
context (obj): Used to run specific commands
"""
exec_cmd = "pytest -vv --doctest-modules netutils/ && coverage run --source=netutils -m pytest && coverage report"
run_command(context, exec_cmd)
@task(aliases=("a",))
def autoformat(context):
"""Run code autoformatting."""
ruff(context, action=["format"], fix=True)
@task(
help={
"action": "Available values are `['lint', 'format']`. Can be used multiple times. (default: `['lint', 'format']`)",
"target": "File or directory to inspect, repeatable (default: all files in the project will be inspected)",
"fix": "Automatically fix selected actions. May not be able to fix all issues found. (default: False)",
"output_format": "See https://docs.astral.sh/ruff/settings/#output-format for details. (default: `concise`)",
},
iterable=["action", "target"],
)
def ruff(context, action=None, target=None, fix=False, output_format="concise"):
"""Run ruff to perform code formatting and/or linting."""
if not action:
action = ["lint", "format"]
if not target:
target = ["."]
exit_code = 0
if "format" in action:
command = "ruff format "
if not fix:
command += "--check "
command += " ".join(target)
if not run_command(context, command):
exit_code = 1
if "lint" in action:
command = "ruff check "
if fix:
command += "--fix "
command += f"--output-format {output_format} "
command += " ".join(target)
if not run_command(context, command):
exit_code = 1
if exit_code != 0:
raise Exit(code=exit_code)
@task
def mypy(context):
"""Run mypy to validate typing-hints.
Args:
context (obj): Used to run specific commands
local (bool): Define as `True` to execute locally
"""
exec_cmd = "mypy ./netutils"
run_command(context, exec_cmd)
@task
def pylint(context):
"""Run pylint for the specified name and Python version.
Args:
context (obj): Used to run specific commands
local (bool): Define as `True` to execute locally
"""
exec_cmd = 'find . -name "*.py" | grep -vE "(tests/unit/mock|netutils/data_files)" | xargs pylint'
run_command(context, exec_cmd)
@task
def yamllint(context):
"""Run yamllint to validate formatting adheres to NTC defined YAML standards.
Args:
context (obj): Used to run specific commands
local (bool): Define as `True` to execute locally
"""
exec_cmd = "yamllint ."
run_command(context, exec_cmd)
@task
def cli(context):
"""Enter the image to perform troubleshooting or dev work.
Args:
context (obj): Used to run specific commands
"""
dev = f"docker run -it -v {context.netutils.pwd}:/local {context.netutils.image_name}:{context.netutils.image_ver} /bin/bash"
context.run(f"{dev}", pty=True)
@task
def tests(context):
"""Run all tests for the specified name and Python version.
Args:
context (obj): Used to run specific commands
"""
ruff(context)
pylint(context)
yamllint(context)
mypy(context)
pytest(context)
print("All tests have passed!")
@task
def build_and_check_docs(context):
"""Build documentation to be available within Docs Sites."""
command = "mkdocs build --no-directory-urls --strict"
run_command(context, command)
# Check for the existence of a release notes file for the current version if it's not a prerelease.
version = context.run("poetry version --short", hide=True)
match = re.match(r"^(\d+)\.(\d+)\.\d+$", version.stdout.strip())
if match:
major = match.group(1)
minor = match.group(2)
release_notes_file = Path(__file__).parent / "docs" / "admin" / "release_notes" / f"version_{major}.{minor}.md"
if not release_notes_file.exists():
print(f"Release notes file `version_{major}.{minor}.md` does not exist.")
raise Exit(code=1)
@task
def docs(context):
"""Build and serve docs locally for development."""
exec_cmd = "mkdocs serve -v --dev-addr=0.0.0.0:8001"
run_command(context, exec_cmd, port="8001:8001")
@task(
help={
"version": "Version of netutils to generate the release notes for.",
}
)
def generate_release_notes(context, version=""):
"""Generate Release Notes using Towncrier."""
command = "poetry run towncrier build"
if version:
command += f" --version {version}"
else:
command += " --version `poetry version -s`"
# Due to issues with git repo ownership in the containers, this must always run locally.
context.run(command)