|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +Wrapper script that manages kotlin versions. |
| 5 | +Usage: add this directory to your PATH, then |
| 6 | +* `kotlin* --select x.y.z` will select the version for the next invocations, checking it actually exists |
| 7 | +* `kotlin* --clear` will remove any state of the wrapper (deselecting a previous version selection) |
| 8 | +* `kotlinc -version` will print the selected version information. It will not print `JRE` information as a normal |
| 9 | + `kotlinc` invocation would do though. In exchange, the invocation incurs no overhead. |
| 10 | +* Any other invocation will forward to the selected kotlin tool version, downloading it if necessary. If no version was |
| 11 | + previously selected with `--select`, a default will be used (see `DEFAULT_VERSION` below) |
| 12 | +
|
| 13 | +In order to install kotlin, ripunzip will be used if installed, or if running on Windows within `semmle-code` (ripunzip |
| 14 | +is available in `resources/lib/windows/ripunzip` then). |
| 15 | +""" |
| 16 | + |
| 17 | +import pathlib |
| 18 | +import urllib |
| 19 | +import urllib.request |
| 20 | +import urllib.error |
| 21 | +import argparse |
| 22 | +import sys |
| 23 | +import platform |
| 24 | +import subprocess |
| 25 | +import zipfile |
| 26 | +import shutil |
| 27 | +import io |
| 28 | +import os |
| 29 | + |
| 30 | +DEFAULT_VERSION = "2.0.0" |
| 31 | + |
| 32 | +def options(): |
| 33 | + parser = argparse.ArgumentParser(add_help=False) |
| 34 | + parser.add_argument("tool") |
| 35 | + parser.add_argument("--select") |
| 36 | + parser.add_argument("--clear", action="store_true") |
| 37 | + parser.add_argument("-version", action="store_true") |
| 38 | + return parser.parse_known_args() |
| 39 | + |
| 40 | + |
| 41 | +url_template = 'https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip' |
| 42 | +this_dir = pathlib.Path(__file__).resolve().parent |
| 43 | +version_file = this_dir / ".kotlinc_version" |
| 44 | +install_dir = this_dir / ".kotlinc_installed" |
| 45 | +windows_ripunzip = this_dir.parents[4] / "resources" / "lib" / "windows" / "ripunzip" / "ripunzip.exe" |
| 46 | + |
| 47 | + |
| 48 | +class Error(Exception): |
| 49 | + pass |
| 50 | + |
| 51 | + |
| 52 | +class ZipFilePreservingPermissions(zipfile.ZipFile): |
| 53 | + def _extract_member(self, member, targetpath, pwd): |
| 54 | + if not isinstance(member, zipfile.ZipInfo): |
| 55 | + member = self.getinfo(member) |
| 56 | + |
| 57 | + targetpath = super()._extract_member(member, targetpath, pwd) |
| 58 | + |
| 59 | + attr = member.external_attr >> 16 |
| 60 | + if attr != 0: |
| 61 | + os.chmod(targetpath, attr) |
| 62 | + return targetpath |
| 63 | + |
| 64 | + |
| 65 | +def check_version(version: str): |
| 66 | + try: |
| 67 | + with urllib.request.urlopen(url_template.format(version=version)) as response: |
| 68 | + pass |
| 69 | + except urllib.error.HTTPError as e: |
| 70 | + if e.code == 404: |
| 71 | + raise Error(f"Version {version} not found in github.com/JetBrains/kotlin/releases") from e |
| 72 | + raise |
| 73 | + |
| 74 | + |
| 75 | +def get_version(): |
| 76 | + try: |
| 77 | + return version_file.read_text() |
| 78 | + except FileNotFoundError: |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def install(version: str, quiet: bool): |
| 83 | + if quiet: |
| 84 | + info_out = subprocess.DEVNULL |
| 85 | + info = lambda *args: None |
| 86 | + else: |
| 87 | + info_out = sys.stderr |
| 88 | + info = lambda *args: print(*args, file=sys.stderr) |
| 89 | + url = url_template.format(version=version) |
| 90 | + if install_dir.exists(): |
| 91 | + shutil.rmtree(install_dir) |
| 92 | + install_dir.mkdir() |
| 93 | + ripunzip = shutil.which("ripunzip") |
| 94 | + if ripunzip is None and platform.system() == "Windows" and windows_ripunzip.exists(): |
| 95 | + ripunzip = windows_ripunzip |
| 96 | + if ripunzip: |
| 97 | + info(f"downloading and extracting {url} using ripunzip") |
| 98 | + subprocess.run([ripunzip, "unzip-uri", url], stdout=info_out, stderr=info_out, cwd=install_dir, |
| 99 | + check=True) |
| 100 | + return |
| 101 | + with io.BytesIO() as buffer: |
| 102 | + info(f"downloading {url}") |
| 103 | + with urllib.request.urlopen(url) as response: |
| 104 | + while True: |
| 105 | + bytes = response.read() |
| 106 | + if not bytes: |
| 107 | + break |
| 108 | + buffer.write(bytes) |
| 109 | + buffer.seek(0) |
| 110 | + info(f"extracting kotlin-compiler-{version}.zip") |
| 111 | + with ZipFilePreservingPermissions(buffer) as archive: |
| 112 | + archive.extractall(install_dir) |
| 113 | + |
| 114 | + |
| 115 | +def forward(tool, forwarded_opts): |
| 116 | + tool = install_dir / "kotlinc" / "bin" / tool |
| 117 | + if platform.system() == "Windows": |
| 118 | + tool = tool.with_suffix(".bat") |
| 119 | + assert tool.exists(), f"{tool} not found" |
| 120 | + args = [tool] |
| 121 | + args.extend(forwarded_opts) |
| 122 | + ret = subprocess.run(args).returncode |
| 123 | + sys.exit(ret) |
| 124 | + |
| 125 | + |
| 126 | +def clear(): |
| 127 | + if install_dir.exists(): |
| 128 | + print(f"removing {install_dir}", file=sys.stderr) |
| 129 | + shutil.rmtree(install_dir) |
| 130 | + if version_file.exists(): |
| 131 | + print(f"removing {version_file}", file=sys.stderr) |
| 132 | + version_file.unlink() |
| 133 | + |
| 134 | + |
| 135 | +def main(opts, forwarded_opts): |
| 136 | + if opts.clear: |
| 137 | + clear() |
| 138 | + return |
| 139 | + current_version = get_version() |
| 140 | + if opts.select == "default": |
| 141 | + selected_version = DEFAULT_VERSION |
| 142 | + elif opts.select is not None: |
| 143 | + check_version(opts.select) |
| 144 | + selected_version = opts.select |
| 145 | + else: |
| 146 | + selected_version = current_version or DEFAULT_VERSION |
| 147 | + if selected_version != current_version: |
| 148 | + # don't print information about install procedure unless explicitly using --select |
| 149 | + install(selected_version, quiet=opts.select is None) |
| 150 | + version_file.write_text(selected_version) |
| 151 | + if opts.select and not forwarded_opts and not opts.version: |
| 152 | + print(f"selected {selected_version}") |
| 153 | + return |
| 154 | + if opts.version: |
| 155 | + if opts.tool == "kotlinc": |
| 156 | + print(f"info: kotlinc-jvm {selected_version} (codeql dev wrapper)", file=sys.stderr) |
| 157 | + return |
| 158 | + forwarded_opts.append("-version") |
| 159 | + |
| 160 | + forward(opts.tool, forwarded_opts) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + try: |
| 165 | + main(*options()) |
| 166 | + except Error as e: |
| 167 | + print(f"Error: {e}", file=sys.stderr) |
| 168 | + sys.exit(1) |
| 169 | + except KeyboardInterrupt: |
| 170 | + sys.exit(1) |
0 commit comments