Make do_install possible from dict instead of just list

This commit is contained in:
flyingscorpio@arch-desktop 2020-04-23 11:30:47 +02:00
parent 2a72401456
commit 426f602a99

View file

@ -105,30 +105,39 @@ class Installer:
if executable:
p_dest.chmod(0o755)
def do_install(self, *packages) -> None:
"""Install packages with OS-dependent package manager."""
def do_install(self, *packages, **os_specific_packages) -> None:
"""Install packages with OS-specific package manager.
Packages can either be in a list for non OS-specific packages, or in a
dict for OS-specific packages.
"""
if not packages: # we got the dict, make a list out of it
try:
packages = os_specific_packages[self.operating_system].split()\
+ os_specific_packages["both"].split()
except KeyError:
ui.fatal("Operating System not understood.")
return
failed_installs = []
ui.info_2("Installing packages...")
for package in packages:
ui.info_2("Installing", package, ui.ellipsis, end='')
if self.operating_system == "arch based":
command = "sudo pacman -S --needed {}".format(package)
elif self.operating_system == "debian based":
command = "sudo apt install {}".format(package)
else:
ui.fatal("Operating System not understood.")
return
install = subprocess.run(command, check=False, shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL)
if install.returncode != 0:
ui.info(ui.cross)
ui.info(ui.cross, package)
failed_installs.append(package)
else:
ui.info(ui.check)
ui.info(ui.check, package)
if len(failed_installs) > 0:
ui.warning("These packages failed to install:")
@ -269,8 +278,11 @@ def define_os() -> str:
return "debian based"
ui.warning("Operating system wasn't found")
operating_system = input("What is it ? ")
if operating_system in ("arch", "debian"):
return operating_system + " based"
return input("What is it ? ")
return ui.fatal("I only support Arch and Debian based distros.")
def main() -> None: