Skip to content

Commit d69559d

Browse files
authored
fix: correctly handle absolute URLs in parse_simpleapi_html.bzl (#2112)
This PR addresses a typo and improves the handling of absolute URLs in the `parse_simpleapi_html.bzl` file and corrects a minor issue in the `simpleapi_download.bzl` file. Summary: 1. parse_simpleapi_html.bzl: - Introduced a new private function `_get_root_directory(url)` to extract the root directory from a given URL. - Enhanced `_absolute_url` function to correctly handle absolute URLs by utilizing the `_get_root_directory` function. This ensures that URLs starting with a "/" are correctly resolved to their full path, avoiding potential incorrect concatenation. 2. simpleapi_download.bzl: Corrected the handling of the `real_url` variable in the `_read_simpleapi` function, ensuring that the correct URL is passed to `_read_index_result` when using non-blocking downloads.
1 parent 4f97f1a commit d69559d

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ A brief description of the categories of changes:
3535
* `3.12 -> 3.12.4`
3636

3737
### Fixed
38+
* (rules) correctly handle absolute URLs in parse_simpleapi_html.bzl.
3839
* (rules) Fixes build targets linking against `@rules_python//python/cc:current_py_cc_libs`
3940
in host platform builds on macOS, by editing the `LC_ID_DYLIB` field of the hermetic interpreter's
4041
`libpython3.x.dylib` using `install_name_tool`, setting it to its absolute path under Bazel's

python/private/pypi/parse_simpleapi_html.bzl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,25 @@ def parse_simpleapi_html(*, url, content):
9696
whls = whls,
9797
)
9898

99+
def _get_root_directory(url):
100+
scheme_end = url.find("://")
101+
if scheme_end == -1:
102+
fail("Invalid URL format")
103+
104+
scheme = url[:scheme_end]
105+
host_end = url.find("/", scheme_end + 3)
106+
if host_end == -1:
107+
host_end = len(url)
108+
host = url[scheme_end + 3:host_end]
109+
110+
return "{}://{}".format(scheme, host)
111+
99112
def _absolute_url(index_url, candidate):
113+
if candidate.startswith("/"):
114+
# absolute url
115+
root_directory = _get_root_directory(index_url)
116+
return "{}{}".format(root_directory, candidate)
117+
100118
if not candidate.startswith(".."):
101119
return candidate
102120

python/private/pypi/simpleapi_download.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,10 @@ def _read_simpleapi(ctx, url, attr, cache, **download_kwargs):
185185
if download_kwargs.get("block") == False:
186186
# Simulate the same API as ctx.download has
187187
return struct(
188-
wait = lambda: _read_index_result(ctx, download.wait(), output, url, cache, cache_key),
188+
wait = lambda: _read_index_result(ctx, download.wait(), output, real_url, cache, cache_key),
189189
)
190190

191-
return _read_index_result(ctx, download, output, url, cache, cache_key)
191+
return _read_index_result(ctx, download, output, real_url, cache, cache_key)
192192

193193
def _read_index_result(ctx, result, output, url, cache, cache_key):
194194
if not result.success:

tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,40 @@ def _test_whls(env):
221221
yanked = False,
222222
),
223223
),
224+
(
225+
struct(
226+
attrs = [
227+
'href="/whl/torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl#sha256=deadbeef"',
228+
],
229+
filename = "torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
230+
url = "https://download.pytorch.org/whl/cpu/torch",
231+
),
232+
struct(
233+
filename = "torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
234+
metadata_sha256 = "",
235+
metadata_url = "",
236+
sha256 = "deadbeef",
237+
url = "https://download.pytorch.org/whl/torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
238+
yanked = False,
239+
),
240+
),
241+
(
242+
struct(
243+
attrs = [
244+
'href="/whl/torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl#sha256=notdeadbeef"',
245+
],
246+
filename = "torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
247+
url = "http://download.pytorch.org/whl/cpu/torch",
248+
),
249+
struct(
250+
filename = "torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
251+
metadata_sha256 = "",
252+
metadata_url = "",
253+
sha256 = "notdeadbeef",
254+
url = "http://download.pytorch.org/whl/torch-2.0.0-cp38-cp38-manylinux2014_aarch64.whl",
255+
yanked = False,
256+
),
257+
),
224258
]
225259

226260
for (input, want) in tests:

0 commit comments

Comments
 (0)