diff --git a/blend b/blend index 98335d7..32de591 100755 --- a/blend +++ b/blend @@ -19,7 +19,7 @@ import os import sys -import glob +from sys import stdout import time import shutil import socket @@ -27,6 +27,7 @@ import getpass import pexpect import argparse import subprocess +import yaml __version = '2.0.0' @@ -83,22 +84,63 @@ def error(err): print(colors.bold + colors.fg.red + '>> e: ' + colors.reset + colors.bold + err + colors.reset) + +def podman_it_remover(cmd: list): + ''' + Removes `-it` from a podman command if stdout is not a tty + ''' + return [x for x in cmd if x != '-it' or stdout.isatty()] + # END +# TODO: fix temp paths before committing -distro_map = { - 'arch-linux': 'quay.io/toolbx/arch-toolbox:latest', - 'debian': 'quay.io/toolbx-images/debian-toolbox:testing', - 'fedora-42': 'quay.io/fedora/fedora-toolbox:42', - 'centos': 'quay.io/toolbx-images/centos-toolbox:latest', - 'ubuntu-22.04': 'quay.io/toolbx/ubuntu-toolbox:22.04', - 'ubuntu-24.04': 'quay.io/toolbx/ubuntu-toolbox:24.04', -} + +def image_data_from_dict(dictionary): + ''' + Returns a distro map and aliases from a dict in this format: + + ``` + {'arch-linux': {'image': 'quay.io/toolbx/arch-toolbox:latest', 'aliases': ['arch']}} + ``` + + which is loaded in from yaml config files in this format: + + ```yaml + arch-linux: + image: "quay.io/toolbx/arch-toolbox:latest" + aliases: + - arch + ``` + ''' + distro_map = {} + aliases = {} + + for distro in dictionary.keys(): + distro_map[distro] = dictionary[distro]['image'] + try: + aliases[distro] = dictionary[distro]['aliases'] + except KeyError: + pass + + return (distro_map, aliases) + + +distro_map, aliases = image_data_from_dict( + yaml.safe_load(open('/usr/share/blend/images.yaml'))) +tmp = image_data_from_dict(yaml.safe_load(open('/system.yaml'))['images']) +distro_map += tmp[0] +aliases += tmp[1] +if os.path.exists(f'{os.getenv('XDG_CONFIG_HOME')}/blend/images.yaml'): + tmp = image_data_from_dict(yaml.safe_load( + open(f'{os.getenv('XDG_CONFIG_HOME')}/blend/images.yaml'))) + distro_map += tmp[0] + aliases += tmp[1] default_distro = 'arch-linux' -def get_distro(): +def get_image(): try: return distro_map[args.distro] except: @@ -214,7 +256,7 @@ def core_create_container(): '--userns', 'keep-id', '--annotation', 'run.oci.keep_original_groups=1']) - podman_command.extend([get_distro()]) + podman_command.extend([get_image()]) # User (for init-blend) podman_command.extend(['--uid', str(os.geteuid())]) @@ -238,20 +280,19 @@ def host_get_output(cmd): return subprocess.run(['bash', '-c', cmd], stdout=subprocess.PIPE, stderr=subprocess.DEVNULL).stdout.decode('UTF-8').strip() -def core_get_retcode(cmd): return subprocess.run(['podman', 'exec', '--user', getpass.getuser(), '-it', args.container_name, 'bash', '-c', cmd], - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode +def core_get_retcode(cmd): + podman_command = podman_it_remover(['podman', 'exec', '--user', getpass.getuser(), '-it', + args.container_name, 'bash', '-c', cmd]) + return subprocess.run(podman_command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL).returncode def core_run_container(cmd): if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'): - subprocess.call(['podman', 'exec', '--user', getpass.getuser(), - '-w', os.getcwd(), '-it', args.container_name, 'bash', '-c', cmd]) + subprocess.call(podman_it_remover(['podman', 'exec', '--user', getpass.getuser(), + '-w', os.getcwd(), '-it', args.container_name, 'bash', '-c', cmd])) def core_install_pkg(pkg): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): if args.noconfirm == True: core_run_container(f'sudo dnf -y install {pkg}') @@ -278,9 +319,6 @@ def core_install_pkg(pkg): def core_remove_pkg(pkg): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): if args.noconfirm == True: core_run_container(f'sudo dnf -y remove {pkg}') @@ -300,9 +338,6 @@ def core_remove_pkg(pkg): def core_search_pkg(pkg): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): core_run_container(f'dnf search {pkg}') elif args.distro == 'arch-linux': @@ -314,9 +349,6 @@ def core_search_pkg(pkg): def core_show_pkg(pkg): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): core_run_container(f'dnf info {pkg}') elif args.distro == 'arch-linux': @@ -371,9 +403,6 @@ def show_blend(): def sync_blends(): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): core_run_container(f'dnf makecache') elif args.distro == 'arch-linux': @@ -383,9 +412,6 @@ def sync_blends(): def update_blends(): - if args.distro == 'arch': - args.distro = 'arch-linux' - if args.distro.startswith('fedora-'): if args.noconfirm == True: core_run_container(f'sudo dnf -y upgrade') @@ -424,30 +450,32 @@ def enter_container(): if not os.environ.get('BLEND_COMMAND'): if args.pkg == []: if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'): - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, - '-w', os.getcwd(), '-it', args.container_name, 'bash'])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, + '-w', os.getcwd(), '-it', args.container_name, 'bash']))) + else: - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', - '/run/host' + os.getcwd(), '-it', args.container_name, 'bash'])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w', + '/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))) else: if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'): - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, - '-w', os.getcwd(), '-it', args.container_name, *args.pkg])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, + '-w', os.getcwd(), '-it', args.container_name, *args.pkg]))) else: - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', - '/run/host' + os.getcwd(), '-it', args.container_name, *args.pkg])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w', + '/run/host' + os.getcwd(), '-it', args.container_name, *args.pkg]))) + else: if os.getcwd() == os.path.expanduser('~') or os.getcwd().startswith(os.path.expanduser('~') + '/'): - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', os.getcwd( - ), '-it', args.container_name, 'bash', '-c', os.environ.get('BLEND_COMMAND')])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w', os.getcwd(), '-it', + args.container_name, 'bash', '-c', os.environ.get('BLEND_COMMAND')]))) + else: - exit(subprocess.call([*sudo, 'podman', 'exec', *podman_args, '-w', - '/run/host' + os.getcwd(), '-it', args.container_name, 'bash'])) + exit(subprocess.call(podman_it_remover([*sudo, 'podman', 'exec', *podman_args, '-w', + '/run/host' + os.getcwd(), '-it', args.container_name, 'bash']))) def create_container(): for container in args.pkg: - container = 'ubuntu-24.04' if container == 'ubuntu-24.04-lts' else container args.container_name = container if container in distro_map.keys() and distro_input == None: args.distro = container @@ -531,6 +559,10 @@ if len(sys.argv) == 1: exit() args = parser.parse_intermixed_args() +if args.distro not in distro_map.keys(): + for (distro, al) in aliases: + if args.distro in al: + args.distro = distro command = command_map[args.command] diff --git a/images.yaml b/images.yaml new file mode 100644 index 0000000..8753f84 --- /dev/null +++ b/images.yaml @@ -0,0 +1,18 @@ +arch-linux: + image: quay.io/toolbx/arch-toolbox:latest + aliases: + - arch +debian: + image: quay.io/toolbx-images/debian-toolbox:testing +fedora-42: + image: quay.io/fedora/fedora-toolbox:42 + aliases: + - fedora +centos: + image: quay.io/toolbx-images/centos-toolbox:latest +ubuntu-22.04: + image: quay.io/toolbx/ubuntu-toolbox:22.04 +ubuntu-24.04: + image: quay.io/toolbx/ubuntu-toolbox:24.04 + aliases: + - ubuntu