Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add requirement.constraint key to support package-wise constraints #97

Merged
merged 6 commits into from
Feb 4, 2025
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## [0.29.3] - 2025/02/04

### Added

- Added new configuration variable `default_cross_build_env_url`.
[#85](https://github.com/pyodide/pyodide-build/pull/85)

- Added a new recipe key `requirement.constraint` to set the package-level constraints.
[#97](https://github.com/pyodide/pyodide-build/pull/97)

## [0.29.2] - 2024/11/29

### Fixed
Expand Down
31 changes: 31 additions & 0 deletions 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 @@ -337,6 +338,34 @@ def _download_and_extract(self) -> None:
shutil.move(self.build_dir / extract_dir_name, self.src_extract_dir)
self.src_dist_dir.mkdir(parents=True, exist_ok=True)

def _create_constraints_file(self) -> str:
"""
Creates a pip constraints file by concatenating 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,
bash_runner: BashRunnerWithSharedEnvironment,
Expand Down Expand Up @@ -383,6 +412,8 @@ def _compile(
)
build_env = runner.env

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

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,9 @@
package:
name: pkg_test_constraint
version: "1.0.0"
requirements:
constraint:
- numpy < 2.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe numpy and scipy are bad examples though since realistically this is broken I'd assume?

- 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 = []
28 changes: 27 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,32 @@ def rlist(input_dir):
assert n_moved == 3


def test_create_constraints_file_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._create_constraints_file()
assert path == get_build_flag("PIP_CONSTRAINT")


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

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

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


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