Skip to content

Commit

Permalink
Fix the url used for installation when fallbacking on PyPI (#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored Feb 28, 2020
1 parent ae6d64d commit c9fb348
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
26 changes: 9 additions & 17 deletions poetry/repositories/pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions poetry/repositories/remote_repository.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions tests/repositories/test_pypi_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c9fb348

Please sign in to comment.