Chroot.initialise(): add reset param, refactor in general

This commit is contained in:
InsanePrawn 2021-10-24 04:32:47 +02:00
parent 46b638059c
commit 38438d5fda
3 changed files with 70 additions and 57 deletions

View file

@ -3,6 +3,9 @@ import logging
import subprocess import subprocess
import os import os
import atexit import atexit
from glob import glob
from shutil import rmtree
from config import config from config import config
from distro import get_base_distro, RepoInfo from distro import get_base_distro, RepoInfo
from shlex import quote as shell_quote from shlex import quote as shell_quote
@ -10,7 +13,6 @@ from utils import mount, umount
from distro import get_kupfer_local from distro import get_kupfer_local
from wrapper import enforce_wrap from wrapper import enforce_wrap
from constants import Arch, GCC_HOSTSPECS, CROSSDIRECT_PKGS from constants import Arch, GCC_HOSTSPECS, CROSSDIRECT_PKGS
from glob import glob
from generator import generate_makepkg_conf from generator import generate_makepkg_conf
BIND_BUILD_DIRS = 'BINDBUILDDIRS' BIND_BUILD_DIRS = 'BINDBUILDDIRS'
@ -163,12 +165,12 @@ class Chroot:
def initialize( def initialize(
self, self,
reset: bool = False,
fail_if_initialized: bool = False, fail_if_initialized: bool = False,
): ):
base_distro = get_base_distro(self.arch)
pacman_conf_target = self.get_path('etc/pacman.conf') pacman_conf_target = self.get_path('etc/pacman.conf')
if self.initialized: if self.initialized and not reset:
# chroot must have been initialized already! # chroot must have been initialized already!
if fail_if_initialized: if fail_if_initialized:
raise Exception(f"Chroot {self.name} is already initialized, this seems like a bug") raise Exception(f"Chroot {self.name} is already initialized, this seems like a bug")
@ -177,6 +179,7 @@ class Chroot:
self.deactivate() self.deactivate()
if self.copy_base: if self.copy_base:
if reset or not os.path.exists(self.get_path('usr/bin')):
base_chroot = get_base_chroot(self.arch) base_chroot = get_base_chroot(self.arch)
if base_chroot == self: if base_chroot == self:
raise Exception('base_chroot == self, bailing out. this is a bug') raise Exception('base_chroot == self, bailing out. this is a bug')
@ -198,6 +201,13 @@ class Chroot:
]) ])
if result.returncode != 0: if result.returncode != 0:
raise Exception(f'Failed to copy {base_chroot.name} to {self.name}') raise Exception(f'Failed to copy {base_chroot.name} to {self.name}')
self.write_pacman_conf()
self.activate()
self.try_install_packages(self.base_packages, refresh=True, allow_fail=False)
self.deactivate()
else:
logging.debug(f'{self.name}: Reusing existing installation')
self.write_pacman_conf()
# patch makepkg # patch makepkg
with open(self.get_path('/usr/bin/makepkg'), 'r') as file: with open(self.get_path('/usr/bin/makepkg'), 'r') as file:
@ -207,19 +217,17 @@ class Chroot:
file.write(data) file.write(data)
# configure makepkg # configure makepkg
data = generate_makepkg_conf(self.arch, cross=False) self.write_makepkg_conf(self.arch, cross_chroot_relative=None, cross=False)
data = data.replace('xz -c', 'xz -T0 -c') else:
data = data.replace(' check ', ' !check ') # base chroot
with open(self.get_path('/etc/makepkg.conf'), 'w') as file: if reset:
file.write(data) logging.info(f'Resetting {self.name}')
for dir in glob(os.join(self.path, '*')):
rmtree(dir)
os.makedirs(self.get_path('/etc'), exist_ok=True) self.write_pacman_conf()
conf_text = base_distro.get_pacman_conf(self.extra_repos) logging.info(f'Pacstrapping chroot {self.name}: {", ".join(self.base_packages)}')
with open(pacman_conf_target, 'w') as file:
file.write(conf_text)
logging.info(f'Installing packages to {self.name}: {", ".join(self.base_packages)}')
result = subprocess.run([ result = subprocess.run([
'pacstrap', 'pacstrap',
@ -235,6 +243,7 @@ class Chroot:
]) ])
if result.returncode != 0: if result.returncode != 0:
raise Exception(f'Failed to initialize chroot "{self.name}"') raise Exception(f'Failed to initialize chroot "{self.name}"')
self.initialized = True self.initialized = True
def mount( def mount(
@ -301,10 +310,6 @@ class Chroot:
self.umount('proc') self.umount('proc')
self.active = False self.active = False
def reset(self):
self.initialized = False
self.initialize()
def run_cmd(self, def run_cmd(self,
script: str, script: str,
inner_env: dict[str, str] = {}, inner_env: dict[str, str] = {},
@ -458,6 +463,12 @@ class Chroot:
f.write(makepkg_cross_conf) f.write(makepkg_cross_conf)
return makepkg_conf_path_relative return makepkg_conf_path_relative
def write_pacman_conf(self):
os.makedirs(self.get_path('/etc'), exist_ok=True)
conf_text = get_base_distro(self.arch).get_pacman_conf(self.extra_repos)
with open(self.get_path('etc/pacman.conf'), 'w') as file:
file.write(conf_text)
@click.command('chroot') @click.command('chroot')
@click.argument('type', required=False, default='build') @click.argument('type', required=False, default='build')

View file

@ -28,6 +28,7 @@ PROFILE_EMPTY: Profile = {key: None for key in PROFILE_DEFAULTS.keys()}
CONFIG_DEFAULTS = { CONFIG_DEFAULTS = {
'build': { 'build': {
'ccache': True, 'ccache': True,
'clean_mode': True,
'crosscompile': True, 'crosscompile': True,
'crossdirect': True, 'crossdirect': True,
'threads': 0, 'threads': 0,

View file

@ -399,9 +399,7 @@ def setup_build_chroot(arch: Arch, extra_packages: list[str] = [], clean_chroot:
extra_repos=get_kupfer_local(arch).repos, extra_repos=get_kupfer_local(arch).repos,
) )
logging.info(f'Initializing {arch} build chroot') logging.info(f'Initializing {arch} build chroot')
if clean_chroot: chroot.initialize(reset=clean_chroot)
chroot.reset()
chroot.initialize()
chroot.activate() chroot.activate()
chroot.mount_pacman_cache() chroot.mount_pacman_cache()
chroot.mount_pkgbuilds() chroot.mount_pkgbuilds()
@ -448,9 +446,7 @@ def build_package(
extra_packages=['base-devel'] + CROSSDIRECT_PKGS, extra_packages=['base-devel'] + CROSSDIRECT_PKGS,
clean_chroot=clean_chroot, clean_chroot=clean_chroot,
) )
cross = foreign_arch and package.mode == 'cross' and enable_crosscompile cross = foreign_arch and package.mode == 'cross' and enable_crosscompile
chroots = set([target_chroot, native_chroot])
if cross: if cross:
logging.info(f'Cross-compiling {package.path}') logging.info(f'Cross-compiling {package.path}')
@ -522,6 +518,7 @@ def build_packages(
enable_crosscompile: bool = True, enable_crosscompile: bool = True,
enable_crossdirect: bool = True, enable_crossdirect: bool = True,
enable_ccache: bool = True, enable_ccache: bool = True,
clean_chroot: bool = False,
): ):
build_levels = get_unbuilt_package_levels(repo, packages, arch, force=force) build_levels = get_unbuilt_package_levels(repo, packages, arch, force=force)
@ -539,6 +536,7 @@ def build_packages(
enable_crosscompile=enable_crosscompile, enable_crosscompile=enable_crosscompile,
enable_crossdirect=enable_crossdirect, enable_crossdirect=enable_crossdirect,
enable_ccache=enable_ccache, enable_ccache=enable_ccache,
clean_chroot=clean_chroot,
) )
files += add_package_to_repo(package, arch) files += add_package_to_repo(package, arch)
return files return files
@ -552,6 +550,7 @@ def build_packages_by_paths(
enable_crosscompile: bool = True, enable_crosscompile: bool = True,
enable_crossdirect: bool = True, enable_crossdirect: bool = True,
enable_ccache: bool = True, enable_ccache: bool = True,
clean_chroot: bool = False,
): ):
if isinstance(paths, str): if isinstance(paths, str):
paths = [paths] paths = [paths]
@ -567,6 +566,7 @@ def build_packages_by_paths(
enable_crosscompile=enable_crosscompile, enable_crosscompile=enable_crosscompile,
enable_crossdirect=enable_crossdirect, enable_crossdirect=enable_crossdirect,
enable_ccache=enable_ccache, enable_ccache=enable_ccache,
clean_chroot=clean_chroot,
) )
@ -622,6 +622,7 @@ def build(paths: list[str], force: bool, arch: Arch):
enable_crosscompile=config.file['build']['crosscompile'], enable_crosscompile=config.file['build']['crosscompile'],
enable_crossdirect=config.file['build']['crossdirect'], enable_crossdirect=config.file['build']['crossdirect'],
enable_ccache=config.file['build']['ccache'], enable_ccache=config.file['build']['ccache'],
clean_chroot=config.file['build']['clean_mode'],
) )