forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdownload_get_pip.py
More file actions
75 lines (56 loc) · 2.53 KB
/
Copy pathdownload_get_pip.py
File metadata and controls
75 lines (56 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
# This file is now a standalone Python script that
# updates the vendored get-pip.py and cgmanifest.json
# to the latest pip release.
import argparse
import json
import pathlib
import urllib.request
from typing import Optional
EXTENSION_ROOT = pathlib.Path(__file__).parent.parent
GET_PIP_DEST = EXTENSION_ROOT / "python_files" / "get-pip.py"
CGMANIFEST_PATH = EXTENSION_ROOT / "cgmanifest.json"
PIP_METADATA_URL = "https://pypi.org/pypi/pip/json"
GET_PIP_URL = "https://raw.githubusercontent.com/pypa/get-pip/{version}/public/get-pip.py"
def _get_latest_version() -> str:
with urllib.request.urlopen(PIP_METADATA_URL) as response:
metadata = json.load(response)
version = metadata.get("info", {}).get("version")
if not isinstance(version, str) or not version:
raise ValueError(f"PyPI metadata from {PIP_METADATA_URL} did not contain a version")
return version
def _download_get_pip(version: str) -> bytes:
url = GET_PIP_URL.format(version=version)
print(f"Downloading {url}")
with urllib.request.urlopen(url) as response:
get_pip = response.read()
expected_version = f"pip (version {version})".encode()
if expected_version not in get_pip[:1024]:
raise ValueError(f"Downloaded get-pip.py did not contain pip {version}")
return get_pip
def _update_cgmanifest(version: str) -> str:
manifest = json.loads(CGMANIFEST_PATH.read_text(encoding="utf-8"))
for registration in manifest["Registrations"]:
component = registration.get("Component", {}).get("Other", {})
if component.get("Name") == "get-pip":
component["Version"] = version
return f"{json.dumps(manifest, indent=4)}\n"
raise ValueError(f"get-pip registration was not found in {CGMANIFEST_PATH}")
def refresh_get_pip(version: Optional[str] = None) -> None:
version = version or _get_latest_version()
get_pip = _download_get_pip(version)
cgmanifest = _update_cgmanifest(version)
GET_PIP_DEST.write_bytes(get_pip)
CGMANIFEST_PATH.write_text(cgmanifest, encoding="utf-8")
print(f"Updated {GET_PIP_DEST} and {CGMANIFEST_PATH} to get-pip {version}")
def main() -> None:
parser = argparse.ArgumentParser(description="Refresh the vendored get-pip.py")
parser.add_argument(
"--version",
help="pip release to vendor; defaults to the latest release on PyPI",
)
args = parser.parse_args()
refresh_get_pip(args.version)
if __name__ == "__main__":
main()