Skip to content
Open
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
20 changes: 19 additions & 1 deletion src/macaron/build_spec_generator/common_spec/base_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
from packageurl import PackageURL


class SpecBuildRequirementDict(TypedDict, total=False):
"""
Initialize build requirement section of the build specification.

It contains information about tools/packages that must be available before
running a build command.
"""

#: Build requirement name, e.g., "maturin", "rust", "rustup", "pkg-config".
name: Required[str]

#: Build requirement version or version constraint, e.g., "1.75.0", ">=1.4,<2".
version: NotRequired[str]

#: Build requirement installer, e.g., "pip", "rustup", "system", or "bootstrap".
installer: Required[str]


class SpecBuildCommandDict(TypedDict, total=False):
"""
Initialize build command section of the build specification.
Expand Down Expand Up @@ -100,7 +118,7 @@ class BaseBuildSpecDict(TypedDict, total=False):
entry_point: NotRequired[str | None]

#: The build_requires is the required packages that need to be available in the build environment.
build_requires: NotRequired[dict[str, str]]
build_requires: NotRequired[list[SpecBuildRequirementDict]]

#: A "back end" is tool that a "front end" (such as pip/build) would call to
#: package the source distribution into the wheel format. build_backends would
Expand Down
350 changes: 302 additions & 48 deletions src/macaron/build_spec_generator/common_spec/pypi_spec.py

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ def build_backend_commands(buildspec: BaseBuildSpecDict) -> list[str]:
if not buildspec["build_requires"]:
return []
commands: list[str] = []
for backend, version_constraint in buildspec["build_requires"].items():
for requirement in buildspec["build_requires"]:
if requirement["installer"] != "pip":
continue
backend = requirement["name"]
version_constraint = requirement.get("version", "")
if backend == "setuptools":
commands.append("/deps/bin/pip install --upgrade setuptools")
else:
Expand Down
13 changes: 11 additions & 2 deletions src/macaron/resources/schemas/macaron_buildspec_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,17 @@
"description": "Entry point script, class, or binary for running the project."
},
"build_requires": {
"type": "object",
"additionalProperties": { "type": "string" },
"type": "array",
"items": {
"type": "object",
"properties": {
"name": { "type": "string" },
"version": { "type": "string" },
"installer": { "type": "string" }
},
"required": ["name", "installer"],
"additionalProperties": false
},
"description": "Required packages that must be available in the build environment."
},
"build_backends": {
Expand Down
3 changes: 3 additions & 0 deletions tests/build_spec_generator/common_spec/test_pypi_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
@pytest.mark.parametrize(
("build_tool", "expected_command"),
[
("pip", ["python", "-m", "build", "--wheel", "-n"]),
("poetry", ["poetry", "build"]),
("flit", ["flit", "build"]),
("uv", ["uv", "build"]),
("hatch", ["hatch", "build"]),
("maturin", ["maturin", "build", "--release"]),
],
)
def test_set_default_build_commands_for_pypi_tools(build_tool: str, expected_command: list[str]) -> None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from macaron.build_spec_generator.common_spec.base_spec import BaseBuildSpecDict, SpecBuildCommandDict
from macaron.build_spec_generator.dockerfile.pypi_dockerfile_output import gen_dockerfile
from macaron.build_spec_generator.dockerfile.pypi_dockerfile_output import build_backend_commands, gen_dockerfile


@pytest.fixture(name="pypi_build_spec")
Expand Down Expand Up @@ -37,7 +37,10 @@ def fixture_base_build_spec() -> BaseBuildSpecDict:
confidence_score=1.0,
)
],
"build_requires": {"setuptools": "==80.9.0", "wheel": ""},
"build_requires": [
{"name": "setuptools", "version": "==80.9.0", "installer": "pip"},
{"name": "wheel", "installer": "pip"},
],
"build_backends": ["setuptools.build_meta"],
"upstream_artifacts": {
"wheels": [
Expand All @@ -56,3 +59,18 @@ def fixture_base_build_spec() -> BaseBuildSpecDict:
def test_successful_generation(snapshot: str, pypi_build_spec: BaseBuildSpecDict) -> None:
"""Ensure that dockerfile is correctly generated for pypi_build_spec"""
assert gen_dockerfile(pypi_build_spec) == snapshot


def test_build_backend_commands_only_uses_pip_requirements(pypi_build_spec: BaseBuildSpecDict) -> None:
"""Ensure non-pip build requirements are ignored in pip installation commands."""
pypi_build_spec["build_requires"] = [
{"name": "setuptools", "version": "==80.9.0", "installer": "pip"},
{"name": "wheel", "installer": "pip"},
{"name": "rustup", "installer": "bootstrap"},
{"name": "rust", "version": "1.75.0", "installer": "rustup"},
]

assert build_backend_commands(pypi_build_spec) == [
'/deps/bin/pip install "wheel"',
"/deps/bin/pip install --upgrade setuptools",
]
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,17 @@
}
],
"has_binaries": false,
"build_requires": {
"setuptools": "==80.9.0",
"wheel": ""
},
"build_requires": [
{
"name": "setuptools",
"version": "==80.9.0",
"installer": "pip"
},
{
"name": "wheel",
"installer": "pip"
}
],
"build_backends": [
"setuptools.build_meta"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
}
],
"has_binaries": false,
"build_requires": {
"flit": "==3.12.0",
"flit_core": "<4,>=3.4"
},
"build_requires": [
{
"name": "flit",
"version": "==3.12.0",
"installer": "pip"
},
{
"name": "flit_core",
"version": "<4,>=3.4",
"installer": "pip"
}
],
"build_backends": [
"flit_core.buildapi"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@
}
],
"has_binaries": false,
"build_requires": {
"setuptools": "==67.7.2"
},
"build_requires": [
{
"name": "setuptools",
"version": "==67.7.2",
"installer": "pip"
}
],
"build_backends": [
"setuptools.build_meta"
],
Expand Down
Loading
Loading