Introduce constants.CHROOT_PATHS to spec paths _inside_ chroots

This commit is contained in:
InsanePrawn 2022-01-26 15:50:41 +01:00
parent c898e0a6b5
commit b65823bc10
5 changed files with 56 additions and 41 deletions

View file

@ -12,7 +12,7 @@ from shlex import quote as shell_quote
from utils import mount, umount, check_findmnt, log_or_exception from utils import mount, umount, check_findmnt, log_or_exception
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, BASE_PACKAGES from constants import Arch, GCC_HOSTSPECS, CROSSDIRECT_PKGS, BASE_PACKAGES, CHROOT_PATHS
from generator import generate_makepkg_conf from generator import generate_makepkg_conf
BIND_BUILD_DIRS = 'BINDBUILDDIRS' BIND_BUILD_DIRS = 'BINDBUILDDIRS'
@ -481,24 +481,37 @@ class Chroot:
return native_mount return native_mount
def mount_pkgbuilds(self, fail_if_mounted: bool = False) -> str: def mount_pkgbuilds(self, fail_if_mounted: bool = False) -> str:
pkgbuilds = config.get_path('pkgbuilds') return self.mount(
return self.mount(absolute_source=pkgbuilds, relative_destination=pkgbuilds.lstrip('/'), fail_if_mounted=fail_if_mounted) absolute_source=config.get_path('pkgbuilds'),
relative_destination=CHROOT_PATHS['pkgbuilds'].lstrip('/'),
fail_if_mounted=fail_if_mounted,
)
def mount_pacman_cache(self, fail_if_mounted: bool = False) -> str: def mount_pacman_cache(self, fail_if_mounted: bool = False) -> str:
arch_cache = os.path.join(config.get_path('pacman'), self.arch) arch_cache = os.path.join(config.get_path('pacman'), self.arch)
rel_target = os.path.join('var/cache/pacman', self.arch) rel_target = os.path.join(CHROOT_PATHS['pacman'].lstrip('/'), self.arch)
for dir in [arch_cache, self.get_path(rel_target)]: for dir in [arch_cache, self.get_path(rel_target)]:
os.makedirs(dir, exist_ok=True) os.makedirs(dir, exist_ok=True)
return self.mount(arch_cache, rel_target, fail_if_mounted=fail_if_mounted) return self.mount(
arch_cache,
rel_target,
fail_if_mounted=fail_if_mounted,
)
def mount_packages(self, fail_if_mounted: bool = False) -> str: def mount_packages(self, fail_if_mounted: bool = False) -> str:
packages = config.get_path('packages') return self.mount(
return self.mount(absolute_source=packages, relative_destination=packages.lstrip('/'), fail_if_mounted=fail_if_mounted) absolute_source=config.get_path('packages'),
relative_destination=CHROOT_PATHS['packages'].lstrip('/'),
fail_if_mounted=fail_if_mounted,
)
def mount_crosscompile(self, foreign_chroot: Chroot, fail_if_mounted: bool = False): def mount_crosscompile(self, foreign_chroot: Chroot, fail_if_mounted: bool = False):
mount_dest = os.path.join('chroot', os.path.basename(foreign_chroot.path)) mount_dest = os.path.join(CHROOT_PATHS['chroots'].lstrip('/'), os.path.basename(foreign_chroot.path))
os.makedirs(os.path.join(self.path, mount_dest), exist_ok=True) return self.mount(
return self.mount(absolute_source=foreign_chroot.path, relative_destination=mount_dest, fail_if_mounted=fail_if_mounted) absolute_source=foreign_chroot.path,
relative_destination=mount_dest,
fail_if_mounted=fail_if_mounted,
)
def write_makepkg_conf(self, target_arch: Arch, cross_chroot_relative: str, cross: bool = True) -> str: def write_makepkg_conf(self, target_arch: Arch, cross_chroot_relative: str, cross: bool = True) -> str:
""" """

