From 35abf7b27a25a48870e679acdd0a5dc67c943e49 Mon Sep 17 00:00:00 2001 From: developomp Date: Thu, 27 Jan 2022 21:10:43 +0900 Subject: [PATCH] added more python files --- setup.py | 106 ++++++++++++++++++++++++++++++++++++++++++++++ src/__init__.py | 0 src/entry.py | 5 +++ src/initialize.py | 15 +++++++ src/log.py | 40 +++++++++++++++++ 5 files changed, 166 insertions(+) mode change 100644 => 100755 setup.py create mode 100644 src/__init__.py create mode 100644 src/entry.py create mode 100644 src/initialize.py create mode 100644 src/log.py diff --git a/setup.py b/setup.py old mode 100644 new mode 100755 index e5a0d9b..a6572cb --- a/setup.py +++ b/setup.py @@ -1 +1,107 @@ #!/usr/bin/env python3 + +""" +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. +""" + +import os +import sys +from shutil import rmtree + + +def minimal_check(): + """ + Minimal checks before executing any code. + Full checks will happen after downloading codes from the internet + """ + + # + # check if OS is linux and if pacman exists + # + + if ( + "linux" not in sys.platform.lower() + or os.system("command -v pacman &> /dev/null") != 0 + ): + print("This script should only be used on arch linux.", file=sys.stderr) + exit(1) + + # + # check if the script is running 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) + + # + # check if there's internet connection + # + + if os.system("ping -c 1 archlinux.org &> /dev/null") != 0: + print("Failed to connect to internet.", file=sys.stderr) + exit(1) + + +def minimal_initialization(): + """ + Does minimal initialization. + Full initialization will happen after downloading codes from the internet. + """ + + # + # Install git + # + + if os.system("command -v git &> /dev/null") != 0: + print("git was not installed already. Installing now.") + os.system("sudo pacman -S --noconfirm --needed git") + + # + # Download necessary files + # + + tmp_dir = "/tmp/com.developomp.setup" + + # remove existing files + if os.path.exists(tmp_dir): + rmtree(tmp_dir) + + # todo: change branch to master when merging with master + if ( + os.system( + f"git clone --depth 1 -b dev https://github.com/developomp/setup.git {tmp_dir} &> /dev/null" + ) + != 0 + ): + 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}") != 0: + print("Failed to change file permission for cloned repo", file=sys.stderr) + exit(1) + + # + # Add cloned directory to path + # + + sys.path.append(tmp_dir) + + +def main(): + minimal_check() + minimal_initialization() + + from src.entry import entry + + # hand over control to cloned code + entry() + + +if __name__ == "__main__": + main() diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/entry.py b/src/entry.py new file mode 100644 index 0000000..5f2b66e --- /dev/null +++ b/src/entry.py @@ -0,0 +1,5 @@ +from .initialize import initialize + + +def entry(): + initialize() diff --git a/src/initialize.py b/src/initialize.py new file mode 100644 index 0000000..a05b01a --- /dev/null +++ b/src/initialize.py @@ -0,0 +1,15 @@ +import os + + +def initialize(): + """ + - install [pytermgui](https://github.com/bczsalba/pytermgui) + - install pip + - install flatpak + """ + + # + # Install flatpak + # + + os.system("sudo pacman -S --noconfirm --needed flatpak") diff --git a/src/log.py b/src/log.py new file mode 100644 index 0000000..0cd9667 --- /dev/null +++ b/src/log.py @@ -0,0 +1,40 @@ +# https://stackoverflow.com/a/33206814/12979111 + +RESET = "\e[0m" + +# style +BOLD = "\e[1m" +INVERT = "\e[7m" + +# colors +RED = "\e[91m" # actually light red +GREEN = "\e[92m" # actually light green +YELLOW = "\e[33m" + + +def log_no_label(msg: str): + print(f"{GREEN}{BOLD}{msg}{RESET}") + + +def warn_no_label(msg: str): + print(f"{YELLOW}{BOLD}{msg}{RESET}") + + +def error_no_label(msg: str): + print(f"{RED}{BOLD}{msg}{RESET}") + + +def log(msg: str): + print(f" {GREEN}{BOLD} INFO | {msg}{RESET}") + + +def warn(msg: str): + print(f" {YELLOW}{BOLD} WARNING | {msg}{RESET}") + + +def error(msg: str): + print(f" {RED}{BOLD} ERROR | {msg}{RESET}") + + +def title(msg: str): + print(f"\n{BOLD}{GREEN}====================[ {msg} ]===================={RESET}")