Skip to content

Commit

Permalink
tooling: add tox wrapper that enables mypyc (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin authored Nov 29, 2024
1 parent 5723018 commit 42a570d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
34 changes: 34 additions & 0 deletions scripts/mypyc_tox
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
"""A tox wrapper to make it build Tomli with setuptools + mypyc.
Run this instead of tox. For example: `./scripts/mypyc_tox -e benchmark`
All arguments are passed to tox.
Prior to running tox, this edits the [build-system] table in pyproject.toml
to support mypyc builds. The change is reverted before exiting. This also
sets the `TOMLI_USE_MYPYC=1` environment variable that setup.py reads to
enable mypyc.
"""
import os
from pathlib import Path
import shutil
import subprocess
import sys

from use_setuptools import use_setuptools

project_root = Path(__file__).parent.parent
pyproject_path = project_root / "pyproject.toml"
backup_path = project_root / ".backup.pyproject.toml"

tox_env_vars = os.environ.copy()
tox_env_vars["TOMLI_USE_MYPYC"] = "1"

shutil.copy2(pyproject_path, backup_path)
try:
use_setuptools()
result = subprocess.run(["tox", *sys.argv[1:]], env=tox_env_vars)
raise SystemExit(result.returncode)
finally:
shutil.copy2(backup_path, pyproject_path)
backup_path.unlink()
25 changes: 18 additions & 7 deletions scripts/use_setuptools.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
"""Script that switches build backend to setuptools with mypyc support.
Overwrites pyproject.toml. Does not create a backup.
"""

from pathlib import Path
import tomllib

import tomli_w # type: ignore[import-not-found]

pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
data = tomllib.loads(pyproject_path.read_bytes().decode())
data["build-system"] = {
"requires": ["setuptools>=69", "mypy[mypyc]>=1.13"],
"build-backend": "setuptools.build_meta",
}
pyproject_path.write_bytes(tomli_w.dumps(data).encode())

def use_setuptools() -> None:
pyproject_path = Path(__file__).parent.parent / "pyproject.toml"
data = tomllib.loads(pyproject_path.read_bytes().decode())
data["build-system"] = {
"requires": ["setuptools>=69", "mypy[mypyc]>=1.13"],
"build-backend": "setuptools.build_meta",
}
pyproject_path.write_bytes(tomli_w.dumps(data).encode())


if __name__ == "__main__":
use_setuptools()

0 comments on commit 42a570d

Please sign in to comment.