View file

@ -142,3 +142,12 @@ SSH_COMMON_OPTIONS = [
'-o', '-o',
'StrictHostKeyChecking=no', 'StrictHostKeyChecking=no',
] ]
CHROOT_PATHS = {
'chroots': '/chroot',
'jumpdrive': '/var/cache/jumpdrive',
'pacman': '/var/cache/pacman',
'packages': '/prebuilts',
'pkgbuilds': '/pkgbuilds',
'images': '/images',
}

View file

@ -5,7 +5,7 @@ import os
import tarfile import tarfile
import logging import logging
from constants import ARCHES, BASE_DISTROS, REPOSITORIES, KUPFER_HTTPS from constants import ARCHES, BASE_DISTROS, REPOSITORIES, KUPFER_HTTPS, CHROOT_PATHS
from config import config from config import config
@ -148,7 +148,7 @@ class Distro:
# If you wish to use different paths, uncomment and update the paths. # If you wish to use different paths, uncomment and update the paths.
#RootDir = / #RootDir = /
#DBPath = /var/lib/pacman/ #DBPath = /var/lib/pacman/
CacheDir = /var/cache/pacman/{self.arch}/ CacheDir = {CHROOT_PATHS['pacman']}/{self.arch}
#LogFile = /var/log/pacman.log #LogFile = /var/log/pacman.log
#GPGDir = /etc/pacman.d/gnupg/ #GPGDir = /etc/pacman.d/gnupg/
#HookDir = /etc/pacman.d/hooks/ #HookDir = /etc/pacman.d/hooks/

View file

