atat-builder/run.py

107 lines
2.5 KiB
Python
Executable File

#!/usr/bin/python3
# Copyright (C) 2023 Sofus Albert Høgsbro Rose
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import sys
if not all([
sys.version_info.major == 3,
sys.version_info.minor in [9, 10, 11, 12, 13],
]):
sys.exit(1)
import os
from pathlib import Path
from uuid import uuid4
import platform
import shutil
import subprocess
import contextlib
####################
# - Constants
####################
SCRIPT_PATH = Path(__file__).resolve().parent
CMD_DEPENDENCIES = [
'podman',
'git',
]
PATH_COMPILE_ATAT = SCRIPT_PATH / 'scripts' / 'compile-debian.sh'
ATAT_GIT_URL = "https://git.sofus.io/so-rose/atat-mirror.git"
IMAGE_NAME = "docker.io/debian"
IMAGE_TAG_FMT = "{}-slim"
####################
# - Utilities
####################
@contextlib.contextmanager
def cd_dir(path_dir):
cwd_orig = Path.cwd()
os.chdir(path_dir)
try:
yield
finally:
os.chdir(cwd_orig)
def cmd_exists(cmd: str) -> bool:
"""Checks if a command exists. Supports Linux, Mac, Windows.
"""
return shutil.which(cmd) is not None
####################
# - Actions - Run
####################
def action_compile(debian_release: str) -> None:
path_atat_mirror = (SCRIPT_PATH / '.atat-mirror')
path_atat_bin = (SCRIPT_PATH / 'bin')
path_atat_bin.mkdir(exist_ok=True)
subprocess.run([
"git", "clone", ATAT_GIT_URL, path_atat_mirror
])
with open(PATH_COMPILE_ATAT, 'r') as f:
subprocess.run([
"podman", "run", "--rm", "-it",
"--volume", f"{path_atat_mirror}:/src",
"--volume", f"{path_atat_bin}:/root/bin",
"--workdir", "/src",
f"{IMAGE_NAME}:{IMAGE_TAG_FMT.format(debian_release)}",
"bash", "-xc", f.read()
])
####################
# - Script
####################
if __name__ == "__main__":
# Check Available Commands
for cmd in CMD_DEPENDENCIES:
if not cmd_exists(cmd) :
print(f"{cmd} is not installed. Please see --help for instructions.")
sys.exit(1)
with cd_dir(SCRIPT_PATH):
{
"compile": action_compile,
}[sys.argv[1]](*sys.argv[2:])