From 707c61f026ef3640e109129868b0c6e6a40d9912 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Tue, 16 Aug 2022 02:14:19 +0200 Subject: [PATCH] replace os.makedirs with exec.{root_,}makedir where applicable --- boot.py | 3 ++- chroot/abstract.py | 13 +++++++------ chroot/base.py | 4 ++++ chroot/build.py | 6 ++++-- chroot/device.py | 3 ++- image.py | 6 +++--- packages/__init__.py | 10 +++++----- 7 files changed, 27 insertions(+), 18 deletions(-) diff --git a/boot.py b/boot.py index bcc96a7..0793b8b 100644 --- a/boot.py +++ b/boot.py @@ -4,6 +4,7 @@ import click 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 wrapper import enforce_wrap @@ -29,7 +30,7 @@ def cmd_boot(type): if type == JUMPDRIVE: file = f'boot-{device}.img' path = os.path.join(config.get_path('jumpdrive'), file) - os.makedirs(os.path.dirname(path), exist_ok=True) + makedir(os.path.dirname(path)) if not os.path.exists(path): urllib.request.urlretrieve(f'https://github.com/dreemurrs-embedded/Jumpdrive/releases/download/{JUMPDRIVE_VERSION}/{file}', path) else: diff --git a/chroot/abstract.py b/chroot/abstract.py index 2ff17d2..ed58569 100644 --- a/chroot/abstract.py +++ b/chroot/abstract.py @@ -8,7 +8,7 @@ from typing import Protocol, Union, Optional, Mapping from uuid import uuid4 from exec.cmd import run_root_cmd, generate_env_cmd, flatten_shell_script, wrap_in_bash -from exec.file import root_write_file +from exec.file import makedir, root_makedir, root_write_file, write_file from config import config from constants import Arch, CHROOT_PATHS from distro.distro import get_base_distro, get_kupfer_local, RepoInfo @@ -140,7 +140,7 @@ class Chroot(AbstractChroot): options=['bind'], fs_type: str = None, fail_if_mounted: bool = True, - makedir: bool = True, + mkdir: bool = True, strict_cache_consistency: bool = False, ): """returns the absolute path `relative_target` was mounted at""" @@ -160,8 +160,8 @@ class Chroot(AbstractChroot): else: if pseudo_absolute in self.active_mounts: log_or_exc(f'{self.name}: Mount {pseudo_absolute} was in active_mounts but not actually mounted. ({absolute_destination})') - if makedir and os.path.isdir(absolute_source): - os.makedirs(absolute_destination, exist_ok=True) + if mkdir and os.path.isdir(absolute_source): + root_makedir(absolute_destination) result = mount(absolute_source, absolute_destination, options=options, fs_type=fs_type, register_unmount=False) if result.returncode != 0: raise Exception(f'{self.name}: failed to mount {absolute_source} to {absolute_destination}') @@ -248,8 +248,8 @@ class Chroot(AbstractChroot): def mount_pacman_cache(self, fail_if_mounted: bool = False) -> str: arch_cache = os.path.join(config.get_path('pacman'), self.arch) rel_target = os.path.join(CHROOT_PATHS['pacman'].lstrip('/'), self.arch) - for dir in [arch_cache, self.get_path(rel_target)]: - os.makedirs(dir, exist_ok=True) + makedir(arch_cache) + root_makedir(self.get_path(rel_target)) return self.mount( arch_cache, rel_target, @@ -273,6 +273,7 @@ class Chroot(AbstractChroot): filename = 'makepkg' + (f'_cross_{target_arch}' if cross else '') + '.conf' makepkg_conf_path_relative = os.path.join('etc', filename) makepkg_conf_path = os.path.join(self.path, makepkg_conf_path_relative) + root_makedir(self.get_path('/etc')) root_write_file(makepkg_conf_path, makepkg_cross_conf) return makepkg_conf_path_relative diff --git a/chroot/base.py b/chroot/base.py index 7c4d97d..82a1c5f 100644 --- a/chroot/base.py +++ b/chroot/base.py @@ -6,6 +6,8 @@ from shutil import rmtree from constants import Arch from exec.cmd import run_root_cmd +from exec.file import makedir, root_makedir +from config import config from .abstract import Chroot, get_chroot from .helpers import base_chroot_name @@ -20,6 +22,8 @@ class BaseChroot(Chroot): logging.info(f'Resetting {self.name}') for dir in glob(os.path.join(self.path, '*')): rmtree(dir) + makedir(config.get_path('chroots')) + root_makedir(self.get_path()) self.write_pacman_conf() self.mount_pacman_cache() diff --git a/chroot/build.py b/chroot/build.py index 1a8905a..4efa283 100644 --- a/chroot/build.py +++ b/chroot/build.py @@ -8,7 +8,7 @@ from config import config from constants import Arch, GCC_HOSTSPECS, CROSSDIRECT_PKGS, CHROOT_PATHS from distro.distro import get_kupfer_local from exec.cmd import run_root_cmd -from exec.file import root_write_file, remove_file +from exec.file import makedir, remove_file, root_makedir, root_write_file from .abstract import Chroot, get_chroot from .helpers import build_chroot_name @@ -20,6 +20,8 @@ class BuildChroot(Chroot): copy_base: bool = True def create_rootfs(self, reset: bool, pacman_conf_target: str, active_previously: bool): + makedir(config.get_path('chroots')) + root_makedir(self.get_path()) if reset or not os.path.exists(self.get_path('usr/bin')): base_chroot = get_base_chroot(self.arch) if base_chroot == self: @@ -116,7 +118,7 @@ class BuildChroot(Chroot): logging.debug('Disabling crossdirect rustc') remove_file(rustc) - os.makedirs(native_mount, exist_ok=True) + root_makedir(native_mount) logging.debug(f'Mounting {native_chroot.name} to {native_mount}') self.mount(native_chroot.path, 'native', fail_if_mounted=fail_if_mounted) return native_mount diff --git a/chroot/device.py b/chroot/device.py index 4573f28..80ab1c5 100644 --- a/chroot/device.py +++ b/chroot/device.py @@ -3,6 +3,7 @@ import os from constants import Arch, BASE_PACKAGES from distro.distro import get_kupfer_local, get_kupfer_https +from exec.file import makedir, root_makedir from utils import check_findmnt from typing import Optional @@ -36,7 +37,7 @@ class DeviceChroot(BuildChroot): raise Exception(f'{self.name}: There is already something mounted at {self.path}, not mounting over it.') if os.path.exists(os.path.join(self.path, 'usr/bin')): raise Exception(f'{self.name}: {self.path}/usr/bin exists, not mounting over existing rootfs.') - os.makedirs(self.path, exist_ok=True) + makedir(self.path) atexit.register(self.deactivate) self.mount(source_path, '/', fs_type=fs_type, options=options) diff --git a/image.py b/image.py index b6df605..0d92b2c 100644 --- a/image.py +++ b/image.py @@ -14,7 +14,7 @@ from constants import Arch, BASE_PACKAGES, DEVICES, 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 +from exec.file import root_write_file, root_makedir, makedir from packages import build_enable_qemu_binfmt, discover_packages, build_packages from ssh import copy_ssh_keys from wrapper import enforce_wrap @@ -195,7 +195,7 @@ def mount_chroot(rootfs_source: str, boot_src: str, chroot: DeviceChroot): chroot.mount_rootfs(rootfs_source) assert (os.path.ismount(chroot.path)) - os.makedirs(chroot.get_path('boot'), exist_ok=True) + root_makedir(chroot.get_path('boot')) logging.debug(f'Mounting {boot_src} at {chroot.path}/boot') chroot.mount(boot_src, '/boot', options=['defaults']) @@ -409,7 +409,7 @@ def cmd_build(profile_name: str = None, image_path = block_target or get_image_path(device, flavour) - os.makedirs(os.path.dirname(image_path), exist_ok=True) + makedir(os.path.dirname(image_path)) logging.info(f'Creating new file at {image_path}') create_img_file(image_path, f"{rootfs_size_mb + size_extra_mb}M") diff --git a/packages/__init__.py b/packages/__init__.py index d18db74..56ed3c7 100644 --- a/packages/__init__.py +++ b/packages/__init__.py @@ -16,7 +16,7 @@ from binfmt import register as binfmt_register from constants import REPOSITORIES, CROSSDIRECT_PKGS, QEMU_BINFMT_PKGS, GCC_HOSTSPECS, ARCHES, Arch, CHROOT_PATHS, MAKEPKG_CMD from config import config from exec.cmd import run_cmd, run_root_cmd -from exec.file import remove_file +from exec.file import makedir, remove_file from chroot.build import get_build_chroot, BuildChroot from distro.distro import PackageInfo, get_kupfer_https, get_kupfer_local from ssh import run_ssh_command, scp_put_files @@ -80,12 +80,12 @@ def init_pkgbuilds(interactive=False): def init_prebuilts(arch: Arch, dir: str = None): """Ensure that all `constants.REPOSITORIES` inside `dir` exist""" prebuilts_dir = dir or config.get_package_dir(arch) - os.makedirs(prebuilts_dir, exist_ok=True) + makedir(prebuilts_dir) for repo in REPOSITORIES: repo_dir = os.path.join(prebuilts_dir, repo) if not os.path.exists(repo_dir): logging.info(f"Creating local repo {repo} ({arch})") - os.makedirs(repo_dir, exist_ok=True) + makedir(repo_dir) for ext1 in ['db', 'files']: for ext2 in ['', '.tar.xz']: if not os.path.exists(os.path.join(prebuilts_dir, repo, f'{repo}.{ext1}{ext2}')): @@ -293,7 +293,7 @@ def add_file_to_repo(file_path: str, repo_name: str, arch: Arch): file_name = os.path.basename(file_path) target_file = os.path.join(repo_dir, file_name) - os.makedirs(repo_dir, exist_ok=True) + makedir(repo_dir) if file_path != target_file: logging.debug(f'moving {file_path} to {target_file} ({repo_dir})') shutil.copy( @@ -391,7 +391,7 @@ def try_download_package(dest_file_path: str, package: Pkgbuild, arch: Arch) -> assert url try: logging.info(f"Trying to download package {url}") - os.makedirs(os.path.dirname(dest_file_path), exist_ok=True) + makedir(os.path.dirname(dest_file_path)) with urlopen(url) as fsrc, open(dest_file_path, 'wb') as fdst: copyfileobj(fsrc, fdst) logging.info(f"{filename} downloaded from repos")