forked from google/bumble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
228 lines (178 loc) · 7.03 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
# Copyright 2021-2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Invoke tasks
"""
# -----------------------------------------------------------------------------
# Imports
# -----------------------------------------------------------------------------
import os
import glob
import shutil
import urllib
from pathlib import Path
from invoke import task, call, Collection
from invoke.exceptions import Exit, UnexpectedExit
# -----------------------------------------------------------------------------
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
ns = Collection()
# -----------------------------------------------------------------------------
# Build
# -----------------------------------------------------------------------------
build_tasks = Collection()
ns.add_collection(build_tasks, name="build")
# -----------------------------------------------------------------------------
@task
def build(ctx, install=False):
if install:
ctx.run('python -m pip install .[build]')
ctx.run("python -m build")
# -----------------------------------------------------------------------------
@task
def release_build(ctx):
build(ctx, install=True)
# -----------------------------------------------------------------------------
@task
def mkdocs(ctx):
ctx.run("mkdocs build -f docs/mkdocs/mkdocs.yml")
# -----------------------------------------------------------------------------
build_tasks.add_task(build, default=True)
build_tasks.add_task(release_build, name="release")
build_tasks.add_task(mkdocs, name="mkdocs")
# -----------------------------------------------------------------------------
# Test
# -----------------------------------------------------------------------------
test_tasks = Collection()
ns.add_collection(test_tasks, name="test")
# -----------------------------------------------------------------------------
@task(incrementable=["verbose"])
def test(ctx, match=None, junit=False, install=False, html=False, verbose=0):
# Install the package before running the tests
if install:
ctx.run("python -m pip install .[test]")
args = ""
if junit:
args += "--junit-xml test-results.xml"
if match is not None:
args += f" -k '{match}'"
if html:
args += " --html results.html"
if verbose > 0:
args += f" -{'v' * verbose}"
ctx.run(f"python -m pytest {os.path.join(ROOT_DIR, 'tests')} {args}")
# -----------------------------------------------------------------------------
@task
def release_test(ctx):
test(ctx, install=True)
# -----------------------------------------------------------------------------
test_tasks.add_task(test, default=True)
test_tasks.add_task(release_test, name="release")
# -----------------------------------------------------------------------------
# Project
# -----------------------------------------------------------------------------
project_tasks = Collection()
ns.add_collection(project_tasks, name="project")
# -----------------------------------------------------------------------------
@task
def lint(ctx, disable='C,R', errors_only=False):
options = []
if disable:
options.append(f"--disable={disable}")
if errors_only:
options.append("-E")
if errors_only:
qualifier = ' (errors only)'
else:
qualifier = f' (disabled: {disable})' if disable else ''
print(f">>> Running the linter{qualifier}...")
try:
ctx.run(f"pylint {' '.join(options)} bumble apps examples tasks.py")
print("The linter is happy. ✅ 😊 🐝")
except UnexpectedExit as exc:
print("Please check your code against the linter messages. ❌")
raise Exit(code=1) from exc
# -----------------------------------------------------------------------------
@task
def format_code(ctx, check=False, diff=False):
options = []
if check:
options.append("--check")
if diff:
options.append("--diff")
print(">>> Running the formatter...")
try:
ctx.run(f"black -S {' '.join(options)} .")
except UnexpectedExit as exc:
print("Please run 'invoke project.format' or 'black .' to format the code. ❌")
raise Exit(code=1) from exc
# -----------------------------------------------------------------------------
@task
def check_types(ctx):
checklist = ["apps", "bumble", "examples", "tests", "tasks.py"]
try:
ctx.run(f"mypy {' '.join(checklist)}")
except UnexpectedExit as exc:
print("Please check your code against the mypy messages.")
raise Exit(code=1) from exc
# -----------------------------------------------------------------------------
@task(
pre=[
call(format_code, check=True),
call(lint, errors_only=True),
call(check_types),
test,
]
)
def pre_commit(_ctx):
print("All good!")
# -----------------------------------------------------------------------------
project_tasks.add_task(lint)
project_tasks.add_task(format_code, name="format")
project_tasks.add_task(check_types, name="check-types")
project_tasks.add_task(pre_commit)
# -----------------------------------------------------------------------------
# Web
# -----------------------------------------------------------------------------
web_tasks = Collection()
ns.add_collection(web_tasks, name="web")
# -----------------------------------------------------------------------------
@task
def serve(ctx, port=8000):
"""
Run a simple HTTP server for the examples under the `web` directory.
"""
import http.server
address = ("", port)
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, directory="web", **kwargs)
server = http.server.HTTPServer(address, Handler)
print(f"Now serving on port {port} 🕸️")
server.serve_forever()
# -----------------------------------------------------------------------------
@task
def web_build(ctx):
# Step 1: build the wheel
build(ctx)
# Step 2: Copy the wheel to the web folder, so the http server can access it
newest_wheel = Path(max(glob.glob('dist/*.whl'), key=lambda f: os.path.getmtime(f)))
shutil.copy(newest_wheel, Path('web/'))
# Step 3: Write wheel's name to web/packageFile
with open(Path('web', 'packageFile'), mode='w') as package_file:
package_file.write(str(Path('/') / newest_wheel.name))
# Step 4: Success!
print('Include ?packageFile=true in your URL!')
# -----------------------------------------------------------------------------
web_tasks.add_task(serve)
web_tasks.add_task(web_build, name="build")