forked from ampas/idt-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
225 lines (172 loc) · 4.73 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
"""
Invoke - Tasks
==============
"""
import platform
from invoke import Context, task
from invoke.exceptions import Failure
from colour.hints import Boolean
from colour.utilities import message_box
import app
__author__ = "Alex Forsythe, Gayle McAdams, Thomas Mansencal, Nick Shaw"
__copyright__ = "Copyright 2020 Academy of Motion Picture Arts and Sciences"
__license__ = "Academy of Motion Picture Arts and Sciences License Terms"
__maintainer__ = "Academy of Motion Picture Arts and Sciences"
__email__ = "[email protected]"
__status__ = "Production"
__all__ = [
"APPLICATION_NAME",
"ORG",
"CONTAINER",
"clean",
"precommit",
"requirements",
"docker_build",
"docker_remove",
"docker_run",
"docker_push",
]
APPLICATION_NAME = app.__application_name__
ORG = "ampas"
CONTAINER = APPLICATION_NAME.replace(" ", "").lower()
def _patch_invoke_annotations_support():
"""See https://github.com/pyinvoke/invoke/issues/357."""
import invoke
from unittest.mock import patch
from inspect import getfullargspec, ArgSpec
def patched_inspect_getargspec(function):
spec = getfullargspec(function)
return ArgSpec(*spec[0:4])
org_task_argspec = invoke.tasks.Task.argspec
def patched_task_argspec(*args, **kwargs):
with patch(
target="inspect.getargspec", new=patched_inspect_getargspec
):
return org_task_argspec(*args, **kwargs)
invoke.tasks.Task.argspec = patched_task_argspec
_patch_invoke_annotations_support()
@task
def clean(
ctx: Context,
docs: Boolean = True,
bytecode: Boolean = False,
mypy: Boolean = True,
pytest: Boolean = 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.
mypy
Whether to clean the *Mypy* cache directory.
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 mypy:
patterns.append(".mypy_cache")
if pytest:
patterns.append(".pytest_cache")
for pattern in patterns:
ctx.run(f"rm -rf {pattern}")
@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):
"""
Export the *requirements.txt* file.
Parameters
----------
ctx : invoke.context.Context
Context.
"""
message_box('Exporting "requirements.txt" file...')
ctx.run(
"poetry run pip list --format=freeze | "
'egrep -v "idt-calculator=" '
"> requirements.txt"
)
@task(precommit, requirements)
def docker_build(ctx: Context):
"""
Build the *docker* image.
Parameters
----------
ctx : invoke.context.Context
Context.
"""
message_box('Building "docker" image...')
for archictecture in ("arm64", "amd64"):
ctx.run(
f"docker build --platform=linux/{archictecture} "
f"-t {ORG}/{CONTAINER}:latest "
f"-t {ORG}/{CONTAINER}:latest-{archictecture} "
f"-t {ORG}/{CONTAINER}:v{app.__version__}-{archictecture} ."
)
@task
def docker_remove(ctx: Context):
"""
Stop and remove the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
"""
message_box('Stopping "docker" container...')
try:
ctx.run(f"docker stop {CONTAINER}")
except Failure:
pass
message_box('Removing "docker" container...')
try:
ctx.run(f"docker rm {CONTAINER}")
except Failure:
pass
@task(docker_remove, docker_build)
def docker_run(ctx):
"""
Run the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
"""
message_box('Running "docker" container...')
ctx.run(
"docker run -d "
f"--name={CONTAINER} "
f"-p 8010:8000 {ORG}/{CONTAINER}:latest-{platform.uname()[4].lower()}"
)
@task(clean, precommit, docker_run)
def docker_push(ctx: Context):
"""
Push the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
"""
message_box('Pushing "docker" container...')
ctx.run(f"docker push --all-tags {ORG}/{CONTAINER}")