-
Notifications
You must be signed in to change notification settings - Fork 0
/
dodo.py
555 lines (445 loc) · 14.4 KB
/
dodo.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import os
import subprocess
from hashlib import sha256
import doit
from doit.tools import PythonInteractiveAction, config_changed, InteractiveAction
from _scripts import project as P
from _scripts.reporter import GithubActionsReporter
os.environ.update(PYTHONIOENCODING="utf-8", MAMBA_NO_BANNER="1")
DOIT_CONFIG = {
"backend": "sqlite3",
"verbosity": 2,
"par_type": "thread",
"default_tasks": ["binder"],
"reporter": GithubActionsReporter,
}
def task_binder():
"""get to a basic interactive state"""
return dict(actions=[["echo", "ok"]], file_dep=[P.PIP_LISTS["test"]])
def task_release():
"""the full set of tasks needed for a new release"""
return dict(
actions=[["echo", "ok"]],
file_dep=[
P.SHA256SUMS,
P.DOCS_BUILDINFO,
P.OK.pyflakes,
P.OK.robot,
P.OK.prettier,
],
)
def _calc_hash():
lines = []
for p in P.HASH_DEPS:
lines += [" ".join([sha256(p.read_bytes()).hexdigest(), p.name])]
output = "\n".join(lines)
return output
def task_build():
"""build packages"""
env = "meta"
env_lock = P.CONDA_LISTS[env]
run_in = P.RUN_IN[env]
yield dict(
name="pypi",
doc="build the pypi sdist/wheel",
actions=[[*run_in, *P.PY, "setup.py", "sdist", "bdist_wheel"]],
targets=[P.SDIST, P.WHEEL],
file_dep=[*P.PY_SRC, *P.ROBOT_SRC, P.VERSION_FILE, *P.SETUP_CRUFT, env_lock],
)
def _update_hash():
# mimic sha256sum CLI
if P.SHA256SUMS.exists():
P.SHA256SUMS.unlink()
output = _calc_hash()
print(output)
P.SHA256SUMS.write_text(output)
yield dict(
name="hash",
doc="generate a hash file of all distributions",
file_dep=P.HASH_DEPS,
targets=[P.SHA256SUMS],
actions=[_update_hash],
)
def task_conda_build():
"""build conda package"""
def _template():
sums = {
line.split(" ")[1]: line.split(" ")[0]
for line in P.SHA256SUMS.read_text().splitlines()
if line.strip()
}
recipe_text = (
P.META_YAML_IN.read_text()
.replace("REPLACE_THIS_VERSION", P.VERSION)
.replace("REPLACE_THIS_PATH", P.SDIST.as_uri())
.replace("REPLACE_THIS_SHA256", sums[P.SDIST.name])
)
print(recipe_text)
P.META_YAML.write_text(recipe_text)
if P.CI:
# we _don't_ want to force irreproducibly re-building the tarball
file_dep = [P.META_YAML_IN, P.VERSION_FILE]
else:
file_dep = [P.META_YAML_IN, P.SDIST, P.VERSION_FILE, P.SHA256SUMS]
yield dict(
name="recipe",
doc="update the conda recipe",
file_dep=file_dep,
targets=[P.META_YAML],
actions=[_template],
)
yield dict(
name="build",
doc="use boa to build the conda package",
file_dep=[P.META_YAML],
actions=[
[
P.CONDA_EXE,
"mambabuild",
"-c",
"conda-forge",
"--output-folder",
P.CONDA_BLD,
P.RECIPE,
]
],
targets=[P.CONDA_PKG],
)
def task_docs():
"""build HTML docs"""
env = "docs"
run_in = P.RUN_IN[env]
lockfile = P.get_lockfile(env)
frozen = P.PIP_LISTS[env]
clean, touch = P.get_ok_actions(P.RTD_ENV)
def _env_from_lock():
try:
header, tarballs = lockfile.read_text().split("@EXPLICIT")
except:
header = "# NO LOCKFILE"
tarballs = []
header = (
f"# Probably don't edit by hand! \n"
f"#\n"
f"# This was generated from {lockfile.relative_to(P.ROOT)}\n"
f"#\n"
f"# doit docs:rtdenv\n"
f"#\n"
) + header
P.RTD_ENV.write_text(
header
+ P.safe_dump(
dict(
name="rtd",
channels=["conda-forge", "nodefaults"],
dependencies=[
line.strip() for line in tarballs.strip().splitlines()
],
)
)
)
yield dict(
name="rtd:env",
doc="generate a readthedocs-compatible env",
file_dep=[lockfile],
actions=[clean, _env_from_lock],
targets=[P.RTD_ENV],
)
yield dict(
name="sphinx",
doc="build the docs with sphinx",
actions=[
[
*run_in,
"sphinx-build",
"-W",
"-a",
"-b",
"html",
"docs",
"build/docs/html",
]
],
file_dep=[frozen, *P.ALL_DOCS_SRC, *P.SETUP_CRUFT, *P.ROBOT_SRC, P.DODO],
targets=[P.DOCS_BUILDINFO],
)
def _make_env(env):
lockfile = P.get_lockfile(env)
explicit_list = P.BUILD / env / lockfile.name
if not explicit_list.parent.exists():
explicit_list.parent.mkdir(parents=True)
actions = [
lambda: explicit_list.unlink() if explicit_list.exists() else None,
]
if P.CI:
env_args = ["-n", env]
else:
env_args = ["-p", P.ENVS / env]
actions += [
[P.CONDA_EXE, "create", "-y", *env_args, "--file", lockfile],
]
actions += [
lambda: [
explicit_list.write_bytes(
subprocess.check_output(
[P.CONDA_EXE, "list", "--explicit", "--md5", *env_args]
)
),
None,
][-1],
]
yield dict(
name=env,
doc=f"create the local {env} environment",
file_dep=[lockfile],
actions=actions,
targets=[explicit_list],
)
def task_env():
"""prepare envs"""
for env in P.ENV_NAMES:
task = _make_env(env)
if task:
yield task
def task_lint():
"""lint code"""
env = "lint"
env_lock = P.CONDA_LISTS["lint"]
run_in = P.RUN_IN[env]
pym = [*run_in, *P.PYM]
clean, touch = P.get_ok_actions(P.OK.black)
yield dict(
name="black",
doc="ensure python code is well-formatted",
actions=[clean, [*pym, "black", "--quiet", *P.ALL_PY], touch],
file_dep=[*P.ALL_PY, env_lock],
targets=[P.OK.black],
)
clean, touch = P.get_ok_actions(P.OK.pyflakes)
yield dict(
name="pyflakes",
doc="ensure python code is well-behaved",
actions=[clean, [*pym, "pyflakes", *P.ALL_PY], touch],
file_dep=[*P.ALL_PY, env_lock, P.OK.black],
targets=[P.OK.pyflakes],
)
clean, touch = P.get_ok_actions(P.OK.robotidy)
yield dict(
name="robotidy",
doc="ensure robot code is well-formatted",
actions=[clean, [*run_in, *P.ROBOTIDY_ARGS, P.SRC, P.ATEST], touch],
file_dep=[*P.ALL_ROBOT, env_lock],
targets=[P.OK.robotidy],
)
clean, touch = P.get_ok_actions(P.OK.robocop)
yield dict(
name="robocop",
doc="ensure robot code is well-behaved",
actions=[clean, [*run_in, *P.ROBOCOP_ARGS, P.SRC, P.ATEST], touch],
file_dep=[*P.ALL_ROBOT, env_lock, P.OK.robotidy],
targets=[P.OK.robocop],
)
clean, touch = P.get_ok_actions(P.OK.prettier)
yield dict(
name="prettier",
doc="ensure markdown, YAML, JSON, etc. are well-formatted",
actions=[clean, [*run_in, "yarn", "--silent", "prettier"], touch],
file_dep=[*P.ALL_PRETTIER, P.YARN_INTEGRITY],
targets=[P.OK.prettier],
)
def task_js():
"""javascript cruft"""
env = "lint"
run_in = P.RUN_IN[env]
env_lock = P.CONDA_LISTS[env]
yield dict(
name="yarn",
doc="install nodejs dev dependencies",
uptodate=[
config_changed({k: P.PACKAGE[k] for k in ["devDependencies", "prettier"]})
],
file_dep=[P.YARN_LOCK, env_lock],
actions=[
[*run_in, "yarn", "--silent", "--prefer-offline", "--ignore-optional"],
],
targets=[P.YARN_INTEGRITY],
)
def task_lab():
"""start a jupyter lab server (with all other extensions)"""
env = "test"
lockfile = P.get_lockfile(env)
str_lock = str(lockfile)
needs_build = "lab1" in str_lock or "lab2" in str_lock
frozen = P.PIP_LISTS[env]
run_in = P.RUN_IN[env]
pym = [*run_in, *P.PYM]
app_dir = []
if needs_build and not P.IN_BINDER:
app_dir = ["--app-dir", P.APP_DIR]
lab = [*pym, "jupyter", "lab"]
lab_ext = [*pym, "jupyter", "labextension"]
serve_deps = [frozen]
if needs_build:
yield dict(
name="ext",
uptodate=[config_changed({"labextensions": P.LAB_EXTENSIONS})],
actions=[
[*lab_ext, "install", *app_dir, *P.LAB_EXTENSIONS, "--no-build"],
[*lab, "build", *app_dir, "--debug"],
],
file_dep=[frozen],
targets=[P.APP_INDEX],
)
serve_deps += [P.APP_INDEX]
def _lab():
p = subprocess.Popen(
[*lab, *app_dir, "--no-browser", "--debug"], stdin=subprocess.PIPE
)
try:
p.wait()
except KeyboardInterrupt:
p.terminate()
p.communicate(b"y\n")
p.terminate()
finally:
p.wait()
print("maybe check your process log")
yield dict(
name="serve",
doc="runs lab (never stops)",
uptodate=[lambda: False],
actions=[PythonInteractiveAction(_lab)],
file_dep=serve_deps,
)
def _make_setup(env):
frozen = P.PIP_LISTS[env]
run_in = P.RUN_IN[env]
pym = [*run_in, *P.PYM]
file_dep = [P.CONDA_LISTS[env], *P.SETUP_CRUFT]
if P.INSTALL_ARTIFACT:
pip_args = [
"--no-index",
"--find-links",
P.DIST,
P.SETUP["metadata"]["name"],
]
doc = f"[{env}] install from dist"
else:
pip_args = ["-e", "."]
doc = f"[{env}] python development install"
yield dict(
name=env,
doc=doc,
actions=[
lambda: frozen.unlink() if frozen.exists() else None,
[*pym, "pip", "install", "--no-deps", "--ignore-installed", *pip_args],
[*pym, "pip", "check"],
lambda: [
frozen.write_bytes(subprocess.check_output([*pym, "pip", "freeze"])),
None,
][-1],
],
targets=[frozen],
file_dep=file_dep,
)
def task_setup():
"""do an editable install of the package"""
for env in P.ENV_NAMES:
if env == "meta":
continue
yield _make_setup(env)
def task_test():
"""(dry)run tests"""
env = "test"
pym = [*P.RUN_IN[env], *P.PYM]
dry_run_stem = P.get_atest_stem(
extra_args=["--dryrun"], lockfile=P.get_lockfile(env), browser=P.BROWSER
)
real_stem = P.get_atest_stem(lockfile=P.get_lockfile(env), browser=P.BROWSER)
dry_target = P.ATEST_OUT / dry_run_stem / P.ATEST_OUT_XML
real_target = P.ATEST_OUT / real_stem / P.ATEST_OUT_XML
clean, touch = P.get_ok_actions(P.OK.robot_dry_run)
robot_deps = [*P.PY_SRC, *P.ALL_ROBOT, P.PIP_LISTS[env], P.SCRIPTS / "atest.py"]
yield dict(
name="dryrun",
doc="pass the tests through the robot machinery, but don't actually _run_ anything",
uptodate=[config_changed(os.environ.get("ATEST_ARGS", ""))],
actions=[clean, [*pym, "_scripts.atest", "--dryrun"], touch],
file_dep=robot_deps,
targets=[dry_target, P.OK.robot_dry_run],
)
clean, touch = P.get_ok_actions(P.OK.robot)
yield dict(
name="atest",
doc="run acceptance tests with robot",
uptodate=[config_changed(os.environ.get("ATEST_ARGS", ""))],
actions=[clean, [*pym, "_scripts.atest"], touch],
file_dep=[P.OK.robot_dry_run, *robot_deps],
targets=[real_target, P.OK.robot],
)
# Presently not running this on CI
yield dict(
name="combine",
doc="combine all robot outputs into a single HTML report",
actions=[[*pym, "_scripts.combine"]],
file_dep=[
real_target,
*P.ATEST_OUT.rglob(P.ATEST_OUT_XML),
P.SCRIPTS / "combine.py",
],
)
def _make_lock_task_name(key):
return "__".join([p for p in key if p]).replace(".", "_")
def _make_lock_task(key, target):
(flow, pf, py, lab) = key
ft = [k for k in [py, lab] if k]
if ft:
ft = f"(ft. {', '.join(ft)})"
task = dict(
name=_make_lock_task_name(key),
doc=f"lock the {flow} environment for {pf} {ft}".strip(),
actions=[[*P.SCRIPT_LOCK, target]],
file_dep=[*P.ENV_DEPS[key]],
targets=[target],
)
if P.THIS_META_ENV_LOCK != target:
task["actions"][0] = [*P.RUN_IN["meta"], *task["actions"][0]]
task["task_dep"] = ["env:meta"]
return task
# at some point, we'll want a scheduled excursion just for locking
if not (P.CI or P.IN_BINDER):
def task_lock():
"""generate conda lock files for all the excursions"""
meta_lock_exists = P.THIS_META_ENV_LOCK.exists()
can_bootstrap = P.CAN_CONDA_LOCK
if not (meta_lock_exists or can_bootstrap):
raise RuntimeError(
f"{P.THIS_META_ENV_LOCK} is missing: this, or `conda-lock` on path"
" is needed to bootstrap the lock environment"
)
for key, target in P.ENVENTURES.items():
if not meta_lock_exists and target != P.THIS_META_ENV_LOCK:
continue
yield _make_lock_task(key, target)
def task_publish():
"""publish distributioons"""
def _check_hash():
on_disk = P.SHA256SUMS.read_text()
calculated = _calc_hash()
print("\n--\n".join(["on-disk:", on_disk, "calculated", calculated]))
if calculated != on_disk:
raise RuntimeError("SHA256SUMS do not match:")
print("SHA256SUMS are OK")
yield dict(
name="pypi",
doc="upload python sdist and wheel to PyPI",
actions=[
_check_hash,
InteractiveAction(
[*P.RUN_IN["meta"], "twine", "upload", P.SDIST, P.WHEEL],
shell=False,
),
],
)
if __name__ == "__main__":
doit.run(globals())