-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tooling: add tox wrapper that enables mypyc (#246)
- Loading branch information
Showing
2 changed files
with
52 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |