| 1 |
""" |
| 2 |
Fetch metadata from remote .conda or .tar.bz2 package. |
| 3 |
|
| 4 |
Try to fetch less than the whole file if possible. |
| 5 |
|
| 6 |
This module should only be used to make *partial* reads against a remote |
| 7 |
package, typically just the ``info`` portion. If a full ``.conda`` format |
| 8 |
package is needed, it is more efficient to download locally first and then use |
| 9 |
the file-based API. |
| 10 |
""" |
| 11 |
|
| 12 |
import logging |
| 13 |
import sys |
| 14 |
import urllib.parse |
| 15 |
from pathlib import Path |
| 16 |
|
| 17 |
import requests |
| 18 |
|
| 19 |
from . import package_streaming |
| 20 |
|
| 21 |
# Excellent HTTP Range request file-like object |
| 22 |
from .lazy_wheel import LazyConda |
| 23 |
|
| 24 |
log = logging.getLogger(__name__) |
| 25 |
|
| 26 |
session = requests.Session() |
| 27 |
session.headers["User-Agent"] = "conda-package-streaming/0.1.0" |
| 28 |
|
| 29 |
|
| 30 |
METADATA_CHECKLIST = frozenset({"info/index.json", "info/recipe/meta.yaml"}) |
| 31 |
|
| 32 |
|
| 33 |
def extract_conda_info(url, destdir, checklist=METADATA_CHECKLIST, session=session): |
| 34 |
""" |
| 35 |
Extract info/index.json and info/recipe/meta.yaml from url to destdir; close |
| 36 |
url as soon as those files are found. |
| 37 |
""" |
| 38 |
checklist = set(checklist) |
| 39 |
stream = stream_conda_info(url, session=session) |
| 40 |
for tar, member in stream: |
| 41 |
if member.name in checklist: |
| 42 |
tar.extract(member, destdir) |
| 43 |
checklist.remove(member.name) |
| 44 |
if not checklist: |
| 45 |
stream.close() |
| 46 |
# next iteraton of for loop raises GeneratorExit in stream |
| 47 |
|
| 48 |
|
| 49 |
def stream_conda_info(url, session=session): |
| 50 |
""" |
| 51 |
Yield (tar, member) for conda package at url |
| 52 |
|
| 53 |
Just "info/" for .conda, all members for tar. |
| 54 |
""" |
| 55 |
filename, conda = conda_reader_for_url(url, session=session) |
| 56 |
|
| 57 |
try: |
| 58 |
yield from package_streaming.stream_conda_info(filename, conda) |
| 59 |
finally: |
| 60 |
if hasattr(conda, "release_conn"): |
| 61 |
# For .tar.bz2. Take extra care to drop connections after we are |
| 62 |
# done reading a partial response. |
| 63 |
conda.release_conn() |
| 64 |
conda.close() |
| 65 |
|
| 66 |
|
| 67 |
def conda_reader_for_url( |
| 68 |
url, session: requests.Session = session, fall_back_to_full_download: bool = False |
| 69 |
): |
| 70 |
""" |
| 71 |
Return (name, file_like) suitable for package_streaming APIs |
| 72 |
|
| 73 |
:param url: URL to conda package |
| 74 |
:param session: The session to use for web requests. |
| 75 |
:param fall_back_to_full_download: If true, fall back to downloading the entire |
| 76 |
package instead of streaming it in the case of a specific issue where the server |
| 77 |
incorrectly responds with 416 (Range Not Satisfiable). See LazyZipOverHTTP for |
| 78 |
more details. |
| 79 |
""" |
| 80 |
parsed_url = urllib.parse.urlparse(url) |
| 81 |
*_, filename = parsed_url.path.rsplit("/", 1) |
| 82 |
if filename.endswith(".conda"): |
| 83 |
file_id = filename[: -len(".conda")] |
| 84 |
conda = LazyConda( |
| 85 |
url, session, fall_back_to_full_download=fall_back_to_full_download |
| 86 |
) |
| 87 |
conda.prefetch(file_id) |
| 88 |
elif filename.endswith(".tar.bz2"): |
| 89 |
response = session.get(url, stream=True, headers={"Connection": "close"}) |
| 90 |
conda = response.raw |
| 91 |
else: |
| 92 |
raise ValueError("Unsupported extension %s", url) |
| 93 |
return filename, conda |
| 94 |
|
| 95 |
|
| 96 |
if __name__ == "__main__": # pragma: no cover |
| 97 |
import logging |
| 98 |
|
| 99 |
logging.basicConfig(level=logging.DEBUG) |
| 100 |
extract_conda_info(sys.argv[1], Path(sys.argv[2]).absolute()) |