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
11 changes: 6 additions & 5 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,12 @@ bzl_library(
],
)

bzl_library(
name = "labels",
srcs = ["labels.bzl"],
deps = ["//python/private:normalize_name"],
)

bzl_library(
name = "argparse",
srcs = ["argparse.bzl"],
Expand All @@ -654,11 +660,6 @@ bzl_library(
srcs = ["hash.bzl"],
)

bzl_library(
name = "labels",
srcs = ["labels.bzl"],
)

bzl_library(
name = "package_annotation",
srcs = ["package_annotation.bzl"],
Expand Down
29 changes: 29 additions & 0 deletions python/private/pypi/labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Constants used by parts of pip_repository for naming libraries and wheels."""

load("//python/private:normalize_name.bzl", "normalize_name")

EXTRACTED_WHEEL_FILES = "extracted_whl_files"
WHEEL_FILE_PUBLIC_LABEL = "whl"
WHEEL_FILE_IMPL_LABEL = "_whl"
Expand All @@ -24,3 +26,30 @@ PY_SRCS_LABEL = "srcs"
DATA_LABEL = "data"
DIST_INFO_LABEL = "dist_info"
NODEPS_LABEL = "no_deps"

def extra_label(extra):
"""Return the label name for the target that includes an extra.

Args:
extra: {type}`str` the normalized name of the extra, an empty string
means the base package without any extras.

Returns:
The target name, e.g. `[extra]`.
"""
return "[{}]".format(extra)

def group_member(spec):
"""Normalize a member of a dependency group.

Args:
spec: {type}`str` the package name, e.g. `Foo`, or the package name
with a single extra, e.g. `Foo[Bar]`.

Returns:
The normalized member, e.g. `foo` or `foo[bar]`.
"""
name, _, extra = spec.partition("[")
name = normalize_name(name.strip())
extra = normalize_name(extra.partition("]")[0].strip()) if extra else ""
return "{}[{}]".format(name, extra) if extra else name
31 changes: 19 additions & 12 deletions python/private/pypi/pep508_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,32 @@ def deps(
requires_dist,
extras = [],
excludes = [],
include = []):
include = [],
keep_extras = False):
"""Parse the RequiresDist from wheel METADATA

Args:
name: {type}`str` the name of the wheel.
requires_dist: {type}`list[str]` the list of RequiresDist lines from the
METADATA file.
excludes: {type}`list[str]` what packages should we exclude.
excludes: {type}`list[str]` what dependencies should we exclude, e.g.
`foo` or `foo[bar]`, in the same format as the returned values.
include: {type}`list[str]` what packages should we exclude. If it is not
specified, then we will include all deps from `requires_dist`.
extras: {type}`list[str]` the requested extras to generate targets for.
keep_extras: {type}`bool` keep the extras of the dependencies, if
`True`, the dependencies with extras are returned as `foo[bar]`.

Returns:
A struct with attributes:
* deps: {type}`list[str]` dependencies to include unconditionally.
* deps_select: {type}`dict[str, list[str]]` dependencies to include on particular
subset of target platforms.

The dependencies are normalized names, e.g. `foo`, or normalized names
with a single extra, e.g. `foo[bar]`, if the dependency is requested
with extras and `keep_extras` is set. A dependency with multiple
extras, e.g. `foo[bar,baz]`, is returned as `foo[bar]` and `foo[baz]`.
"""
if not requires_dist:
return struct(
Expand All @@ -60,25 +69,25 @@ def deps(
want_extras = _resolve_extras(name, reqs, extras)
include = [normalize_name(n) for n in include]

# drop self edges
excludes = [name] + [normalize_name(x) for x in excludes]

reqs_by_name = {}
reqs_by_dep = {}

for req in reqs:
if req.name_ in excludes:
# drop self edges
if req.name_ == name:
continue

if include and req.name_ not in include:
continue

reqs_by_name.setdefault(req.name, []).append(req)
for dep in ["{}[{}]".format(req.name_, e) for e in req.extras if keep_extras] or [req.name_]:
if dep not in excludes:
reqs_by_dep.setdefault(dep, []).append(req)

for name, reqs in reqs_by_name.items():
for dep, reqs in reqs_by_dep.items():
_add_reqs(
deps,
deps_select,
normalize_name(name),
dep,
reqs,
extras = want_extras,
)
Expand All @@ -92,8 +101,6 @@ def deps(
)

def _add(deps, deps_select, dep, markers = None):
dep = normalize_name(dep)

if not markers:
deps[dep] = True
elif len(markers) == 1:
Expand Down
19 changes: 19 additions & 0 deletions python/private/pypi/pep508_requirement.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,22 @@ def requirement(spec):
extras = extras,
version = version,
)

def strip_extras(requirement_line):
"""Remove the extras from the requirement line.

Args:
requirement_line: {type}`str` the requirement line, e.g.
`foo[bar]==1.0 --hash=sha256:...`.

Returns:
The requirement line without the extras, e.g. `foo==1.0 --hash=sha256:...`.
"""
for i, c in enumerate(requirement_line.elems()):
if c in "=<>!~;@( ":
break
if c == "[":
_, _, tail = requirement_line[i:].partition("]")
return requirement_line[:i] + tail

return requirement_line
37 changes: 37 additions & 0 deletions tests/pypi/pep508/deps_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,43 @@ def test_extras_with_hyphens_are_normalized(env):

_tests.append(test_extras_with_hyphens_are_normalized)

def test_deps_with_extras(env):
got = deps(
"foo",
requires_dist = [
"bar[Socks]",
"baz[a,b]; sys_platform == 'linux'",
"qux",
"qux[c]; extra == 'feat'",
],
extras = ["feat"],
keep_extras = True,
)

# The extras of the dependencies are kept so that the targets with the
# extras can be used.
env.expect.that_collection(got.deps).contains_exactly(["bar[socks]", "qux", "qux[c]"])
env.expect.that_dict(got.deps_select).contains_exactly({
"baz[a]": "sys_platform == \"linux\"",
"baz[b]": "sys_platform == \"linux\"",
})

_tests.append(test_deps_with_extras)

def test_deps_with_extras_excludes_and_includes(env):
got = deps(
"foo",
requires_dist = ["bar", "bar[socks]", "baz[a]", "baz[b]", "qux[b]"],
# The excludes are dependencies and the includes are packages
excludes = ["bar", "baz[a]"],
include = ["bar", "baz"],
keep_extras = True,
)

env.expect.that_collection(got.deps).contains_exactly(["bar[socks]", "baz[b]"])

_tests.append(test_deps_with_extras_excludes_and_includes)

def deps_test_suite(name): # buildifier: disable=function-docstring
test_suite(
name = name,
Expand Down
15 changes: 14 additions & 1 deletion tests/pypi/pep508/requirement_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""Tests for parsing the requirement specifier."""

load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private/pypi:pep508_requirement.bzl", "requirement") # buildifier: disable=bzl-visibility
load("//python/private/pypi:pep508_requirement.bzl", "requirement", "strip_extras") # buildifier: disable=bzl-visibility

_tests = []

Expand Down Expand Up @@ -42,6 +42,19 @@ def _test_requirement_line_parsing(env):

_tests.append(_test_requirement_line_parsing)

def _test_strip_extras(env):
for line, want in {
"foo": "foo",
"foo @ https://example.org/foo[1].whl": "foo @ https://example.org/foo[1].whl",
"foo==1.0 ; python_version < '3.12' --hash=sha256:deadbeef": "foo==1.0 ; python_version < '3.12' --hash=sha256:deadbeef",
"foo[bar,baz] @ https://example.org/foo.whl": "foo @ https://example.org/foo.whl",
"foo[bar]": "foo",
"foo[bar]==1.0 --hash=sha256:deadbeef": "foo==1.0 --hash=sha256:deadbeef",
}.items():
env.expect.where(line = line).that_str(strip_extras(line)).equals(want)

_tests.append(_test_strip_extras)

def requirement_test_suite(name): # buildifier: disable=function-docstring
test_suite(
name = name,
Expand Down
Loading