From 5a565662ebd3684cda02ca12f5aff5e084d43679 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Mon, 29 Aug 2022 23:30:00 +0200 Subject: [PATCH] image.py: use Device instead of the device name from config --- boot.py | 8 +++++--- flash.py | 6 ++++-- image.py | 38 +++++++++++++++++++++----------------- 3 files changed, 30 insertions(+), 22 deletions(-) diff --git a/boot.py b/boot.py index 0793b8b..593f361 100644 --- a/boot.py +++ b/boot.py @@ -6,7 +6,8 @@ from config import config from constants import BOOT_STRATEGIES, FLASH_PARTS, FASTBOOT, JUMPDRIVE, JUMPDRIVE_VERSION from exec.file import makedir from fastboot import fastboot_boot, fastboot_erase_dtbo -from image import get_device_and_flavour, losetup_rootfs_image, get_image_path, dump_aboot, dump_lk2nd +from image import get_flavour, get_device_name, losetup_rootfs_image, get_image_path, dump_aboot, dump_lk2nd +from packages.device import get_profile_device from wrapper import enforce_wrap LK2ND = FLASH_PARTS['LK2ND'] @@ -20,7 +21,8 @@ TYPES = [LK2ND, JUMPDRIVE, ABOOT] def cmd_boot(type): """Boot JumpDrive or the Kupfer aboot image. Erases Android DTBO in the process.""" enforce_wrap() - device, flavour = get_device_and_flavour() + device = get_profile_device() + flavour = get_flavour() # TODO: parse arch and sector size sector_size = 4096 image_path = get_image_path(device, flavour) @@ -28,7 +30,7 @@ def cmd_boot(type): if strategy == FASTBOOT: if type == JUMPDRIVE: - file = f'boot-{device}.img' + file = f'boot-{get_device_name(device)}.img' path = os.path.join(config.get_path('jumpdrive'), file) makedir(os.path.dirname(path)) if not os.path.exists(path): diff --git a/flash.py b/flash.py index 72a9ea2..0afd3fd 100644 --- a/flash.py +++ b/flash.py @@ -6,7 +6,8 @@ from constants import FLASH_PARTS, LOCATIONS from exec.cmd import run_root_cmd from exec.file import get_temp_dir from fastboot import fastboot_flash -from image import dd_image, partprobe, shrink_fs, losetup_rootfs_image, losetup_destroy, dump_aboot, dump_lk2nd, dump_qhypstub, get_device_and_flavour, get_image_name, get_image_path +from image import dd_image, partprobe, shrink_fs, losetup_rootfs_image, losetup_destroy, dump_aboot, dump_lk2nd, dump_qhypstub, get_flavour, get_image_name, get_image_path +from packages.device import get_profile_device from wrapper import enforce_wrap ABOOT = FLASH_PARTS['ABOOT'] @@ -21,7 +22,8 @@ ROOTFS = FLASH_PARTS['ROOTFS'] def cmd_flash(what: str, location: str): """Flash a partition onto a device. `location` takes either a path to a block device or one of emmc, sdcard""" enforce_wrap() - device, flavour = get_device_and_flavour() + device = get_profile_device() + flavour = get_flavour() device_image_name = get_image_name(device, flavour) device_image_path = get_image_path(device, flavour) diff --git a/image.py b/image.py index adb44dc..3e8677a 100644 --- a/image.py +++ b/image.py @@ -7,16 +7,16 @@ import click import logging from signal import pause from subprocess import CompletedProcess -from typing import Optional +from typing import Optional, Union from chroot.device import DeviceChroot, get_device_chroot -from constants import Arch, BASE_PACKAGES, DEVICES, FLAVOURS +from constants import Arch, BASE_PACKAGES, FLAVOURS from config import config, Profile from distro.distro import get_base_distro, get_kupfer_https from exec.cmd import run_root_cmd, generate_cmd_su from exec.file import root_write_file, root_makedir, makedir from packages import build_enable_qemu_binfmt, build_packages_by_paths -from packages.device import get_profile_device +from packages.device import Device, get_profile_device from ssh import copy_ssh_keys from wrapper import check_programs_wrap, wrap_if_foreign_arch @@ -131,23 +131,25 @@ def losetup_destroy(loop_device): ) -def get_device_and_flavour(profile_name: Optional[str] = None) -> tuple[str, str]: +def get_flavour(profile_name: Optional[str] = None) -> str: config.enforce_config_loaded() profile = config.get_profile(profile_name) - if not profile['device']: - raise Exception("Please set the device using 'kupferbootstrap config init ...'") if not profile['flavour']: raise Exception("Please set the flavour using 'kupferbootstrap config init ...'") - return (profile['device'], profile['flavour']) + return profile['flavour'] -def get_image_name(device, flavour, img_type='full') -> str: - return f'{device}-{flavour}-{img_type}.img' +def get_device_name(device: Union[str, Device]) -> str: + return device.name if isinstance(device, Device) else device -def get_image_path(device, flavour, img_type='full') -> str: +def get_image_name(device: Union[str, Device], flavour, img_type='full') -> str: + return f'{get_device_name(device)}-{flavour}-{img_type}.img' + + +def get_image_path(device: Union[str, Device], flavour, img_type='full') -> str: return os.path.join(config.get_path('images'), get_image_name(device, flavour, img_type)) @@ -299,7 +301,7 @@ def create_boot_fs(device: str, blocksize: int): def install_rootfs( rootfs_device: str, bootfs_device: str, - device: str, + device: Union[str, Device], flavour: str, arch: Arch, packages: list[str], @@ -308,7 +310,7 @@ def install_rootfs( ): user = profile['username'] or 'kupfer' post_cmds = FLAVOURS[flavour].get('post_cmds', []) - chroot = get_device_chroot(device=device, flavour=flavour, arch=arch, packages=packages, use_local_repos=use_local_repos) + chroot = get_device_chroot(device=get_device_name(device), flavour=flavour, arch=arch, packages=packages, use_local_repos=use_local_repos) mount_chroot(rootfs_device, bootfs_device, chroot) @@ -388,16 +390,17 @@ def cmd_build(profile_name: str = None, Unless overriden, required packages will be built or preferably downloaded from HTTPS repos. """ - arch = get_profile_device(profile_name).arch + device = get_profile_device(profile_name) + arch = device.arch check_programs_wrap(['makepkg', 'pacman', 'pacstrap']) profile: Profile = config.get_profile(profile_name) - device, flavour = get_device_and_flavour(profile_name) + flavour = get_flavour(profile_name) size_extra_mb: int = int(profile["size_extra_mb"]) sector_size = 4096 rootfs_size_mb = FLAVOURS[flavour].get('size', 2) * 1000 - packages = BASE_PACKAGES + DEVICES[device] + FLAVOURS[flavour]['packages'] + profile['pkgs_include'] + packages = BASE_PACKAGES + [device.package.name] + FLAVOURS[flavour]['packages'] + profile['pkgs_include'] if arch != config.runtime.arch: build_enable_qemu_binfmt(arch) @@ -459,9 +462,10 @@ def cmd_build(profile_name: str = None, @click.argument('profile', required=False) def cmd_inspect(profile: str = None, shell: bool = False): """Open a shell in a device image""" - arch = get_profile_device(profile).arch + device = get_profile_device(profile) + arch = device.arch wrap_if_foreign_arch(arch) - device, flavour = get_device_and_flavour(profile) + flavour = get_flavour(profile) # TODO: PARSE DEVICE SECTOR SIZE sector_size = 4096 chroot = get_device_chroot(device, flavour, arch)