|
| 1 | +from __future__ import annotations |
| 2 | +from http.client import HTTPResponse |
| 3 | +from LSP.plugin import AbstractPlugin |
| 4 | +from LSP.plugin import ClientConfig |
| 5 | +from LSP.plugin import register_plugin |
| 6 | +from LSP.plugin import unregister_plugin |
| 7 | +from pathlib import Path |
| 8 | +from typing import cast, final |
| 9 | +from typing_extensions import override |
| 10 | +import shutil |
| 11 | +import sublime |
| 12 | +import tarfile |
| 13 | +import urllib.request |
| 14 | +import zipfile |
| 15 | + |
| 16 | + |
| 17 | +TAG = "0.1.2" |
| 18 | +ARTIFACT_URL = "https://github.com/terror/pyproject/releases/download/{tag}/{filename}" |
| 19 | +ARTIFACT_ARCH_MAPPING = { |
| 20 | + 'x64': 'x86_64', |
| 21 | + 'arm64': 'aarch64', |
| 22 | + 'x32': False, |
| 23 | +} |
| 24 | +ARTIFACT_PLATFORM_MAPPING = { |
| 25 | + 'windows': 'pc-windows-msvc', |
| 26 | + 'osx': 'apple-darwin', |
| 27 | + 'linux': 'unknown-linux-gnu', |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +def get_artifact_name() -> str: |
| 32 | + sublime_arch = sublime.arch() |
| 33 | + arch = ARTIFACT_ARCH_MAPPING[sublime_arch] |
| 34 | + if arch is False: |
| 35 | + raise RuntimeError(f'Unsupported architecture: {sublime_arch}') |
| 36 | + sublime_platform = sublime.platform() |
| 37 | + platform = ARTIFACT_PLATFORM_MAPPING[sublime_platform] |
| 38 | + extension = 'zip' if sublime_platform == 'windows' else 'tar.gz' |
| 39 | + return f'pyproject-{TAG}-{arch}-{platform}.{extension}' |
| 40 | + |
| 41 | + |
| 42 | +@final |
| 43 | +class LspPyproject(AbstractPlugin): |
| 44 | + |
| 45 | + @classmethod |
| 46 | + @override |
| 47 | + def name(cls) -> str: |
| 48 | + return 'pyproject' |
| 49 | + |
| 50 | + @classmethod |
| 51 | + def basedir(cls) -> Path: |
| 52 | + return Path(cls.storage_path()) / str(__package__) |
| 53 | + |
| 54 | + @classmethod |
| 55 | + def server_version(cls) -> str: |
| 56 | + return TAG |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def current_server_version(cls) -> str: |
| 60 | + with open(cls.basedir() / "VERSION", "r") as fp: |
| 61 | + return fp.read() |
| 62 | + |
| 63 | + @classmethod |
| 64 | + @override |
| 65 | + def is_applicable(cls, view: sublime.View, config: ClientConfig) -> bool: |
| 66 | + is_applicable = super().is_applicable(view, config) |
| 67 | + return is_applicable and bool(filename := view.file_name()) and Path(filename).name == 'pyproject.toml' |
| 68 | + |
| 69 | + @classmethod |
| 70 | + @override |
| 71 | + def needs_update_or_installation(cls) -> bool: |
| 72 | + try: |
| 73 | + return cls.server_version() != cls.current_server_version() |
| 74 | + except OSError: |
| 75 | + return True |
| 76 | + |
| 77 | + @classmethod |
| 78 | + @override |
| 79 | + def install_or_update(cls) -> None: |
| 80 | + try: |
| 81 | + if cls.basedir().is_dir(): |
| 82 | + shutil.rmtree(cls.basedir()) |
| 83 | + cls.basedir().mkdir(exist_ok=True) |
| 84 | + version = cls.server_version() |
| 85 | + is_windows = sublime.platform() == "windows" |
| 86 | + extension = "zip" if is_windows else "tar.gz" |
| 87 | + archive_file = cls.basedir() / f"artifact.{extension}" |
| 88 | + server_binary_filename = "pyproject.exe" if is_windows else "pyproject" |
| 89 | + server_binary_path = cls.basedir() / server_binary_filename |
| 90 | + url = ARTIFACT_URL.format(tag=TAG, filename=get_artifact_name()) |
| 91 | + with urllib.request.urlopen(url) as fp: |
| 92 | + with open(archive_file, "wb") as f: |
| 93 | + f.write(cast(HTTPResponse, fp).read()) |
| 94 | + if is_windows: |
| 95 | + with zipfile.ZipFile(archive_file, "r") as zip_ref: |
| 96 | + zip_ref.extract(server_binary_filename, cls.basedir()) |
| 97 | + else: |
| 98 | + with tarfile.open(archive_file) as fp: |
| 99 | + names = fp.getnames() |
| 100 | + bad_members = [x for x in names if x.startswith('/') or x.startswith('..')] |
| 101 | + if bad_members: |
| 102 | + raise Exception(f'{archive_file} appears to be malicious, bad filenames: {bad_members}') |
| 103 | + fp.extractall(cls.basedir()) |
| 104 | + archive_file.unlink() |
| 105 | + server_binary_path.chmod(0o744) |
| 106 | + with open(cls.basedir() / "VERSION", "w") as fp: |
| 107 | + fp.write(version) |
| 108 | + except BaseException: |
| 109 | + shutil.rmtree(cls.basedir(), ignore_errors=True) |
| 110 | + raise |
| 111 | + |
| 112 | + |
| 113 | +def plugin_loaded() -> None: |
| 114 | + register_plugin(LspPyproject) |
| 115 | + |
| 116 | + |
| 117 | +def plugin_unloaded() -> None: |
| 118 | + unregister_plugin(LspPyproject) |
0 commit comments