|
| 1 | +import os |
| 2 | +import sys |
| 3 | +from collections import defaultdict |
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +PACKAGE_NAME = sys.argv[1] |
| 9 | +CONDA_CHANNEL = sys.argv[2] |
| 10 | +OUTPUT_DIR = sys.argv[3] |
| 11 | +BASE_URL = f"https://api.anaconda.org/package/{CONDA_CHANNEL}" |
| 12 | + |
| 13 | +print(f"Getting conda-forge details for package '{PACKAGE_NAME}'") |
| 14 | +res = requests.get(url=f"{BASE_URL}/{PACKAGE_NAME}", timeout=30) |
| 15 | +res.raise_for_status() |
| 16 | +release_info = res.json() |
| 17 | + |
| 18 | +latest_version = release_info["versions"][-1] |
| 19 | +files = [f for f in release_info["files"] if f["version"] == latest_version] |
| 20 | + |
| 21 | + |
| 22 | +@dataclass |
| 23 | +class _ReleaseFile: |
| 24 | + filename: str |
| 25 | + package_type: str |
| 26 | + url: str |
| 27 | + |
| 28 | + |
| 29 | +files_by_type = defaultdict(list) |
| 30 | +for file_info in files: |
| 31 | + pkg_type = file_info["attrs"]["target-triplet"] |
| 32 | + url = file_info["download_url"] |
| 33 | + if url.startswith("//"): |
| 34 | + url = f"https:{url}" |
| 35 | + files_by_type[pkg_type].append( |
| 36 | + _ReleaseFile( |
| 37 | + filename=file_info["basename"].replace("/", "-"), |
| 38 | + package_type=pkg_type, |
| 39 | + url=url, |
| 40 | + ) |
| 41 | + ) |
| 42 | + |
| 43 | +print(f"Found the following file types for '{PACKAGE_NAME}=={latest_version}':") |
| 44 | +for file_type, release_files in files_by_type.items(): |
| 45 | + print(f" * {file_type} ({len(release_files)})") |
| 46 | + |
| 47 | + |
| 48 | +for file_type in files_by_type: |
| 49 | + sample_release = files_by_type[file_type][0] |
| 50 | + output_file = os.path.join(OUTPUT_DIR, sample_release.filename) |
| 51 | + print(f"Downloading '{sample_release.filename}'") |
| 52 | + res = requests.get( |
| 53 | + url=sample_release.url, headers={"Accept": "application/octet-stream"}, timeout=30 |
| 54 | + ) |
| 55 | + res.raise_for_status() |
| 56 | + with open(output_file, "wb") as f: |
| 57 | + f.write(res.content) |
| 58 | + |
| 59 | +print(f"Done downloading files into '{OUTPUT_DIR}'") |
0 commit comments