Commit cd3026f7 by Bernardo Meurer Committed by Arun Babu Neelicattu

fix(link_sources): don't bail on an invalid version

parent 86036944
from __future__ import annotations
import contextlib
import logging
import re
from abc import abstractmethod
......@@ -19,6 +19,9 @@ if TYPE_CHECKING:
from poetry.core.packages.utils.link import Link
logger = logging.getLogger(__name__)
class LinkSource:
VERSION_REGEX = re.compile(r"(?i)([a-z0-9_\-.]+?)-(?=\d)([a-z0-9_.!+-]+)")
CLEAN_REGEX = re.compile(r"[^a-z0-9$&+,/:;=?@.#%_\\|-]", re.I)
......@@ -46,7 +49,7 @@ class LinkSource:
for link in self.links:
pkg = self.link_package_data(link)
if pkg.name == name and pkg.version and pkg.version not in seen:
if pkg and pkg.name == name and pkg.version and pkg.version not in seen:
seen.add(pkg.version)
yield pkg.version
......@@ -55,7 +58,7 @@ class LinkSource:
for link in self.links:
pkg = self.link_package_data(link)
if pkg.name and pkg.version:
if pkg and pkg.name and pkg.version:
yield pkg
@property
......@@ -63,7 +66,7 @@ class LinkSource:
def links(self) -> Iterator[Link]:
raise NotImplementedError()
def link_package_data(self, link: Link) -> Package:
def link_package_data(self, link: Link) -> Package | None:
name, version = None, None
m = wheel_file_re.match(link.filename) or sdist_file_re.match(link.filename)
......@@ -76,8 +79,13 @@ class LinkSource:
if match:
version_string = match.group(2)
with contextlib.suppress(ValueError):
try:
version = Version.parse(version_string)
except ValueError:
logger.debug(
"Skipping url (%s) due to invalid version (%s)", link.url, version
)
return None
return Package(name, version, source_url=link.url)
......@@ -87,7 +95,7 @@ class LinkSource:
for link in self.links:
pkg = self.link_package_data(link)
if pkg.name == name and pkg.version and pkg.version == version:
if pkg and pkg.name == name and pkg.version and pkg.version == version:
yield link
def clean_link(self, url: str) -> str:
......
<!DOCTYPE html>
<html>
<head>
<title>Links for poetry</title>
</head>
<body>
<h1>Links for poetry</h1>
<a href="poetry-21.07.28.5ffb65e2ff8067c732e2b178d03b707c7fb27855-py3-none-any.whl#sha256=1d85132efab8ead3c6f69202843da40a03823992091c29f8d65a31af68940163" data-requires-python="&gt;=3.6.0">poetry-21.07.28.5ffb65e2ff8067c732e2b178d03b707c7fb27855-py3-none-any.whl</a><br/>
<a href="poetry-0.1.0-py3-none-any.whl#sha256=1d85132efab8ead3c6f69202843da40a03823992091c29f8d65a31af68940163" data-requires-python="&gt;=3.6.0">poetry-0.1.0-py3-none-any.whl</a><br/>
</body>
</html>
<!--SERIAL 3907384-->
......@@ -82,6 +82,37 @@ def test_page_clean_link():
assert cleaned == "https://legacy.foo.bar/test%20/the%22/cleaning%00"
def test_page_invalid_version_link():
repo = MockRepository()
page = repo._get_page("/invalid-version")
links = list(page.links)
assert len(links) == 2
versions = list(page.versions("poetry"))
assert len(versions) == 1
assert versions[0].to_string() == "0.1.0"
invalid_link = None
for link in links:
if link.filename.startswith("poetry-21"):
invalid_link = link
break
links_010 = list(page.links_for_version("poetry", versions[0]))
assert invalid_link not in links_010
assert invalid_link
assert not page.link_package_data(invalid_link)
packages = list(page.packages)
assert len(packages) == 1
assert packages[0].name == "poetry"
assert packages[0].version.to_string() == "0.1.0"
def test_sdist_format_support():
repo = MockRepository()
page = repo._get_page("/relative")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment