diff --git a/docs/pypi/lock.md b/docs/pypi/lock.md index c40e21f45b..34c10db60c 100644 --- a/docs/pypi/lock.md +++ b/docs/pypi/lock.md @@ -4,12 +4,16 @@ # Lock :::{note} -`rules_python` supports `requirements.txt` and `uv.lock` formats. Generating +`rules_python` supports `requirements.txt`, `uv.lock` and `pylock.toml` +formats. Generating `uv.lock` with {obj}`lock` and consuming it to install packages via {attr}`pip.parse.uv_lock` or {attr}`pip_parse.uv_lock` is supported under both `bzlmod` and `WORKSPACE`. -#{gh-issue}`2787` tracks `pylock.toml` support. +`pylock.toml` (PEP 751) files, e.g. exported with +`uv export --format pylock.toml`, can be consumed with +{attr}`pip.parse.pylock` under `bzlmod`; the `vcs`, `archive` and `directory` +sources are tracked in {gh-issue}`2787`. ::: ## requirements.txt diff --git a/docs/pypi/rules-pycross-comparison.md b/docs/pypi/rules-pycross-comparison.md index 15b37cbc4f..15f719f569 100644 --- a/docs/pypi/rules-pycross-comparison.md +++ b/docs/pypi/rules-pycross-comparison.md @@ -24,7 +24,7 @@ The status words used in the tables are: | rules_pycross | rules_python | Notes | |---|---|---| -| `uv`/`pdm`/`poetry`/`pylock` extensions | {obj}`pip.parse` | `pip.parse` reads `requirements.txt` files and `uv.lock`. | +| `uv`/`pdm`/`poetry`/`pylock` extensions | {obj}`pip.parse` | `pip.parse` reads `requirements.txt`, `uv.lock` and `pylock.toml` files. | | `workspace` tag (one lock file) | one {obj}`pip.parse` per hub and Python version | The same `uv_lock` can be passed to several `pip.parse` calls. | | `repo` tag (a set of packages) | a hub repository (`hub_name`) | A workspace with N repos becomes N hubs. | | `repo(dependency_groups = ...)` | {obj}`pip.configure` | Requires {gh-pr}`4395`. The groups are read from `pyproject.toml`, not from the lock file. | @@ -202,8 +202,8 @@ module. Do not depend on the same distribution from both in one binary. |---|---|---| | `uv.lock` | {attr}`pip.parse.uv_lock` | have | | uv workspace members, `projects`, `default-groups`, `extra_project_files` | the uv workspace members are used as the roots; {obj}`pip.configure` reads one `pyproject.toml` | partial | -| `pylock.toml` ({gh-issue}`2787`) | none | planned | -| `pdm.lock`, `poetry.lock` | export them to `requirements.txt` (`pdm export -f requirements`, `poetry export`) for {attr}`pip.parse.requirements_lock`; `pylock.toml` is planned | partial | +| `pylock.toml` | {attr}`pip.parse.pylock`; `vcs`, `archive` and `directory` sources are planned | partial | +| `pdm.lock`, `poetry.lock` | export them to `pylock.toml` (`pdm export -f pylock`, `poetry-plugin-export` 1.10+) for {attr}`pip.parse.pylock` | partial: no `vcs`, `archive` or `directory` sources | | dependency groups, `transitive` | {obj}`pip.configure`, requires {gh-pr}`4395` | have | | `;testonly` | none, see {gh-pr}`4428` | planned | | extras targets | requires {gh-pr}`4490` and {gh-pr}`4523` | have | diff --git a/news/pip-parse-pylock.added.md b/news/pip-parse-pylock.added.md new file mode 100644 index 0000000000..0f3ea439a2 --- /dev/null +++ b/news/pip-parse-pylock.added.md @@ -0,0 +1,5 @@ +(pypi) The {attr}`pip.parse.pylock` attribute reads a `pylock.toml` (PEP 751) +lock file, e.g. exported by `uv`, `pdm` or `pip lock`, instead of the +requirements files (bzlmod only). The packages from `vcs`, `archive` and +`directory` sources are not supported yet +([#2787](https://github.com/bazel-contrib/rules_python/issues/2787)). diff --git a/python/private/pypi/BUILD.bazel b/python/private/pypi/BUILD.bazel index 66bffb2d4d..88478d1a15 100644 --- a/python/private/pypi/BUILD.bazel +++ b/python/private/pypi/BUILD.bazel @@ -288,6 +288,7 @@ bzl_library( ":parse_requirements_txt", ":pep508_evaluate", ":pep508_requirement", + ":pylock", ":select_whl", ":uv_lock", "//python/private:normalize_name", @@ -408,6 +409,19 @@ bzl_library( ], ) +bzl_library( + name = "pylock", + srcs = ["pylock.bzl"], + deps = [ + ":hash", + ":lock_model", + ":pep508_evaluate", + ":uv_lock", + "//python/private:normalize_name", + "//python/private:version", + ], +) + bzl_library( name = "pypi_cache", srcs = ["pypi_cache.bzl"], diff --git a/python/private/pypi/extension.bzl b/python/private/pypi/extension.bzl index 94d7fd8377..3f3772a268 100644 --- a/python/private/pypi/extension.bzl +++ b/python/private/pypi/extension.bzl @@ -873,7 +873,8 @@ Using the hub name `"pypi"` is deprecated and is changed to "local_wheels": attr.label_list( allow_files = [".whl"], doc = """\ -Local wheels to use instead of the wheels in the {attr}`uv_lock` with the same +Local wheels to use instead of the wheels in the {attr}`uv_lock` or the +{attr}`pylock` with the same package name and version, e.g. wheels that are checked in or that come from another repository because they are not available on an index. The wheels are read when the repositories are fetched, so wheels that are built by Bazel @@ -901,6 +902,27 @@ find in case extra indexes are specified. """, default = True, ), + "pylock": attr.label( + allow_single_file = True, + doc = """\ +A `pylock.toml` lock file (PEP 751) to use instead of the requirements files, +e.g. exported with `uv export --format pylock.toml`, `pdm export -f pylock` or +`pip lock`. + +All of the extras, the `default-groups` and the `dependency-groups` that the +lock file lists are selected, so that the hub has all of the packages; use +{obj}`pip.configure` to limit which of them are exposed. A package is used on +the platforms where its `marker` is true and it fails if its +`requires-python` does not match such a platform. Each platform needs to match +the top-level `requires-python`; the platforms that do not match the top-level +`environments` are ignored with a warning. The files without a `url` are +ignored, use {attr}`local_wheels` for them. The packages from `vcs`, `archive` +and `directory` sources are not supported yet. It cannot be used together with +the {attr}`uv_lock` or the requirements files. + +:::{versionadded} VERSION_NEXT_FEATURE +:::""", + ), "pyproject_toml": attr.label( mandatory = False, doc = """\ diff --git a/python/private/pypi/hub_builder.bzl b/python/private/pypi/hub_builder.bzl index f7d244688d..edd9d9e73a 100644 --- a/python/private/pypi/hub_builder.bzl +++ b/python/private/pypi/hub_builder.bzl @@ -378,8 +378,9 @@ def _add_whl_library(self, *, python_version, whl, repo): self._logger.fail(lambda: ( "Attempting to override an existing repo '{}' for config setting '{}' with a new repo '{}'. " + "This happens when the lock file has more than one entry (e.g. different versions or sources) " + - "of '{}' for the same platform, e.g. with the uv `[tool.uv] conflicts` setting, which is not " + - "supported. Use a separate hub for each of the conflicting groups." + "of '{}' for the same platform, e.g. with the uv `[tool.uv] conflicts` setting or markers that " + + "rules_python cannot tell apart, which is not supported. Use a separate hub for each of the " + + "conflicting groups." ).format( mapping[repo.config_setting], repo.config_setting, @@ -575,6 +576,12 @@ def _create_whl_repos( """ logger = self._logger platforms = self._platforms[python_version] + if getattr(pip_attr, "pylock", None) and ( + pip_attr.requirements_lock or pip_attr.requirements_by_platform or + pip_attr.requirements_linux or pip_attr.requirements_darwin or pip_attr.requirements_windows + ): + logger.fail("The 'pylock' cannot be used with the requirements files") + return requirements_by_platform = parse_requirements( module_ctx, requirements_by_platform = requirements_files_by_platform( @@ -583,13 +590,14 @@ def _create_whl_repos( requirements_lock = pip_attr.requirements_lock, requirements_osx = pip_attr.requirements_darwin, requirements_windows = pip_attr.requirements_windows, - uv_lock = pip_attr.uv_lock, + uv_lock = pip_attr.uv_lock or getattr(pip_attr, "pylock", None), extra_pip_args = pip_attr.extra_pip_args, platforms = sorted(platforms), # here we only need keys python_version = full_python_version, logger = logger, ), uv_lock = pip_attr.uv_lock, + pylock = getattr(pip_attr, "pylock", None), local_wheels = getattr(pip_attr, "local_wheels", []), build_mode = self._build_mode, platforms = platforms, diff --git a/python/private/pypi/parse_requirements.bzl b/python/private/pypi/parse_requirements.bzl index ce56b623cc..847a4d39b6 100644 --- a/python/private/pypi/parse_requirements.bzl +++ b/python/private/pypi/parse_requirements.bzl @@ -35,6 +35,7 @@ load(":lock_model.bzl", "lock_requirements") load(":parse_requirements_txt.bzl", "parse_requirements_txt") load(":pep508_evaluate.bzl", "evaluate") load(":pep508_requirement.bzl", "requirement") +load(":pylock.bzl", "pylock_entries") load(":select_whl.bzl", "select_whl") load(":uv_lock.bzl", "uv_lock_entries") @@ -47,6 +48,7 @@ def parse_requirements( get_index_urls = None, extract_url_srcs = True, uv_lock = None, + pylock = None, local_wheels = [], build_mode = None, toml_decode = None, @@ -72,9 +74,11 @@ def parse_requirements( If provided, the function will use the uv.lock file as the primary source for package metadata and perform a consistency check against requirements files if both are provided. + pylock: {type}`str | None` an optional label/file path to the + pylock.toml file, which is used instead of the requirements files. local_wheels: {type}`list[Label]` the local wheels to use instead of the locked wheels of the same package version. Only supported - with the `uv_lock`. + with the `uv_lock` or the `pylock`. build_mode: {type}`callable | None` returns the build mode of a normalized package name, `always` ignores the wheels. toml_decode: {type}`callable | None` A function to decode TOML @@ -94,6 +98,31 @@ def parse_requirements( `target_platforms`, `filename`, `digest`, `url`, `yanked`. The `digest` is a `:` string or empty if the artifact digest is unknown. """ + if pylock: + if uv_lock: + logger.fail("Only one of the 'pylock' and 'uv_lock' can be used") + return [] + if not toml_decode: + logger.fail("BUG: 'toml_decode' is needed to read the 'pylock'") + return [] + lock = pylock_entries( + toml_decode(ctx.read(pylock)), + all_platforms = sorted(platforms.keys()), + platforms = platforms, + local_wheels = local_wheels, + logger = logger, + ) + ret = lock_requirements( + lock.entries, + all_platforms = lock.platforms, + platforms = platforms, + extra_pip_args = extra_pip_args, + logger = logger, + build_mode = build_mode, + ) + logger.debug(lambda: "Parsed {} packages from pylock.toml".format(len(ret))) + return ret + lock = toml_decode(ctx.read(uv_lock)) if uv_lock and toml_decode else None # NOTE: an empty uv.lock falls back to the requirements files. @@ -116,7 +145,7 @@ def parse_requirements( return ret if local_wheels: - logger.fail("The local wheels are only supported with a 'uv_lock'") + logger.fail("The local wheels are only supported with a 'uv_lock' or a 'pylock'") return [] return _parse_requirements_from_req_files( diff --git a/python/private/pypi/pylock.bzl b/python/private/pypi/pylock.bzl new file mode 100644 index 0000000000..a6452ce319 --- /dev/null +++ b/python/private/pypi/pylock.bzl @@ -0,0 +1,151 @@ +"""Translate the pylock.toml (PEP 751) files into the lock model, see `lock_model.bzl`.""" + +load("//python/private:normalize_name.bzl", "normalize_name") +load("//python/private:version.bzl", pep440 = "version") +load(":hash.bzl", "hash") +load(":lock_model.bzl", "lock_dist", "lock_entry") +load(":pep508_evaluate.bzl", "evaluate") +load(":uv_lock.bzl", "local_wheels_by_name") + +def pylock_entries(pylock, *, all_platforms, platforms, local_wheels = [], logger, _fail = fail): + """Translate the pylock.toml contents into the lock model. + + All of the extras, the default groups and the dependency groups that the + lock file lists are selected. A package is used on the platforms where its + `marker` is true; the dependency graph is not used, as the standard + requires. + + Args: + pylock: {type}`dict` The decoded pylock.toml contents. + all_platforms: {type}`list[str]` The list of all platform names. + platforms: {type}`dict[str, struct]` A dict of platform name to platform info + (containing `.env` with PEP 508 marker environment). + local_wheels: {type}`list[Label]` the local wheels to use instead of + the locked wheels of the same package version. + logger: {type}`struct` A logger for diagnostic messages. + _fail: {type}`callable` the function to call on errors, for tests. + + Returns: + {type}`struct` with `entries` - the {func}`lock_entry` values and + `platforms` - the platforms that match the lock file. + """ + lock_version = pylock.get("lock-version", "") + if not lock_version.startswith("1."): + return _fail("Only the pylock.toml 'lock-version' 1.x is supported, got: '{}'".format(lock_version)) + if lock_version != "1.0": + logger.warn(lambda: "The pylock.toml 'lock-version' {} is newer than the supported 1.0".format(lock_version)) + + envs = {} + for p in all_platforms: + env = dict(platforms[p].env) + env.setdefault("extra", "") + env["extras"] = pylock.get("extras", []) + env["dependency_groups"] = pylock.get("default-groups", []) + pylock.get("dependency-groups", []) + + requires_python = pylock.get("requires-python") + if requires_python and not _requires_python(requires_python, env): + return _fail("The pylock.toml 'requires-python = \"{}\"' does not match the Python version {} of the '{}' platform".format( + requires_python, + env.get("python_full_version"), + p, + )) + + environments = pylock.get("environments", []) + if environments and not [m for m in environments if evaluate(m, env = env)]: + logger.warn(lambda: "The pylock.toml 'environments' do not match the '{}' platform, ignoring it".format(p)) + continue + + envs[p] = env + + local_by_name = local_wheels_by_name(local_wheels) + used_local_wheels = {} + + entries = [] + for pkg in pylock.get("packages", []): + name = pkg["name"] + version = pkg.get("version", "") + for key in ["vcs", "archive", "directory"]: + if key in pkg: + return _fail("The pylock.toml package '{}' has a '{}' source, which is not supported".format(name, key)) + if not version: + return _fail("The pylock.toml package '{}' has no 'version'".format(name)) + + target_platforms = [ + p + for p, env in envs.items() + if not pkg.get("marker") or evaluate(pkg["marker"], env = env) + ] + for p in target_platforms: + if pkg.get("requires-python") and not _requires_python(pkg["requires-python"], envs[p]): + return _fail("The pylock.toml package '{}' has 'requires-python = \"{}\"', which does not match the Python version {} of the '{}' platform".format( + name, + pkg["requires-python"], + envs[p].get("python_full_version"), + p, + )) + + local = [] + if version and normalize_name(name) in local_by_name: + locked_version = pep440.parse(version) + local = [ + whl + for v, whl in local_by_name[normalize_name(name)] + if v and locked_version and pep440.is_eq(v, locked_version) + ] + for whl in local: + used_local_wheels[whl.file] = None + + sdist = pkg.get("sdist") + if [d for d in pkg.get("wheels", []) + ([sdist] if sdist else []) if "url" not in d]: + logger.warn(lambda: "The pylock.toml package '{}' has files without a 'url', which are ignored, use the 'local_wheels' instead".format(name)) + entries.append(lock_entry( + name = name, + version = version, + target_platforms = target_platforms, + index_url = "{}/{}".format(pkg["index"].rstrip("/"), normalize_name(name).replace("_", "-")) if pkg.get("index") else "", + wheels = [_dist(wheel) for wheel in pkg.get("wheels", []) if "url" in wheel], + local_wheels = local, + sdist = _dist(sdist) if sdist and "url" in sdist else None, + )) + + for whls in local_by_name.values(): + for _, whl in whls: + if whl.file not in used_local_wheels: + _fail("The local wheel '{}' does not match the version of any package in the lock file".format(whl.file)) + + return struct( + entries = entries, + platforms = sorted(envs), + ) + +def _requires_python(specifiers, env): + """Evaluate a `requires-python` specifier set, e.g. `>=3.9,<4`.""" + return evaluate( + " and ".join([ + "python_full_version {}".format(_quote_version(s.strip())) + for s in specifiers.split(",") + if s.strip() + ]), + env = env, + ) + +def _quote_version(specifier): + """Turn `>=3.9` into `>= "3.9"`.""" + for i, c in enumerate(specifier.elems()): + if c not in "<>=!~ ": + return "{} \"{}\"".format(specifier[:i].strip(), specifier[i:].strip()) + return specifier + +def _dist(dist): + url = dist["url"] + path, _, _ = url.partition("#") + path, _, _ = path.partition("?") + _, _, filename = path.rpartition("/") + return lock_dist( + filename = dist.get("name") or filename.replace("%2B", "+").replace("%2b", "+"), + url = url, + digest = hash.preferred_digest([ + hash.digest(algo, hex_digest) + for algo, hex_digest in dist.get("hashes", {}).items() + ]), + ) diff --git a/python/private/pypi/requirements_files_by_platform.bzl b/python/private/pypi/requirements_files_by_platform.bzl index c6f53ae611..e8c3af3ded 100644 --- a/python/private/pypi/requirements_files_by_platform.bzl +++ b/python/private/pypi/requirements_files_by_platform.bzl @@ -112,7 +112,7 @@ def requirements_files_by_platform( uv_lock ): fail_fn( - "A 'requirements_lock' or 'uv_lock' attribute must be specified, a platform-specific lockfiles " + + "A 'requirements_lock', 'uv_lock' or 'pylock' attribute must be specified, a platform-specific lockfiles " + "via 'requirements_by_platform' or an os-specific lockfiles must be specified " + "via 'requirements_*' attributes", ) diff --git a/tests/pypi/hub_builder/hub_builder_tests.bzl b/tests/pypi/hub_builder/hub_builder_tests.bzl index dd94f50751..57274192e1 100644 --- a/tests/pypi/hub_builder/hub_builder_tests.bzl +++ b/tests/pypi/hub_builder/hub_builder_tests.bzl @@ -258,6 +258,30 @@ def _test_uv_lock_build_modes(env): _tests.append(_test_uv_lock_build_modes) +def _test_pylock(env): + builder = hub_builder(env) + builder.pip_parse( + _mock_mctx( + os_name = "osx", + arch_name = "aarch64", + mock_files = { + "pylock.toml": """{"lock-version":"1.0","packages":[{"name":"simple","version":"0.0.1","index":"https://pypi.org/simple","wheels":[{"url":"https://files.pythonhosted.org/packages/simple-0.0.1-py3-none-any.whl","hashes":{"sha256":"deadbeef"}}]}]}""", + }, + ), + _parse( + hub_name = "pypi", + python_version = "3.15", + requirements_lock = None, + pylock = "pylock.toml", + ), + ) + pypi = builder.build() + + pypi.exposed_packages().contains_exactly(["simple"]) + pypi.whl_libraries().keys().contains_exactly(["pypi_315_simple_py3_none_any_deadbeef"]) + +_tests.append(_test_pylock) + def _test_uv_lock_conflicting_versions(env): logs = {} builder = hub_builder( diff --git a/tests/pypi/pylock/BUILD.bazel b/tests/pypi/pylock/BUILD.bazel new file mode 100644 index 0000000000..238ba7b8ec --- /dev/null +++ b/tests/pypi/pylock/BUILD.bazel @@ -0,0 +1,3 @@ +load(":pylock_tests.bzl", "pylock_test_suite") + +pylock_test_suite(name = "pylock_tests") diff --git a/tests/pypi/pylock/pylock_tests.bzl b/tests/pypi/pylock/pylock_tests.bzl new file mode 100644 index 0000000000..61ebe013ed --- /dev/null +++ b/tests/pypi/pylock/pylock_tests.bzl @@ -0,0 +1,158 @@ +"" + +load("@rules_testing//lib:test_suite.bzl", "test_suite") +load("@toml.bzl", "toml") +load("//python/private:repo_utils.bzl", "repo_utils") # buildifier: disable=bzl-visibility +load("//python/private/pypi:lock_model.bzl", "lock_requirements") # buildifier: disable=bzl-visibility +load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility +load("//python/private/pypi:pylock.bzl", "pylock_entries") # buildifier: disable=bzl-visibility + +_tests = [] + +def _platform(os, arch, whl_platform_tags): + return struct( + env = pep508_env(python_version = "3.12.4", os = os, arch = arch), + whl_abi_tags = ["cp312", "abi3", "none"], + whl_platform_tags = whl_platform_tags + ["any"], + ) + +_PLATFORMS = { + "linux_x86_64": _platform("linux", "x86_64", ["manylinux_*_x86_64"]), + "osx_aarch64": _platform("osx", "aarch64", ["macosx_*_arm64", "macosx_*_universal2"]), +} + +_PYLOCK = """\ +lock-version = "1.0" +created-by = "uv" +requires-python = ">=3.10" +extras = ["socks"] +default-groups = ["default"] +dependency-groups = ["dev"] + +[[packages]] +name = "requests" +version = "2.32.3" +index = "https://pypi.org/simple" +sdist = { url = "https://files.pythonhosted.org/requests-2.32.3.tar.gz", upload-time = 2024-05-29T15:37:49Z, size = 131218, hashes = { sha256 = "deadbeef" } } +wheels = [{ url = "https://files.pythonhosted.org/requests-2.32.3-py3-none-any.whl", upload-time = 2024-05-29T15:37:47Z, size = 64928, hashes = { sha256 = "deadbaaf" } }] + +[[packages]] +name = "pysocks" +version = "1.7.1" +marker = "'socks' in extras" +wheels = [{ name = "PySocks-1.7.1-py3-none-any.whl", url = "https://example.org/simple/pysocks/download", hashes = { sha256 = "deadb00f" } }] + +[[packages]] +name = "pytest" +version = "8.0.0" +marker = "'test' in dependency_groups" +wheels = [{ url = "https://example.org/pytest-8.0.0-py3-none-any.whl", hashes = { sha256 = "deadb11f" } }] + +[[packages]] +name = "uvloop" +version = "0.21.0" +marker = "sys_platform != 'win32' and 'dev' in dependency_groups" +requires-python = ">=3.8" +wheels = [ + { url = "https://example.org/uvloop-0.21.0-cp312-cp312-manylinux_2_17_x86_64.whl", hashes = { sha256 = "deadb22f" } }, + { url = "https://example.org/uvloop-0.21.0-cp312-cp312-macosx_10_13_universal2.whl", hashes = { sha256 = "deadb33f" } }, +] + +[[packages]] +name = "attrs" +version = "24.0" +marker = "'default' in dependency_groups" +requires-python = ">=3.8" +wheels = [{ url = "https://example.org/attrs-24.0-py3-none-any.whl?x=1#sha256=deadb44f", hashes = { sha256 = "deadb44f" } }] +""" + +def _logger(failures): + return repo_utils.logger(struct(getenv = {}.get), "unit-test", printer = lambda _, message: failures.append(message)) + +def _test_simple(env): + failures = [] + lock = pylock_entries( + toml.decode(_PYLOCK), + all_platforms = sorted(_PLATFORMS), + platforms = _PLATFORMS, + logger = _logger(failures), + _fail = failures.append, + ) + env.expect.that_collection(failures).contains_exactly([]) + env.expect.that_collection(lock.platforms).contains_exactly(sorted(_PLATFORMS)) + env.expect.that_collection([(e.name, e.target_platforms) for e in lock.entries]).contains_exactly([ + ("requests", ["linux_x86_64", "osx_aarch64"]), + ("pysocks", ["linux_x86_64", "osx_aarch64"]), + ("pytest", []), + ("uvloop", ["linux_x86_64", "osx_aarch64"]), + ("attrs", ["linux_x86_64", "osx_aarch64"]), + ]) + + got = lock_requirements( + lock.entries, + all_platforms = lock.platforms, + platforms = _PLATFORMS, + logger = _logger(failures), + ) + env.expect.that_collection([ + (r.name, r.index_url, r.is_exposed, [(s.requirement_line, s.filename, s.digest, s.target_platforms) for s in r.srcs]) + for r in got + ]).contains_exactly([ + ("attrs", "", True, [("attrs==24.0", "attrs-24.0-py3-none-any.whl", "sha256:deadb44f", ["linux_x86_64", "osx_aarch64"])]), + ("pysocks", "", True, [("pysocks==1.7.1", "PySocks-1.7.1-py3-none-any.whl", "sha256:deadb00f", ["linux_x86_64", "osx_aarch64"])]), + ("pytest", "", False, []), + ("requests", "https://pypi.org/simple/requests", True, [("requests==2.32.3", "requests-2.32.3-py3-none-any.whl", "sha256:deadbaaf", ["linux_x86_64", "osx_aarch64"])]), + ("uvloop", "", True, [ + ("uvloop==0.21.0", "uvloop-0.21.0-cp312-cp312-manylinux_2_17_x86_64.whl", "sha256:deadb22f", ["linux_x86_64"]), + ("uvloop==0.21.0", "uvloop-0.21.0-cp312-cp312-macosx_10_13_universal2.whl", "sha256:deadb33f", ["osx_aarch64"]), + ]), + ]) + +_tests.append(_test_simple) + +def _test_errors(env): + for contents, want in [ + ('lock-version = "2.0"', "Only the pylock.toml 'lock-version' 1.x is supported, got: '2.0'"), + ('lock-version = "1.0"\nrequires-python = ">=3.13"', "The pylock.toml 'requires-python = \">=3.13\"' does not match the Python version 3.12.4 of the 'linux_x86_64' platform"), + ('lock-version = "1.0"\n[[packages]]\nname = "foo"\nvcs = { type = "git", url = "https://example.org/foo.git", commit-id = "abc" }', "The pylock.toml package 'foo' has a 'vcs' source, which is not supported"), + ('lock-version = "1.0"\n[[packages]]\nname = "foo"', "The pylock.toml package 'foo' has no 'version'"), + ('lock-version = "1.0"\n[[packages]]\nname = "foo"\nversion = "1.0"\nrequires-python = "<3.12"', "The pylock.toml package 'foo' has 'requires-python = \"<3.12\"', which does not match the Python version 3.12.4 of the 'linux_x86_64' platform"), + ]: + failures = [] + pylock_entries( + toml.decode(contents), + all_platforms = sorted(_PLATFORMS), + platforms = _PLATFORMS, + logger = _logger(failures), + _fail = failures.append, + ) + env.expect.where(contents = contents).that_collection(failures).contains_exactly([want]) + + # The platforms that do not match the environments are ignored + failures = [] + lock = pylock_entries( + toml.decode('lock-version = "1.0"\nenvironments = ["sys_platform == \'linux\'"]\n[[packages]]\nname = "foo"\nversion = "1.0"\nwheels = [{ url = "https://example.org/foo-1.0-py3-none-any.whl" }]'), + all_platforms = sorted(_PLATFORMS), + platforms = _PLATFORMS, + logger = _logger(failures), + _fail = failures.append, + ) + env.expect.that_collection(lock.platforms).contains_exactly(["linux_x86_64"]) + env.expect.that_collection([e.target_platforms for e in lock.entries]).contains_exactly([["linux_x86_64"]]) + + # The package is exposed on all of the platforms that the lock supports + got = lock_requirements(lock.entries, all_platforms = lock.platforms, platforms = _PLATFORMS, logger = _logger(failures)) + env.expect.that_collection([r.is_exposed for r in got]).contains_exactly([True]) + env.expect.that_collection(failures).contains_exactly([ + "The pylock.toml 'environments' do not match the 'osx_aarch64' platform, ignoring it", + ]) + +_tests.append(_test_errors) + +def pylock_test_suite(name): + """Create the test suite. + + Args: + name: the name of the test suite + """ + test_suite(name = name, basic_tests = _tests) diff --git a/tests/pypi/requirements_files_by_platform/requirements_files_by_platform_tests.bzl b/tests/pypi/requirements_files_by_platform/requirements_files_by_platform_tests.bzl index 7f4260081b..31f81d04aa 100644 --- a/tests/pypi/requirements_files_by_platform/requirements_files_by_platform_tests.bzl +++ b/tests/pypi/requirements_files_by_platform/requirements_files_by_platform_tests.bzl @@ -43,7 +43,7 @@ def _test_fail_no_requirements(env): fail_fn = errors.append, ) env.expect.that_str(errors[0]).equals("""\ -A 'requirements_lock' or 'uv_lock' attribute must be specified, a platform-specific lockfiles via 'requirements_by_platform' or an os-specific lockfiles must be specified via 'requirements_*' attributes""") +A 'requirements_lock', 'uv_lock' or 'pylock' attribute must be specified, a platform-specific lockfiles via 'requirements_by_platform' or an os-specific lockfiles must be specified via 'requirements_*' attributes""") _tests.append(_test_fail_no_requirements)