-
Notifications
You must be signed in to change notification settings - Fork 0
/
installer.py
396 lines (360 loc) · 14.7 KB
/
installer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import argparse
import logging
import os
import re
import subprocess
import sys
import urllib.request
import winreg
import zipfile
from pathlib import Path
from shutil import rmtree, which
import requests
from tqdm import tqdm
class CustomFormatter(logging.Formatter):
grey = "\x1b[38;21m"
yellow = "\x1b[33;21m"
red = "\x1b[31;21m"
bold_red = "\x1b[31;1m"
reset = "\x1b[0m"
f = "%(levelname)s: %(message)s"
FORMATS = {
logging.DEBUG: grey + f + reset,
logging.INFO: grey + f + reset,
logging.WARNING: yellow + f + reset,
logging.ERROR: red + f + reset,
logging.CRITICAL: bold_red + f + reset,
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt)
return formatter.format(record)
class TqdmUpTo(tqdm):
"""Alternative Class-based version of the above.
Provides `update_to(n)` which uses `tqdm.update(delta_n)`.
Inspired by [twine#242](https://github.com/pypa/twine/pull/242),
[here](https://github.com/pypa/twine/commit/42e55e06).
"""
def update_to(self, b=1, bsize=1, tsize=None):
"""
b : int, optional
Number of blocks transferred so far [default: 1].
bsize : int, optional
Size of each block (in tqdm units) [default: 1].
tsize : int, optional
Total size (in tqdm units). If [default: None] remains unchanged.
"""
if tsize is not None:
self.total = tsize
return self.update(b * bsize - self.n) # also sets self.n = b * bsize
def update_path():
os.environ["PATH"] = subprocess.run(
[
"pwsh",
"-c",
'echo ([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";"'
' + [System.Environment]::GetEnvironmentVariable("Path","User"))',
],
capture_output=True,
).stdout.decode("utf-8")
def append_to_path(path, env="user"):
if env == "machine":
m_env = winreg.OpenKey(
winreg.HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment",
)
path_update = subprocess.run(
[
"pwsh",
"-c",
'[Environment]::SetEnvironmentVariable("Path",'
f' "{winreg.QueryValueEx(m_env, "Path")[0]};{path}",'
" [System.EnvironmentVariableTarget]::Machine)",
]
)
m_env.Close()
return path_update
elif env == "user":
u_env = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Environment")
path_update = subprocess.run(
[
"pwsh",
"-c",
'[Environment]::SetEnvironmentVariable("Path",'
f' "{winreg.QueryValueEx(u_env, "Path")[0]};{path}",'
" [System.EnvironmentVariableTarget]::User)",
]
)
u_env.Close()
return path_update
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="A simple Windows installer for Flutter development tools"
)
parser.add_argument(
"-v",
"--verbosity",
dest="loglevel",
help="verbosity level",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
)
# TODO: Add option to install to a different path
args = parser.parse_args()
logging.basicConfig(level=getattr(logging, args.loglevel))
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(CustomFormatter())
logger = logging.getLogger("logger")
logger.propagate = False
logger.addHandler(ch)
logger.info("Checking for Git...")
git_path = "git"
update_path()
if which("git", path=os.environ["PATH"]):
git_path = which("git", path=os.environ["PATH"]) # type: ignore
logger.info("Git found")
else:
logger.info("Attempting to install Git with winget...")
git_winget = subprocess.run(
["winget", "install", "--id", "Git.Git", "-e", "--source", "winget"]
)
update_path()
if git_winget.returncode == 0 and which("git", path=os.environ["PATH"]):
git_path = which("git", path=os.environ["PATH"]) # type: ignore
logger.info("Git successfully installed with winget")
else:
logger.warning(
"Failed to install Git with winget (perhaps winget not installed?),"
" trying manual download"
)
git_release = requests.get(
"https://github.com/git-for-windows/git/releases/latest"
)
git_installer_re = re.search(
r"(/git-for-windows/git/releases/download/(\S*)64-bit\.exe)",
git_release.text,
)
if git_installer_re is not None:
git_installer_url = f"https://github.com{git_installer_re.group()}"
git_installer_name = git_installer_url.split("/")[-1]
with TqdmUpTo(
unit="B",
unit_scale=True,
unit_divisor=1024,
miniters=1,
desc=git_installer_name,
) as t:
urllib.request.urlretrieve(
git_installer_url,
git_installer_name,
reporthook=t.update_to,
)
t.total = t.n
git_manual = subprocess.run([git_installer_name])
update_path()
if git_manual.returncode == 0 and which("git", path=os.environ["PATH"]):
git_path = which("git", path=os.environ["PATH"]) # type: ignore
logger.info("Git successfully installed")
logger.info("Cleaning up Git installer...")
os.remove(git_installer_name)
else:
logger.error(
"Failed to install Git, install manually from"
" https://git-scm.com/download/win and try running again"
)
input("Press enter to exit...")
sys.exit(1)
else:
logger.error(
"Failed to install Git, install manually from"
" https://git-scm.com/download/win and try running again"
)
input("Press enter to exit...")
sys.exit(1)
logger.info("Checking for Flutter...")
flutter_path = "flutter"
update_path()
if which("flutter", path=os.environ["PATH"]):
flutter_path = which("flutter", path=os.environ["PATH"]) # type: ignore
logger.info("Flutter SDK is already installed, skipping...")
else:
logger.info("Attempting to clone Flutter in C:\\src...")
subprocess.run(
[
git_path,
"clone",
"https://github.com/flutter/flutter.git",
"-b",
"stable",
"C:\\src",
]
)
logger.info("Cloned Flutter to C:\\src\\flutter")
logger.info("Attempting to update PATH...")
path_update = append_to_path("C:\\src\\flutter\\bin", "machine")
if path_update.returncode == 0:
logger.info("Updated PATH")
else:
logger.warning(
"Failed to update PATH, may be caused by permission issues, please"
" update PATH manually"
)
update_path()
if which("flutter", path=os.environ["PATH"]):
flutter_path = which("flutter", path=os.environ["PATH"]) # type: ignore
logger.info("Flutter SDK successfully installed")
else:
logger.error(
"Failed to install Flutter SDK, please install manually:"
" https://docs.flutter.dev/get-started/install/windows"
)
input("Press enter to exit...")
sys.exit(1)
if (
input(
"Do you want to install Android SDK, Android SDK Command-line Tools, and"
" Android SDK Build-Tools (only install if you do not already have it"
" installed; if unsure, do not install and opt for manual installation"
" instead)? [y/N] "
).lower()
== "y"
):
logger.info("Attempting to install Android toolchain..")
logger.info("Downloading Android command line tools...")
studio_page = requests.get("https://developer.android.com/studio")
cmdlinetools_installer_re = re.search(
r"(commandlinetools-win-(\S*)_latest.zip)",
studio_page.text,
)
if cmdlinetools_installer_re is not None:
cmdlinetools_installer_url = f"https://dl.google.com/android/repository/{cmdlinetools_installer_re.group()}"
cmdlinetools_installer_name = cmdlinetools_installer_url.split("/")[-1]
with TqdmUpTo(
unit="B",
unit_scale=True,
unit_divisor=1024,
miniters=1,
desc=cmdlinetools_installer_name,
) as t:
urllib.request.urlretrieve(
cmdlinetools_installer_url,
cmdlinetools_installer_name,
reporthook=t.update_to,
)
t.total = t.n
sdk_dir = Path(os.path.expandvars("%LOCALAPPDATA%\\Android\\Sdk"))
logger.info(
"Downloaded Android command line tools, attempting to unzip to"
f" {sdk_dir}"
)
logger.debug(f"Creating {sdk_dir} if not exists")
sdk_dir.mkdir(parents=True, exist_ok=True)
logger.debug(f"Unzipping {cmdlinetools_installer_name} to {sdk_dir}")
if (sdk_dir / "cmdline-tools").exists():
logger.warning(
f"{sdk_dir / 'cmdline-tools'} already exists, overwrite? [y/N]"
)
if input().lower() == "y":
rmtree(sdk_dir / "cmdline-tools")
(sdk_dir / "cmdline-tools").mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(cmdlinetools_installer_name) as z:
z.extractall(sdk_dir / "cmdline-tools")
(sdk_dir / "cmdline-tools" / "cmdline-tools").rename(
sdk_dir / "cmdline-tools" / "latest"
)
else:
logger.info("Skipping Android command line tools installation")
logger.info("Cleaning up Android command line tools installer...")
os.remove(cmdlinetools_installer_name)
logger.info("Attempting to update PATH...")
path_update = append_to_path(
"%LocalAppData%\\Android\\Sdk\\cmdline-tools\\latest\\bin", "user"
)
if path_update.returncode == 0:
logger.info("Updated PATH")
else:
logger.warning(
"Failed to update PATH, may be caused by permission issues, please"
" update PATH manually"
)
update_path()
if which("sdkmanager.bat", path=os.environ["PATH"]):
logger.info("Android command line tools successfully installed")
else:
logger.error(
"Failed to install Android command line tools, please continue"
" install manually:"
" https://docs.flutter.dev/get-started/install/windows#android-setup"
)
input("Press enter to exit...")
sys.exit(1)
logger.info(
"Attempting to install Android platform-tools, platforms;android-32,"
" build-tools;32.0.0, emulator, Google USB Driver..."
)
sdkmanager_install = subprocess.run(
[
f"{sdk_dir / 'cmdline-tools' / 'latest' / 'bin' / 'sdkmanager.bat'}",
"--install",
"platform-tools",
"platforms;android-32",
"build-tools;32.0.0",
"emulator",
"extras;google;usb_driver",
]
)
if sdkmanager_install.returncode == 0:
logger.info(
"Android platform-tools, platforms;android-32, build-tools;32.0.0,"
" emulator, Google USB Driver successfully installed"
)
logger.info("Attempting to update PATH...")
path_update = append_to_path(
"%LocalAppData%\\Android\\Sdk\\platform-tools", "user"
)
if path_update.returncode == 0:
logger.info("Updated PATH")
else:
logger.warning(
"Failed to update PATH, may be caused by permission issues,"
" please update PATH manually"
)
update_path()
else:
logger.error(
"Failed to install Android command line tools, please continue"
" install manually:"
" https://docs.flutter.dev/get-started/install/windows#android-setup"
)
input("Press enter to exit...")
sys.exit(1)
sdkmanager_update = subprocess.run(
[
f"{sdk_dir / 'cmdline-tools' / 'latest' / 'bin' / 'sdkmanager.bat'}",
"--update",
]
)
if sdkmanager_update.returncode == 0:
logger.info("Android toolchain successfully updated")
else:
logger.error(
"Failed to install Android command line tools, please continue"
" install manually:"
" https://docs.flutter.dev/get-started/install/windows#android-setup"
)
input("Press enter to exit...")
sys.exit(1)
else:
logger.info("Skipping Android toolchain installation")
android_licenses = subprocess.run([flutter_path, "doctor", "--android-licenses"])
if android_licenses.returncode == 0:
logger.info("Android licenses successfully accepted")
else:
logger.error(
"Failed to accept Android licenses (may need Java installation), please"
" continue manually:"
" https://docs.flutter.dev/get-started/install/windows#agree-to-android-licenses"
)
logger.info("Installation completed, running flutter doctor...")
subprocess.run([flutter_path, "doctor"])
input("Press enter to exit...")