@@ -332,6 +332,12 @@ def _whl_extract(rctx, *, whl_path, logger, sdist_filename = None):
332332 read_fn = rctx .read ,
333333 logger = logger ,
334334 )
335+ rctx .file ("metadata.json" , json .encode_indent ({
336+ "name" : metadata .name ,
337+ "provides_extra" : metadata .provides_extra ,
338+ "requires_dist" : metadata .requires_dist ,
339+ "version" : metadata .version ,
340+ }))
335341 namespace_package_files = pypi_repo_utils .find_namespace_package_files (rctx , install_dir_path )
336342
337343 entry_points = _get_entry_points (rctx , install_dir_path , metadata )
@@ -429,6 +435,8 @@ def _whl_archive_impl(rctx):
429435 whl_path = rctx .path (filename )
430436 else :
431437 fail ("Only wheels are supported" )
438+ else :
439+ fail ("Either 'whl_file' or 'urls' and 'filename' needs to be specified" )
432440
433441 return _whl_extract (rctx , whl_path = whl_path , logger = logger )
434442
@@ -571,9 +579,6 @@ For example if your whl depends on `numpy` and your Python package repo is named
571579 "index_url" : attr .string (
572580 doc = "The index_url that the package will be downloaded from." ,
573581 ),
574- "repo" : attr .string (
575- doc = "Pointer to parent repo name. Used to make these rules rerun if the parent repo changes." ,
576- ),
577582 "repo_prefix" : attr .string (
578583 doc = """
579584Prefix for the generated packages will be of the form `@<prefix><sanitized-package-name>//...`
@@ -676,7 +681,6 @@ whl_archive = repository_rule(
676681 "group_deps" ,
677682 "group_name" ,
678683 "index_url" ,
679- "repo" ,
680684 "repo_prefix" ,
681685 "requirement" ,
682686 "sha256" ,
@@ -702,7 +706,74 @@ Does not depend on any python.
702706 ],
703707)
704708
705- def whl_library (name , ** kwargs ):
709+ def _whl_deps_library_impl (rctx ):
710+ logger = repo_utils .logger (rctx )
711+
712+ if rctx .attr .metadata_file and rctx .attr .metadata :
713+ logger .fail ("Only one of 'metadata_file' and 'metadata' can be specified" )
714+ return
715+ if not (rctx .attr .metadata_file or rctx .attr .metadata ):
716+ logger .fail ("At least one of 'metadata_file' and 'metadata' must be specified" )
717+ return
718+
719+ if rctx .attr .metadata_file :
720+ metadata_contents = rctx .read (rctx .attr .metadata_file )
721+ else :
722+ metadata_contents = rctx .attr .metadata
723+
724+ metadata = struct (** json .decode (metadata_contents ))
725+
726+ build_file_contents = generate_whl_library_build_bazel (
727+ dep_template = rctx .attr .dep_template or "@{}{{name}}//:{{target}}" .format (
728+ rctx .attr .repo_prefix ,
729+ ),
730+ config_load = rctx .attr .config_load ,
731+ metadata_name = metadata .name ,
732+ metadata_version = metadata .version ,
733+ requires_dist = metadata .requires_dist ,
734+ group_deps = rctx .attr .group_deps ,
735+ group_name = rctx .attr .group_name ,
736+ repo = rctx .attr .repo or (
737+ str (rctx .attr .metadata_file ) if rctx .attr .metadata_file else None
738+ ),
739+ extras = requirement (rctx .attr .requirement ).extras ,
740+ )
741+ rctx .file ("BUILD.bazel" , build_file_contents )
742+
743+ whl_deps_library = repository_rule (
744+ attrs = {
745+ k : _pip_archive_attrs [k ]
746+ for k in [
747+ "config_load" ,
748+ "dep_template" ,
749+ "group_deps" ,
750+ "group_name" ,
751+ "requirement" ,
752+ ]
753+ } | {
754+ "metadata" : attr .string (
755+ doc = """
756+ The subset of the METADATA contents that is needed for generation of the dependencies.
757+ * name: {type}`str`
758+ * version: {type}`str`
759+ * provides_extra: {type}`list[str]`
760+ * requires_dist: {type}`list[str]`
761+ """ ,
762+ ),
763+ "metadata_file" : attr .label (doc = "An alternative way to pass {attr}`metadata` but as a file." ),
764+ "repo" : attr .label (doc = "A label at the root of the repo to get stuff from." ),
765+ },
766+ doc = """
767+ A repo rule that reuses the sources from a different place and then creates the necessary targets
768+ so that this can be used in the repo.
769+
770+ Does not depend on any python.
771+ """ ,
772+ implementation = _whl_deps_library_impl ,
773+ environ = [REPO_DEBUG_ENV_VAR ],
774+ )
775+
776+ def whl_library (name , repo = None , ** kwargs ):
706777 """Create a whl_library.
707778
708779 This proxies to one of the underlying implementations:
@@ -711,15 +782,18 @@ def whl_library(name, **kwargs):
711782
712783 Args:
713784 name: {type}`str` The name of the repo.
785+ repo: Unused, will be dropped in the next major release.
714786 **kwargs: The args passed to the underlying implementation.
715787
716788 Returns:
717789 the repo metadata.
718790 """
791+ _ = repo # buildifier: disable=unused-variable
792+
719793 whl_file = kwargs .get ("whl_file" )
720794 urls = kwargs .get ("urls" , [])
721795 filename = kwargs .get ("filename" )
722796 if whl_file or (urls and filename and filename .endswith (".whl" )):
723- return whl_archive (name = name , ** kwargs )
724-
725- return pip_archive (name = name , ** kwargs )
797+ whl_archive (name = name , ** kwargs )
798+ else :
799+ pip_archive (name = name , ** kwargs )
0 commit comments