@@ -228,6 +228,61 @@ def _create_repository_execution_environment(rctx, python_interpreter, logger =
228228 env [_CPPFLAGS ] = " " .join (cppflags )
229229 return env
230230
231+ def _read_and_parse_required_metadata (rctx , logger ):
232+ contents = []
233+ for entry in rctx .path ("site-packages" ).readdir ():
234+ if not entry .basename .endswith (".dist-info" ):
235+ continue
236+
237+ if not entry .is_dir :
238+ continue
239+
240+ metadata_file = entry .get_child ("METADATA" )
241+
242+ if not metadata_file .exists :
243+ logger .fail ("The METADATA file for the wheel could not be found" )
244+ return None
245+
246+ contents .extend (rctx .read (metadata_file ).split ("\n " ))
247+ break
248+
249+ single_value_fields = {
250+ "License: " : "license" ,
251+ "Name: " : "name" ,
252+ "Version: " : "version" ,
253+ }
254+ requires_dist = "Requires-Dist: "
255+ parsed = {}
256+ for line in contents :
257+ if not line or line .startswith ("Dynamic" ):
258+ # Stop parsing on first empty line
259+ break
260+
261+ found_prefix = None
262+ for prefix in single_value_fields :
263+ if line .startswith (prefix ):
264+ found_prefix = prefix
265+ break
266+
267+ if found_prefix :
268+ key = single_value_fields .pop (found_prefix )
269+ _ , _ , value = line .partition (found_prefix )
270+ parsed [key ] = value .strip ()
271+ continue
272+
273+ if not line .startswith (requires_dist ):
274+ continue
275+
276+ _ , _ , value = line .partition (requires_dist )
277+ parsed .setdefault ("requires_dist" , []).append (value .strip (" " ))
278+
279+ return struct (
280+ name = parsed ["name" ],
281+ version = parsed ["version" ],
282+ license = parsed .get ("license" ),
283+ requires_dist = parsed .get ("requires_dist" , []),
284+ )
285+
231286def _whl_library_impl (rctx ):
232287 logger = repo_utils .logger (rctx )
233288 python_interpreter = pypi_repo_utils .resolve_python_interpreter (
@@ -400,7 +455,7 @@ def _whl_library_impl(rctx):
400455 #
401456 # This means that whl_library_targets will have to accept the following args:
402457 # * name - the name of the package in the METADATA.
403- # * requires_dist - the list of METADATA RequiresDist .
458+ # * requires_dist - the list of METADATA Requires-Dist .
404459 # * platforms - the list of target platforms. The target_platforms
405460 # should come from the hub repo via a 'load' statement so that they don't
406461 # need to be passed as an argument to `whl_library`.
@@ -419,20 +474,19 @@ def _whl_library_impl(rctx):
419474 # * group_name, group_deps - this info can stay in the hub repository so that
420475 # it is piped at the analysis time and changing the requirement groups does
421476 # cause to re-fetch the deps.
477+ whl_metadata = _read_and_parse_required_metadata (rctx , logger )
478+ python_version = metadata ["python_version" ]
479+
480+ # TODO @aignas 2025-04-09: this will later be removed when loaded through the hub
481+ major_minor , _ , _ = python_version .rpartition ("." )
422482 package_deps = deps (
423- # TODO @aignas 2025-04-04: get the following from manually parsing
424- # METADATA to avoid Python dependency:
425- # * name of the package
426- # * version of the package
427- # * RequiresDist
428- # * ProvidesExtras
429- name = metadata ["name" ],
430- requires_dist = metadata ["requires_dist" ],
483+ name = whl_metadata .name ,
484+ requires_dist = whl_metadata .requires_dist ,
431485 platforms = target_platforms or [
432- "{}_{}" .format (metadata [ "abi" ] , host_platform (rctx )),
486+ "cp {}_{}" .format (major_minor . replace ( "." , "" ) , host_platform (rctx )),
433487 ],
434488 extras = requirement (rctx .attr .requirement ).extras ,
435- host_python_version = metadata [ " python_version" ] ,
489+ host_python_version = python_version ,
436490 )
437491
438492 build_file_contents = generate_whl_library_build_bazel (
@@ -444,8 +498,8 @@ def _whl_library_impl(rctx):
444498 group_deps = rctx .attr .group_deps ,
445499 data_exclude = rctx .attr .pip_data_exclude ,
446500 tags = [
447- "pypi_name=" + metadata [ " name" ] ,
448- "pypi_version=" + metadata [ " version" ] ,
501+ "pypi_name=" + whl_metadata . name ,
502+ "pypi_version=" + whl_metadata . version ,
449503 ],
450504 entry_points = entry_points ,
451505 annotation = None if not rctx .attr .annotation else struct (** json .decode (rctx .read (rctx .attr .annotation ))),
0 commit comments