|
| 1 | +import click |
| 2 | +import typing |
| 3 | +import requests |
| 4 | +import logging |
| 5 | +import click_log |
| 6 | +import sys |
| 7 | +import tools.wheel_resolver.wheel as wheel |
| 8 | +import tools.wheel_resolver.output as output |
| 9 | +import packaging.tags as tags |
| 10 | +import distlib.locators |
| 11 | + |
| 12 | +_LOGGER = logging.getLogger(__name__) |
| 13 | + |
| 14 | +click_log.basic_config(_LOGGER) |
| 15 | + |
| 16 | +@click.command() |
| 17 | +@click.option( |
| 18 | + "--url", |
| 19 | + "--urls", |
| 20 | + multiple=True, |
| 21 | + metavar="URL", |
| 22 | + default=[], |
| 23 | + help="URLs to check for package before looking in the wheel index", |
| 24 | +) |
| 25 | +@click.option( |
| 26 | + "--package-name", |
| 27 | + "--package", |
| 28 | + metavar="NAME", |
| 29 | + required=True, |
| 30 | + help="Name of Python package in PyPI", |
| 31 | +) |
| 32 | +@click.option( |
| 33 | + "--package-version", |
| 34 | + "--version", |
| 35 | + metavar="VERSION", |
| 36 | + help="Version of Python package in PyPI", |
| 37 | +) |
| 38 | +@click.option( |
| 39 | + "--interpreter", |
| 40 | + default={t.interpreter for t in tags.sys_tags()}, |
| 41 | + multiple=True, |
| 42 | + metavar="INTERPRETER", |
| 43 | + show_default=True, |
| 44 | + help="The interpreter name or abbreviation code with version, for example py31 or cp310", |
| 45 | +) |
| 46 | +@click.option( |
| 47 | + "--platform", |
| 48 | + # Must cast to list so click knows we want multiple default values. |
| 49 | + default=set(tags.platform_tags()), |
| 50 | + metavar="PLATFORM", |
| 51 | + multiple=True, |
| 52 | + help="The platform identifier, for example linux_x86_64 or linux_i686", |
| 53 | +) |
| 54 | +@click.option( |
| 55 | + "--abi", |
| 56 | + default={t.abi for t in tags.sys_tags()}, |
| 57 | + metavar="ABI", |
| 58 | + show_default=True, |
| 59 | + multiple=True, |
| 60 | + help="The ABI identifier, for example cp310 or abi3", |
| 61 | +) |
| 62 | +@click_log.simple_verbosity_option(_LOGGER) |
| 63 | +def main( |
| 64 | + url: typing.List[str], |
| 65 | + package_name: typing.Optional[str], |
| 66 | + package_version: typing.Optional[str], |
| 67 | + interpreter: typing.Tuple[str, ...], |
| 68 | + platform: typing.Tuple[str, ...], |
| 69 | + abi: typing.Tuple[str, ...], |
| 70 | +): |
| 71 | + """Resolve a wheel by name and version to a URL. |
| 72 | +
|
| 73 | + If URLs are specified, they are checked literally before doing a lookup in |
| 74 | + PyPI for PACKAGE with VERSION. |
| 75 | +
|
| 76 | + """ |
| 77 | + for u in url: |
| 78 | + response = requests.head(u) |
| 79 | + if response.status_code != requests.codes.ok: |
| 80 | + _LOGGER.warning( |
| 81 | + "%s-%s is not available, tried %r", package_name, package_version, u |
| 82 | + ) |
| 83 | + else: |
| 84 | + click.echo(u) |
| 85 | + return |
| 86 | + |
| 87 | + u = wheel.url( |
| 88 | + package_name=package_name, |
| 89 | + package_version=package_version, |
| 90 | + tags=[ |
| 91 | + str(x) |
| 92 | + for i in interpreter |
| 93 | + for x in tags.generic_tags( |
| 94 | + interpreter=i, |
| 95 | + abis=set(abi), |
| 96 | + platforms=set(platform).union({"any"}), |
| 97 | + ) |
| 98 | + ], |
| 99 | + # We're currently hardcoding PyPI but we should consider allowing other |
| 100 | + # repositories |
| 101 | + locator=distlib.locators.SimpleScrapingLocator(url="https://pypi.org/simple"), |
| 102 | + ) |
| 103 | + response = requests.head(u) |
| 104 | + if response.status_code != requests.codes.ok: |
| 105 | + _LOGGER.error( |
| 106 | + "%s-%s is not available, tried %r", package_name, package_version, u |
| 107 | + ) |
| 108 | + sys.exit(1) |
| 109 | + |
| 110 | + if not output.try_download(u): |
| 111 | + _LOGGER.error("Could not download from %r", u) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + click.echo(u) |
0 commit comments