diff --git a/index.html b/index.html index 2e788c2..1520712 100644 --- a/index.html +++ b/index.html @@ -14,22 +14,43 @@ from shutil import rmtree # must be synced with `src/__init__.py` tmp_dir = "/tmp/com.developomp.setup" +# +# Common +# -def minimal_check(): - """ - Minimal checks before executing any code. - Full checks will happen after downloading codes from the internet - """ - # check if the script is running as root +def cleanup(): + """Removes temporary files and folders downloaded by this script""" + + if os.path.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 os.geteuid() == 0: print("Do not run this script as root.", file=sys.stderr) exit(1) - # todo: check if python version is higher than 3.7 - # check if OS is linux and if pacman exists +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 os.system( "command -v pacman &> /dev/null" @@ -37,52 +58,87 @@ def minimal_check(): print("This script should only be used on arch linux.", file=sys.stderr) exit(1) - # ping archlinux.org to test if there's internet connection + +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 os.system("ping -c 1 archlinux.org &> /dev/null"): 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 os.system("command -v git &> /dev/null"): + print("git was not installed already. Installing now.") + os.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 os.system( + f"git clone --depth 1 https://github.com/developomp/setup.git {tmp_dir} &> /dev/null" + ): + print("Failed to clone repository", file=sys.stderr) + exit(1) + + # allow everyone to read and write. + if os.system(f"chmod -R a+rw {tmp_dir}"): + print("Failed to change file permission for cloned repo", file=sys.stderr) + exit(1) + + +# +# +# + + +def minimal_check(): + """ + Minimal checks before executing any code. + Full checks will happen after downloading codes from the internet + """ + + exit_if_root() + exit_if_invalid_python_version() + exit_if_system_not_compatible() + exit_if_no_internet() + + def minimal_initialization(): """ Does minimal initialization. Full initialization will happen after downloading codes from the internet. """ - print("Initializing git") - if os.system("command -v git &> /dev/null"): - print("git was not installed already. Installing now.") - os.system("sudo pacman -S --noconfirm --needed git") - - # - # Download necessary files - # - - print("Cloning git repository") - - # remove existing files - if os.path.exists(tmp_dir): - rmtree(tmp_dir) - - if os.system( - f"git clone --depth 1 https://github.com/developomp/setup.git {tmp_dir} &> /dev/null" - ): - print("Failed to clone repository", file=sys.stderr) - exit(1) - - # allow everyone to read and write but not execute. - if os.system(f"chmod -R a+rw {tmp_dir}"): - print("Failed to change file permission for cloned repo", file=sys.stderr) - exit(1) - - # - # Add cloned directory to path - # + install_git() + clone_repository() + # Add cloned directory to system path sys.path.append(tmp_dir) +# +# +# + + def main(): minimal_check() minimal_initialization() @@ -91,6 +147,8 @@ def main(): entry() + cleanup() + if __name__ == "__main__": main()