-
-
Notifications
You must be signed in to change notification settings - Fork 15
Accept-Encoding: gzip (take 2) #93
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |||
|
|
||||
| import argparse | ||||
| import configparser | ||||
| import gzip | ||||
| import hashlib | ||||
| import json | ||||
| import os | ||||
|
|
@@ -161,24 +162,7 @@ def _mirror( # noqa: C901, PLR0912 | |||
| self._processed_pkgs.add(requirement) # Mark as processed | ||||
| return None | ||||
|
|
||||
| data: dict | None = None | ||||
|
|
||||
| # get information about this package from the Simple API in JSON | ||||
| # format as per PEP 691 | ||||
| request = urllib.request.Request( # noqa: S310 | ||||
| f"{self.index_url}{requirement.name}/", | ||||
| headers={ | ||||
| "Accept": "application/vnd.pypi.simple.v1+json", | ||||
| }, | ||||
| ) | ||||
|
|
||||
| response_url = "" | ||||
| with urllib.request.urlopen(request) as response: # noqa: S310 | ||||
| data = json.load(response) | ||||
| response_url = str(response.url) | ||||
| if not data: | ||||
| msg = f"Failed loading metadata: {response}" | ||||
| raise RuntimeError(msg) | ||||
| data, response_url = download_req(self.index_url, requirement.name) | ||||
|
|
||||
| # check metadata version ~1.0 | ||||
| v_str = data["meta"]["api-version"] | ||||
|
|
@@ -851,5 +835,32 @@ def my_url(arg): | |||
| Mirrorer(args).copy_server() | ||||
|
|
||||
|
|
||||
| def download_req(index_url: str, req_name: str) -> tuple[dict, str]: | ||||
| # get information about this package from the Simple API in JSON | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this method is only used by |
||||
| # format as per PEP 691 | ||||
| url = index_url.rstrip("/") | ||||
| request = urllib.request.Request( # noqa: S310 | ||||
| f"{url}/{req_name}/", | ||||
| headers={ | ||||
| "Accept": "application/vnd.pypi.simple.v1+json", | ||||
| "Accept-Encoding": "gzip", | ||||
| }, | ||||
| ) | ||||
|
|
||||
| response_url = "" | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This line here is unnecessary |
||||
| with urllib.request.urlopen(request) as response: # noqa: S310 | ||||
| # Check if response is gzip-encoded | ||||
| if response.headers.get("Content-Encoding") == "gzip": | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comparison should be case-insensitive ( |
||||
| with gzip.GzipFile(fileobj=response) as gzip_response: | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A broken server or a corrupted or truncated body can raise a A suggestion: class MetadataError(RuntimeError):
"""The index returned an unusable metadata response."""
# and then inside the function's `with urllib.request.urlopen(...)` block:
try:
if response.headers.get("Content-Encoding") == "gzip":
with gzip.GzipFile(fileobj=response) as gzip_response:
data = json.load(gzip_response)
else:
data = json.load(response)
except (gzip.BadGzipFile, EOFError, zlib.error, json.JSONDecodeError) as err:
msg = f"Failed decoding metadata from {response.url}: {err}"
raise MetadataError(msg) from errand then expanding |
||||
| data = json.load(gzip_response) | ||||
| else: | ||||
| data = json.load(response) | ||||
| response_url = str(response.url) | ||||
| if data: | ||||
| return data, response_url | ||||
| msg = f"Failed loading metadata: {response}" | ||||
| raise RuntimeError(msg) | ||||
|
|
||||
|
|
||||
| if __name__ == "__main__": | ||||
| main() | ||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a more descriptive name, like
fetch_package_metadata