From e18f7e2d05e1bddddb987a3ef8db8037833a34c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Fri, 28 Feb 2020 14:18:56 +0100 Subject: [PATCH] Fix the url used for installation when fallbacking on PyPI --- poetry/repositories/pypi_repository.py | 26 ++++++++-------------- poetry/repositories/remote_repository.py | 16 +++++++++++++ tests/repositories/test_pypi_repository.py | 7 ++++++ 3 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 poetry/repositories/remote_repository.py diff --git a/poetry/repositories/pypi_repository.py b/poetry/repositories/pypi_repository.py index cdfaf84cebd..767a7353b3c 100644 --- a/poetry/repositories/pypi_repository.py +++ b/poetry/repositories/pypi_repository.py @@ -32,7 +32,7 @@ from poetry.version.markers import parse_marker from .exceptions import PackageNotFound -from .repository import Repository +from .remote_repository import RemoteRepository try: @@ -46,12 +46,14 @@ logger = logging.getLogger(__name__) -class PyPiRepository(Repository): +class PyPiRepository(RemoteRepository): CACHE_VERSION = parse_constraint("1.0.0") def __init__(self, url="https://pypi.org/", disable_cache=False, fallback=True): - self._url = url + super(PyPiRepository, self).__init__(url.rstrip("/") + "/simple/") + + self._base_url = url self._disable_cache = disable_cache self._fallback = fallback @@ -71,18 +73,8 @@ def __init__(self, url="https://pypi.org/", disable_cache=False, fallback=True): self._session = CacheControl(session(), cache=self._cache_control_cache) self._inspector = Inspector() - super(PyPiRepository, self).__init__() - self._name = "PyPI" - @property - def url(self): # type: () -> str - return self._url - - @property - def authenticated_url(self): # type: () -> str - return self._url - def find_packages( self, name, # type: str @@ -224,7 +216,7 @@ def search(self, query): search = {"q": query} - response = session().get(self._url + "search", params=search) + response = session().get(self._base_url + "search", params=search) content = parse(response.content, namespaceHTMLElements=False) for result in content.findall(".//*[@class='package-snippet']"): name = result.find("h3/*[@class='package-snippet__name']").text @@ -361,12 +353,12 @@ def _get_release_info(self, name, version): # type: (str, str) -> dict def _get(self, endpoint): # type: (str) -> Union[dict, None] try: - json_response = self._session.get(self._url + endpoint) + json_response = self._session.get(self._base_url + endpoint) except TooManyRedirects: # Cache control redirect loop. # We try to remove the cache and try again - self._cache_control_cache.delete(self._url + endpoint) - json_response = self._session.get(self._url + endpoint) + self._cache_control_cache.delete(self._base_url + endpoint) + json_response = self._session.get(self._base_url + endpoint) if json_response.status_code == 404: return None diff --git a/poetry/repositories/remote_repository.py b/poetry/repositories/remote_repository.py new file mode 100644 index 00000000000..7717740d87c --- /dev/null +++ b/poetry/repositories/remote_repository.py @@ -0,0 +1,16 @@ +from .repository import Repository + + +class RemoteRepository(Repository): + def __init__(self, url): # type: (str) -> None + self._url = url + + super(RemoteRepository, self).__init__() + + @property + def url(self): # type: () -> str + return self._url + + @property + def authenticated_url(self): # type: () -> str + return self._url diff --git a/tests/repositories/test_pypi_repository.py b/tests/repositories/test_pypi_repository.py index 48f47880046..f8a0f9336aa 100644 --- a/tests/repositories/test_pypi_repository.py +++ b/tests/repositories/test_pypi_repository.py @@ -208,3 +208,10 @@ def test_get_should_invalid_cache_on_too_many_redirects_error(mocker): repository._get("https://pypi.org/pypi/async-timeout/json") assert delete_cache.called + + +def test_urls(): + repository = PyPiRepository() + + assert "https://pypi.org/simple/" == repository.url + assert "https://pypi.org/simple/" == repository.authenticated_url