forked from microsoft/vscode-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall_ptvsd.py
More file actions
63 lines (51 loc) · 2.39 KB
/
install_ptvsd.py
File metadata and controls
63 lines (51 loc) · 2.39 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
from io import BytesIO
from os import path
from zipfile import ZipFile
import json
import urllib.request
import sys
ROOT = path.dirname(path.dirname(path.abspath(__file__)))
REQUIREMENTS = path.join(ROOT, "requirements.txt")
PYTHONFILES = path.join(ROOT, "pythonFiles", "lib", "python")
PYPI_PTVSD_URL = "https://pypi.org/pypi/ptvsd/json"
def install_ptvsd():
sys.path.insert(0, PYTHONFILES)
from packaging.requirements import Requirement
with open(REQUIREMENTS, "r", encoding="utf-8") as reqsfile:
for line in reqsfile:
pkgreq = Requirement(line)
if pkgreq.name == "ptvsd":
specs = pkgreq.specifier
version = next(iter(specs)).version
break
try:
version
except NameError:
raise Exception("ptvsd requirement not found.")
# Response format: https://warehouse.readthedocs.io/api-reference/json/#project
with urllib.request.urlopen(PYPI_PTVSD_URL) as response:
json_response = json.loads(response.read())
releases = json_response["releases"]
# Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst
for wheel_info in releases[version]:
# Download only if it's a 3.7 wheel.
if not wheel_info["python_version"].endswith(("37", "3.7")):
continue
# Trim the file extension and remove the ptvsd version from the folder name.
filename = wheel_info["filename"].rpartition(".")[0]
filename = filename.replace(f"{version}-", "")
ptvsd_path = path.join(PYTHONFILES, filename)
with urllib.request.urlopen(wheel_info["url"]) as wheel_response:
wheel_file = BytesIO(wheel_response.read())
# Extract only the contents of the purelib subfolder (parent folder of ptvsd),
# since ptvsd files rely on the presence of a 'ptvsd' folder.
prefix = path.join(f"ptvsd-{version}.data", "purelib")
with ZipFile(wheel_file, "r") as wheel:
for zip_info in wheel.infolist():
# Normalize path for Windows, the wheel folder structure uses forward slashes.
normalized = path.normpath(zip_info.filename)
# Flatten the folder structure.
zip_info.filename = normalized.split(prefix)[-1]
wheel.extract(zip_info, ptvsd_path)
if __name__ == "__main__":
install_ptvsd()