Move enforce_wrapper() into appropiate CMDs. This speeds up --help as a side effect!

This commit is contained in:
InsanePrawn 2021-09-29 23:18:12 +02:00
parent 18c689f897
commit f3794f939e
11 changed files with 34 additions and 25 deletions

View file

@ -4,6 +4,7 @@ from image import get_device_and_flavour, get_image_name, dump_bootimg, dump_lk2
from fastboot import fastboot_boot, fastboot_erase_dtbo from fastboot import fastboot_boot, fastboot_erase_dtbo
from constants import BOOT_STRATEGIES, FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION from constants import BOOT_STRATEGIES, FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION
import click import click
from wrapper import enforce_wrap
LK2ND = FLASH_PARTS['LK2ND'] LK2ND = FLASH_PARTS['LK2ND']
BOOTIMG = FLASH_PARTS['BOOTIMG'] BOOTIMG = FLASH_PARTS['BOOTIMG']
@ -15,6 +16,7 @@ TYPES = [LK2ND, JUMPDRIVE, BOOTIMG]
@click.argument('type', required=False, default=BOOTIMG) @click.argument('type', required=False, default=BOOTIMG)
def cmd_boot(type): def cmd_boot(type):
f"""Flash one of {', '.join(TYPES)}""" f"""Flash one of {', '.join(TYPES)}"""
enforce_wrap()
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
image_name = get_image_name(device, flavour) image_name = get_image_name(device, flavour)
strategy = BOOT_STRATEGIES[device] strategy = BOOT_STRATEGIES[device]

View file

@ -1,6 +1,9 @@
import shutil import shutil
import click import click
import os import os
from config import config
from wrapper import enforce_wrap
import logging
@click.group(name='cache') @click.group(name='cache')
@ -8,15 +11,15 @@ def cmd_cache():
pass pass
@click.command(name='clean') @cmd_cache.command(name='clean')
def cmd_clean(): def cmd_clean():
for dir in ['/chroot', '/var/cache/pacman/pkg', '/var/cache/jumpdrive']: enforce_wrap()
for path_name in ['chroots', 'pacman', 'jumpdrive']:
dir = config.file['paths'][path_name]
for file in os.listdir(dir): for file in os.listdir(dir):
path = os.path.join(dir, file) path = os.path.join(dir, file)
logging.debug('Removing "{path}"')
if os.path.isdir(path): if os.path.isdir(path):
shutil.rmtree(path) shutil.rmtree(path)
else: else:
os.unlink(path) os.unlink(path)
cmd_cache.add_command(cmd_clean)

View file

@ -1,7 +1,6 @@
import logging import logging
import subprocess import subprocess
import os import os
import shutil
from config import config from config import config
from distro import get_base_distros, RepoInfo from distro import get_base_distros, RepoInfo

View file

@ -42,6 +42,7 @@ CONFIG_RUNTIME_DEFAULTS = {
'verbose': False, 'verbose': False,
'config_file': None, 'config_file': None,
'arch': None, 'arch': None,
'no_wrap': False,
} }

View file

@ -7,6 +7,7 @@ import os
import subprocess import subprocess
import click import click
import tempfile import tempfile
from wrapper import enforce_wrap
BOOTIMG = FLASH_PARTS['BOOTIMG'] BOOTIMG = FLASH_PARTS['BOOTIMG']
LK2ND = FLASH_PARTS['LK2ND'] LK2ND = FLASH_PARTS['LK2ND']
@ -18,6 +19,7 @@ ROOTFS = FLASH_PARTS['ROOTFS']
@click.argument('what') @click.argument('what')
@click.argument('location', required=False) @click.argument('location', required=False)
def cmd_flash(what, location): def cmd_flash(what, location):
enforce_wrap()
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
image_name = get_image_name(device, flavour) image_name = get_image_name(device, flavour)

View file

