Rework tmpdir & associated a bit.

- Split out tmpdir logic into a separate entity & add the appropriate
  arguments and checks.
- sysb can be removed since there is now no associated logic.
- Move disk/etc logic into tmpdir.py.
This commit is contained in:
fosslinux 2023-01-28 11:11:32 +11:00
parent 4f9f56f006
commit 51b0bf8405
8 changed files with 170 additions and 195 deletions

57
sysc.py
View file

@ -2,13 +2,12 @@
"""System C"""
# SPDX-License-Identifier: GPL-3.0-or-later
# SPDX-FileCopyrightText: 2022-2023 Dor Askayo <dor.askayo@gmail.com>
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
# SPDX-FileCopyrightText: 2021-23 fosslinux <fosslinux@aussies.space>
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
import os
import getpass
from lib.utils import mount, umount, create_disk, run, copytree
from lib.utils import copytree
from lib.sysgeneral import SysGeneral
# pylint: disable=consider-using-with
@ -21,60 +20,30 @@ class SysC(SysGeneral):
git_dir = os.path.dirname(os.path.join(__file__))
sys_dir = os.path.join(git_dir, 'sysc')
cache_dir = os.path.join(sys_dir, 'distfiles')
dev_name = None
def __init__(self, arch, preserve_tmp, tmpdir, external_sources):
def __init__(self, tmpdir, arch, external_sources):
self.arch = arch
self.preserve_tmp = preserve_tmp
self.external_sources = external_sources
self._tmpdir = tmpdir
if tmpdir is None:
self.tmp_dir = os.path.join(self.sys_dir, 'tmp')
else:
self.tmp_dir = os.path.join(tmpdir, 'sysc')
self.tmp_dir = tmpdir.add_sys("sysc")
def __del__(self):
if not self.preserve_tmp:
if self.dev_name is not None:
print(f"Detaching {self.dev_name}")
run('sudo', 'losetup', '-d', self.dev_name)
super().__del__()
def prepare(self, mount_tmpfs, create_disk_image):
def prepare(self, create_disk_image):
"""
Prepare directory structure for System C.
"""
if mount_tmpfs:
self.mount_tmpfs()
else:
os.mkdir(self.tmp_dir)
rootfs_dir = None
if create_disk_image:
# Create + mount a disk for QEMU to use
disk_path = os.path.join(self.tmp_dir, 'disk.img')
if self.external_sources:
self.dev_name = create_disk(disk_path, "msdos", "ext4", '8G')
rootfs_dir = os.path.join(self.tmp_dir, 'mnt')
os.mkdir(rootfs_dir)
mount(self.dev_name + "p1", rootfs_dir, 'ext4')
else:
self.dev_name = create_disk(disk_path, "none", "ext4", '8G')
# Use chown to allow executing user to access it
run('sudo', 'chown', getpass.getuser(), self.dev_name)
if self.external_sources:
run('sudo', 'chown', getpass.getuser(), rootfs_dir)
else:
rootfs_dir = self.tmp_dir
self._tmpdir.add_disk("sysc")
if self.external_sources:
if create_disk_image:
rootfs_dir = self._tmpdir.mount_disk("sysc")
else:
rootfs_dir = self.tmp_dir
source_manifest = self.get_source_manifest()
self.get_packages(source_manifest)
copytree(self.cache_dir, os.path.join(rootfs_dir, "distfiles"))
# Unmount tmp/mnt if it was mounted
if create_disk_image and self.external_sources:
umount(rootfs_dir)
if create_disk_image:
self._tmpdir.umount_disk("sysc")