diff --git a/setup.py b/setup.py index 291a719..4785de9 100755 --- a/setup.py +++ b/setup.py @@ -1,10 +1,9 @@ #!/usr/bin/env python3 """ -developomp's arch linux setup script +> developomp's Arch Linux setup script < -This file is all that's needed for execution. -It'll download all the dependencies and related files automatically. +For more information, please refer to the repository (https://github.com/developomp/setup). """ from os import system, geteuid, popen @@ -16,111 +15,32 @@ import sys tmp_dir = "/tmp/com.developomp.setup" # -# Util +# Utility functions # -# These commands are from `src/util.py`. +# These functions are copied from `src/util.py`. # Comments can only be found over there. # -def silent_system(command: str) -> None: - system(f"{command} &> /dev/null") +def silent_system(command: str, suppress_error: bool = True) -> None: + if suppress_error: + system(f"{command} &> /dev/null") + else: + system(f"{command} > /dev/null") -def command_exits(command: str) -> bool: - return len(popen(f"command -v {command}").readlines()) == 1 - - -# -# Common -# - - -def cleanup(): - """Removes temporary files and folders downloaded by this script""" - - if exists(tmp_dir): - rmtree(tmp_dir) - - -# -# Checks -# - - -def exit_if_root(): - """Exits script if it was executed as root""" - - # todo: allow running script as root when implementing user creation/arch installation logic - if geteuid() == 0: - print("Do not run this script as root.", file=sys.stderr) - exit(1) - - -def exit_if_invalid_python_version(): - """Exits script if python version is lower than 3.7""" - - # todo: implement - - pass - - -def exit_if_system_not_compatible(): - """Exits script if the OS is not linux or if pacman does not exist""" - - print("Checking if system is compatible") - if "linux" not in sys.platform.lower() or not command_exits("pacman"): - print("This script should only be used on arch linux.", file=sys.stderr) - exit(1) - - -def exit_if_no_internet(): - """Exits script if there's no internet connection. - Pings archlinux.org for testing.""" - - print("Checking if there's internet connection") - if silent_system("ping -c 1 archlinux.org"): - print("Failed to connect to internet.", file=sys.stderr) - exit(1) - - -# -# Initializations -# - - -def install_git(): - """Installs git if it's not installed already""" - - print("Initializing git") - if not command_exits("git"): - print("git was not installed already. Installing now.") - system("sudo pacman -S --noconfirm --needed git") - - -def clone_repository(): - """Clone this repository (https://github.com/developomp/setup) to `/tmp` directory""" - - print("Cloning git repository") - - # remove existing files first - cleanup() - - # clone repository - if silent_system( - f"git clone --depth 1 https://github.com/developomp/setup.git {tmp_dir}" - ): - print("Failed to clone repository", file=sys.stderr) - exit(1) - - # allow everyone to read and write. - if system(f"chmod -R a+rw {tmp_dir}"): - print("Failed to change file permission for cloned repo", file=sys.stderr) - exit(1) +def run(command: str) -> list[str]: + return popen(command).readlines() + + +def command_exists(command: str) -> bool: + return len(run(f"command -v {command}")) == 1 # +# Check functions # +# Checks environment incompatibility and apply fix if possible. # @@ -130,45 +50,119 @@ def minimal_check(): Full checks will happen after downloading codes from the internet """ + print("Running checks...") + exit_if_root() exit_if_invalid_python_version() - exit_if_system_not_compatible() + exit_if_os_not_compatible() exit_if_no_internet() -def minimal_initialization(): +def exit_if_root(): + """Terminate execution if the script is being executed with superuser privilege.""" + + if geteuid() == 0: + print(" Do not run this script as root.", file=sys.stderr) + exit(1) + + +def exit_if_invalid_python_version(): + """Terminates execution if the python version is lower than 3.7""" + + if sys.version_info.major != 3 or sys.version_info.minor < 7: + from platform import python_version + + print( + f" Your python version ({python_version()}) does not meet the minimum requirement (3.7).", + file=sys.stderr, + ) + exit(1) + + +def exit_if_os_not_compatible(): + """Terminate execution if the OS is not Arch Linux based.""" + + if "linux" not in sys.platform.lower() or not command_exists("pacman"): + print(" Incompatible OS.", file=sys.stderr) + exit(1) + + +def exit_if_no_internet(): + """Exits script if there's no internet connection. + Pings archlinux.org for testing.""" + + if silent_system("ping -c 1 archlinux.org"): + print(" Failed to connect to the internet.", file=sys.stderr) + exit(1) + + +# +# Bootstrap functions +# + + +def bootstrap(): """ Does minimal initialization. Full initialization will happen after downloading codes from the internet. """ + print("Bootstrapping...") + install_git() - # skip if testing - if exists(".git/") and exists("src/"): - return + # When testing, there is no need to download the repository. + if not exists("src/"): + clone_repository() - clone_repository() - - # Add cloned directory to system path - sys.path.append(tmp_dir) - - -# -# -# - - -def main(): - minimal_check() - minimal_initialization() + # Add cloned directory to system path + sys.path.append(tmp_dir) from src.entry import entry entry() + +def install_git(): + """Install git if it's not installed already.""" + + if not command_exists("git"): + system("sudo pacman -S --noconfirm --needed git") + + +def clone_repository(): + """Clone the setup repository (https://github.com/developomp/setup) to `/tmp` directory""" + + # remove existing files first cleanup() + # clone repository + if silent_system( + f"git clone --depth 1 https://github.com/developomp/setup.git {tmp_dir}" + ): + print(" Failed to clone repository", file=sys.stderr) + exit(1) + + # allow everyone to read and write. + if system(f"chmod -R a+rw {tmp_dir}"): + print(" Failed to change file permission for cloned repo", file=sys.stderr) + exit(1) + + +# +# Main function +# + + +def main(): + minimal_check() + bootstrap() + + # Cleanup + # Remove temporary files downloaded by this script + if exists(tmp_dir): + rmtree(tmp_dir) + if __name__ == "__main__": main()