Skip to content

Commit

Permalink
apply review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
radoering committed Jan 13, 2024
1 parent 22626ba commit be15429
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/poetry/console/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def unique_config_values(self) -> dict[str, tuple[Any, Any]]:
),
"virtualenvs.path": (str, lambda val: str(Path(val))),
"virtualenvs.prefer-active-python": (boolean_validator, boolean_normalizer),
"virtualenvs.prompt": (str, lambda val: str(val)),
"virtualenvs.prompt": (str, str),
"experimental.system-git-client": (boolean_validator, boolean_normalizer),
"installer.modern-installation": (boolean_validator, boolean_normalizer),
"installer.parallel": (boolean_validator, boolean_normalizer),
Expand Down
41 changes: 29 additions & 12 deletions src/poetry/inspection/lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
if TYPE_CHECKING:
from collections.abc import Iterable
from collections.abc import Iterator
from types import TracebackType

from packaging.metadata import RawMetadata
from requests import Session
Expand All @@ -51,12 +52,12 @@ class UnsupportedWheel(Exception):
class InvalidWheel(Exception):
"""Invalid (e.g. corrupt) wheel."""

def __init__(self, location: str, name: str):
def __init__(self, location: str, name: str) -> None:
self.location = location
self.name = name

def __str__(self) -> str:
return f"Wheel '{self.name}' located at {self.location} is invalid."
return f"Wheel {self.name} located at {self.location} is invalid."


def metadata_from_wheel_url(
Expand Down Expand Up @@ -240,8 +241,13 @@ def __enter__(self) -> ReadOnlyIOWrapper:
self._file.__enter__()
return self

def __exit__(self, *exc: Any) -> None:
self._file.__exit__(*exc)
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
self._file.__exit__(exc_type, exc_value, traceback)

def __iter__(self) -> Iterator[bytes]:
raise NotImplementedError
Expand Down Expand Up @@ -298,10 +304,15 @@ def __enter__(self) -> LazyRemoteResource:
self._setup_content()
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
"""Call ``self._reset_content()``."""
self._reset_content()
super().__exit__(*exc)
super().__exit__(exc_type, exc_value, traceback)

@contextmanager
def _stay(self) -> Iterator[None]:
Expand Down Expand Up @@ -455,10 +466,15 @@ def __enter__(self) -> LazyHTTPFile:
super().__enter__()
return self

def __exit__(self, *exc: Any) -> None:
def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> None:
"""Logs request count to quickly identify any pathological cases in log data."""
logger.debug("%d requests for url %s", self._request_count, self._url)
super().__exit__(*exc)
super().__exit__(exc_type, exc_value, traceback)

def _content_length_from_head(self) -> int:
"""Performs a HEAD request to extract the Content-Length.
Expand Down Expand Up @@ -514,6 +530,8 @@ class LazyWheelOverHTTP(LazyHTTPFile):
# multiple times in the same invocation against an index without this support.
_domains_without_negative_range: ClassVar[set[str]] = set()

_metadata_regex = re.compile(r"^[^/]*\.dist-info/METADATA$")

# This override is needed for mypy so we can call ``.prefetch_metadata()``
# within a ``with`` block.
def __enter__(self) -> LazyWheelOverHTTP:
Expand Down Expand Up @@ -711,7 +729,6 @@ def _prefetch_metadata(self, name: str) -> str:
can be downloaded in a single ranged GET request."""
logger.debug("begin prefetching METADATA for %s", name)

metadata_regex = re.compile(r"^[^/]*\.dist-info/METADATA$")
start: int | None = None
end: int | None = None

Expand All @@ -723,19 +740,19 @@ def _prefetch_metadata(self, name: str) -> str:
filename = ""
for info in zf.infolist():
if start is None:
if metadata_regex.search(info.filename):
if self._metadata_regex.search(info.filename):
filename = info.filename
start = info.header_offset
continue
else:
# The last .dist-info/ entry may be before the end of the file if the
# wheel's entries are sorted lexicographically (which is unusual).
if not metadata_regex.search(info.filename):
if not self._metadata_regex.search(info.filename):
end = info.header_offset
break
if start is None:
raise UnsupportedWheel(
f"no {metadata_regex!r} found for {name} in {self.name}"
f"no {self._metadata_regex!r} found for {name} in {self.name}"
)
# If it is the last entry of the zip, then give us everything
# until the start of the central directory.
Expand Down
28 changes: 12 additions & 16 deletions tests/inspection/test_lazy_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

from tests.types import FixtureDirGetter

HttPrettyResponse = Tuple[int, Dict[str, Any], bytes] # status code, headers, body
HttPrettyRequestCallback = Callable[
[HTTPrettyRequest, str, Dict[str, Any]], HttPrettyResponse
HTTPrettyResponse = Tuple[int, Dict[str, Any], bytes] # status code, headers, body
HTTPrettyRequestCallback = Callable[
[HTTPrettyRequest, str, Dict[str, Any]], HTTPrettyResponse
]

class RequestCallbackFactory(Protocol):
Expand All @@ -39,15 +39,15 @@ def __call__(
*,
accept_ranges: str | None = "bytes",
negative_offset_error: tuple[int, bytes] | None = None,
) -> HttPrettyRequestCallback: ...
) -> HTTPrettyRequestCallback: ...


NEGATIVE_OFFSET_AS_POSITIVE = -1


def build_head_response(
accept_ranges: str | None, content_length: int, response_headers: dict[str, Any]
) -> HttPrettyResponse:
) -> HTTPrettyResponse:
response_headers["Content-Length"] = content_length
if accept_ranges:
response_headers["Accept-Ranges"] = accept_ranges
Expand All @@ -60,7 +60,7 @@ def build_partial_response(
response_headers: dict[str, Any],
*,
negative_offset_as_positive: bool = False,
) -> HttPrettyResponse:
) -> HTTPrettyResponse:
status_code = 206
response_headers["Accept-Ranges"] = "bytes"
total_length = len(wheel_bytes)
Expand All @@ -81,9 +81,7 @@ def build_partial_response(
body = wheel_bytes[offset:]
else:
# range with start and end
rng_parts = rng.split("-")
start = int(rng_parts[0])
end = int(rng_parts[1])
start, end = map(int, rng.split("-"))
body = wheel_bytes[start : end + 1]
response_headers["Content-Range"] = f"bytes {start}-{end}/{total_length}"
return status_code, response_headers, body
Expand All @@ -95,13 +93,13 @@ def _factory(
*,
accept_ranges: str | None = "bytes",
negative_offset_error: tuple[int, bytes] | None = None,
) -> HttPrettyRequestCallback:
) -> HTTPrettyRequestCallback:
def handle_request(
request: HTTPrettyRequest, uri: str, response_headers: dict[str, Any]
) -> HttPrettyResponse:
) -> HTTPrettyResponse:
name = Path(urlparse(uri).path).name

