Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/pdm/backend/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def __init__(
) -> None:
self._old_cwd: str | None = None
self.location = Path(location)
self.config = Config.from_pyproject(self.location)
self.config = Config.from_pyproject(self.location, validate=False)
self.config_settings = dict(config_settings or {})
self._hooks = list(self.get_hooks())

Expand Down Expand Up @@ -160,6 +160,11 @@ def __exit__(self, *args: Any) -> None:
assert self._old_cwd
os.chdir(self._old_cwd)

def configure(self, context: Context) -> None:
"""Finalize and validate configuration, calling hooks"""
self.call_hook("pdm_build_configure", context)
self.config.validate()

def clean(self, context: Context) -> None:
"""Clean up the build directory."""
self.call_hook("pdm_build_clean", context)
Expand Down Expand Up @@ -202,6 +207,8 @@ def finalize(self, context: Context, artifact: Path) -> None:
def build(self, build_dir: str, **kwargs: Any) -> Path:
"""Build the package and return the path to the artifact."""
context = self.build_context(Path(build_dir), **kwargs)
self.configure(context)

should_clean = True

if "no-clean-build" in self.config_settings:
Expand Down
10 changes: 6 additions & 4 deletions src/pdm/backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ class Config:
Parameters:
root: The root directory of the project
data: The parsed pyproject.toml data
validate: Perform validation immediately on instantiation.

Attributes:
metadata (Metadata): The project metadata from the `project` table
build_config (BuildConfig): The build config from the `tool.pdm.build` table
"""

def __init__(self, root: Path, data: dict[str, Any]) -> None:
def __init__(self, root: Path, data: dict[str, Any], validate: bool = True) -> None:
self.root = root
self.data = data
self.validate()
if validate:
self.validate()

def validate(self) -> StandardMetadata:
"""Validate the pyproject.toml data."""
Expand All @@ -64,7 +66,7 @@ def build_config(self) -> BuildConfig:
)

@classmethod
def from_pyproject(cls, root: str | Path) -> Config:
def from_pyproject(cls, root: str | Path, validate: bool = True) -> Config:
"""Load the pyproject.toml file from the given project root."""
root = Path(root)
pyproject = root / "pyproject.toml"
Expand All @@ -75,7 +77,7 @@ def from_pyproject(cls, root: str | Path) -> Config:
data = tomllib.load(fp)
except tomllib.TOMLDecodeError as e:
raise ConfigError(f"Invalid pyproject.toml file: {e}") from e
return cls(root, data)
return cls(root, data, validate=validate)

def write_to(self, path: str | Path) -> None:
"""Write the pyproject.toml file to the given path."""
Expand Down
6 changes: 6 additions & 0 deletions src/pdm/backend/hooks/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ def pdm_build_hook_enabled(self, context: Context) -> bool:
"""
...

def pdm_build_configure(self, context: Context) -> None:
"""Wraps the pyproject.toml configuration loading and validation.
Modify values or prepare the build environment before the config is validated.
"""
...

def pdm_build_clean(self, context: Context) -> None:
"""An optional clean step which will be called before the build starts

Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/hooks/hook_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class BuildHook:
def __init__(self, name: str = "2") -> None:
self.name = name

def pdm_build_configure(self, context: Context) -> None:
logger.info("Hook%s build configure called", self.name)

def pdm_build_clean(self, context: Context) -> None:
logger.info("Hook%s build clean called", self.name)

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/hooks/hook_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
logger = logging.getLogger("hooks")


def pdm_build_configure(context: Context) -> None:
logger.info("Hook1 build configure called")


def pdm_build_clean(context: Context) -> None:
logger.info("Hook1 build clean called")

Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/hooks/local_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
logger = logging.getLogger("hooks")


def pdm_build_configure(context: Context) -> None:
logger.info("Hook4 build configure called")


def pdm_build_clean(context: Context) -> None:
logger.info("Hook4 build clean called")

Expand Down
1 change: 1 addition & 0 deletions tests/test_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_load_hooks(project_with_hooks, caplog: pytest.LogCaptureFixture):

messages = [record.message for record in caplog.records if record.name == "hooks"]
for num in range(1, 5):
assert f"Hook{num} build configure called" in messages
assert f"Hook{num} build clean called" in messages
assert f"Hook{num} build initialize called" in messages
assert f"Hook{num} build update files called" in messages
Expand Down
Loading