-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstall.py
More file actions
519 lines (406 loc) · 16.1 KB
/
install.py
File metadata and controls
519 lines (406 loc) · 16.1 KB
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
import os
import json
from subprocess import check_call, PIPE, Popen, CalledProcessError
from functools import partial
from urllib.request import urlretrieve
from shutil import rmtree
from setup.base import SetupBase
class Installer(SetupBase):
"""Class to install the application"""
def __init__(self, title, description):
"""Constructor for the installer class
Parameters
----------
title : str
The title of the installer
description : str
The description of the installer
"""
super(Installer, self).__init__(
title=title,
description=description
)
self._os_info_map = {}
self.prequisite_dependencies = []
self._application_components = []
self._process_os_info()
def _process_os_info(self):
"""Process the OS info and add it to the os info map"""
# determine which universal package manager to use for the current OS
_os_name = os.name
_os_package_manager_map = {
"nt": ["winget"],
"posix": ["apt", "yum", "dnf", "pacman", "zypper", "brew"],
"darwin": ["brew"],
"linux": ["apt"]
}
_pkg_manager_install_command_map = {
"apt": "sudo apt install",
"yum": "sudo yum install",
"dnf": "sudo dnf install",
"pacman": "sudo pacman -S",
"zypper": "sudo zypper install",
"brew": "brew install"
}
_pkg_manager_uninstall_command_map = {
"apt": "sudo apt remove",
"yum": "sudo yum remove",
"dnf": "sudo dnf remove",
"pacman": "sudo pacman -R",
"zypper": "sudo zypper remove",
"brew": "brew uninstall"
}
_pkg_manager_update_command_map = {
"apt": "sudo apt update",
"yum": "sudo yum update",
"dnf": "sudo dnf update",
"pacman": "sudo pac --noconfirm -Sy",
"zypper": "sudo zypper update",
"brew": "brew update"
}
self._os_info_map["os_name"] = _os_name
# determine which package manager to use if current OS is posix by installing a small test package
if _os_name in _os_package_manager_map:
if len(_os_package_manager_map[_os_name]) > 0:
for _package_manager in _os_package_manager_map[_os_name]:
if self._test_package_manager(_package_manager):
self._os_info_map["package_manager"] = _package_manager
break
else:
continue
if "package_manager" not in self._os_info_map:
raise Exception(f"No package manager found for {_os_name}")
else:
self._os_info_map["package_manager"] = _os_package_manager_map[_os_name][0]
self._os_info_map["install"] = _pkg_manager_install_command_map[self._os_info_map["package_manager"]]
self._os_info_map["uninstall"] = _pkg_manager_uninstall_command_map[self._os_info_map["package_manager"]]
self._os_info_map["update"] = _pkg_manager_update_command_map[self._os_info_map["package_manager"]]
self.log(
f"Installer Config: {json.dumps(self._os_info_map, indent=4)}")
def _test_package_manager(self, package_manager):
"""Test if a package manager is compatible with current OS
Parameters
----------
package_manager : str
The package manager to test
Returns
-------
bool
True if compatible, False otherwise
"""
_package_manager_alias_map = {
"apt": "apt-get",
"yum": "yum",
"dnf": "dnf",
"pacman": "pacman",
"zypper": "zypper",
"brew": "brew"
}
if package_manager in _package_manager_alias_map:
package_manager = _package_manager_alias_map[package_manager]
# check if package manaager is available on current OS
_package_manager_test_command = {
"apt": "apt-get --version",
"yum": "yum --version",
"dnf": "dnf --version",
"pacman": "pacman --version",
"zypper": "zypper --version",
"brew": "brew --version"
}
if package_manager in _package_manager_test_command:
_command = _package_manager_test_command[package_manager]
else:
_command = package_manager
try:
check_call(
_command, shell=True, stdout=PIPE, stderr=PIPE)
except CalledProcessError:
return False
return True
def install_dependecy(self, dependency, dep_check_command=None):
"""Install a dependency
Parameters
----------
dependency : str
The dependency to install
dep_check_command : str, optional
Command to check if the dependency is already installed
"""
dep_check_command = dep_check_command or ""
_return_dict = self._run_command(dep_check_command)
_returncode = _return_dict["returncode"]
if _returncode == 0:
self.log(
f"{dependency} is already installed, skipping...")
return
_commands = [
self._os_info_map["update"],
f"{self._os_info_map['install']} {dependency}"
]
for _command in _commands:
_return_dict = self._run_command(_command)
_returncode = _return_dict["returncode"]
_stderr = _return_dict["stderr"]
if _returncode != 0:
self.log(
f"[red]Failed to install {dependency}[/]")
self.log(
f"[red]{_stderr}[/]")
raise Exception(f"Failed to install {dependency}")
else:
self.log(
f"[green]{dependency} installed![/]")
def uninstall_dependency(self, dependency):
"""Uninstall a dependency
Parameters
----------
dependency : str
The dependency to uninstall
"""
_commands = [
f"{self._os_info_map['uninstall']} {dependency}"
]
for _command in _commands:
_return_dict = self._run_command(_command)
_returncode = _return_dict["returncode"]
_stderr = _return_dict["stderr"]
if _returncode != 0:
self.log(
f"[red]Failed to uninstall {dependency}[/]")
self.log(
f"[red]{_stderr}[/]")
raise Exception(f"Failed to uninstall {dependency}")
else:
self.log(
f"[green]{dependency} uninstalled![/]")
def add_prequisite_dependency(self, dependency):
"""Add prequisite dependency to the setup
Parameters
----------
dependency : str
The dependency to add
"""
self.prequisite_dependencies.append(dependency)
def add_application_component(self, component):
"""Add application component to the setup
Parameters
----------
component: dict
The component to add, should include the following keys:
- name: the name of the component
- package_manager_installable: whether the component is installable by the package manager
- install_command: the command to install the component
- install_url: the url to download the component
"""
self._application_components.append(component)
def _install_prequisite_dependencies(installer, task_name, prequisite_dependencies):
"""Install prequisite dependencies
Parameters
----------
installer : :class: `install.Installer`
The installer object
task_name : str
The task name
prequisite_dependencies : list of str
The prequisite dependencies
"""
_dep_check_command_map = {
"python@3.8": "python3 --version"
}
task_id = installer.get_task_id(task_name)
for _dependency in prequisite_dependencies:
installer.add_prequisite_dependency(_dependency)
for _dependency in installer.prequisite_dependencies:
installer._progress.update(
task_id, description=f"Installing {_dependency}")
installer.install_dependecy(
_dependency, _dep_check_command_map.get(_dependency, None))
installer._progress.update(task_id, advance=1)
installer._progress.update(
task_id, description="Prequisite dependencies installed!")
def _post_installation_commands(installer, task_name, post_install_commands):
"""Run commands after prequisite dependencies have been installed.
Parameters
----------
installer: :class: `install.Installer`
The installer object
taks_name: str
The task name
post_install_commands : list of str
The post install commands
"""
task_id = installer.get_task_id(task_name)
for _command in post_install_commands:
installer._progress.update(
task_id, description=f"Running {_command}")
_process = Popen(
_command, shell=True, stdout=PIPE, stderr=PIPE)
_process.wait()
if _process.returncode != 0:
installer.log(
f"[red]Failed to run {_command}[/]")
installer.log(
f"[red]{_process.stderr.read().decode('utf-8')}[/]")
raise Exception(f"Failed to run {_command}")
else:
installer.log(
f"[green]{_command} completed![/]")
installer.log(
f"[green]{_command}: Post install command completed[/]")
installer._progress.update(task_id, advance=1)
def _install_application_components(installer, task_name, component_dependencies):
task_id = installer.get_task_id(task_name)
def _handle_external_installation(component):
"""Handle external installation of application component
Parameters
----------
component: dict
The component to be installed
task_name: str
The task name
component_dependencies: list of dict
The list of components that are dependencies of the component
"""
_download_url = component["install_url"]
_user_directory = os.path.expanduser("~")
_installation_path = os.path.join(
_user_directory, ".checkpoint")
if not os.path.exists(_installation_path):
os.makedirs(_installation_path)
# download the component to the installation path using urlretrieve
_download_path = os.path.join(
_installation_path, os.path.basename(_download_url))
if not os.path.exists(_download_path):
urlretrieve(_download_url, _download_path)
def _handle_command_installation(component):
"""Handle command installation of application component
Parameters
----------
component: dict
The component to be installed
"""
_command = component["install_command"]
_return_dict = installer._run_command(_command)
_returncode = _return_dict["returncode"]
_stderr = _return_dict["stderr"]
_stdout = _return_dict["stdout"]
installer.log(
_stdout)
if _returncode != 0:
installer.log(
f"[red]{_stderr}[/]")
raise Exception(f"Failed to install {component['name']}")
def _determine_install_type(component):
"""Determine what type of installation is required for the component
Parameters
----------
component: dict
The component to be installed
Returns
-------
str
The type of installation required
"""
if component["package_manager_installable"]:
return "package_manager"
elif component["install_command"]:
return "command"
elif component["install_url"]:
return "external"
else:
raise ValueError("Couldn't determine intallation strategy")
_install_strategy2method = {
"package_manager": installer.install_dependecy,
"command": _handle_command_installation,
"external": _handle_external_installation
}
# install application components
for _component in component_dependencies:
installer._progress.update(
task_id, description=f"Installing {_component['name']}")
_strat = _determine_install_type(_component)
if _strat == "package_manager":
installer.install_dependecy(_component["name"])
else:
_install_strategy2method[_strat](_component)
installer._progress.update(task_id, advance=1)
installer._progress.update(
task_id, description="Application components installed!")
installer._progress.update(task_id, advance=1)
def _remove_prequisite_deps():
"""Remove prerequisite dependencies"""
installer._progress.update(
task_id, description="Removing prerequisite dependencies"
)
for _dependency in installer.prequisite_dependencies:
installer.uninstall_dependency(_dependency)
installer._progress.update(task_id, advance=1)
# skip venv cleanup for now
# _remove_venv()
_remove_prequisite_deps()
installer._progress.update(
task_id, description="Post installation cleanup complete!")
def _post_install_cleanup(installer, task_name):
"""Remove any artifacts left post-installation
Parameters
----------
installer: :class: `install.Installer`
The installer object
task_name: str
Name of the task
"""
task_id = installer.get_task_id(task_name)
def _remove_venv():
"""Remove the virtual environment"""
installer._progress.update(
task_id, description="Removing virutal environment"
)
_venv_path = os.path.join(
os.path.join(os.path.abspath(os.path.dirname(__file__))),
"venv"
)
installer.log(
f"[green]Removing {_venv_path}[/]")
if os.path.exists(_venv_path):
rmtree(_venv_path)
installer._progress.update(task_id, advance=1)
def install():
installer = Installer(title="Install checkpoint",
description="Your installation is starting, it may take a while...")
_prequisite_deps = [
"python@3.8"
]
_post_install_commands = [
"python3 -m pip install --upgrade pip"
]
_component_deps = [
{
"name": "Checkpoint CLI Tool",
"package_manager_installable": False,
"install_command": "pip3 install git+https://github.com/antrikshmisri/checkpoint.git@master",
"install_url": None,
},
{
"name": "Checkpoint GUI",
"package_manager_installable": False,
"install_command": None,
"install_url": "https://github.com/antrikshmisri/checkpoint/releases/download/v1.3/checkpoint.exe"
}
]
installer.add_task(task_name="Installing prequisite dependencies",
callback=partial(
_install_prequisite_dependencies, prequisite_dependencies=_prequisite_deps),
total=len(_prequisite_deps))
installer.add_task(task_name="Running post-installation commands",
callback=partial(
_post_installation_commands, post_install_commands=_post_install_commands),
total=len(_post_install_commands))
installer.add_task(task_name="Installing application components",
callback=partial(
_install_application_components, component_dependencies=_component_deps),
total=len(_component_deps))
# installer.add_task(task_name="Performing post-installation cleanup",
# callback=_post_install_cleanup, total=1)
installer.start()
if __name__ == "__main__":
install()