@ -53,8 +53,7 @@ class Package:
native_chroot: Chroot, native_chroot: Chroot,
) -> None: ) -> None:
self.path = path self.path = path
dir = config.get_path('pkgbuilds') self._loadinfo(CHROOT_PATHS['pkgbuilds'], native_chroot)
self._loadinfo(dir, native_chroot)
def _loadinfo(self, dir, native_chroot: Chroot): def _loadinfo(self, dir, native_chroot: Chroot):
result = native_chroot.run_cmd( result = native_chroot.run_cmd(
@ -84,10 +83,10 @@ class Package:
self.names = list(set(names)) self.names = list(set(names))
self.depends = list(set(depends)) self.depends = list(set(depends))
self.repo = self.path.split('/')[2] self.repo = self.path.split('/')[0]
mode = '' mode = ''
with open(os.path.join(self.path, 'PKGBUILD'), 'r') as file: with open(os.path.join(config.get_path('pkgbuilds'), self.path, 'PKGBUILD'), 'r') as file:
for line in file.read().split('\n'): for line in file.read().split('\n'):
if line.startswith('_mode='): if line.startswith('_mode='):
mode = line.split('=')[1] mode = line.split('=')[1]
@ -98,7 +97,7 @@ class Package:
self.mode = mode self.mode = mode
def __repr__(self): def __repr__(self):
return f'package({self.name},{repr(self.names)})' return f'Package({self.name},{repr(self.names)},{repr(self.path)})'
def clone_pkbuilds(pkgbuilds_dir: str, repo_url: str, branch: str, interactive=False, update=True): def clone_pkbuilds(pkgbuilds_dir: str, repo_url: str, branch: str, interactive=False, update=True):
@ -158,13 +157,13 @@ def init_prebuilts(arch: Arch, dir: str = None):
def discover_packages() -> dict[str, Package]: def discover_packages() -> dict[str, Package]:
dir = config.get_path('pkgbuilds') pkgbuilds_dir = config.get_path('pkgbuilds')
packages = {} packages = {}
paths = [] paths = []
init_pkgbuilds(interactive=False) init_pkgbuilds(interactive=False)
for repo in REPOSITORIES: for repo in REPOSITORIES:
for _dir in os.listdir(os.path.join(dir, repo)): for dir in os.listdir(os.path.join(pkgbuilds_dir, repo)):
paths.append(os.path.join(dir, repo, _dir)) paths.append(os.path.join(repo, dir))
native_chroot = setup_build_chroot(config.runtime['arch'], add_kupfer_repos=False) native_chroot = setup_build_chroot(config.runtime['arch'], add_kupfer_repos=False)
results = Parallel(n_jobs=multiprocessing.cpu_count() * 4)(delayed(Package)(path, native_chroot) for path in paths) results = Parallel(n_jobs=multiprocessing.cpu_count() * 4)(delayed(Package)(path, native_chroot) for path in paths)
@ -196,7 +195,7 @@ def filter_packages_by_paths(repo: dict[str, Package], paths: list[str]) -> list
return repo.values() return repo.values()
result = [] result = []
for pkg in repo.values(): for pkg in repo.values():
if pkg.path.replace(config.get_path('pkgbuilds').rstrip('/') + '/', '') in paths: if pkg.path in paths:
result += [pkg] result += [pkg]
return result return result
@ -371,7 +370,7 @@ def check_package_version_built(package: Package, arch: Arch) -> bool:
cross=True, cross=True,
) )
cmd = ['cd', package.path, '&&'] + makepkg_cmd + [ cmd = ['cd', os.path.join(CHROOT_PATHS['pkgbuilds'], package.path), '&&'] + makepkg_cmd + [
'--config', '--config',
config_path, config_path,
'--nobuild', '--nobuild',
@ -418,7 +417,7 @@ def setup_build_chroot(
def setup_sources(package: Package, chroot: Chroot, makepkg_conf_path = '/etc/makepkg.conf', pkgbuilds_dir: str = None): def setup_sources(package: Package, chroot: Chroot, makepkg_conf_path = '/etc/makepkg.conf', pkgbuilds_dir: str = None):
pkgbuilds_dir = pkgbuilds_dir if pkgbuilds_dir else config.get_path('pkgbuilds') pkgbuilds_dir = pkgbuilds_dir if pkgbuilds_dir else CHROOT_PATHS['pkgbuilds']
makepkg_setup_args = [ makepkg_setup_args = [
'--config', '--config',
makepkg_conf_path, makepkg_conf_path,
@ -428,7 +427,7 @@ def setup_sources(package: Package, chroot: Chroot, makepkg_conf_path = '/etc/ma
] ]
logging.info(f'Setting up sources for {package.path} in {chroot.name}') logging.info(f'Setting up sources for {package.path} in {chroot.name}')
result = chroot.run_cmd(makepkg_cmd + makepkg_setup_args, cwd=package.path) result = chroot.run_cmd(makepkg_cmd + makepkg_setup_args, cwd=os.path.join(CHROOT_PATHS['pkgbuilds'], package.path))
if result.returncode != 0: if result.returncode != 0:
raise Exception(f'Failed to check sources for {package.path}') raise Exception(f'Failed to check sources for {package.path}')
@ -472,7 +471,7 @@ def build_package(
if results['crossdirect'].returncode != 0: if results['crossdirect'].returncode != 0:
raise Exception('Unable to install crossdirect') raise Exception('Unable to install crossdirect')
# mount foreign arch chroot inside native chroot # mount foreign arch chroot inside native chroot
chroot_relative = os.path.join('chroot', target_chroot.name) chroot_relative = os.path.join(CHROOT_PATHS['chroots'], target_chroot.name)
makepkg_path_absolute = native_chroot.write_makepkg_conf(target_arch=arch, cross_chroot_relative=chroot_relative, cross=True) makepkg_path_absolute = native_chroot.write_makepkg_conf(target_arch=arch, cross_chroot_relative=chroot_relative, cross=True)
makepkg_conf_path = os.path.join('etc', os.path.basename(makepkg_path_absolute)) makepkg_conf_path = os.path.join('etc', os.path.basename(makepkg_path_absolute))
native_chroot.mount_crosscompile(target_chroot) native_chroot.mount_crosscompile(target_chroot)
@ -495,7 +494,7 @@ def build_package(
build_cmd = f'makepkg --config {makepkg_conf_absolute} --needed --noconfirm --ignorearch {" ".join(makepkg_compile_opts)}' build_cmd = f'makepkg --config {makepkg_conf_absolute} --needed --noconfirm --ignorearch {" ".join(makepkg_compile_opts)}'
logging.debug(f'Building: Running {build_cmd}') logging.debug(f'Building: Running {build_cmd}')
result = build_root.run_cmd(build_cmd, inner_env=env, cwd=package.path) result = build_root.run_cmd(build_cmd, inner_env=env, cwd=os.path.join(CHROOT_PATHS['pkgbuilds'], package.path))
if result.returncode != 0: if result.returncode != 0:
raise Exception(f'Failed to compile package {package.path}') raise Exception(f'Failed to compile package {package.path}')
@ -725,15 +724,15 @@ def cmd_check(paths):
source_key: False, source_key: False,
sha256sums_key: False, sha256sums_key: False,
} }
pkgbuild_path = os.path.join(config.get_path('pkgbuilds'), package.path, 'PKGBUILD')
with open(os.path.join(package.path, 'PKGBUILD'), 'r') as file: with open(pkgbuild_path, 'r') as file:
content = file.read() content = file.read()
if '\t' in content: if '\t' in content:
logging.fatal(f'\\t is not allowed in {os.path.join(package.path, "PKGBUILD")}') logging.fatal(f'\\t is not allowed in {pkgbuild_path}')
exit(1) exit(1)
lines = content.split('\n') lines = content.split('\n')
if len(lines) == 0: if len(lines) == 0:
logging.fatal(f'Empty {os.path.join(package.path, "PKGBUILD")}') logging.fatal(f'Empty {pkgbuild_path}')
exit(1) exit(1)
line_index = 0 line_index = 0
key_index = 0 key_index = 0
@ -811,7 +810,7 @@ def cmd_check(paths):
reason = f'Expected to find "{key}"' reason = f'Expected to find "{key}"'
if not formatted: if not formatted:
logging.fatal(f'Line {line_index+1} in {os.path.join(package.path, "PKGBUILD")} is not formatted correctly: "{line}"') logging.fatal(f'Formatting error in {pkgbuild_path}: Line {line_index+1}: "{line}"')
if reason != "": if reason != "":
logging.fatal(reason) logging.fatal(reason)
exit(1) exit(1)
@ -819,9 +818,9 @@ def cmd_check(paths):
if key == arch_key: if key == arch_key:
if line.endswith(')'): if line.endswith(')'):
if line.startswith(f'{arch_key}=('): if line.startswith(f'{arch_key}=('):
check_arches_hint(os.path.join(package.path, "PKGBUILD"), required_arches, [line[6:-1]]) check_arches_hint(pkgbuild_path, required_arches, [line[6:-1]])
else: else:
check_arches_hint(os.path.join(package.path, "PKGBUILD"), required_arches, provided_arches) check_arches_hint(pkgbuild_path, required_arches, provided_arches)
elif line.startswith(' '): elif line.startswith(' '):
provided_arches.append(line[4:]) provided_arches.append(line[4:])

View file

@ -7,16 +7,10 @@ import uuid
import click import click
import logging import logging
from config import config, dump_file as dump_config_file from config import config, dump_file as dump_config_file
from constants import CHROOT_PATHS
from utils import programs_available from utils import programs_available
DOCKER_PATHS = { DOCKER_PATHS = CHROOT_PATHS.copy()
'chroots': '/chroot',
'jumpdrive': '/var/cache/jumpdrive',
'pacman': '/var/cache/pacman',
'packages': '/prebuilts',
'pkgbuilds': '/pkgbuilds',
'images': '/images',
}
def wrap_docker(): def wrap_docker():