51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
import logging, os, tempfile
|
|
import os
|
|
import os.path as path
|
|
import shutil
|
|
|
|
|
|
class Chroot(object):
|
|
def __init__(self, config, static=False):
|
|
self.config = config
|
|
|
|
# Create directories
|
|
if static:
|
|
self.basedir = "/home/eldeberen/tmp/middlearch-chroot"
|
|
logging.debug(f"use static directory in {self.basedir}")
|
|
os.makedirs(self.basedir, exist_ok=True)
|
|
else:
|
|
logging.debug(f"create temporary directory in {self.basedir}")
|
|
self.basedir = tempfile.mkdtemp()
|
|
|
|
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')}"
|
|
|
|
# Initialize base-devel packages
|
|
if not path.exists(self.rootdir):
|
|
logging.info(f"create chroot in {self.rootdir}")
|
|
_exec(f"mkarchroot {self.flags} {self.rootdir} base-devel")
|
|
|
|
# Make sure everything is up-to-date
|
|
_exec(f"arch-nspawn {self.rootdir} pacman -Syu")
|
|
|
|
logging.info(f"chroot ready ({self.rootdir})")
|
|
|
|
def install(self, package):
|
|
logging.info(f"install {package.name} from remote repository")
|
|
_exec(f"arch-nspawn {self.rootdir} pacman -S {package.name}")
|
|
|
|
def makepkg(self, package):
|
|
logging.info(f"build {package.name}")
|
|
ref_dir = os.getcwd()
|
|
os.chdir(path.join(self.config.get('output'), package.name))
|
|
_exec(f"makechrootpkg -r {self.basedir}")
|
|
os.chdir(ref_dir)
|
|
|
|
def _exec(cmd, sudo=False):
|
|
if sudo:
|
|
cmd = "sudo " + cmd
|
|
logging.debug(cmd)
|
|
r = os.popen(cmd)
|
|
logging.debug(r.read())
|