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
1 change: 1 addition & 0 deletions aws_lambda_builders/workflows/python_uv/packager.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ def _build_from_pyproject(
lock_args = ["lock", "--no-progress"]
if python_version:
lock_args.extend(["--python", python_version])
lock_args.extend(config.index_args())
# uv lock also needs a writable cache, same as export and pip install.
lock_args.extend(config.cache_args())

Expand Down
10 changes: 6 additions & 4 deletions aws_lambda_builders/workflows/python_uv/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,18 @@ def cache_args(self) -> List[str]:
args.append("--no-cache")
return args

def to_uv_args(self) -> List[str]:
"""Convert configuration to UV command line arguments."""
def index_args(self) -> List[str]:
"""Share package index arguments between lock generation and installation."""
args = []

if self.index_url:
args.extend(["--index-url", self.index_url])

for extra_url in self.extra_index_urls:
args.extend(["--extra-index-url", extra_url])
return args

def to_uv_args(self) -> List[str]:
"""Convert configuration to UV command line arguments."""
args = self.index_args()
args.extend(self.cache_args())

if self.prerelease != "disallow":
Expand Down
98 changes: 98 additions & 0 deletions tests/functional/workflows/python_uv/test_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import base64
import hashlib
import os
import shutil
import subprocess
import sys
import zipfile

import pytest

from aws_lambda_builders.workflows.python_uv.exceptions import UvBuildError
from aws_lambda_builders.workflows.python_uv.packager import PythonUvDependencyBuilder
from aws_lambda_builders.workflows.python_uv.utils import UvConfig


@pytest.fixture
def indexed_project(tmp_path, monkeypatch):
project = tmp_path / "project"
project.mkdir()
manifest = project / "pyproject.toml"
manifest.write_text(
'[project]\nname="function-demo"\nversion="0.0.0"\nrequires-python=">=3.10"\n'
'dependencies=["lambda-builders-index-demo==1.0.0"]\n',
encoding="utf-8",
)
index = tmp_path / "index"
package = index / "lambda-builders-index-demo"
package.mkdir(parents=True)
wheel = package / "lambda_builders_index_demo-1.0.0-py3-none-any.whl"
with zipfile.ZipFile(wheel, "w") as archive:
archive.writestr("lambda_builders_index_demo.py", "VALUE = 42\n")
archive.writestr(
"lambda_builders_index_demo-1.0.0.dist-info/METADATA",
"Metadata-Version: 2.1\nName: lambda-builders-index-demo\nVersion: 1.0.0\n",
)
archive.writestr(
"lambda_builders_index_demo-1.0.0.dist-info/WHEEL",
"Wheel-Version: 1.0\nRoot-Is-Purelib: true\nTag: py3-none-any\n",
)
records = []
for name in archive.namelist():
content = archive.read(name)
digest = base64.urlsafe_b64encode(hashlib.sha256(content).digest()).rstrip(b"=").decode("ascii")
records.append(f"{name},sha256={digest},{len(content)}\n")
record_path = "lambda_builders_index_demo-1.0.0.dist-info/RECORD"
archive.writestr(record_path, "".join(records) + f"{record_path},,\n")
(package / "index.html").write_text(f'<a href="{wheel.name}">wheel</a>\n', encoding="utf-8")
artifacts = tmp_path / "artifacts"
artifacts.mkdir()
scratch = tmp_path / "scratch"
scratch.mkdir()
for name in list(os.environ):
if name.startswith("UV_"):
monkeypatch.delenv(name)
monkeypatch.setenv("UV_OFFLINE", "1")
monkeypatch.setenv("UV_PYTHON_DOWNLOADS", "never")
monkeypatch.setenv("UV_NO_CONFIG", "1")
monkeypatch.setenv("UV_CACHE_DIR", str(tmp_path / "cache"))
return project, manifest, index, artifacts, scratch


@pytest.mark.skipif(shutil.which("uv") is None, reason="uv not available")
@pytest.mark.parametrize("locked", [False, True])
@pytest.mark.parametrize("index_option", ["index_url", "extra_index_urls"])
def test_build_pyproject_with_configured_index(indexed_project, index_option, locked):
project, manifest, index, artifacts, scratch = indexed_project
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
index_value = index.as_uri() if index_option == "index_url" else [index.as_uri()]
config = UvConfig(**{index_option: index_value})
lock_path = project / "uv.lock"
if locked:
# Independently establish that the fixture is valid and preserve this lock during the build.
subprocess.run(
["uv", "lock", "--python", python_version, "--index-url", index.as_uri()],
cwd=project,
check=True,
capture_output=True,
)
original_lock = lock_path.read_bytes()

PythonUvDependencyBuilder(runtime=f"python{python_version}").build_dependencies(
str(artifacts), str(scratch), str(manifest), config=config
)

assert (artifacts / "lambda_builders_index_demo.py").read_text(encoding="utf-8") == "VALUE = 42\n"
if locked:
assert lock_path.read_bytes() == original_lock


@pytest.mark.skipif(shutil.which("uv") is None, reason="uv not available")
def test_unconfigured_index_does_not_resolve_offline_package(indexed_project):
_, manifest, _, artifacts, scratch = indexed_project
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
with pytest.raises(UvBuildError):
PythonUvDependencyBuilder(runtime=f"python{python_version}").build_dependencies(
str(artifacts), str(scratch), str(manifest), config=UvConfig()
)
assert not (artifacts / "lambda_builders_index_demo.py").exists()