-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
tasks.py
458 lines (359 loc) · 13 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
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
from __future__ import annotations
import json
import logging
import os
import re
import sys
from dataclasses import dataclass
from pathlib import Path
from shutil import rmtree
from typing import TYPE_CHECKING, Any, Callable
import semver
import toml
from invoke import task
from invoke.context import Context
from invoke.exceptions import Exit
from invoke.runners import Result
# --- Typing Preamble ------------------------------------------------------------------
if TYPE_CHECKING:
# not available in typing module until Python 3.8
# not available in typing module until Python 3.10
from typing import Literal, Protocol, TypeAlias
class ReleasePrepFunc(Protocol):
def __call__(
self, context: Context, package: PackageInfo
) -> Callable[[bool], None]: ...
LanguageName: TypeAlias = "Literal['py', 'js']"
# --- Constants ------------------------------------------------------------------------
log = logging.getLogger(__name__)
log.setLevel("INFO")
log_handler = logging.StreamHandler(sys.stdout)
log_handler.setFormatter(logging.Formatter("%(message)s"))
log.addHandler(log_handler)
# --- Constants ------------------------------------------------------------------------
ROOT = Path(__file__).parent
DOCS_DIR = ROOT / "docs"
SRC_DIR = ROOT / "src"
JS_DIR = SRC_DIR / "js"
PY_DIR = SRC_DIR / "py"
PY_PROJECTS = [p for p in PY_DIR.iterdir() if (p / "pyproject.toml").exists()]
TAG_PATTERN = re.compile(
# start
r"^"
# package name
r"(?P<name>[0-9a-zA-Z-@/]+)-"
# package version
r"v(?P<version>[0-9][0-9a-zA-Z-\.\+]*)"
# end
r"$"
)
# --- Tasks ----------------------------------------------------------------------------
@task
def env(context: Context):
"""Install development environment"""
env_py(context)
env_js(context)
@task
def env_py(context: Context):
"""Install Python development environment"""
for py_proj in [
DOCS_DIR,
# Docs installs non-editable versions of packages - ensure
# we overwrite that by installing projects afterwards.
*PY_PROJECTS,
]:
py_proj_toml_tools = toml.load(py_proj / "pyproject.toml")["tool"]
if "hatch" in py_proj_toml_tools:
install_func = install_hatch_project
elif "poetry" in py_proj_toml_tools:
install_func = install_poetry_project
else:
raise Exit(f"Unknown project type: {py_proj}")
with context.cd(py_proj):
install_func(context, py_proj)
@task
def env_js(context: Context):
"""Install JS development environment"""
in_js(
context,
"npm ci",
"npm run build",
hide="out",
)
@task
def lint_py(context: Context, fix: bool = False):
"""Run linters and type checkers"""
if fix:
context.run("ruff --fix .")
context.run("black .")
else:
context.run("ruff .")
context.run("black --check --diff .")
in_py(
context,
f"flake8 --toml-config '{ROOT / 'pyproject.toml'}' .",
"hatch run lint:all",
)
@task(pre=[env_js])
def lint_js(context: Context, fix: bool = False):
"""Run linters and type checkers"""
if fix:
in_js(context, "npm run fix:format")
else:
in_js(context, "npm run check:format")
in_js(context, "npm run check:types")
@task
def test_py(context: Context, no_cov: bool = False):
"""Run test suites"""
in_py(
context,
f"hatch run {'test' if no_cov else 'cov'} --maxfail=3 --reruns=3",
)
@task(pre=[env_js])
def test_js(context: Context):
"""Run test suites"""
in_js(context, "npm run check:tests")
@task(pre=[env_py])
def test_docs(context: Context):
with context.cd(DOCS_DIR):
context.run("poetry install")
context.run(
"poetry run sphinx-build "
"-a " # re-write all output files
"-T " # show full tracebacks
"-W " # turn warnings into errors
"--keep-going " # complete the build, but still report warnings as errors
"-b doctest "
"source "
"build",
)
context.run("poetry run sphinx-build -b doctest source build")
context.run("docker build . --file ./docs/Dockerfile")
@task
def docs(context: Context, docker: bool = False):
"""Build documentation"""
if docker:
_docker_docs(context)
else:
_live_docs(context)
def _docker_docs(context: Context) -> None:
context.run("docker build . --file ./docs/Dockerfile --tag reactpy-docs:latest")
context.run(
"docker run -it -p 5000:5000 -e DEBUG=1 --rm reactpy-docs:latest", pty=True
)
def _live_docs(context: Context) -> None:
with context.cd(DOCS_DIR):
context.run("poetry install")
context.run(
"poetry run python main.py "
"--open-browser "
# watch python source too
"--watch=../src/py "
# for some reason this matches absolute paths
"--ignore=**/_auto/* "
"--ignore=**/_static/custom.js "
"--ignore=**/node_modules/* "
"--ignore=**/package-lock.json "
"-a "
"-E "
"-b "
"html "
"source "
"build"
)
@task
def publish(context: Context, dry_run: str = ""):
"""Publish packages that have been tagged for release in the current commit
To perform a test run use `--dry-run=<name>-v<version>` to specify a comma-separated
list of tags to simulate a release of. For example, to simulate a release of
`@foo/bar-v1.2.3` and `baz-v4.5.6` use `--dry-run=@foo/bar-v1.2.3,baz-v4.5.6`.
"""
packages = get_packages(context)
release_prep: dict[LanguageName, ReleasePrepFunc] = {
"js": prepare_js_release,
"py": prepare_py_release,
}
current_tags = dry_run.split(",") if dry_run else get_current_tags(context)
parsed_tags = [parse_tag(tag) for tag in current_tags]
publishers: list[Callable[[bool], None]] = []
for tag_info in parsed_tags:
if tag_info.name not in packages:
msg = f"Tag {tag_info.tag} references package {tag_info.name} that does not exist"
raise Exit(msg)
pkg_info = packages[tag_info.name]
if pkg_info.version != tag_info.version:
msg = f"Tag {tag_info.tag} references version {tag_info.version} of package {tag_info.name}, but the current version is {pkg_info.version}"
raise Exit(msg)
log.info(f"Preparing {tag_info.name} for release...")
publishers.append(release_prep[pkg_info.language](context, pkg_info))
for publish in publishers:
publish(bool(dry_run))
# --- Utilities ------------------------------------------------------------------------
def in_py(context: Context, *commands: str, **kwargs: Any) -> None:
for p in PY_PROJECTS:
with context.cd(p):
log.info(f"Running commands in {p}...")
for c in commands:
context.run(c, **kwargs)
def in_js(context: Context, *commands: str, **kwargs: Any) -> None:
with context.cd(JS_DIR):
for c in commands:
context.run(c, **kwargs)
def get_packages(context: Context) -> dict[str, PackageInfo]:
packages: list[PackageInfo] = []
for maybe_pkg in PY_DIR.glob("*"):
if (maybe_pkg / "pyproject.toml").exists():
packages.append(make_py_pkg_info(context, maybe_pkg))
else:
msg = f"unexpected dir or file: {maybe_pkg}"
raise Exit(msg)
packages_dir = JS_DIR / "packages"
for maybe_pkg in packages_dir.glob("*"):
if (maybe_pkg / "package.json").exists():
packages.append(make_js_pkg_info(maybe_pkg))
elif maybe_pkg.is_dir():
for maybe_ns_pkg in maybe_pkg.glob("*"):
if (maybe_ns_pkg / "package.json").exists():
packages.append(make_js_pkg_info(maybe_ns_pkg))
else:
msg = f"unexpected dir or file: {maybe_pkg}"
raise Exit(msg)
packages_by_name = {p.name: p for p in packages}
if len(packages_by_name) != len(packages):
raise Exit("duplicate package names detected")
return packages_by_name
def make_py_pkg_info(context: Context, pkg_dir: Path) -> PackageInfo:
with context.cd(pkg_dir):
proj_metadata = json.loads(
ensure_result(context, "hatch project metadata").stdout
)
return PackageInfo(
name=proj_metadata["name"],
path=pkg_dir,
language="py",
version=proj_metadata["version"],
)
def make_js_pkg_info(pkg_dir: Path) -> PackageInfo:
with (pkg_dir / "package.json").open() as f:
pkg_json = json.load(f)
return PackageInfo(
name=pkg_json["name"],
path=pkg_dir,
language="js",
version=pkg_json["version"],
)
@dataclass
class PackageInfo:
name: str
path: Path
language: LanguageName
version: str
def get_current_tags(context: Context) -> set[str]:
"""Get tags for the current commit"""
# check if unstaged changes
try:
context.run("git diff --cached --exit-code", hide=True)
context.run("git diff --exit-code", hide=True)
except Exception:
log.error("Cannot get current tags - there are uncommitted changes")
return set()
# get tags for current commit
tags = {
line
for line in map(
str.strip,
ensure_result(
context, "git tag --points-at HEAD", hide=True
).stdout.splitlines(),
)
if line
}
if not tags:
log.error("No tags found for current commit")
for t in tags:
if not TAG_PATTERN.match(t):
msg = f"Invalid tag: {t}"
raise Exit(msg)
log.info(f"Found tags: {tags}")
return tags
def parse_tag(tag: str) -> TagInfo:
match = TAG_PATTERN.match(tag)
if not match:
msg = f"Invalid tag: {tag}"
raise Exit(msg)
version = match.group("version")
if not semver.VersionInfo.isvalid(version):
raise Exit(f"Invalid version: {version} in tag {tag}")
return TagInfo(tag=tag, name=match.group("name"), version=match.group("version"))
@dataclass
class TagInfo:
tag: str
name: str
version: str
def prepare_js_release(
context: Context, package: PackageInfo
) -> Callable[[bool], None]:
node_auth_token = os.getenv("NODE_AUTH_TOKEN")
if node_auth_token is None:
msg = "NODE_AUTH_TOKEN environment variable must be set"
raise Exit(msg)
with context.cd(JS_DIR):
context.run("npm ci")
context.run("npm run build")
def publish(dry_run: bool) -> None:
with context.cd(JS_DIR):
if dry_run:
context.run(f"npm --workspace {package.name} pack --dry-run")
return
context.run(
f"npm --workspace {package.name} publish --access public",
env={"NODE_AUTH_TOKEN": node_auth_token},
)
return publish
def prepare_py_release(
context: Context, package: PackageInfo
) -> Callable[[bool], None]:
twine_username = os.getenv("PYPI_USERNAME")
twine_password = os.getenv("PYPI_PASSWORD")
if not (twine_password and twine_username):
msg = "PYPI_USERNAME and PYPI_PASSWORD environment variables must be set"
raise Exit(msg)
for build_dir_name in ["build", "dist"]:
build_dir_path = Path.cwd() / build_dir_name
if build_dir_path.exists():
rmtree(str(build_dir_path))
with context.cd(package.path):
context.run("hatch build")
def publish(dry_run: bool):
with context.cd(package.path):
context.run("twine check dist/*")
if dry_run:
return
context.run(
"twine upload dist/*",
env={
"TWINE_USERNAME": twine_username,
"TWINE_PASSWORD": twine_password,
},
)
return publish
def install_hatch_project(context: Context, path: Path) -> None:
py_proj_toml = toml.load(path / "pyproject.toml")
hatch_default_env = py_proj_toml["tool"]["hatch"]["envs"].get("default", {})
hatch_default_features = hatch_default_env.get("features", [])
hatch_default_deps = hatch_default_env.get("dependencies", [])
context.run(f"pip install -e '.[{','.join(hatch_default_features)}]'")
context.run(f"pip install {' '.join(map(repr, hatch_default_deps))}")
def install_poetry_project(context: Context, path: Path) -> None:
# install dependencies from poetry into the current environment - not in Poetry's venv
poetry_lock = toml.load(path / "poetry.lock")
packages_to_install = [
f"{package['name']}=={package['version']}" for package in poetry_lock["package"]
]
context.run("pip install -e .")
context.run(f"pip install {' '.join(packages_to_install)}")
def ensure_result(context: Context, *args: Any, **kwargs: Any) -> Result:
result = context.run(*args, **kwargs)
if result is None:
raise Exit("Command failed")
return result