From db27f0637a1bd1083b6e7d39e8029048d330c7cf Mon Sep 17 00:00:00 2001 From: mathioud Date: Mon, 26 Jun 2023 18:50:00 +0200 Subject: [PATCH] extract logic into a seperate function Signed-off-by: mathioud --- src/python_inspector/utils_pypi.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/python_inspector/utils_pypi.py b/src/python_inspector/utils_pypi.py index c01a5d9..7027c0e 100644 --- a/src/python_inspector/utils_pypi.py +++ b/src/python_inspector/utils_pypi.py @@ -1593,20 +1593,24 @@ def fetch_links( for anchor_tag in anchor_tags: python_requires = None url, _, _sha256 = anchor_tag["href"].partition("#sha256=") - if url.startswith(".."): - # Handle relative links - url = urljoin(package_url, url) if "data-requires-python" in anchor_tag.attrs: python_requires = anchor_tag.attrs["data-requires-python"] - # Check if the link is a relative URL - if not url.startswith(("http://", "https://")): - base_url = "/".join(package_url.split("/")[:-1]) # Extract base URL - url = urljoin(base_url, url) # Resolve relative URL + url = resolve_relative_url(package_url, url) # Resolve relative URL links.append(Link(url=url, python_requires=python_requires)) # TODO: keep sha256 return links +def resolve_relative_url(package_url, url): + """ + Resolve a relative URL based on the package URL. + """ + if not url.startswith(("http://", "https://")): + base_url = "/".join(package_url.split("/")[:-1]) # Extract base URL + url = urljoin(base_url, url) # Resolve relative URL + return url + + PYPI_PUBLIC_REPO = PypiSimpleRepository(index_url=PYPI_SIMPLE_URL) DEFAULT_PYPI_REPOS = (PYPI_PUBLIC_REPO,) DEFAULT_PYPI_REPOS_BY_URL = {r.index_url: r for r in DEFAULT_PYPI_REPOS}