From fe481b0f0a765f64b962d712f55f2f06bcdfd8b5 Mon Sep 17 00:00:00 2001 From: "flyingscorpio@arch-desktop" Date: Fri, 4 Sep 2020 15:08:07 +0200 Subject: [PATCH] Fix subprocess output hidden, move define_os in class --- install.py | 93 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 48 insertions(+), 45 deletions(-) diff --git a/install.py b/install.py index 6e7eaff..ae391ba 100755 --- a/install.py +++ b/install.py @@ -29,12 +29,56 @@ class Installer: self.conf = yaml.load(Path("configs.yml").text()) self.base_dir = Path.getcwd() self.home = Path("~").expanduser() - self.operating_system = define_os() + self.operating_system = self.define_os() self.force = force self.first_install = first_install self.update = update self.hide_commands = hide_commands + def define_os(self) -> str: + """Define what OS we are using.""" + + os_release = Path("/etc/os-release") + + oses = ("arch", "debian", "manjaro", "ubuntu") + + # defaults for subprocess.run() + this_stdout: Optional[int] = subprocess.DEVNULL if self.hide_commands else None + this_stderr: Optional[int] = subprocess.DEVNULL if self.hide_commands else None + + for operating_system in oses: + if operating_system in os_release.read_text().lower(): + ui.info_3("Found", operating_system) + + if operating_system in ("arch", "manjaro"): + subprocess.run( + "sudo pacman -Syu", + check=True, + shell=True, + stdout=this_stdout, + stderr=this_stderr, + ) + return "arch based" + + if operating_system in ("debian", "ubuntu"): + subprocess.run( + "sudo apt update", + check=True, + shell=True, + stdout=this_stdout, + stderr=this_stderr, + ) + 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" + + ui.fatal("I only support Arch and Debian based distros.") + + return "Unsupported OS" + def evaluate_condition(self, condition: str) -> bool: """Run a bash command. On success return True, else return False.""" @@ -157,11 +201,9 @@ class Installer: failed_installs = [] - this_stdout: Optional[int] = None - this_stderr: Optional[int] = None - if self.hide_commands: - this_stdout = subprocess.DEVNULL - this_stderr = subprocess.DEVNULL + # defaults for subprocess.run() + this_stdout: Optional[int] = subprocess.DEVNULL if self.hide_commands else None + this_stderr: Optional[int] = subprocess.DEVNULL if self.hide_commands else None ui.info_2("Installing packages...") for i, package in enumerate(packages): @@ -337,45 +379,6 @@ class Installer: ui.info() -def define_os() -> str: - """Define what OS we are using.""" - - os_release = Path("/etc/os-release") - - oses = ("arch", "debian", "manjaro", "ubuntu") - - for operating_system in oses: - if operating_system in os_release.read_text().lower(): - ui.info_3("Found", operating_system) - - if operating_system in ("arch", "manjaro"): - subprocess.run( - "sudo pacman -Syu", - check=True, - shell=True, - stdout=subprocess.DEVNULL, - ) - return "arch based" - - if operating_system in ("debian", "ubuntu"): - subprocess.run( - "sudo apt update", - check=True, - shell=True, - stdout=subprocess.DEVNULL, - ) - 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" - - ui.fatal("I only support Arch and Debian based distros.") - - return "Unsupported OS" - - def read_programs_from_process_list() -> List[str]: """Read the process_list file to return a list of programs to process."""