wheel = Path(__file__).parent.parent.joinpath(
wheel = Path(__file__).parents[1] / (
"repositories/fixtures/pypi.org/dists/" + name
)

Expand All @@ -122,9 +120,7 @@ def handle_request(
accept_ranges, len(wheel_bytes), response_headers
)

rng = request.headers.get("Range", "")
if rng:
rng = rng.split("=")[1]
rng = request.headers.get("Range", "=").split("=")[1]

negative_offset_as_positive = False
if negative_offset_error and rng.startswith("-"):
Expand Down Expand Up @@ -226,7 +222,7 @@ def test_metadata_from_wheel_url_smaller_than_initial_chunk_size(
handle_request_factory: RequestCallbackFactory,
negative_offset_as_positive: bool,
) -> None:
domain = f"tiny-wheel-{str(negative_offset_as_positive).lower()}.com"
domain = f"tiny-wheel-{str(negative_offset_as_positive).casefold()}.com"
uri_regex = re.compile(f"^https://{domain}/.*$")
request_callback = handle_request_factory(
negative_offset_error=(
Expand Down
2 changes: 1 addition & 1 deletion tests/repositories/test_http_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def test_get_info_from_wheel(
'importlib-metadata (>=1.7.0) ; python_version < "3.8"'
]

if lazy_wheel and (supports_range_requests or supports_range_requests is None):
if lazy_wheel and supports_range_requests is not False:
mock_metadata_from_wheel_url.assert_called_once_with(
filename, url, repo.session
)
Expand Down

0 comments on commit be15429

Please sign in to comment.