Skip to content

Commit

Permalink
Generate local dev configs
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeNaccarato committed Jan 26, 2024
1 parent 137e8aa commit 9bb5755
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 49 deletions.
88 changes: 88 additions & 0 deletions .tools/scripts/local_dev_configs.py
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()
46 changes: 0 additions & 46 deletions .tools/scripts/local_pyrightconfig.py

This file was deleted.

2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
},
"notebook.codeActionsOnSave": {
"notebook.source.fixAll": "explicit",
"notebook.source.organizeImports": "explicit"
"source.organizeImports": "explicit"
},
//! Extensions
//* GitHub Actions
Expand Down
4 changes: 2 additions & 2 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@
"problemMatcher": []
},
{
"label": "proj: local pyrightconfig",
"label": "proj: local dev configs (Pyrightconfig, pytest.ini)",
"type": "shell",
"options": { "shell": { "executable": "pwsh", "args": ["-Command"] } },
"command": "python .tools/scripts/local_pyrightconfig.py",
"command": "python .tools/scripts/local_dev_configs.py",
"icon": { "id": "graph" },
"problemMatcher": []
},
Expand Down

0 comments on commit 9bb5755

Please sign in to comment.