-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
445 lines (355 loc) · 9.91 KB
/
tasks.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
"""
Invoke - Tasks
==============
"""
from __future__ import annotations
import fnmatch
import inspect
import os
import re
import uuid
from colour.utilities import message_box
import colour_visuals
if not hasattr(inspect, "getargspec"):
inspect.getargspec = inspect.getfullargspec # pyright: ignore
from invoke.context import Context
from invoke.tasks import task
__author__ = "Colour Developers"
__copyright__ = "Copyright 2023 Colour Developers"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "[email protected]"
__status__ = "Production"
__all__ = [
"APPLICATION_NAME",
"APPLICATION_VERSION",
"PYTHON_PACKAGE_NAME",
"PYPI_PACKAGE_NAME",
"PYPI_ARCHIVE_NAME",
"BIBLIOGRAPHY_NAME",
"clean",
"formatting",
"quality",
"precommit",
"tests",
"examples",
"preflight",
"docs",
"todo",
"requirements",
"build",
"virtualise",
"tag",
"release",
"sha256",
]
APPLICATION_NAME: str = colour_visuals.__application_name__
APPLICATION_VERSION: str = colour_visuals.__version__
PYTHON_PACKAGE_NAME: str = colour_visuals.__name__
PYPI_PACKAGE_NAME: str = "colour-visuals"
PYPI_ARCHIVE_NAME: str = PYPI_PACKAGE_NAME.replace("-", "_")
BIBLIOGRAPHY_NAME: str = "BIBLIOGRAPHY.bib"
@task
def clean(
ctx: Context,
docs: bool = True,
bytecode: bool = False,
pytest: bool = True,
):
"""
Clean the project.
Parameters
----------
ctx
Context.
docs
Whether to clean the *docs* directory.
bytecode
Whether to clean the bytecode files, e.g., *.pyc* files.
pytest
Whether to clean the *Pytest* cache directory.
"""
message_box("Cleaning project...")
patterns = ["build", "*.egg-info", "dist"]
if docs:
patterns.append("docs/_build")
patterns.append("docs/generated")
if bytecode:
patterns.append("**/__pycache__")
patterns.append("**/*.pyc")
if pytest:
patterns.append(".pytest_cache")
for pattern in patterns:
ctx.run(f"rm -rf {pattern}")
@task
def formatting(
ctx: Context,
asciify: bool = True,
):
"""
Convert unicode characters to ASCII and cleanup the *BibTeX* file.
Parameters
----------
ctx
Context.
asciify
Whether to convert unicode characters to ASCII.
"""
if asciify:
message_box("Converting unicode characters to ASCII...")
with ctx.cd("utilities"):
ctx.run("./unicode_to_ascii.py")
@task
def quality(
ctx: Context,
pyright: bool = True,
rstlint: bool = True,
):
"""
Check the codebase with *Pyright* and lints various *restructuredText*
files with *rst-lint*.
Parameters
----------
ctx
Context.
pyright
Whether to check the codebase with *Pyright*.
rstlint
Whether to lint various *restructuredText* files with *rst-lint*.
"""
if pyright:
message_box('Checking codebase with "Pyright"...')
ctx.run("pyright --threads --skipunannotated --level warning")
if rstlint:
message_box('Linting "README.rst" file...')
ctx.run("rst-lint README.rst")
@task
def precommit(ctx: Context):
"""
Run the "pre-commit" hooks on the codebase.
Parameters
----------
ctx
Context.
"""
message_box('Running "pre-commit" hooks on the codebase...')
ctx.run("pre-commit run --all-files")
@task
def tests(ctx: Context):
"""
Run the unit tests with *Pytest*.
Parameters
----------
ctx
Context.
"""
message_box('Running "Pytest"...')
ctx.run(
"pytest "
"--doctest-modules "
f"--ignore={PYTHON_PACKAGE_NAME}/examples "
f"--cov={PYTHON_PACKAGE_NAME} "
f"{PYTHON_PACKAGE_NAME}"
)
@task
def examples(ctx: Context):
"""
Run the examples.
Parameters
----------
ctx
Context.
"""
message_box("Running examples...")
for root, _dirnames, filenames in os.walk(
os.path.join(PYTHON_PACKAGE_NAME, "examples")
):
for filename in fnmatch.filter(filenames, "*.py"):
ctx.run(f"python {os.path.join(root, filename)}")
@task(formatting, quality, precommit, tests, examples)
def preflight(ctx: Context): # noqa: ARG001
"""
Perform the preflight tasks, i.e., *formatting*, *tests*, *quality*, and
*examples*.
Parameters
----------
ctx
Context.
"""
message_box('Finishing "Preflight"...')
@task
def docs(
ctx: Context,
plots: bool = True,
html: bool = True,
pdf: bool = True,
):
"""
Build the documentation.
Parameters
----------
ctx
Context.
plots
Whether to generate the documentation plots.
html
Whether to build the *HTML* documentation.
pdf
Whether to build the *PDF* documentation.
"""
if plots:
with ctx.cd("utilities"):
message_box("Generating plots...")
ctx.run("./generate_plots.py")
with ctx.prefix("export COLOUR_SCIENCE__DOCUMENTATION_BUILD=True"), ctx.cd("docs"):
if html:
message_box('Building "HTML" documentation...')
ctx.run("make html")
if pdf:
message_box('Building "PDF" documentation...')
ctx.run("make latexpdf")
@task
def todo(ctx: Context):
"""
Export the TODO items.
Parameters
----------
ctx
Context.
"""
message_box('Exporting "TODO" items...')
with ctx.cd("utilities"):
ctx.run("./export_todo.py")
@task
def requirements(ctx: Context):
"""
Export the *requirements.txt* file.
Parameters
----------
ctx
Context.
"""
message_box('Exporting "requirements.txt" file...')
ctx.run('uv export --no-hashes --all-extras | grep -v "-e \\." > requirements.txt')
message_box('Exporting "docs/requirements.txt" file...')
ctx.run(
'uv export --no-hashes --all-extras --no-dev | grep -v "-e \\." > '
"docs/requirements.txt"
)
@task(clean, preflight, docs, todo, requirements)
def build(ctx: Context):
"""
Build the project and runs dependency tasks, i.e., *docs*, *todo*, and
*preflight*.
Parameters
----------
ctx
Context.
"""
message_box("Building...")
ctx.run("uv build")
ctx.run("twine check dist/*")
@task
def virtualise(ctx: Context, tests: bool = True):
"""
Create a virtual environment for the project build.
Parameters
----------
ctx
Context.
tests
Whether to run tests on the virtual environment.
"""
unique_name = f"{PYPI_PACKAGE_NAME}-{uuid.uuid1()}"
with ctx.cd("dist"):
ctx.run(f"tar -xvf {PYPI_ARCHIVE_NAME}-{APPLICATION_VERSION}.tar.gz")
ctx.run(f"mv {PYPI_ARCHIVE_NAME}-{APPLICATION_VERSION} {unique_name}")
ctx.run(f"rm -rf {unique_name}/{PYTHON_PACKAGE_NAME}/resources")
ctx.run(
"ln -s ../../../{0}/resources {1}/{0}".format(
PYTHON_PACKAGE_NAME, unique_name
)
)
with ctx.cd(unique_name):
ctx.run("uv sync --all-extras --no-dev")
ctx.run(
'uv run python -c "import imageio;imageio.plugins.freeimage.download()"'
)
if tests:
ctx.run(
"source .venv/bin/activate && "
"uv run pytest "
"--doctest-modules "
f"--ignore={PYTHON_PACKAGE_NAME}/examples "
f"{PYTHON_PACKAGE_NAME}",
env={"MPLBACKEND": "AGG"},
)
@task
def tag(ctx: Context):
"""
Tag the repository according to defined version using *git-flow*.
Parameters
----------
ctx
Context.
"""
message_box("Tagging...")
result = ctx.run("git rev-parse --abbrev-ref HEAD", hide="both")
if result.stdout.strip() != "develop": # pyright: ignore
raise RuntimeError("Are you still on a feature or master branch?")
with open(os.path.join(PYTHON_PACKAGE_NAME, "__init__.py")) as file_handle:
file_content = file_handle.read()
major_version = re.search(
'__major_version__\\s+=\\s+"(.*)"', file_content
).group( # pyright: ignore
1
)
minor_version = re.search(
'__minor_version__\\s+=\\s+"(.*)"', file_content
).group( # pyright: ignore
1
)
change_version = re.search(
'__change_version__\\s+=\\s+"(.*)"', file_content
).group( # pyright: ignore
1
)
version = ".".join((major_version, minor_version, change_version))
result = ctx.run("git ls-remote --tags upstream", hide="both")
remote_tags = result.stdout.strip().split("\n") # pyright: ignore
tags = set()
for remote_tag in remote_tags:
tags.add(remote_tag.split("refs/tags/")[1].replace("refs/tags/", "^{}"))
version_tags = sorted(tags)
if f"v{version}" in version_tags:
raise RuntimeError(
f'A "{PYTHON_PACKAGE_NAME}" "v{version}" tag already exists in '
f"remote repository!"
)
ctx.run(f"git flow release start v{version}")
ctx.run(f"git flow release finish v{version}")
@task(build)
def release(ctx: Context):
"""
Release the project to *Pypi* with *Twine*.
Parameters
----------
ctx
Context.
"""
message_box("Releasing...")
with ctx.cd("dist"):
ctx.run("twine upload *.tar.gz")
ctx.run("twine upload *.whl")
@task
def sha256(ctx: Context):
"""
Compute the project *Pypi* package *sha256* with *OpenSSL*.
Parameters
----------
ctx
Context.
"""
message_box('Computing "sha256"...')
with ctx.cd("dist"):
ctx.run(f"openssl sha256 {PYPI_ARCHIVE_NAME}-*.tar.gz")