1
0
Fork 0

auto deploy from workflow

This commit is contained in:
github-actions[bot] 2022-07-01 04:25:32 +00:00
parent 78dc82fa92
commit 6ad4bd0080

View file

@ -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,61 +15,75 @@ 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:
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
def run(command: str) -> list[str]:
return popen(command).readlines()
def command_exists(command: str) -> bool:
return len(run(f"command -v {command}")) == 1
#
# Common
# Check functions
#
# Checks environment incompatibility and apply fix if possible.
#
def cleanup():
"""Removes temporary files and folders downloaded by this script"""
def minimal_check():
"""
Minimal checks before executing any code.
Full checks will happen after downloading codes from the internet
"""
if exists(tmp_dir):
rmtree(tmp_dir)
print("Running checks...")
#
# Checks
#
exit_if_root()
exit_if_invalid_python_version()
exit_if_os_not_compatible()
exit_if_no_internet()
def exit_if_root():
"""Exits script if it was executed as root"""
"""Terminate execution if the script is being executed with superuser privilege."""
# 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"""
"""Terminates execution if the python version is lower than 3.7"""
# todo: implement
if sys.version_info.major != 3 or sys.version_info.minor < 7:
from platform import python_version
pass
print(
f" Your python version ({python_version()}) does not meet the minimum requirement (3.7).",
file=sys.stderr,
)
exit(1)
def exit_if_system_not_compatible():
"""Exits script if the OS is not linux or if pacman does not exist"""
def exit_if_os_not_compatible():
"""Terminate execution if the OS is not Arch Linux based."""
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)
if "linux" not in sys.platform.lower() or not command_exists("pacman"):
print(" Incompatible OS.", file=sys.stderr)
exit(1)
@ -78,30 +91,47 @@ 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)
print(" Failed to connect to the internet.", file=sys.stderr)
exit(1)
#
# Initializations
# Bootstrap functions
#
def install_git():
"""Installs git if it's not installed already"""
def bootstrap():
"""
Does minimal initialization.
Full initialization will happen after downloading codes from the internet.
"""
print("Initializing git")
if not command_exits("git"):
print("git was not installed already. Installing now.")
print("Bootstrapping...")
install_git()
# When testing, there is no need to download the repository.
if not exists("src/"):
clone_repository()
# 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 this repository (https://github.com/developomp/setup) to `/tmp` directory"""
print("Cloning git repository")
"""Clone the setup repository (https://github.com/developomp/setup) to `/tmp` directory"""
# remove existing files first
cleanup()
@ -120,54 +150,18 @@ def clone_repository():
#
#
#
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.
"""
install_git()
# skip if testing
if exists(".git/") and exists("src/"):
return
clone_repository()
# Add cloned directory to system path
sys.path.append(tmp_dir)
#
#
# Main function
#
def main():
minimal_check()
minimal_initialization()
bootstrap()
from src.entry import entry
entry()
cleanup()
# Cleanup
# Remove temporary files downloaded by this script
if exists(tmp_dir):
rmtree(tmp_dir)
if __name__ == "__main__":