29
29
30
30
DEFAULT_VERSION = "2.0.0"
31
31
32
+
32
33
def options ():
33
34
parser = argparse .ArgumentParser (add_help = False )
34
35
parser .add_argument ("tool" )
@@ -38,11 +39,15 @@ def options():
38
39
return parser .parse_known_args ()
39
40
40
41
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"
42
44
this_dir = pathlib .Path (__file__ ).resolve ().parent
43
45
version_file = this_dir / ".kotlinc_version"
44
46
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
+ )
46
51
47
52
48
53
class Error (Exception ):
@@ -62,16 +67,6 @@ def _extract_member(self, member, targetpath, pwd):
62
67
return targetpath
63
68
64
69
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
70
def get_version ():
76
71
try :
77
72
return version_file .read_text ()
@@ -86,29 +81,39 @@ def install(version: str, quiet: bool):
86
81
else :
87
82
info_out = sys .stderr
88
83
info = lambda * args : print (* args , file = sys .stderr )
84
+ file = file_template .format (version = version )
89
85
url = url_template .format (version = version )
90
86
if install_dir .exists ():
91
87
shutil .rmtree (install_dir )
92
88
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 )
93
98
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
+ ):
95
104
ripunzip = windows_ripunzip
96
105
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 :
112
117
archive .extractall (install_dir )
113
118
114
119
@@ -130,6 +135,9 @@ def clear():
130
135
if version_file .exists ():
131
136
print (f"removing { version_file } " , file = sys .stderr )
132
137
version_file .unlink ()
138
+ if zips_dir .exists ():
139
+ print (f"removing { zips_dir } " , file = sys .stderr )
140
+ shutil .rmtree (zips_dir )
133
141
134
142
135
143
def main (opts , forwarded_opts ):
@@ -140,7 +148,6 @@ def main(opts, forwarded_opts):
140
148
if opts .select == "default" :
141
149
selected_version = DEFAULT_VERSION
142
150
elif opts .select is not None :
143
- check_version (opts .select )
144
151
selected_version = opts .select
145
152
else :
146
153
selected_version = current_version or DEFAULT_VERSION
@@ -153,7 +160,10 @@ def main(opts, forwarded_opts):
153
160
return
154
161
if opts .version :
155
162
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
+ )
157
167
return
158
168
forwarded_opts .append ("-version" )
159
169
0 commit comments