Skip to content

Commit 0421cef

Browse files
authored
Merge pull request #16922 from github/redsun82/kotlin-wrapper
Kotlin: make wrapper cache downloaded zips
2 parents 31a5a7a + a30e7d2 commit 0421cef

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

java/kotlin-extractor/dev/.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
/.kotlinc_version
2-
/.kotlinc_installed
1+
/.kotlinc_*

java/kotlin-extractor/dev/wrapper.py

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
DEFAULT_VERSION = "2.0.0"
3131

32+
3233
def options():
3334
parser = argparse.ArgumentParser(add_help=False)
3435
parser.add_argument("tool")
@@ -38,11 +39,15 @@ def options():
3839
return parser.parse_known_args()
3940

4041

41-
url_template = 'https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip'
42+
file_template = "kotlin-compiler-{version}.zip"
43+
url_template = "https://github.com/JetBrains/kotlin/releases/download/v{version}/kotlin-compiler-{version}.zip"
4244
this_dir = pathlib.Path(__file__).resolve().parent
4345
version_file = this_dir / ".kotlinc_version"
4446
install_dir = this_dir / ".kotlinc_installed"
45-
windows_ripunzip = this_dir.parents[4] / "resources" / "lib" / "windows" / "ripunzip" / "ripunzip.exe"
47+
zips_dir = this_dir / ".kotlinc_zips"
48+
windows_ripunzip = (
49+
this_dir.parents[4] / "resources" / "lib" / "windows" / "ripunzip" / "ripunzip.exe"
50+
)
4651

4752

4853
class Error(Exception):
@@ -62,16 +67,6 @@ def _extract_member(self, member, targetpath, pwd):
6267
return targetpath
6368

6469

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-
7570
def get_version():
7671
try:
7772
return version_file.read_text()
@@ -86,29 +81,39 @@ def install(version: str, quiet: bool):
8681
else:
8782
info_out = sys.stderr
8883
info = lambda *args: print(*args, file=sys.stderr)
84+
file = file_template.format(version=version)
8985
url = url_template.format(version=version)
9086
if install_dir.exists():
9187
shutil.rmtree(install_dir)
9288
install_dir.mkdir()
89+
zips_dir.mkdir(exist_ok=True)
90+
zip = zips_dir / file
91+
92+
if not zip.exists():
93+
info(f"downloading {url}")
94+
tmp_zip = zip.with_suffix(".tmp")
95+
with open(tmp_zip, "wb") as out, urllib.request.urlopen(url) as response:
96+
shutil.copyfileobj(response, out)
97+
tmp_zip.rename(zip)
9398
ripunzip = shutil.which("ripunzip")
94-
if ripunzip is None and platform.system() == "Windows" and windows_ripunzip.exists():
99+
if (
100+
ripunzip is None
101+
and platform.system() == "Windows"
102+
and windows_ripunzip.exists()
103+
):
95104
ripunzip = windows_ripunzip
96105
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:
106+
info(f"extracting {zip} using ripunzip")
107+
subprocess.run(
108+
[ripunzip, "unzip-file", zip],
109+
stdout=info_out,
110+
stderr=info_out,
111+
cwd=install_dir,
112+
check=True,
113+
)
114+
else:
115+
info(f"extracting {zip}")
116+
with ZipFilePreservingPermissions(zip) as archive:
112117
archive.extractall(install_dir)
113118

114119

@@ -130,6 +135,9 @@ def clear():
130135
if version_file.exists():
131136
print(f"removing {version_file}", file=sys.stderr)
132137
version_file.unlink()
138+
if zips_dir.exists():
139+
print(f"removing {zips_dir}", file=sys.stderr)
140+
shutil.rmtree(zips_dir)
133141

134142

135143
def main(opts, forwarded_opts):
@@ -140,7 +148,6 @@ def main(opts, forwarded_opts):
140148
if opts.select == "default":
141149
selected_version = DEFAULT_VERSION
142150
elif opts.select is not None:
143-
check_version(opts.select)
144151
selected_version = opts.select
145152
else:
146153
selected_version = current_version or DEFAULT_VERSION
@@ -153,7 +160,10 @@ def main(opts, forwarded_opts):
153160
return
154161
if opts.version:
155162
if opts.tool == "kotlinc":
156-
print(f"info: kotlinc-jvm {selected_version} (codeql dev wrapper)", file=sys.stderr)
163+
print(
164+
f"info: kotlinc-jvm {selected_version} (codeql dev wrapper)",
165+
file=sys.stderr,
166+
)
157167
return
158168
forwarded_opts.append("-version")
159169

0 commit comments

Comments
 (0)