Skip to content

Commit be5c82c

Browse files
committed
Kotlin: add other tools to dev wrapper
1 parent c6039b3 commit be5c82c

File tree

7 files changed

+187
-163
lines changed

7 files changed

+187
-163
lines changed

java/kotlin-extractor/dev/kapt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
"$(dirname "$0")/wrapper.py" kapt "$@"

java/kotlin-extractor/dev/kapt.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
3+
python "%~dp0wrapper.py" kapt %*
4+
exit /b %ERRORLEVEL%

java/kotlin-extractor/dev/kotlin

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
"$(dirname "$0")/wrapper.py" kotlin "$@"

java/kotlin-extractor/dev/kotlin.bat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@echo off
2+
3+
python "%~dp0wrapper.py" kotlin %*
4+
exit /b %ERRORLEVEL%

java/kotlin-extractor/dev/kotlinc

Lines changed: 2 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,3 @@
1-
#!/usr/bin/env python3
1+
#!/bin/bash
22

3-
"""
4-
Wrapper script that manages kotlinc versions.
5-
Usage: add this directory to your PATH, then
6-
* `kotlinc --select x.y.z` will select the version for the next invocations, checking it actually exists
7-
* `kotlinc --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 kotlinc 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("--select")
35-
parser.add_argument("--clear", action="store_true")
36-
parser.add_argument("-version", action="store_true")
37-
return parser.parse_known_args()
38-
39-
40-
url_template = 'https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip'
41-
this_dir = pathlib.Path(__file__).resolve().parent
42-
version_file = this_dir / ".kotlinc_version"
43-
install_dir = this_dir / ".kotlinc_installed"
44-
windows_ripunzip = this_dir.parents[4] / "resources" / "lib" / "windows" / "ripunzip" / "ripunzip.exe"
45-
46-
47-
class Error(Exception):
48-
pass
49-
50-
51-
class ZipFilePreservingPermissions(zipfile.ZipFile):
52-
def _extract_member(self, member, targetpath, pwd):
53-
if not isinstance(member, zipfile.ZipInfo):
54-
member = self.getinfo(member)
55-
56-
targetpath = super()._extract_member(member, targetpath, pwd)
57-
58-
attr = member.external_attr >> 16
59-
if attr != 0:
60-
os.chmod(targetpath, attr)
61-
return targetpath
62-
63-
64-
def check_version(version: str):
65-
try:
66-
with urllib.request.urlopen(url_template.format(version=version)) as response:
67-
pass
68-
except urllib.error.HTTPError as e:
69-
if e.code == 404:
70-
raise Error(f"Version {version} not found in github.com/JetBrains/kotlin/releases") from e
71-
raise
72-
73-
74-
def get_version():
75-
try:
76-
return version_file.read_text()
77-
except FileNotFoundError:
78-
return None
79-
80-
81-
def install(version: str, quiet: bool):
82-
if quiet:
83-
info_out = subprocess.DEVNULL
84-
info = lambda *args: None
85-
else:
86-
info_out = sys.stderr
87-
info = lambda *args: print(*args, file=sys.stderr)
88-
url = url_template.format(version=version)
89-
if install_dir.exists():
90-
shutil.rmtree(install_dir)
91-
install_dir.mkdir()
92-
ripunzip = shutil.which("ripunzip")
93-
if ripunzip is None and platform.system() == "Windows" and windows_ripunzip.exists():
94-
ripunzip = windows_ripunzip
95-
if ripunzip:
96-
info(f"downloading and extracting {url} using ripunzip")
97-
subprocess.run([ripunzip, "unzip-uri", url], stdout=info_out, stderr=info_out, cwd=install_dir,
98-
check=True)
99-
return
100-
with io.BytesIO() as buffer:
101-
info(f"downloading {url}")
102-
with urllib.request.urlopen(url) as response:
103-
while True:
104-
bytes = response.read()
105-
if not bytes:
106-
break
107-
buffer.write(bytes)
108-
buffer.seek(0)
109-
info(f"extracting kotlin-compiler-{version}.zip")
110-
with ZipFilePreservingPermissions(buffer) as archive:
111-
archive.extractall(install_dir)
112-
113-
114-
def forward(forwarded_opts):
115-
kotlinc = install_dir / "kotlinc" / "bin" / "kotlinc"
116-
if platform.system() == "Windows":
117-
kotlinc = kotlinc.with_suffix(".bat")
118-
assert kotlinc.exists(), f"{kotlinc} not found"
119-
args = [kotlinc]
120-
args.extend(forwarded_opts)
121-
ret = subprocess.run(args).returncode
122-
sys.exit(ret)
123-
124-
125-
def clear():
126-
if install_dir.exists():
127-
print(f"removing {install_dir}", file=sys.stderr)
128-
shutil.rmtree(install_dir)
129-
if version_file.exists():
130-
print(f"removing {version_file}", file=sys.stderr)
131-
version_file.unlink()
132-
133-
134-
def main(opts, forwarded_opts):
135-
if opts.clear:
136-
clear()
137-
return
138-
current_version = get_version()
139-
if opts.select == "default":
140-
selected_version = DEFAULT_VERSION
141-
elif opts.select is not None:
142-
check_version(opts.select)
143-
selected_version = opts.select
144-
else:
145-
selected_version = current_version or DEFAULT_VERSION
146-
if selected_version != current_version:
147-
# don't print information about install procedure unless explicitly using --select
148-
install(selected_version, quiet=opts.select is None)
149-
version_file.write_text(selected_version)
150-
if opts.version or (opts.select and not forwarded_opts):
151-
print(f"info: kotlinc-jvm {selected_version} (codeql dev wrapper)", file=sys.stderr)
152-
return
153-
forward(forwarded_opts)
154-
155-
156-
if __name__ == "__main__":
157-
try:
158-
main(*options())
159-
except Error as e:
160-
print(f"Error: {e}", file=sys.stderr)
161-
sys.exit(1)
162-
except KeyboardInterrupt:
163-
sys.exit(1)
3+
"$(dirname "$0")/wrapper.py" kotlinc "$@"

java/kotlin-extractor/dev/kotlinc.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@echo off
22

3-
python "%~dp0kotlinc" %*
3+
python "%~dp0wrapper.py" kotlinc %*
44
exit /b %ERRORLEVEL%

java/kotlin-extractor/dev/wrapper.py

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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

Comments
 (0)