This repository has been archived by the owner on Nov 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtasks.py
254 lines (194 loc) · 4.84 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
# -*- coding: utf-8 -*-
"""
Invoke - Tasks
==============
"""
import fileinput
import re
import sys
from invoke import task
from invoke.exceptions import Failure
from colour.utilities import message_box
import app
__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2018-2021 - Colour Developers'
__license__ = 'New BSD License - https://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = '[email protected]'
__status__ = 'Production'
__all__ = [
'APPLICATION_NAME', 'ORG', 'CONTAINER', 'clean', 'quality', 'formatting',
'npm_build', 'requirements', 'docker_build', 'docker_remove', 'docker_run'
]
APPLICATION_NAME = app.__application_name__
ORG = 'colourscience'
CONTAINER = APPLICATION_NAME.replace(' ', '').lower()
@task
def clean(ctx, bytecode=False):
"""
Cleans the project.
Parameters
----------
bytecode : bool, optional
Whether to clean the bytecode files, e.g. *.pyc* files.
Returns
-------
bool
Task success.
"""
message_box('Cleaning project...')
patterns = []
if bytecode:
patterns.append('**/*.pyc')
for pattern in patterns:
ctx.run("rm -rf {}".format(pattern))
@task
def quality(ctx, flake8=True):
"""
Checks the codebase with *Flake8*.
Parameters
----------
ctx : invoke.context.Context
Context.
flake8 : bool, optional
Whether to check the codebase with *Flake8*.
Returns
-------
bool
Task success.
"""
if flake8:
message_box('Checking codebase with "Flake8"...')
ctx.run('flake8')
@task
def formatting(ctx, yapf=True):
"""
Formats the codebase with *Yapf*.
Parameters
----------
ctx : invoke.context.Context
Context.
yapf : bool, optional
Whether to format the codebase with *Yapf*.
Returns
-------
bool
Task success.
"""
if yapf:
message_box('Formatting codebase with "Yapf"...')
ctx.run('yapf -p -i -r .')
@task
def npm_build(ctx):
"""
Builds the *npm* package.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Building "npm" package...')
for package_file in ('package.json', 'package-lock.json'):
version_found = False
for line in fileinput.input(package_file, inplace=True):
if re.match('\\s*"version": "[\\w.]+",',
line) and not version_found:
line = ' "version": "{0}",\n'.format(app.__version__)
version_found = True
sys.stdout.write(line)
ctx.run('npm run build')
@task
def requirements(ctx):
"""
Exports the *requirements.txt* file.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Exporting "requirements.txt" file...')
ctx.run('poetry run pip freeze | '
'egrep -v '
'"github.com/colour-science/colour-analysis-three.js|enum34" '
'> requirements.txt')
@task(npm_build, requirements)
def docker_build(ctx):
"""
Builds the *docker* image.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Building "docker" image...')
ctx.run('docker build '
'--build-arg CACHE_DATE="$(date)" '
'-t {0}/{1}:latest -t {0}/{1}:v{2} .'.format(
ORG, CONTAINER, app.__version__))
@task
def docker_remove(ctx):
"""
Stops and remove the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Stopping "docker" container...')
try:
ctx.run('docker stop {0}'.format(CONTAINER))
except Failure:
pass
message_box('Removing "docker" container...')
try:
ctx.run('docker rm {0}'.format(CONTAINER))
except Failure:
pass
@task(docker_remove, docker_build)
def docker_run(ctx):
"""
Runs the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Running "docker" container...')
ctx.run('docker run -d '
'--name={1} '
'-p 8020:5000 {0}/{1}'.format(ORG, CONTAINER))
@task
def docker_push(ctx):
"""
Pushes the *docker* container.
Parameters
----------
ctx : invoke.context.Context
Context.
Returns
-------
bool
Task success.
"""
message_box('Pushing "docker" container...')
ctx.run('docker push {0}/{1}'.format(ORG, CONTAINER))