-
Notifications
You must be signed in to change notification settings - Fork 0
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
a7412ee
commit 4bb0352
Showing
6 changed files
with
142 additions
and
85 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Module names.""" | ||
|
||
from importlib.machinery import ModuleSpec | ||
from pathlib import Path | ||
from types import ModuleType | ||
|
||
|
||
def get_package_dir(package: ModuleType) -> Path: | ||
"""Get the directory of a package given the top-level module.""" | ||
return Path(package.__spec__.submodule_search_locations[0]) # type: ignore | ||
|
||
|
||
def get_module_name(module: ModuleType | ModuleSpec | Path | str) -> str: | ||
"""Get an unqualified module name. | ||
Example: `get_module_name(__spec__ or __file__)`. | ||
""" | ||
if isinstance(module, ModuleType | ModuleSpec): | ||
return get_qualified_module_name(module).split(".")[-1] | ||
path = Path(module) | ||
return path.parent.name if path.stem in ("__init__", "__main__") else path.stem | ||
|
||
|
||
def get_qualified_module_name(module: ModuleType | ModuleSpec) -> str: # type: ignore | ||
"""Get a fully-qualified module name. | ||
Example: `get_module_name(__spec__ or __file__)`. | ||
""" | ||
if isinstance(module, ModuleType): | ||
module: ModuleSpec = module.__spec__ # type: ignore | ||
return module.name |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,90 @@ | ||
"""Contributor environment.""" | ||
"""Contributor environment setup.""" | ||
|
||
import subprocess | ||
from collections.abc import Iterable | ||
from contextlib import chdir, nullcontext | ||
from io import StringIO | ||
from pathlib import Path | ||
from shlex import quote | ||
from sys import executable | ||
|
||
from dotenv import load_dotenv | ||
from dotenv import dotenv_values, load_dotenv | ||
from pydantic import BaseModel, Field | ||
from pydantic_settings import ( | ||
BaseSettings, | ||
PyprojectTomlConfigSettingsSource, | ||
SettingsConfigDict, | ||
) | ||
|
||
import dev | ||
from dev.modules import get_module_name | ||
|
||
def init_shell(path: Path | None = None) -> str: | ||
"""Initialize shell.""" | ||
with chdir(path) if path else nullcontext(): | ||
environment = Environment().model_dump() | ||
dotenv = "\n".join(f"{k}={v}" for k, v in environment.items()) | ||
load_dotenv(stream=StringIO(dotenv)) | ||
return dotenv | ||
|
||
class Constants(BaseModel): | ||
"""Constants for {mod}`~dev.tools.environment`.""" | ||
|
||
def run(args: str | Iterable[str] | None = None): | ||
dev_tool_config: tuple[str, ...] = ("tool", get_module_name(dev)) | ||
"""Path to `dev` tool configuration in `pyproject.toml`.""" | ||
pylance_version_source: str = ".pylance-version" | ||
"""Path to Pylance version file.""" | ||
shell: list[str] = ["pwsh", "-Command"] | ||
"""Shell invocation for running arbitrary commands.""" | ||
uv_run_wrapper: str = "./Invoke-Uv.ps1" | ||
"""Wrapper of `uv run` with extra setup.""" | ||
env: str = ".env" | ||
"""Name of environment file.""" | ||
|
||
|
||
const = Constants() | ||
|
||
|
||
def sync_environment_variables( | ||
path: Path | None = None, pylance_version: str = "", setenv: bool = True | ||
) -> str: | ||
"""Sync `.env` with `pyproject.toml`, optionally setting environment variables.""" | ||
path = Path(path) if path else Path.cwd() / ".env" | ||
config_env = Config().env | ||
if pylance_version: | ||
config_env["PYRIGHT_PYTHON_PYLANCE_VERSION"] = pylance_version | ||
dotenv = dotenv_values(const.env) | ||
keys_set: list[str] = [] | ||
for key in dotenv: | ||
if override := config_env.get(key): | ||
keys_set.append(key) | ||
dotenv[key] = override | ||
for k, v in config_env.items(): | ||
if k not in keys_set: | ||
dotenv[k] = v | ||
if setenv: | ||
load_dotenv(stream=StringIO("\n".join(f"{k}={v}" for k, v in dotenv.items()))) | ||
return "\n".join(f"{k}={v}" for k, v in dotenv.items()) | ||
|
||
|
||
def run(*args: str): | ||
"""Run command.""" | ||
sep = " " | ||
subprocess.run( | ||
check=True, | ||
args=[ | ||
"pwsh", | ||
"-Command", | ||
sep.join([ | ||
f"& {quote(executable)} -m", | ||
*(([args] if isinstance(args, str) else args) or []), | ||
]), | ||
], | ||
) | ||
|
||
|
||
class Environment(BaseSettings): | ||
"""Get environment variables from `pyproject.toml:[tool.env]`.""" | ||
|
||
model_config = SettingsConfigDict( | ||
extra="allow", pyproject_toml_table_header=("tool", "env") | ||
) | ||
with nullcontext() if Path(const.uv_run_wrapper).exists() else chdir(".."): | ||
subprocess.run( | ||
check=True, args=[*const.shell, sep.join([const.uv_run_wrapper, *args])] | ||
) | ||
|
||
@classmethod | ||
def settings_customise_sources(cls, settings_cls, **_): # pyright: ignore[reportIncompatibleMethodOverride] | ||
"""Customize so that all keys are loaded despite not being model fields.""" | ||
return (PyprojectTomlConfigSettingsSource(settings_cls),) | ||
|
||
def run_dev(*args: str): | ||
"""Run command from `dev` CLI.""" | ||
run(f"& {quote(executable)} -m", *args) | ||
|
||
|
||
def escape(path: str | Path) -> str: | ||
"""Escape a path, suitable for passing to e.g. {func}`~subprocess.run`.""" | ||
return quote(Path(path).as_posix()) | ||
|
||
|
||
class Config(BaseSettings): | ||
"""Get tool config from `pyproject.toml`.""" | ||
|
||
model_config = SettingsConfigDict(pyproject_toml_table_header=const.dev_tool_config) | ||
env: dict[str, str] = Field(default_factory=dict) | ||
|
||
@classmethod | ||
def settings_customise_sources(cls, settings_cls, **_): # pyright: ignore[reportIncompatibleMethodOverride] | ||
"""Only load from `pyproject.toml`.""" | ||
return (PyprojectTomlConfigSettingsSource(settings_cls),) |
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,12 @@ | ||
"""Warnings.""" | ||
|
||
from boilercore.warnings import filter_boiler_warnings | ||
|
||
|
||
def filter_boilercv_warnings(): | ||
"""Filter certain warnings for `boilercv`.""" | ||
filter_boiler_warnings(other_warnings=WARNING_FILTERS) | ||
|
||
|
||
WARNING_FILTERS = [] | ||
"""Warning filters.""" |