replace os.makedirs with exec.{root_,}makedir where applicable

This commit is contained in:
InsanePrawn 2022-08-16 02:14:19 +02:00
parent 818b354000
commit 707c61f026
7 changed files with 27 additions and 18 deletions

View file

@ -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

View file

@ -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()

View file

@ -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

View file

@ -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)