@@ -20,7 +20,6 @@ load("@bazel_features//:features.bzl", "bazel_features")
2020load ("//python/private:auth.bzl" , _get_auth = "get_auth" )
2121load ("//python/private:envsubst.bzl" , "envsubst" )
2222load ("//python/private:normalize_name.bzl" , "normalize_name" )
23- load ("//python/private:text_util.bzl" , "render" )
2423load (":parse_simpleapi_html.bzl" , "parse_simpleapi_html" )
2524
2625def simpleapi_download (
@@ -29,9 +28,7 @@ def simpleapi_download(
2928 attr ,
3029 cache ,
3130 parallel_download = True ,
32- read_simpleapi = None ,
33- get_auth = None ,
34- _fail = fail ):
31+ get_auth = None ):
3532 """Download Simple API HTML.
3633
3734 Args:
@@ -61,7 +58,6 @@ def simpleapi_download(
6158 read_simpleapi: a function for reading and parsing of the SimpleAPI contents.
6259 Used in tests.
6360 get_auth: A function to get auth information passed to read_simpleapi. Used in tests.
64- _fail: a function to print a failure. Used in tests.
6561
6662 Returns:
6763 dict of pkg name to the parsed HTML contents - a list of structs.
@@ -77,22 +73,15 @@ def simpleapi_download(
7773
7874 # NOTE @aignas 2024-03-31: we are not merging results from multiple indexes
7975 # to replicate how `pip` would handle this case.
76+ async_downloads = {}
8077 contents = {}
8178 index_urls = [attr .index_url ] + attr .extra_index_urls
82- read_simpleapi = read_simpleapi or _read_simpleapi
83-
84- found_on_index = {}
85- warn_overrides = False
86- for i , index_url in enumerate (index_urls ):
87- if i != 0 :
88- # Warn the user about a potential fix for the overrides
89- warn_overrides = True
90-
91- async_downloads = {}
92- sources = [pkg for pkg in attr .sources if pkg not in found_on_index ]
93- for pkg in sources :
94- pkg_normalized = normalize_name (pkg )
95- result = read_simpleapi (
79+ for pkg in attr .sources :
80+ pkg_normalized = normalize_name (pkg )
81+
82+ success = False
83+ for index_url in index_urls :
84+ result = _read_simpleapi (
9685 ctx = ctx ,
9786 url = "{}/{}/" .format (
9887 index_url_overrides .get (pkg_normalized , index_url ).rstrip ("/" ),
@@ -105,45 +94,42 @@ def simpleapi_download(
10594 )
10695 if hasattr (result , "wait" ):
10796 # We will process it in a separate loop:
108- async_downloads [pkg ] = struct (
109- pkg_normalized = pkg_normalized ,
110- wait = result .wait ,
97+ async_downloads .setdefault (pkg_normalized , []).append (
98+ struct (
99+ pkg_normalized = pkg_normalized ,
100+ wait = result .wait ,
101+ ),
111102 )
112- elif result .success :
113- contents [pkg_normalized ] = result .output
114- found_on_index [pkg ] = index_url
103+ continue
115104
116- if not async_downloads :
117- continue
118-
119- # If we use `block` == False, then we need to have a second loop that is
120- # collecting all of the results as they were being downloaded in parallel.
121- for pkg , download in async_downloads .items ():
105+ if result .success :
106+ contents [pkg_normalized ] = result .output
107+ success = True
108+ break
109+
110+ if not async_downloads and not success :
111+ fail ("Failed to download metadata from urls: {}" .format (
112+ ", " .join (index_urls ),
113+ ))
114+
115+ if not async_downloads :
116+ return contents
117+
118+ # If we use `block` == False, then we need to have a second loop that is
119+ # collecting all of the results as they were being downloaded in parallel.
120+ for pkg , downloads in async_downloads .items ():
121+ success = False
122+ for download in downloads :
122123 result = download .wait ()
123124
124- if result .success :
125+ if result .success and download . pkg_normalized not in contents :
125126 contents [download .pkg_normalized ] = result .output
126- found_on_index [pkg ] = index_url
127-
128- failed_sources = [pkg for pkg in attr .sources if pkg not in found_on_index ]
129- if failed_sources :
130- _fail ("Failed to download metadata for {} for from urls: {}" .format (
131- failed_sources ,
132- index_urls ,
133- ))
134- return None
135-
136- if warn_overrides :
137- index_url_overrides = {
138- pkg : found_on_index [pkg ]
139- for pkg in attr .sources
140- if found_on_index [pkg ] != attr .index_url
141- }
142-
143- # buildifier: disable=print
144- print ("You can use the following `index_url_overrides` to avoid the 404 warnings:\n {}" .format (
145- render .dict (index_url_overrides ),
146- ))
127+ success = True
128+
129+ if not success :
130+ fail ("Failed to download metadata from urls: {}" .format (
131+ ", " .join (index_urls ),
132+ ))
147133
148134 return contents
149135
0 commit comments