@@ -59,7 +59,19 @@ LIBRARY_ATTRS = dicts.add(
5959 PY_SRCS_ATTRS ,
6060 IMPORTS_ATTRS ,
6161 {
62- "site_packages_root" : lambda : attrb .String (
62+ "experimental_venv_site_packages" : lambda : attrb .Bool (
63+ doc = """
64+ Internal attribute. Should only be set by rules_python-internal code.
65+
66+ :::{include} /_includes/experimental_api.md
67+ :::
68+
69+ If true, the library consults {flag}`//python/config_settings:venv_site_packages`
70+ to decide if `srcs` is a site-packages relative layout.
71+ """ ,
72+ default = False ,
73+ ),
74+ "XXsite_packages_root" : lambda : attrb .String (
6375 doc = """
6476Package relative prefix to remove from `srcs` for site-packages layouts.
6577
@@ -156,16 +168,18 @@ def py_library_impl(ctx, *, semantics):
156168
157169 imports = []
158170 site_packages_symlinks = []
159- if ctx .attr .imports and ctx .attr .site_packages_root :
160- fail (("Only one of the `imports` or `site_packages_root` attributes " +
161- "can be set: site_packages_root={}, imports={}" ).format (
162- ctx .attr .site_packages_root ,
163- ctx .attr .imports ,
164- ))
165- elif ctx .attr .site_packages_root :
166- site_packages_symlinks = _get_site_packages_symlinks (ctx )
167- elif ctx .attr .imports :
168- imports = collect_imports (ctx , semantics )
171+
172+ ##if ctx.attr.imports and ctx.attr.site_packages_root:
173+ ## fail(("Only one of the `imports` or `site_packages_root` attributes " +
174+ ## "can be set: site_packages_root={}, imports={}").format(
175+ ## ctx.attr.site_packages_root,
176+ ## ctx.attr.imports,
177+ ## ))
178+ ##elif ctx.attr.site_packages_root:
179+ ## site_packages_symlinks = _get_site_packages_symlinks(ctx)
180+ ##elif ctx.attr.imports:
181+ ## imports = collect_imports(ctx, semantics)
182+ imports , site_packages_symlinks = _get_imports_and_site_packages_symlinks (ctx )
169183
170184 cc_info = semantics .get_cc_info_for_library (ctx )
171185 py_info , deps_transitive_sources , builtins_py_info = create_py_info (
@@ -214,9 +228,24 @@ Source files are no longer added to the runfiles directly.
214228:::
215229"""
216230
231+ def _get_imports_and_site_packages_symlinks (ctx , semantics ):
232+ imports = depset ()
233+ site_packages_symlinks = depset ()
234+ if (ctx .attr .experimental_venv_site_packages and
235+ ctx .attr ._venv_site_packages_flag [BuildSettingInfo ].value ):
236+ site_packages_symlinks = _get_site_packages_symlinks (ctx )
237+ else :
238+ imports = collect_imports (ctx , semantics )
239+ return imports , site_packages_symlinks
240+
217241def _get_site_packages_symlinks (ctx ):
218- if not ctx .attr .site_packages_root :
219- return []
242+ imports = ctx .attr .imports
243+ if len (imports ) == 0 :
244+ fail ("Must specify imports attr" )
245+ elif len (imports ) > 1 :
246+ fail ("Too many imports paths" )
247+ else :
248+ site_packages_root = imports [0 ]
220249
221250 # We have to build a list of (runfiles path, site-packages path) pairs of
222251 # the files to create in the consuming binary's venv site-packages directory.
@@ -233,7 +262,7 @@ def _get_site_packages_symlinks(ctx):
233262 # directories that _do_ have an `__init__.py` file and treat those as
234263 # the path to symlink to.
235264
236- site_packages_root = paths .join (ctx .label .package , ctx . attr . site_packages_root )
265+ site_packages_root = paths .join (ctx .label .package , site_packages_root )
237266 repo_runfiles_dirname = None
238267 dirs_with_init = {} # dirname -> runfile path
239268 for src in ctx .files .srcs :
0 commit comments