-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
137e8aa
commit 9bb5755
Showing
4 changed files
with
91 additions
and
49 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,88 @@ | ||
"""Make local dev configs.""" | ||
|
||
from collections.abc import Iterable, Mapping, Sequence | ||
from datetime import date, time | ||
from json import dumps | ||
from pathlib import Path | ||
from re import sub | ||
from shlex import join, split | ||
from tomllib import loads | ||
from typing import TypeAlias | ||
|
||
PYPROJECT = Path("pyproject.toml") | ||
"""Input project configuration file.""" | ||
PYRIGHTCONFIG = Path("pyrightconfig.json") | ||
"""Resulting pyright configuration file.""" | ||
PYTEST = Path("pytest.ini") | ||
"""Resulting pytest configuration file.""" | ||
|
||
Leaf: TypeAlias = int | float | bool | date | time | str | ||
"""Leaf node.""" | ||
Node: TypeAlias = Leaf | Sequence["Node"] | Mapping[str, "Node"] | ||
"""General nde.""" | ||
|
||
|
||
def main(): | ||
"""Generate modified local dev configs to shadow `pyproject.toml`. | ||
Duplicate pyright and pytest configuration from `pyproject.toml` to | ||
`pyrightconfig.json` and `pytest.ini`, respectively. These files shadow the | ||
configuration in `pyproject.toml`, which drives CI or if shadow configs are not | ||
present. Shadow configs are in `.gitignore` to facilitate local-only shadowing. | ||
Local pyright configuration includes the editable local `boilercore` dependency to | ||
facilitate refactoring and runing on the latest uncommitted code of that dependency. | ||
Concurrent test runs are disabled in the local pytest configuration which slows down | ||
the usual local, granular test workflow. | ||
""" | ||
config = loads(PYPROJECT.read_text("utf-8")) | ||
# Write pyrightconfig.json | ||
pyright = config["tool"]["pyright"] | ||
data = dumps(add_pyright_includes(pyright, [Path("../boilercore/src")]), indent=2) | ||
PYRIGHTCONFIG.write_text(encoding="utf-8", data=f"{data}\n") | ||
# Write pytest.ini | ||
pytest = config["tool"]["pytest"]["ini_options"] | ||
pytest["addopts"] = disable_concurrent_tests(pytest["addopts"]) | ||
PYTEST.write_text( | ||
encoding="utf-8", | ||
data="\n".join(["[pytest]", *[f"{k} = {v}" for k, v in pytest.items()], ""]), | ||
) | ||
|
||
|
||
def add_pyright_includes( | ||
config: dict[str, Node], others: Iterable[Path | str] | ||
) -> dict[str, Node]: | ||
"""Include additional paths in pyright configuration. | ||
Args: | ||
config: Pyright configuration. | ||
others: Local paths to add to includes. | ||
Returns: | ||
Modified pyright configuration. | ||
""" | ||
includes = config.pop("include", []) | ||
if not isinstance(includes, Sequence): | ||
raise TypeError("Expected a sequence of includes.") | ||
return { | ||
"include": [*includes, *[str(Path(incl).as_posix()) for incl in others]], | ||
**config, | ||
} | ||
|
||
|
||
def disable_concurrent_tests(addopts: str) -> str: | ||
"""Normalize `addopts` string and disable concurrent pytest tests. | ||
Normalizes `addopts` to a space-separated one-line string. | ||
Args: | ||
addopts: Pytest `addopts` value. | ||
Returns: | ||
Modified `addopts` value. | ||
""" | ||
return sub(pattern=r"-n\s*[^\s]+", repl="-n 0", string=join(split(addopts))) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file was deleted.
Oops, something went wrong.
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
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