|
1 | | -from io import BytesIO |
2 | | -from os import path |
3 | | -from zipfile import ZipFile |
| 1 | +import io |
4 | 2 | import json |
5 | | -import urllib.request |
6 | | -import sys |
| 3 | +import os |
| 4 | +import urllib.request as url_lib |
| 5 | +import zipfile |
7 | 6 |
|
8 | | -ROOT = path.dirname(path.dirname(path.abspath(__file__))) |
9 | | -REQUIREMENTS = path.join(ROOT, "requirements.txt") |
10 | | -PYTHONFILES = path.join(ROOT, "pythonFiles", "lib", "python") |
11 | | -PYPI_PTVSD_URL = "https://pypi.org/pypi/ptvsd/json" |
12 | 7 |
|
| 8 | +EXTENSION_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 9 | +DEBUGGER_DEST = os.path.join(EXTENSION_ROOT, "pythonFiles", "lib", "python") |
| 10 | +DEBUGGER_PACKAGE = "ptvsd" |
| 11 | +DEBUGGER_VERSION = "5.0.0a5" |
| 12 | +DEBUGGER_PYTHON_VERSIONS = ("cp37",) |
13 | 13 |
|
14 | | -def install_ptvsd(): |
15 | | - sys.path.insert(0, PYTHONFILES) |
16 | | - from packaging.requirements import Requirement |
17 | 14 |
|
18 | | - with open(REQUIREMENTS, "r", encoding="utf-8") as reqsfile: |
19 | | - for line in reqsfile: |
20 | | - pkgreq = Requirement(line) |
21 | | - if pkgreq.name == "ptvsd": |
22 | | - specs = pkgreq.specifier |
23 | | - version = next(iter(specs)).version |
24 | | - break |
| 15 | +def _contains(s, parts=()): |
| 16 | + return any(p for p in parts if p in s) |
25 | 17 |
|
26 | | - try: |
27 | | - version |
28 | | - except NameError: |
29 | | - raise Exception("ptvsd requirement not found.") |
30 | 18 |
|
| 19 | +def _get_debugger_wheel_urls(): |
| 20 | + json_uri = "https://pypi.org/pypi/{0}/json".format(DEBUGGER_PACKAGE) |
31 | 21 | # Response format: https://warehouse.readthedocs.io/api-reference/json/#project |
32 | | - with urllib.request.urlopen(PYPI_PTVSD_URL) as response: |
| 22 | + # Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst |
| 23 | + with url_lib.urlopen(json_uri) as response: |
33 | 24 | json_response = json.loads(response.read()) |
34 | | - releases = json_response["releases"] |
| 25 | + return list( |
| 26 | + r["url"] |
| 27 | + for r in json_response["releases"][DEBUGGER_VERSION] |
| 28 | + if _contains(r["url"], DEBUGGER_PYTHON_VERSIONS) |
| 29 | + ) |
35 | 30 |
|
36 | | - # Release metadata format: https://github.com/pypa/interoperability-peps/blob/master/pep-0426-core-metadata.rst |
37 | | - for wheel_info in releases[version]: |
38 | | - # Download only if it's a 3.7 wheel. |
39 | | - if not wheel_info["python_version"].endswith(("37", "3.7")): |
40 | | - continue |
41 | | - |
42 | | - # Trim the file extension and remove the ptvsd version from the folder name. |
43 | | - filename = wheel_info["filename"].rpartition(".")[0] |
44 | | - filename = filename.replace(f"{version}-", "") |
45 | | - ptvsd_path = path.join(PYTHONFILES, filename) |
46 | | - |
47 | | - with urllib.request.urlopen(wheel_info["url"]) as wheel_response: |
48 | | - wheel_file = BytesIO(wheel_response.read()) |
| 31 | + |
| 32 | +def _download_and_extract(root, url): |
| 33 | + root = os.getcwd() if root is None or root == "." else root |
| 34 | + prefix = os.path.join("ptvsd-{0}.data".format(DEBUGGER_VERSION), "purelib") |
| 35 | + with url_lib.urlopen(url) as response: |
49 | 36 | # Extract only the contents of the purelib subfolder (parent folder of ptvsd), |
50 | 37 | # since ptvsd files rely on the presence of a 'ptvsd' folder. |
51 | | - prefix = path.join(f"ptvsd-{version}.data", "purelib") |
52 | | - |
53 | | - with ZipFile(wheel_file, "r") as wheel: |
| 38 | + with zipfile.ZipFile(io.BytesIO(response.read()), "r") as wheel: |
54 | 39 | for zip_info in wheel.infolist(): |
55 | | - # Normalize path for Windows, the wheel folder structure uses forward slashes. |
56 | | - normalized = path.normpath(zip_info.filename) |
| 40 | + # Ignore dist info since we are merging multiple wheels |
| 41 | + if ".dist-info" in zip_info.filename: |
| 42 | + continue |
| 43 | + # Normalize path for Windows, the wheel folder structure |
| 44 | + # uses forward slashes. |
| 45 | + normalized = os.path.normpath(zip_info.filename) |
57 | 46 | # Flatten the folder structure. |
58 | 47 | zip_info.filename = normalized.split(prefix)[-1] |
59 | | - wheel.extract(zip_info, ptvsd_path) |
| 48 | + wheel.extract(zip_info, root) |
| 49 | + |
| 50 | + |
| 51 | +def main(root): |
| 52 | + for url in _get_debugger_wheel_urls(): |
| 53 | + _download_and_extract(root, url) |
60 | 54 |
|
61 | 55 |
|
62 | 56 | if __name__ == "__main__": |
63 | | - install_ptvsd() |
| 57 | + main(DEBUGGER_DEST) |
0 commit comments