@@ -159,18 +159,8 @@ def py_library_impl(ctx, *, semantics):
159159 imports = []
160160 venv_symlinks = []
161161
162- # TODO @aignas 2025-06-05: refactor code
163- package = None
164- dist_info_metadata = _get_distinfo_metadata (ctx )
165- if dist_info_metadata :
166- # in order to be able to have replacements in the venv, we have to add a
167- # third value into the venv_symlinks, which would be the normalized
168- # package name. This allows us to ensure that we can replace the `dist-info`
169- # directories by checking if the package key is there.
170- dist_info_dir = paths .basename (dist_info_metadata .dirname )
171- package , _ , _suffix = dist_info_dir .rpartition (".dist-info" )
172-
173- imports , venv_symlinks = _get_imports_and_venv_symlinks (ctx , semantics )
162+ package , version_str = _get_package_and_version (ctx )
163+ imports , venv_symlinks = _get_imports_and_venv_symlinks (ctx , semantics , package , version_str )
174164
175165 cc_info = semantics .get_cc_info_for_library (ctx )
176166 py_info , deps_transitive_sources , builtins_py_info = create_py_info (
@@ -220,28 +210,47 @@ Source files are no longer added to the runfiles directly.
220210:::
221211"""
222212
223- def _get_distinfo_metadata (ctx ):
224- data = ctx .files .data or []
225- for d in data :
213+ def _get_package_and_version (ctx ):
214+ """Return package name and version
215+
216+ If the package comes from PyPI then it will have a `.dist-info` as part of `data`, which
217+ allows us to get the name of the package and its version. This means that we can ensure
218+ that package usage closer to the terminal node can override dependencies.
219+ """
220+ dist_info_metadata = None
221+ for d in ctx .files .data :
226222 # work on case insensitive FSes
227223 if d .basename .lower () != "metadata" :
228224 continue
229225
230226 if d .dirname .endswith (".dist-info" ):
231- return d
232-
233- return None
227+ dist_info_metadata = d
228+
229+ if not dist_info_metadata :
230+ return None , None
231+
232+ # in order to be able to have replacements in the venv, we have to add a
233+ # third value into the venv_symlinks, which would be the normalized
234+ # package name. This allows us to ensure that we can replace the `dist-info`
235+ # directories by checking if the package key is there.
236+ dist_info_dir = paths .basename (dist_info_metadata .dirname )
237+ package , _ , _suffix = dist_info_dir .rpartition (".dist-info" )
238+ package , _ , version_str = package .rpartition ("-" )
239+ return (
240+ normalize_name (package ), # will have no dashes
241+ version .normalize (version_str ), # will have no dashes either
242+ )
234243
235- def _get_imports_and_venv_symlinks (ctx , semantics ):
244+ def _get_imports_and_venv_symlinks (ctx , semantics , package , version_str ):
236245 imports = depset ()
237246 venv_symlinks = []
238247 if VenvsSitePackages .is_enabled (ctx ):
239- venv_symlinks = _get_venv_symlinks (ctx )
248+ venv_symlinks = _get_venv_symlinks (ctx , package , version_str )
240249 else :
241250 imports = collect_imports (ctx , semantics )
242251 return imports , venv_symlinks
243252
244- def _get_venv_symlinks (ctx ):
253+ def _get_venv_symlinks (ctx , package , version_str ):
245254 imports = ctx .attr .imports
246255 if len (imports ) == 0 :
247256 fail ("When venvs_site_packages is enabled, exactly one `imports` " +
@@ -262,26 +271,6 @@ def _get_venv_symlinks(ctx):
262271 # Append slash to prevent incorrectly prefix-string matches
263272 site_packages_root += "/"
264273
265- # If the package comes from PyPI then it will have a `.dist-info` as part of `data`, which
266- # allows us to get the name of the package and its version. This means that we can ensure that
267- # package usage closer to the terminal node can override dependencies.
268-
269- package = None
270- version_str = None
271- dist_info_metadata = _get_distinfo_metadata (ctx )
272- if dist_info_metadata :
273- # in order to be able to have replacements in the venv, we have to add a
274- # third value into the venv_symlinks, which would be the normalized
275- # package name. This allows us to ensure that we can replace the `dist-info`
276- # directories by checking if the package key is there.
277- dist_info_dir = paths .basename (dist_info_metadata .dirname )
278- package , _ , _suffix = dist_info_dir .rpartition (".dist-info" )
279- package , _ , version_str = package .rpartition ("-" )
280- package , version_str = (
281- normalize_name (package ), # will have no dashes
282- version .normalize (version_str ), # will have no dashes either
283- )
284-
285274 # We have to build a list of (runfiles path, site-packages path) pairs of the files to
286275 # create in the consuming binary's venv site-packages directory. To minimize the number of
287276 # files to create, we just return the paths to the directories containing the code of
0 commit comments