Refactor define_os()
This commit is contained in:
parent
8c938e170b
commit
fa565874c3
1 changed files with 33 additions and 28 deletions
41
install.py
41
install.py
|
@ -38,26 +38,41 @@ class Installer:
|
||||||
def define_os(self) -> str:
|
def define_os(self) -> str:
|
||||||
"""Define what OS we are using."""
|
"""Define what OS we are using."""
|
||||||
|
|
||||||
os_release = Path("/etc/os-release")
|
|
||||||
|
|
||||||
oses = ("arch", "debian", "manjaro", "ubuntu")
|
oses = ("arch", "debian", "manjaro", "ubuntu")
|
||||||
|
os_release = Path("/etc/os-release")
|
||||||
# defaults for subprocess.run()
|
operating_system = None
|
||||||
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:
|
for operating_system in oses:
|
||||||
if operating_system in os_release.read_text().lower():
|
if operating_system in os_release.read_text().lower():
|
||||||
ui.info_3("Found", operating_system, "based distribution")
|
ui.info_3("Found", operating_system, "based distribution")
|
||||||
|
break
|
||||||
|
|
||||||
|
if operating_system is None:
|
||||||
|
ui.warning("Operating system wasn't found")
|
||||||
|
operating_system = input("What is it ? ")
|
||||||
|
|
||||||
|
if operating_system in ("arch", "debian"):
|
||||||
|
self._update_system(operating_system)
|
||||||
|
return operating_system
|
||||||
|
|
||||||
|
ui.fatal("I only support Arch and Debian based distros.")
|
||||||
|
return "Unsupported OS"
|
||||||
|
|
||||||
|
def _update_system(self, operating_system: str) -> None:
|
||||||
|
# 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
|
||||||
|
|
||||||
if operating_system in ("arch", "manjaro"):
|
if operating_system in ("arch", "manjaro"):
|
||||||
command = "sudo pacman -Syu"
|
command = "sudo pacman -Syu"
|
||||||
operating_system = "arch"
|
operating_system = "arch"
|
||||||
|
|
||||||
if operating_system in ("debian", "ubuntu"):
|
elif operating_system in ("debian", "ubuntu"):
|
||||||
command = "sudo apt update"
|
command = "sudo apt update"
|
||||||
operating_system = "debian"
|
operating_system = "debian"
|
||||||
|
|
||||||
|
else:
|
||||||
|
raise AssertionError("The operating system should have been defined")
|
||||||
|
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
command,
|
command,
|
||||||
check=True,
|
check=True,
|
||||||
|
@ -65,16 +80,6 @@ class Installer:
|
||||||
stdout=this_stdout,
|
stdout=this_stdout,
|
||||||
stderr=this_stderr,
|
stderr=this_stderr,
|
||||||
)
|
)
|
||||||
return operating_system
|
|
||||||
|
|
||||||
ui.warning("Operating system wasn't found")
|
|
||||||
operating_system = input("What is it ? ")
|
|
||||||
if operating_system in ("arch", "debian"):
|
|
||||||
return operating_system
|
|
||||||
|
|
||||||
ui.fatal("I only support Arch and Debian based distros.")
|
|
||||||
|
|
||||||
return "Unsupported OS"
|
|
||||||
|
|
||||||
def evaluate_condition(self, condition: str) -> bool:
|
def evaluate_condition(self, condition: str) -> bool:
|
||||||
"""Run a bash command. On success return True, else return False."""
|
"""Run a bash command. On success return True, else return False."""
|
||||||
|
|
Loading…
Add table
Reference in a new issue