@@ -20,7 +20,6 @@ load("@bazel_features//:features.bzl", "bazel_features")
20
20
load ("//python/private:auth.bzl" , _get_auth = "get_auth" )
21
21
load ("//python/private:envsubst.bzl" , "envsubst" )
22
22
load ("//python/private:normalize_name.bzl" , "normalize_name" )
23
- load ("//python/private:text_util.bzl" , "render" )
24
23
load (":parse_simpleapi_html.bzl" , "parse_simpleapi_html" )
25
24
26
25
def simpleapi_download (
@@ -29,9 +28,7 @@ def simpleapi_download(
29
28
attr ,
30
29
cache ,
31
30
parallel_download = True ,
32
- read_simpleapi = None ,
33
- get_auth = None ,
34
- _fail = fail ):
31
+ get_auth = None ):
35
32
"""Download Simple API HTML.
36
33
37
34
Args:
@@ -61,7 +58,6 @@ def simpleapi_download(
61
58
read_simpleapi: a function for reading and parsing of the SimpleAPI contents.
62
59
Used in tests.
63
60
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.
65
61
66
62
Returns:
67
63
dict of pkg name to the parsed HTML contents - a list of structs.
@@ -77,22 +73,15 @@ def simpleapi_download(
77
73
78
74
# NOTE @aignas 2024-03-31: we are not merging results from multiple indexes
79
75
# to replicate how `pip` would handle this case.
76
+ async_downloads = {}
80
77
contents = {}
81
78
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 (
96
85
ctx = ctx ,
97
86
url = "{}/{}/" .format (
98
87
index_url_overrides .get (pkg_normalized , index_url ).rstrip ("/" ),
@@ -105,45 +94,42 @@ def simpleapi_download(
105
94
)
106
95
if hasattr (result , "wait" ):
107
96
# 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
+ ),
111
102
)
112
- elif result .success :
113
- contents [pkg_normalized ] = result .output
114
- found_on_index [pkg ] = index_url
103
+ continue
115
104
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 :
122
123
result = download .wait ()
123
124
124
- if result .success :
125
+ if result .success and download . pkg_normalized not in contents :
125
126
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
+ ))
147
133
148
134
return contents
149
135
0 commit comments