-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
209 lines (161 loc) · 4.3 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
"""
Invoke - Tasks
==============
"""
import contextlib
import platform
from invoke.exceptions import Failure
from colour.utilities import message_box
import app
import inspect
if not hasattr(inspect, "getargspec"):
inspect.getargspec = inspect.getfullargspec # pyright: ignore
from invoke.tasks import task
from invoke.context import Context
__author__ = "Colour Developers"
__copyright__ = "Copyright 2018 Colour Developers"
__license__ = "BSD-3-Clause - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "colour-developers@colour-science.org"
__status__ = "Production"
__all__ = [
"APPLICATION_NAME",
"ORG",
"CONTAINER",
"clean",
"quality",
"precommit",
"requirements",
"docker_build",
"docker_remove",
"docker_run",
"docker_push",
]
APPLICATION_NAME: str = app.__application_name__
ORG: str = "colourscience"
CONTAINER: str = APPLICATION_NAME.replace(" ", "").lower()
@task
def clean(ctx: Context, bytecode: bool = False):
"""
Clean the project.
Parameters
----------
bytecode : bool, optional
Whether to clean the bytecode files, e.g. *.pyc* files.
"""
message_box("Cleaning project...")
patterns = []
if bytecode:
patterns.append("**/__pycache__")
patterns.append("**/*.pyc")
for pattern in patterns:
ctx.run(f"rm -rf {pattern}")
@task
def quality(
ctx: Context,
pyright: bool = True,
):
"""
Check the codebase with *Pyright*.
Parameters
----------
ctx
Context.
pyright
Whether to check the codebase with *Pyright*.
"""
if pyright:
message_box('Checking codebase with "Pyright"...')
ctx.run("pyright --skipunannotated --level warning")
@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 requirements(ctx: Context):
"""
Export the *requirements.txt* file.
Parameters
----------
ctx
Context.
"""
message_box('Exporting "requirements.txt" file...')
ctx.run(
"poetry export -f requirements.txt "
"--without-hashes "
"--with dev "
"--output requirements.txt"
)
@task(requirements)
def docker_build(ctx: Context):
"""
Build the *docker* image.
Parameters
----------
ctx
Context.
"""
message_box('Building "docker" image...')
for architecture in ("arm64", "amd64"):
ctx.run(
f"docker build --platform=linux/{architecture} "
f"-t {ORG}/{CONTAINER}:latest "
f"-t {ORG}/{CONTAINER}:latest-{architecture} "
f"-t {ORG}/{CONTAINER}:v{app.__version__}-{architecture} ."
)
@task
def docker_remove(ctx: Context):
"""
Stop and remove the *docker* container.
Parameters
----------
ctx
Context.
"""
message_box('Stopping "docker" container...')
with contextlib.suppress(Failure):
ctx.run(f"docker stop {CONTAINER}")
message_box('Removing "docker" container...')
with contextlib.suppress(Failure):
ctx.run(f"docker rm {CONTAINER}")
@task(docker_remove, docker_build)
def docker_run(ctx: Context):
"""
Run the *docker* container.
Parameters
----------
ctx
Context.
"""
message_box('Running "docker" container...')
ctx.run(
"docker run -d "
f"--name={CONTAINER} "
"-e COLOUR_DASH_SERVER=https://www.colour-science.org:8010/ "
"-e COLOUR_DASH_CSS="
"https://www.colour-science.org/assets/css/all-nocdn.css "
"-e COLOUR_DASH_JS="
"https://www.colour-science.org/assets/js/analytics.js,"
"https://cdnjs.cloudflare.com/ajax/libs/iframe-resizer/3.6.1/"
"iframeResizer.contentWindow.min.js "
f"-p 8010:8000 {ORG}/{CONTAINER}:latest-{platform.uname()[4].lower()}"
)
@task(clean, quality, precommit, docker_run)
def docker_push(ctx: Context):
"""
Push the *docker* container.
Parameters
----------
ctx
Context.
"""
message_box('Pushing "docker" container...')
ctx.run(f"docker push --all-tags {ORG}/{CONTAINER}")