@ -2,10 +2,12 @@ import click
import subprocess import subprocess
from logger import logging from logger import logging
from ssh import cmd_ssh from ssh import cmd_ssh
from wrapper import enforce_wrap
@click.command(name='forwarding') @click.command(name='forwarding')
def cmd_forwarding(): def cmd_forwarding():
enforce_wrap()
result = subprocess.run([ result = subprocess.run([
'sysctl', 'sysctl',
'net.ipv4.ip_forward=1', 'net.ipv4.ip_forward=1',

View file

@ -4,9 +4,10 @@ import subprocess
import click import click
from logger import logging from logger import logging
from chroot import create_chroot, create_chroot_user, get_chroot_path, run_chroot_cmd from chroot import create_chroot, create_chroot_user, get_chroot_path, run_chroot_cmd
from constants import DEVICES, FLAVOURS, REPOSITORIES from constants import DEVICES, FLAVOURS
from config import config from config import config
from distro import get_kupfer_https, get_kupfer_local from distro import get_kupfer_https, get_kupfer_local
from wrapper import enforce_wrap
def get_device_and_flavour(profile=None) -> tuple[str, str]: def get_device_and_flavour(profile=None) -> tuple[str, str]:
@ -102,8 +103,9 @@ def cmd_image():
pass pass
@click.command(name='build') @cmd_image.command(name='build')
def cmd_build(): def cmd_build():
enforce_wrap()
profile = config.get_profile() profile = config.get_profile()
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
post_cmds = FLAVOURS[flavour].get('post_cmds', []) post_cmds = FLAVOURS[flavour].get('post_cmds', [])
@ -159,7 +161,7 @@ def cmd_build():
This doesn't work, because the mount isn't passed through to the real host This doesn't work, because the mount isn't passed through to the real host
""" """
""" """
@click.command(name='inspect') @cmd_image.command(name='inspect')
def cmd_inspect(): def cmd_inspect():
device, flavour = get_device_and_flavour() device, flavour = get_device_and_flavour()
image_name = get_image_name(device, flavour) image_name = get_image_name(device, flavour)
@ -171,6 +173,3 @@ def cmd_inspect():
signal.pause() signal.pause()
""" """
cmd_image.add_command(cmd_build)
# cmd_image.add_command(cmd_inspect)

View file

@ -9,7 +9,7 @@ from telnet import cmd_telnet
from logger import logging, setup_logging, verbose_option from logger import logging, setup_logging, verbose_option
import click import click
from config import config, config_option, cmd_config from config import config, config_option, cmd_config
from wrapper import enforce_wrap, nowrapper_option from wrapper import nowrapper_option
from traceback import format_exc as get_trace from traceback import format_exc as get_trace
@ -20,9 +20,8 @@ from traceback import format_exc as get_trace
def cli(verbose: bool = False, config_file: str = None, no_wrapper: bool = False): def cli(verbose: bool = False, config_file: str = None, no_wrapper: bool = False):
setup_logging(verbose) setup_logging(verbose)
config.runtime['verbose'] = verbose config.runtime['verbose'] = verbose
config.runtime['no_wrap'] = no_wrapper
config.try_load_file(config_file) config.try_load_file(config_file)
# TODO: move this only to CMDs where it's needed
enforce_wrap(no_wrapper=no_wrapper)
def main(): def main():
@ -32,6 +31,7 @@ def main():
logging.fatal(get_trace()) logging.fatal(get_trace())
exit(1) exit(1)
cli.add_command(cmd_config) cli.add_command(cmd_config)
cli.add_command(cmd_cache) cli.add_command(cmd_cache)
cli.add_command(cmd_packages) cli.add_command(cmd_packages)

View file

@ -9,7 +9,8 @@ from constants import REPOSITORIES
from config import config from config import config
from chroot import create_chroot from chroot import create_chroot
from joblib import Parallel, delayed from joblib import Parallel, delayed
from distro import RepoInfo, get_kupfer_local from distro import get_kupfer_local
from wrapper import enforce_wrap
makepkg_env = os.environ.copy() | { makepkg_env = os.environ.copy() | {
'LANG': 'C', 'LANG': 'C',
@ -496,10 +497,11 @@ def cmd_packages():
pass pass
@click.command(name='build') @cmd_packages.command(name='build')
@click.option('--force', is_flag=True, default=False) @click.option('--force', is_flag=True, default=False)
@click.argument('paths', nargs=-1) @click.argument('paths', nargs=-1)
def cmd_build(paths: list[str], force=False, arch='aarch64'): def cmd_build(paths: list[str], force=False, arch='aarch64'):
enforce_wrap()
check_prebuilts() check_prebuilts()
paths = list(paths) paths = list(paths)
@ -535,8 +537,9 @@ def cmd_build(paths: list[str], force=False, arch='aarch64'):
add_package_to_repo(package) add_package_to_repo(package)
@click.command(name='clean') @cmd_packages.command(name='clean')
def cmd_clean(): def cmd_clean():
enforce_wrap()
result = subprocess.run([ result = subprocess.run([
'git', 'git',
'clean', 'clean',
@ -547,9 +550,10 @@ def cmd_clean():
exit(1) exit(1)
@click.command(name='check') @cmd_packages.command(name='check')
@click.argument('paths', nargs=-1) @click.argument('paths', nargs=-1)
def cmd_check(paths): def cmd_check(paths):
enforce_wrap()
paths = list(paths) paths = list(paths)
packages = filter_packages_by_paths(discover_packages(), paths) packages = filter_packages_by_paths(discover_packages(), paths)
@ -690,8 +694,3 @@ def cmd_check(paths):
line_index += 1 line_index += 1
logging.info(f'{package.path} nicely formatted!') logging.info(f'{package.path} nicely formatted!')
cmd_packages.add_command(cmd_build)
cmd_packages.add_command(cmd_clean)
cmd_packages.add_command(cmd_check)

View file

@ -1,9 +1,11 @@
import subprocess import subprocess
import click import click
from wrapper import enforce_wrap
@click.command(name='telnet') @click.command(name='telnet')
def cmd_telnet(hostname: str = '172.16.42.1'): def cmd_telnet(hostname: str = '172.16.42.1'):
enforce_wrap()
subprocess.run([ subprocess.run([
'telnet', 'telnet',
hostname, hostname,

View file

@ -103,7 +103,7 @@ def wrap_docker():
def enforce_wrap(no_wrapper=False): def enforce_wrap(no_wrapper=False):
if os.getenv('KUPFERBOOTSTRAP_DOCKER') != '1' and not no_wrapper: if os.getenv('KUPFERBOOTSTRAP_DOCKER') != '1' and not config.runtime['no_wrap'] and not no_wrapper:
wrap_docker() wrap_docker()