MiddleArch/middlearch/chroot.py

38 lines
1.3 KiB
Python
Raw Permalink Normal View History

2022-01-12 21:46:59 +01:00
import logging, os, shutil, tempfile
2021-08-31 00:11:27 +02:00
import os.path as path
2022-01-12 21:46:59 +01:00
from .exec import _exec
2021-08-31 00:11:27 +02:00
class Chroot(object):
2022-01-12 01:07:56 +01:00
def __init__(self, config, static=False):
self.config = config
2021-08-31 00:11:27 +02:00
# Create directories
2022-01-12 21:46:59 +01:00
# TODO: add config keys [chroot and clean]
self.basedir = config.get('chroot.dir')
logging.debug(f"use directory in {self.basedir}")
os.makedirs(self.basedir, exist_ok=True)
2022-01-12 01:07:56 +01:00
self.rootdir = path.join(self.basedir, "root")
self.flags = f"-C {path.join(config.dir, 'pacman', 'pacman.conf')} " \
f"-M {path.join(config.dir, 'pacman', 'makepkg.conf')}"
2021-08-31 00:11:27 +02:00
# Initialize base-devel packages
2022-01-12 01:07:56 +01:00
if not path.exists(self.rootdir):
logging.info(f"create chroot in {self.rootdir}")
_exec(f"mkarchroot {self.flags} {self.rootdir} base-devel")
2021-08-31 00:11:27 +02:00
# Make sure everything is up-to-date
2022-01-12 01:07:56 +01:00
_exec(f"arch-nspawn {self.rootdir} pacman -Syu")
logging.info(f"chroot ready ({self.rootdir})")
2021-08-31 00:11:27 +02:00
2022-01-12 21:46:59 +01:00
def makepkg(self, package, clean=False):
clean_flag = "-c" if clean else ""
logging.info(f"build {package.name}" + clean_flag)
2022-01-12 01:07:56 +01:00
ref_dir = os.getcwd()
os.chdir(path.join(self.config.get('output'), package.name))
2022-01-12 21:46:59 +01:00
_exec(f"makechrootpkg {clean_flag} -n -r {self.basedir}")
2022-01-12 01:07:56 +01:00
os.chdir(ref_dir)