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 the url used for installation when fallbacking on PyPI #2099

Merged
merged 1 commit into from
Feb 28, 2020
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
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