Skip to content

Commit

Permalink
Add constraint key
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanking13 committed Feb 1, 2025
1 parent 742dde8 commit 24ec8db
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 2 deletions.
32 changes: 31 additions & 1 deletion pyodide_build/recipe/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
RUST_BUILD_PRELUDE,
BuildArgs,
get_build_environment_vars,
get_build_flag,
get_pyodide_root,
pyodide_tags,
replace_so_abi_tags,
Expand Down Expand Up @@ -335,7 +336,34 @@ def _download_and_extract(self) -> None:
extract_dir_name = trim_archive_extension(tarballname)

shutil.move(self.build_dir / extract_dir_name, self.src_extract_dir)
self.src_dist_dir.mkdir(parents=True, exist_ok=True)
self.src_dist_dir.mkdir(parents=True, exist_ok=True)\

def _override_constraints(self) -> str:
"""
Override global constraints (PIP_CONSTRAINT) with constraints specific to this package.
returns the path to the new constraints file.
"""
try:
host_constraints = get_build_flag("PIP_CONSTRAINT")
except ValueError:
host_constraints = ""

constraints = self.recipe.requirements.constraint
if not constraints:
# nothing to override
return host_constraints

host_constraints_file = Path(host_constraints)
new_constraints_file = self.build_dir / "constraints.txt"
if host_constraints_file.is_file():
shutil.copy(host_constraints_file, new_constraints_file)

with new_constraints_file.open("a") as f:
for constraint in constraints:
f.write(constraint + "\n")

return str(new_constraints_file)

def _compile(
self,
Expand Down Expand Up @@ -383,6 +411,8 @@ def _compile(
)
build_env = runner.env

build_env["PIP_CONSTRAINT"] = str(self._override_constraints())

pypabuild.build(
self.src_extract_dir, self.src_dist_dir, build_env, config_settings
)
Expand Down
1 change: 1 addition & 0 deletions pyodide_build/recipe/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class _RequirementsSpec(BaseModel):
run: list[str] = []
host: list[str] = []
executable: list[str] = []
constraint: list[str] = []
model_config = ConfigDict(extra="forbid")


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package:
name: pkg_test_constraint
version: "1.0.0"
requirements:
constraint:
- numpy < 2.0
- scipy > 1.0
- pytest == 7.0
source:
path: src
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
requires = ["setuptools>=42", "wheel"]
build-backend = "setuptools.build_meta"

[project]
name = "pkg_test_constraint"
version = "1.0.0"
authors = []
31 changes: 30 additions & 1 deletion pyodide_build/tests/recipe/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

from pyodide_build import common
from pyodide_build.build_env import BuildArgs
from pyodide_build.build_env import BuildArgs, get_build_flag
from pyodide_build.recipe import builder as _builder
from pyodide_build.recipe.builder import (
RecipeBuilder,
Expand Down Expand Up @@ -216,6 +216,35 @@ def rlist(input_dir):
assert n_moved == 3


def test_override_constraints_no_override(tmp_path, dummy_xbuildenv):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_test_executable", # constraints not set, so no override
build_args=BuildArgs(),
build_dir=tmp_path,
)

path = builder._override_constraints()
assert path == get_build_flag("PIP_CONSTRAINT")


def test_override_constraints_override(tmp_path, dummy_xbuildenv):
builder = RecipeBuilder.get_builder(
recipe=RECIPE_DIR / "pkg_test_constraint",
build_args=BuildArgs(),
build_dir=tmp_path,
)

path = builder._override_constraints()
assert path == str(tmp_path / "constraints.txt")

data = Path(path).read_text().strip().split("\n")
assert data[-3:] == [
"numpy < 2.0",
"scipy > 1.0",
"pytest == 7.0"
], data


class MockSourceSpec(_SourceSpec):
@pydantic.model_validator(mode="after")
def _check_patches_extra(self) -> Self:
Expand Down

0 comments on commit 24ec8db

Please sign in to comment.