Repository navigation
feat(pypi): add pip.configure to select the exposed packages of a hub #4395
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
base: main
Are you sure you want to change the base?
Changes from all commits
3ef53b2
7557370
23127e5
6bb7f83
b31a734
9d97360
35c0660
a2049ee
330eb71
7765a28
1ffebb8
a40d1d5
1291919
28b48d5
0f35bd1
7c58d7b
3d1f146
a49b6c8
c29740e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| (pypi) Added {obj}`pip.configure` to limit the packages that a hub exposes to | ||
| the ones listed in the dependency groups of a `pyproject.toml` file | ||
| ([#3413](https://github.com/bazel-contrib/rules_python/issues/3413)). |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| """Select PyPI packages using pyproject.toml dependency groups.""" | ||
|
|
||
| load("@bazel_skylib//lib:types.bzl", "types") | ||
| load("//python/private:normalize_name.bzl", "normalize_name") | ||
| load(":pep508_requirement.bzl", "requirement") | ||
|
|
||
| def select_dependency_groups(pyproject, specs, _fail = fail): | ||
| """Select the packages named by the given dependency group specifiers. | ||
|
|
||
| Supported specifiers are: | ||
|
rickeylev marked this conversation as resolved.
Resolved
|
||
|
|
||
| * `"default"`: the `[project] dependencies`. | ||
| * `"optional:<name>"`: a `[project.optional-dependencies]` entry. | ||
| * `"group:<name>"`: a PEP 735 `[dependency-groups]` entry. | ||
| * `"optional:*"` and `"group:*"`: all entries of the table. | ||
| * `"*"`: all of the above. | ||
| * `"<name>"`: a dependency group, or an optional dependency group if | ||
| there is no dependency group with that name. | ||
| * `"transitive"`: only supported together with `"*"` and selects all | ||
| of the packages, including the transitive dependencies. | ||
|
|
||
| Args: | ||
| pyproject: {type}`struct` returned by `read_pyproject`. | ||
| specs: {type}`list[str]` the specifiers. | ||
| _fail: {type}`callable` the failure function, mainly for testing. | ||
|
|
||
| Returns: | ||
| {type}`struct | None` with the attributes: | ||
| * `packages`: {type}`list[str]` the sorted normalized package names. | ||
| * `all`: {type}`bool` whether all of the packages including the | ||
| transitive dependencies should be selected, i.e. `"*"` and | ||
| `"transitive"` were given. | ||
| Returns `None` on failure. | ||
| """ | ||
| groups = {"default": pyproject.dependencies} | ||
| for name, reqs in pyproject.optional_dependencies.items(): | ||
| groups["optional:" + normalize_name(name)] = reqs | ||
| expanded = _expand_dependency_groups(pyproject.dependency_groups, _fail) | ||
| if expanded == None: | ||
| return None | ||
| for name, reqs in expanded.items(): | ||
| groups["group:" + name] = reqs | ||
|
|
||
| if "transitive" in specs: | ||
| if "*" not in specs: | ||
| _fail("The 'transitive' dependency group specifier is only supported together with '*', which selects all of the packages, got: {}".format( | ||
| specs, | ||
| )) | ||
| return None | ||
| return struct( | ||
| # list[str]: sorted normalized package names selected by `specs`. | ||
| packages = [], | ||
| # bool: True when all hub packages are selected. | ||
| all = True, | ||
| ) | ||
|
|
||
| selected = {} | ||
| for spec in specs: | ||
| keys = _match(spec, groups) | ||
|
|
||
| # A wildcard matches nothing when its table is empty or missing. | ||
| if not keys and spec not in ("*", "optional:*", "group:*"): | ||
| _fail("Unknown dependency group '{}'. Available groups: {}".format( | ||
| spec, | ||
| ", ".join(sorted(groups)), | ||
| )) | ||
| return None | ||
| for key in keys: | ||
| selected[key] = None | ||
|
Comment on lines
+59
to
+69
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Specifiers Suggested fix: Handle wildcard specifiers in select_dependency_groups before for spec in specs:
if spec in ("*", "optional:*", "group:*"):
prefix = "" if spec == "*" else spec[:-1]
for key in groups:
if key.startswith(prefix):
selected[key] = None
continue
key = _match(spec, groups)
if not key:
_fail("Unknown dependency group '{}'. Available groups: {}".format(
spec,
", ".join(sorted(groups)),
))
return None
selected[key] = NoneWhere: python/private/pypi/dependency_groups.bzl:72-91 This is an agent-generated comment reviewed and approved by the user.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I fixed this in c29740e: This is an agent-generated comment unreviewed by the user. |
||
|
|
||
| # A self reference like `my-project[test]` selects the optional | ||
| # dependencies, including nested composite extras. | ||
| project = normalize_name(pyproject.name or "") | ||
| packages = {} | ||
| queue = list(selected) | ||
| for _ in range(len(groups)): | ||
| if not queue: | ||
| break | ||
| for req in groups[queue.pop(0)]: | ||
| req = requirement(req) | ||
| if req.name_ != project: | ||
| packages[req.name_] = None | ||
| continue | ||
| for extra in req.extras: | ||
| opt_key = "optional:" + extra | ||
| if opt_key in groups and opt_key not in selected: | ||
| selected[opt_key] = None | ||
| queue.append(opt_key) | ||
|
|
||
| return struct( | ||
| # list[str]: sorted normalized package names selected by `specs`. | ||
| packages = sorted(packages), | ||
| # bool: True when all packages including transitive ones are selected. | ||
| all = False, | ||
| ) | ||
|
|
||
| def _match(spec, groups): | ||
|
rickeylev marked this conversation as resolved.
Resolved
|
||
| """Match a dependency group specifier against normalized group keys. | ||
|
|
||
| Args: | ||
| spec: {type}`str` the dependency group specifier. | ||
| groups: {type}`dict[str, list[str]]` available groups keyed by | ||
| prefixed group name. | ||
|
|
||
| Returns: | ||
| {type}`list[str]` matching keys in `groups`. | ||
| """ | ||
| if spec == "*": | ||
| return groups.keys() | ||
| if spec in ("optional:*", "group:*"): | ||
| prefix = spec[:-1] | ||
| return [key for key in groups if key.startswith(prefix)] | ||
|
|
||
| kind, _, name = spec.rpartition(":") | ||
| name = normalize_name(name) | ||
| if kind: | ||
| candidates = [kind + ":" + name] | ||
| elif name == "default": | ||
| candidates = ["default"] | ||
| else: | ||
| candidates = ["group:" + name, "optional:" + name] | ||
|
|
||
| for key in candidates: | ||
| if key in groups: | ||
| return [key] | ||
| return [] | ||
|
|
||
| def _expand_dependency_groups(dependency_groups, _fail): | ||
| """Expand the PEP 735 `include-group` entries. | ||
|
|
||
| Args: | ||
| dependency_groups: {type}`dict[str, list[str | dict[str, str]]]` the | ||
| `[dependency-groups]` table. | ||
| _fail: {type}`callable` the failure function. | ||
|
|
||
| Returns: | ||
| {type}`dict[str, list[str]] | None` normalized group names mapped to | ||
| their requirement strings, or `None` on failure. | ||
| """ | ||
| groups = { | ||
| normalize_name(name): items | ||
| for name, items in dependency_groups.items() | ||
| } | ||
|
|
||
| # Each round expands at least one group unless there is a cycle. | ||
| expanded = {} | ||
| for _ in range(len(groups)): | ||
| for name, items in groups.items(): | ||
| if name in expanded: | ||
| continue | ||
|
|
||
| reqs = [] | ||
| for item in items: | ||
| if types.is_string(item): | ||
| reqs.append(item) | ||
| continue | ||
|
|
||
| include = normalize_name(item.get("include-group", "")) | ||
| if include not in groups: | ||
| _fail("Unknown dependency group '{}' included from '{}'".format( | ||
| include, | ||
| name, | ||
| )) | ||
| return None | ||
| if include not in expanded: | ||
| reqs = None | ||
| break | ||
| reqs.extend(expanded[include]) | ||
|
|
||
| if reqs != None: | ||
| expanded[name] = reqs | ||
|
|
||
| if len(expanded) != len(groups): | ||
| unexpanded = [name for name in groups if name not in expanded] | ||
| _fail("Cyclic dependency group includes: {}".format( | ||
| ", ".join(sorted(unexpanded)), | ||
| )) | ||
| return None | ||
| return expanded | ||
Uh oh!
There was an error while loading. Please reload this page.