Skip to content

Commit

Permalink
fix: Handle relative paths properly in _absolute_url (#2153)
Browse files Browse the repository at this point in the history
This updates the simpleapi parser to handle indexes where wheel and
sdist may be an index_url relative path. It also organises the
conditionals with fewer negations so they're easier to read

Fixes: #2150
  • Loading branch information
WillMorrison committed Aug 24, 2024
1 parent b99bb61 commit e331afe
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ A brief description of the categories of changes:
* (gazelle): Fix incorrect use of `t.Fatal`/`t.Fatalf` in tests.
* (toolchain) Omit third-party python packages from coverage reports from
stage2 bootstrap template.
* (bzlmod) Properly handle relative path URLs in parse_simpleapi_html.bzl

### Added
* Nothing yet
Expand Down
4 changes: 2 additions & 2 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions python/private/pypi/parse_simpleapi_html.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def parse_simpleapi_html(*, url, content):
url = _absolute_url(url, dist_url),
sha256 = sha256,
metadata_sha256 = metadata_sha256,
metadata_url = _absolute_url(url, metadata_url),
metadata_url = _absolute_url(url, metadata_url) if metadata_url else "",
yanked = yanked,
)
else:
Expand Down Expand Up @@ -109,18 +109,33 @@ def _get_root_directory(url):

return "{}://{}".format(scheme, host)

def _is_downloadable(url):
"""Checks if the URL would be accepted by the Bazel downloader.
This is based on Bazel's HttpUtils::isUrlSupportedByDownloader
"""
return url.startswith("http://") or url.startswith("https://") or url.startswith("file://")

def _absolute_url(index_url, candidate):
if candidate == "":
return candidate

if _is_downloadable(candidate):
return candidate

if candidate.startswith("/"):
# absolute url
# absolute path
root_directory = _get_root_directory(index_url)
return "{}{}".format(root_directory, candidate)

if not candidate.startswith(".."):
return candidate
if candidate.startswith(".."):
# relative path with up references
candidate_parts = candidate.split("..")
last = candidate_parts[-1]
for _ in range(len(candidate_parts) - 1):
index_url, _, _ = index_url.rstrip("/").rpartition("/")

candidate_parts = candidate.split("..")
last = candidate_parts[-1]
for _ in range(len(candidate_parts) - 1):
index_url, _, _ = index_url.rstrip("/").rpartition("/")
return "{}/{}".format(index_url, last.strip("/"))

return "{}/{}".format(index_url, last.strip("/"))
# relative path without up-references
return "{}/{}".format(index_url, candidate)
34 changes: 34 additions & 0 deletions tests/pypi/parse_simpleapi_html/parse_simpleapi_html_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,40 @@ def _test_whls(env):
yanked = False,
),
),
(
struct(
attrs = [
'href="1.0.0/mypy_extensions-1.0.0-py3-none-any.whl#sha256=deadbeef"',
],
filename = "mypy_extensions-1.0.0-py3-none-any.whl",
url = "https://example.org/simple/mypy_extensions",
),
struct(
filename = "mypy_extensions-1.0.0-py3-none-any.whl",
metadata_sha256 = "",
metadata_url = "",
sha256 = "deadbeef",
url = "https://example.org/simple/mypy_extensions/1.0.0/mypy_extensions-1.0.0-py3-none-any.whl",
yanked = False,
),
),
(
struct(
attrs = [
'href="unknown://example.com/mypy_extensions-1.0.0-py3-none-any.whl#sha256=deadbeef"',
],
filename = "mypy_extensions-1.0.0-py3-none-any.whl",
url = "https://example.org/simple/mypy_extensions",
),
struct(
filename = "mypy_extensions-1.0.0-py3-none-any.whl",
metadata_sha256 = "",
metadata_url = "",
sha256 = "deadbeef",
url = "https://example.org/simple/mypy_extensions/unknown://example.com/mypy_extensions-1.0.0-py3-none-any.whl",
yanked = False,
),
),
]

for (input, want) in tests:
Expand Down

0 comments on commit e331afe

Please sign in to comment.