Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle relative paths properly in _absolute_url #2153

Merged
merged 2 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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