@@ -23,80 +23,13 @@ load("//python/private:normalize_name.bzl", "normalize_name")
2323load ("//python/private:text_util.bzl" , "render" )
2424load (":parse_simpleapi_html.bzl" , "parse_simpleapi_html" )
2525
26- def _read_simpleapi (ctx , url , attr , cache , ** download_kwargs ):
27- """Read SimpleAPI.
28-
29- Args:
30- ctx: The module_ctx or repository_ctx.
31- url: str, the url parameter that can be passed to ctx.download.
32- attr: The attribute that contains necessary info for downloading. The
33- following attributes must be present:
34- * envsubst: The envsubst values for performing substitutions in the URL.
35- * netrc: The netrc parameter for ctx.download, see http_file for docs.
36- * auth_patterns: The auth_patterns parameter for ctx.download, see
37- http_file for docs.
38- cache: A dict for storing the results.
39- **download_kwargs: Any extra params to ctx.download.
40- Note that output and auth will be passed for you.
41-
42- Returns:
43- A similar object to what `download` would return except that in result.out
44- will be the parsed simple api contents.
45- """
46- # NOTE @aignas 2024-03-31: some of the simple APIs use relative URLs for
47- # the whl location and we cannot handle multiple URLs at once by passing
48- # them to ctx.download if we want to correctly handle the relative URLs.
49- # TODO: Add a test that env subbed index urls do not leak into the lock file.
50-
51- real_url = envsubst (
52- url ,
53- attr .envsubst ,
54- ctx .getenv if hasattr (ctx , "getenv" ) else ctx .os .environ .get ,
55- )
56-
57- cache_key = real_url
58- if cache_key in cache :
59- return struct (success = True , output = cache [cache_key ])
60-
61- output_str = envsubst (
62- url ,
63- attr .envsubst ,
64- # Use env names in the subst values - this will be unique over
65- # the lifetime of the execution of this function and we also use
66- # `~` as the separator to ensure that we don't get clashes.
67- {e : "~{}~" .format (e ) for e in attr .envsubst }.get ,
68- )
69-
70- # Transform the URL into a valid filename
71- for char in ["." , ":" , "/" , "\\ " , "-" ]:
72- output_str = output_str .replace (char , "_" )
73-
74- output = ctx .path (output_str .strip ("_" ).lower () + ".html" )
75-
76- # NOTE: this may have block = True or block = False in the download_kwargs
77- download = ctx .download (
78- url = [real_url ],
79- output = output ,
80- auth = get_auth (ctx , [real_url ], ctx_attr = attr ),
81- allow_fail = True ,
82- ** download_kwargs
83- )
84-
85- if download_kwargs .get ("block" ) == False :
86- # Simulate the same API as ctx.download has
87- return struct (
88- wait = lambda : _read_index_result (ctx , download .wait (), output , real_url , cache , cache_key ),
89- )
90-
91- return _read_index_result (ctx , download , output , real_url , cache , cache_key )
92-
9326def simpleapi_download (
9427 ctx ,
9528 * ,
9629 attr ,
9730 cache ,
9831 parallel_download = True ,
99- read_simpleapi = _read_simpleapi ,
32+ read_simpleapi = None ,
10033 _fail = fail ):
10134 """Download Simple API HTML.
10235
@@ -144,6 +77,7 @@ def simpleapi_download(
14477 # to replicate how `pip` would handle this case.
14578 contents = {}
14679 index_urls = [attr .index_url ] + attr .extra_index_urls
80+ read_simpleapi = read_simpleapi or _read_simpleapi
14781
14882 found_on_index = {}
14983 warn_overrides = False
@@ -210,6 +144,73 @@ def simpleapi_download(
210144
211145 return contents
212146
147+ def _read_simpleapi (ctx , url , attr , cache , ** download_kwargs ):
148+ """Read SimpleAPI.
149+
150+ Args:
151+ ctx: The module_ctx or repository_ctx.
152+ url: str, the url parameter that can be passed to ctx.download.
153+ attr: The attribute that contains necessary info for downloading. The
154+ following attributes must be present:
155+ * envsubst: The envsubst values for performing substitutions in the URL.
156+ * netrc: The netrc parameter for ctx.download, see http_file for docs.
157+ * auth_patterns: The auth_patterns parameter for ctx.download, see
158+ http_file for docs.
159+ cache: A dict for storing the results.
160+ **download_kwargs: Any extra params to ctx.download.
161+ Note that output and auth will be passed for you.
162+
163+ Returns:
164+ A similar object to what `download` would return except that in result.out
165+ will be the parsed simple api contents.
166+ """
167+ # NOTE @aignas 2024-03-31: some of the simple APIs use relative URLs for
168+ # the whl location and we cannot handle multiple URLs at once by passing
169+ # them to ctx.download if we want to correctly handle the relative URLs.
170+ # TODO: Add a test that env subbed index urls do not leak into the lock file.
171+
172+ real_url = envsubst (
173+ url ,
174+ attr .envsubst ,
175+ ctx .getenv if hasattr (ctx , "getenv" ) else ctx .os .environ .get ,
176+ )
177+
178+ cache_key = real_url
179+ if cache_key in cache :
180+ return struct (success = True , output = cache [cache_key ])
181+
182+ output_str = envsubst (
183+ url ,
184+ attr .envsubst ,
185+ # Use env names in the subst values - this will be unique over
186+ # the lifetime of the execution of this function and we also use
187+ # `~` as the separator to ensure that we don't get clashes.
188+ {e : "~{}~" .format (e ) for e in attr .envsubst }.get ,
189+ )
190+
191+ # Transform the URL into a valid filename
192+ for char in ["." , ":" , "/" , "\\ " , "-" ]:
193+ output_str = output_str .replace (char , "_" )
194+
195+ output = ctx .path (output_str .strip ("_" ).lower () + ".html" )
196+
197+ # NOTE: this may have block = True or block = False in the download_kwargs
198+ download = ctx .download (
199+ url = [real_url ],
200+ output = output ,
201+ auth = get_auth (ctx , [real_url ], ctx_attr = attr ),
202+ allow_fail = True ,
203+ ** download_kwargs
204+ )
205+
206+ if download_kwargs .get ("block" ) == False :
207+ # Simulate the same API as ctx.download has
208+ return struct (
209+ wait = lambda : _read_index_result (ctx , download .wait (), output , real_url , cache , cache_key ),
210+ )
211+
212+ return _read_index_result (ctx , download , output , real_url , cache , cache_key )
213+
213214def _read_index_result (ctx , result , output , url , cache , cache_key ):
214215 if not result .success :
215216 return struct (success = False )
0 commit comments