Stop using pip._internal

This commit is contained in:
Nguyễn Gia Phong 2020-06-19 11:12:18 +07:00
parent 1d850346b9
commit 4d33136241
4 changed files with 13 additions and 23 deletions

View File

@ -4,6 +4,7 @@ branches:
- /^\d+(\.\d+)+((a|b|rc)\d+)?(\.post\d+)?(\.dev\d+)?$/ - /^\d+(\.\d+)+((a|b|rc)\d+)?(\.post\d+)?(\.dev\d+)?$/
language: python language: python
python: 3.8
install: pip install tox install: pip install tox
script: tox script: tox

View File

@ -18,7 +18,7 @@
"""Lazy ZIP over HTTP""" """Lazy ZIP over HTTP"""
__version__ = '0.0.1' __version__ = '0.0.2'
__all__ = ['Lazip'] __all__ = ['Lazip']
from bisect import bisect_left, bisect_right from bisect import bisect_left, bisect_right
@ -27,13 +27,9 @@ from tempfile import NamedTemporaryFile
from typing import Any, Dict, Iterator, List, Optional, Tuple from typing import Any, Dict, Iterator, List, Optional, Tuple
from zipfile import BadZipFile, ZipFile from zipfile import BadZipFile, ZipFile
from pip._internal.network.utils import response_chunks
from pip._internal.utils.wheel import pkg_resources_distribution_for_wheel
from requests import Session from requests import Session
from requests.models import CONTENT_CHUNK_SIZE, Response from requests.models import CONTENT_CHUNK_SIZE, Response
HEADERS: Dict[str, str] = {'Accept-Encoding': 'identity'}
def init_range(stop: int, size: int) -> Iterator[Tuple[int, int]]: def init_range(stop: int, size: int) -> Iterator[Tuple[int, int]]:
"""Return an iterator of intervals to fetch a file reversedly.""" """Return an iterator of intervals to fetch a file reversedly."""
@ -54,7 +50,7 @@ class Lazip:
def __init__(self, session: Session, url: str, def __init__(self, session: Session, url: str,
chunk_size: int = CONTENT_CHUNK_SIZE) -> None: chunk_size: int = CONTENT_CHUNK_SIZE) -> None:
head = session.head(url, headers=HEADERS) head = session.head(url)
head.raise_for_status() head.raise_for_status()
assert head.status_code == 200 assert head.status_code == 200
self.session, self.url, self.chunk_size = session, url, chunk_size self.session, self.url, self.chunk_size = session, url, chunk_size
@ -104,17 +100,16 @@ class Lazip:
self.download(start, end) self.download(start, end)
with self.stay(): with self.stay():
try: try:
ZipFile(self) ZipFile(self) # type: ignore
except BadZipFile: except BadZipFile:
pass pass
else: else:
break break
def stream_response(self, start: int, end: int, def stream_response(self, start: int, end: int,
base_headers: Dict[str, str] = HEADERS) -> Response: base_headers: Dict[str, str] = {}) -> Response:
"""Return HTTP response to a range request from start to end.""" """Return HTTP response to a range request from start to end."""
headers = {'Range': f'bytes={start}-{end}'} headers = {'Range': f'bytes={start}-{end}', **base_headers}
headers.update(base_headers)
return self.session.get(self.url, headers=headers, stream=True) return self.session.get(self.url, headers=headers, stream=True)
def merge(self, start: int, end: int, def merge(self, start: int, end: int,
@ -128,8 +123,8 @@ class Lazip:
right (int): Index after last overlapping downloaded data right (int): Index after last overlapping downloaded data
""" """
lslice, rslice = self.left[left:right], self.right[left:right] lslice, rslice = self.left[left:right], self.right[left:right]
i = start = min(start, min(lslice, default=start)) i = start = min([start, *lslice[:1]])
end = max(end, max(rslice, default=end)) end = max([end, *rslice[-1:]])
for j, k in zip(lslice, rslice): for j, k in zip(lslice, rslice):
if j > i: yield i, j-1 if j > i: yield i, j-1
i = k + 1 i = k + 1
@ -144,7 +139,8 @@ class Lazip:
response = self.stream_response(start, end) response = self.stream_response(start, end)
response.raise_for_status() response.raise_for_status()
self.seek(start) self.seek(start)
for chunk in response_chunks(response, self.chunk_size): for chunk in response.raw.stream(self.chunk_size,
decode_content=False):
self.file.write(chunk) self.file.write(chunk)
def read(self, size: int = -1) -> bytes: def read(self, size: int = -1) -> bytes:
@ -176,12 +172,3 @@ class Lazip:
def close(self) -> None: def close(self) -> None:
"""Close the file.""" """Close the file."""
self.file.close() self.file.close()
if __name__ == '__main__':
url = ('https://files.pythonhosted.org/packages/17/d9/'
'ff8955ce17c080c956cd5eed9c2da4de139d5eeabb9f9ebf2d981acef31d/'
'brutalmaze-0.9.2-py3-none-any.whl')
with Lazip(Session(), url) as wheel:
print(pkg_resources_distribution_for_wheel(
ZipFile(wheel), 'brutalmaze', wheel.name).requires())

View File

@ -7,7 +7,7 @@ module = 'lazip'
author = 'Nguyễn Gia Phong' author = 'Nguyễn Gia Phong'
author-email = 'mcsinyx@disroot.org' author-email = 'mcsinyx@disroot.org'
home-page = 'https://github.com/McSinyx/lazip' home-page = 'https://github.com/McSinyx/lazip'
requires = ['pip', 'requests'] requires = ['requests']
description-file = 'README.md' description-file = 'README.md'
classifiers = [ classifiers = [
'Development Status :: 1 - Planning', 'Development Status :: 1 - Planning',

View File

@ -7,9 +7,11 @@ isolated_build = True
deps = deps =
flake8-builtins flake8-builtins
isort[requirements] isort[requirements]
mypy
commands = commands =
flake8 flake8
isort -c --diff isort -c --diff
mypy lazip.py
[flake8] [flake8]
hang-closing = True hang-closing = True