Skip to content

Commit 0d7908c

Browse files
committed
Merge branch 'refactor.optimize.venv.namespace.packages' into refactor.optimize.venv.pkgutil.namespace.packages
2 parents 247957c + 85ccfb4 commit 0d7908c

9 files changed

Lines changed: 31 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ END_UNRELEASED_TEMPLATE
118118
`RULES_PYTHON_ENABLE_PIPSTAR=1` by default. Users of `experimental_index_url` that perform
119119
cross-builds should add {obj}`target_platforms` to their `pip.parse` invocations, which will
120120
become mandatory if any cross-builds are required from the next release.
121+
* (py_library) Attribute {obj}`namespace_package_files` added. It is a hint for
122+
optimizing venv creation.
121123

122124
[20251031]: https://github.com/astral-sh/python-build-standalone/releases/tag/20251031
123125
[20251202]: https://github.com/astral-sh/python-build-standalone/releases/tag/20251202

python/private/internal_dev_deps.bzl

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,6 @@ def _internal_dev_deps_impl(mctx):
6767
enable_implicit_namespace_pkgs = False,
6868
)
6969

70-
whl_from_dir_repo(
71-
name = "pkgutil_namespace_whl",
72-
root = "//tests/pkgutil_namespace:testdata/ns-sub2/__init__.py",
73-
output = "my_pkgutil_ns-1.0-any-none-any.whl",
74-
)
75-
whl_library(
76-
name = "pkgutil_namespace",
77-
whl_file = "@pkgutil_namespace_whl//:my_pkgutil_ns-1.0-any-none-any.whl",
78-
requirement = "my-pkgutil-ns",
79-
enable_implicit_namespace_pkgs = False,
80-
)
81-
8270
whl_from_dir_repo(
8371
name = "pkgutil_nspkg1_whl",
8472
root = "//tests/repos/pkgutil_nspkg1:BUILD.bazel",

python/private/py_library.bzl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ LIBRARY_ATTRS = dicts.add(
5959
PY_SRCS_ATTRS,
6060
IMPORTS_ATTRS,
6161
{
62-
"namespace_package_files": lambda: attrb.LabelList(
63-
allow_empty = True,
64-
allow_files = True,
65-
doc = "The files that make up a namespace package.",
66-
),
6762
"experimental_venvs_site_packages": lambda: attrb.Label(
6863
doc = """
6964
**INTERNAL ATTRIBUTE. SHOULD ONLY BE SET BY rules_python-INTERNAL CODE.**
@@ -104,6 +99,20 @@ The topological order has been removed and if 2 different versions of the same P
10499
package are observed, the behaviour has no guarantees except that it is deterministic
105100
and that only one package version will be included.
106101
:::
102+
""",
103+
),
104+
"namespace_package_files": lambda: attrb.LabelList(
105+
allow_empty = True,
106+
allow_files = True,
107+
doc = """
108+
Files whose directories are namespace packages.
109+
110+
When {obj}`--venv_site_packages=yes` is set, this helps inform which directories should be
111+
treated as namespace packages and expect files from other targets to be contributed.
112+
This allows optimizing the generation of symlinks to be cheaper at analysis time.
113+
114+
:::{versionadded} VERSION_NEXT_FEATURE
115+
:::
107116
""",
108117
),
109118
"_add_srcs_to_runfiles_flag": lambda: attrb.Label(

python/private/pypi/whl_library.bzl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
1818
load("//python/private:auth.bzl", "AUTH_ATTRS", "get_auth")
1919
load("//python/private:envsubst.bzl", "envsubst")
2020
load("//python/private:is_standalone_interpreter.bzl", "is_standalone_interpreter")
21-
load("//python/private:py_internal.bzl", "py_internal")
2221
load("//python/private:repo_utils.bzl", "REPO_DEBUG_ENV_VAR", "repo_utils")
2322
load("//python/private:util.bzl", "is_importable_name")
2423
load(":attrs.bzl", "ATTRS", "use_isolated")
@@ -590,18 +589,13 @@ def _find_namespace_package_files(rctx, install_dir):
590589
"""
591590

592591
repo_root = str(rctx.path(".")) + "/"
593-
if "nvidia" in repo_root:
594-
print("============== nvidia")
595592
namespace_package_files = []
596-
print("install_dir:", install_dir)
597593
for top_level_dir in install_dir.readdir():
598-
print("tld:", top_level_dir)
599594
if not is_importable_name(top_level_dir.basename):
600595
continue
601596
init_py = top_level_dir.get_child("__init__.py")
602597
if not init_py.exists:
603598
continue
604-
print("found tld init:", init_py)
605599
content = rctx.read(init_py)
606600

607601
# Look for code resembling the pkgutil namespace setup code:

python/private/pypi/whl_library_targets.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ def whl_library_targets(
170170
enable_implicit_namespace_pkgs: {type}`boolean` generate __init__.py
171171
files for namespace pkgs.
172172
native: {type}`native` The native struct for overriding in tests.
173+
namespace_package_files: {type}`list[str]` A list of labels of files whose
174+
directories are namespace packages.
173175
rules: {type}`struct` A struct with references to rules for creating targets.
174176
"""
175177
dependencies = sorted([normalize_name(d) for d in dependencies])

python/private/venv_runfiles.bzl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ load(
1313
"VenvSymlinkEntry",
1414
"VenvSymlinkKind",
1515
)
16-
load(":py_internal.bzl", "py_internal")
1716
load(":util.bzl", "is_importable_name")
1817

1918
# List of top-level package names that are known to be namespace
@@ -140,8 +139,6 @@ def build_link_map(ctx, entries, return_conflicts = False):
140139
else:
141140
keep_kind_link_map[entry.venv_path] = entry.link_to_path
142141
else:
143-
for v in group:
144-
print("conflict:", v.venv_path, v.link_to_path)
145142
if return_conflicts:
146143
conflicts.append(group)
147144

@@ -313,18 +310,14 @@ def get_venv_symlinks(
313310
dirname: True
314311
for dirname in namespace_package_dirs.keys()
315312
}
316-
print("npf:::", ctx.label, namespace_package_files)
317313
for f in namespace_package_files:
318314
venv_path, _ = _get_file_venv_path(ctx, f, site_packages_root)
319315
if venv_path == None:
320316
continue
321317
ns_dir = paths.dirname(venv_path)
322-
print("nsdir:", ns_dir)
323318
namespace_package_dirs[ns_dir] = True
324319
cannot_be_linked_directly[ns_dir] = True
325320

326-
print("all npd:", namespace_package_dirs)
327-
328321
# dict[str path, VenvSymlinkEntry]
329322
# Where path is the venv path (i.e. relative to site_packages_prefix)
330323
venv_symlinks = {}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# __init__.py
2-
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
2+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# __init__.py
2-
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
2+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)

tests/venv_site_packages_libs/app_files_building/app_files_building_tests.bzl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
44
load("@rules_testing//lib:test_suite.bzl", "test_suite")
55
load("//python:py_info.bzl", "PyInfo")
66
load("//python:py_library.bzl", "py_library")
7-
load("//python/private:common_labels.bzl", "labels")
7+
load("//python/private:common_labels.bzl", "labels") # buildifier: disable=bzl-visibility
88
load("//python/private:py_info.bzl", "VenvSymlinkEntry", "VenvSymlinkKind") # buildifier: disable=bzl-visibility
99
load("//python/private:venv_runfiles.bzl", "build_link_map", "get_venv_symlinks") # buildifier: disable=bzl-visibility
1010

@@ -340,6 +340,9 @@ def _test_optimized_grouping_pkgutil_namespace_packages(name):
340340
paths = [
341341
"site-packages/pkgutilns/__init__.py",
342342
"site-packages/pkgutilns/foo.py",
343+
# Special cases: These dirnames under site-packages are always
344+
# treated as namespace packages
345+
"site-packages/nvidia/whatever/w.py",
343346
],
344347
)
345348
analysis_test(
@@ -353,7 +356,6 @@ _tests.append(_test_optimized_grouping_pkgutil_namespace_packages)
353356
def _test_optimized_grouping_pkgutil_namespace_packages_impl(env, target):
354357
test_ctx = _ctx(workspace_name = env.ctx.workspace_name)
355358
files = target.files.to_list()
356-
pkgutilns_init_py = None
357359
ns_inits = [f for f in files if f.basename == "__init__.py"]
358360

359361
entries = get_venv_symlinks(
@@ -382,6 +384,13 @@ def _test_optimized_grouping_pkgutil_namespace_packages_impl(env, target):
382384
"tests/venv_site_packages_libs/app_files_building/site-packages/pkgutilns/foo.py",
383385
],
384386
),
387+
_venv_symlink(
388+
"nvidia/whatever",
389+
link_to_path = rr + "nvidia/whatever",
390+
files = [
391+
"tests/venv_site_packages_libs/app_files_building/site-packages/nvidia/whatever/w.py",
392+
],
393+
),
385394
]
386395
expected = sorted(expected, key = lambda e: (e.link_to_path, e.venv_path))
387396
env.expect.that_collection(

0 commit comments

Comments
 (0)