|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import json |
| 5 | +import os |
| 6 | +import pathlib |
| 7 | +import urllib.request as url_lib |
| 8 | +from packaging.version import parse as version_parser |
| 9 | + |
| 10 | +EXTENSION_ROOT = pathlib.Path(__file__).parent.parent |
| 11 | +GET_PIP_DEST = EXTENSION_ROOT / "pythonFiles" |
| 12 | +PIP_PACKAGE = "pip" |
| 13 | +PIP_VERSION = "latest" # Can be "latest", or specific version "23.1.2" |
| 14 | + |
| 15 | + |
| 16 | +def _get_package_data(): |
| 17 | + json_uri = "https://pypi.org/pypi/{0}/json".format(PIP_PACKAGE) |
| 18 | + # Response format: https://warehouse.readthedocs.io/api-reference/json/#project |
| 19 | + # Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst |
| 20 | + with url_lib.urlopen(json_uri) as response: |
| 21 | + return json.loads(response.read()) |
| 22 | + |
| 23 | + |
| 24 | +def _download_and_save(root, version): |
| 25 | + root = os.getcwd() if root is None or root == "." else root |
| 26 | + url = f"https://raw.githubusercontent.com/pypa/get-pip/{version}/public/get-pip.py" |
| 27 | + print(url) |
| 28 | + with url_lib.urlopen(url) as response: |
| 29 | + data = response.read() |
| 30 | + get_pip_file = pathlib.Path(root) / "get-pip.py" |
| 31 | + get_pip_file.write_bytes(data) |
| 32 | + |
| 33 | + |
| 34 | +def main(root): |
| 35 | + data = _get_package_data() |
| 36 | + |
| 37 | + if PIP_VERSION == "latest": |
| 38 | + use_version = max(data["releases"].keys(), key=version_parser) |
| 39 | + else: |
| 40 | + use_version = PIP_VERSION |
| 41 | + |
| 42 | + _download_and_save(root, use_version) |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + main(GET_PIP_DEST) |
0 commit comments