2021-07-06 01:52:10 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
"""System C"""
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2023-01-13 13:55:39 +00:00
|
|
|
# SPDX-FileCopyrightText: 2022-2023 Dor Askayo <dor.askayo@gmail.com>
|
2022-03-07 07:58:20 +00:00
|
|
|
# SPDX-FileCopyrightText: 2021-22 fosslinux <fosslinux@aussies.space>
|
2021-07-06 01:52:10 +01:00
|
|
|
# SPDX-FileCopyrightText: 2021 Andrius Štikonas <andrius@stikonas.eu>
|
|
|
|
|
|
|
|
import os
|
|
|
|
import getpass
|
|
|
|
|
2022-04-13 00:08:29 +01:00
|
|
|
from lib.utils import mount, umount, create_disk, run, copytree
|
2021-07-06 01:52:10 +01:00
|
|
|
from lib.sysgeneral import SysGeneral
|
|
|
|
|
2021-08-05 13:16:19 +01:00
|
|
|
# pylint: disable=consider-using-with
|
2022-05-23 06:56:18 +01:00
|
|
|
# pylint: disable=too-many-instance-attributes
|
2021-07-06 01:52:10 +01:00
|
|
|
class SysC(SysGeneral):
|
|
|
|
"""
|
|
|
|
Class responsible for preparing sources for System C.
|
|
|
|
"""
|
2022-05-23 12:45:59 +01:00
|
|
|
|
2023-01-13 11:15:27 +00:00
|
|
|
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')
|
2022-05-23 12:45:59 +01:00
|
|
|
dev_name = None
|
|
|
|
|
2022-05-22 01:03:21 +01:00
|
|
|
def __init__(self, arch, preserve_tmp, tmpdir, external_sources):
|
2021-07-06 01:52:10 +01:00
|
|
|
self.arch = arch
|
|
|
|
self.preserve_tmp = preserve_tmp
|
2022-05-22 01:03:21 +01:00
|
|
|
self.external_sources = external_sources
|
2021-07-06 01:52:10 +01:00
|
|
|
|
|
|
|
if tmpdir is None:
|
|
|
|
self.tmp_dir = os.path.join(self.sys_dir, 'tmp')
|
|
|
|
else:
|
|
|
|
self.tmp_dir = os.path.join(tmpdir, 'sysc')
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
if not self.preserve_tmp:
|
2022-05-23 12:45:59 +01:00
|
|
|
if self.dev_name is not None:
|
|
|
|
print(f"Detaching {self.dev_name}")
|
2021-07-06 01:52:10 +01:00
|
|
|
run('sudo', 'losetup', '-d', self.dev_name)
|
2022-05-23 12:32:51 +01:00
|
|
|
|
|
|
|
super().__del__()
|
2021-07-06 01:52:10 +01:00
|
|
|
|
2022-05-23 15:02:33 +01:00
|
|
|
def prepare(self, mount_tmpfs, create_disk_image):
|
2021-07-06 01:52:10 +01:00
|
|
|
"""
|
|
|
|
Prepare directory structure for System C.
|
|
|
|
"""
|
2022-05-23 15:02:33 +01:00
|
|
|
if mount_tmpfs:
|
|
|
|
self.mount_tmpfs()
|
|
|
|
else:
|
|
|
|
os.mkdir(self.tmp_dir)
|
2022-05-23 13:32:00 +01:00
|
|
|
|
|
|
|
rootfs_dir = None
|
|
|
|
|
2022-05-23 14:00:07 +01:00
|
|
|
if create_disk_image:
|
2021-07-06 01:52:10 +01:00
|
|
|
# Create + mount a disk for QEMU to use
|
|
|
|
disk_path = os.path.join(self.tmp_dir, 'disk.img')
|
2022-05-22 01:03:21 +01:00
|
|
|
if self.external_sources:
|
|
|
|
self.dev_name = create_disk(disk_path, "msdos", "ext4", '8G')
|
2022-05-23 06:56:18 +01:00
|
|
|
rootfs_dir = os.path.join(self.tmp_dir, 'mnt')
|
2022-05-22 01:03:21 +01:00
|
|
|
os.mkdir(rootfs_dir)
|
2022-05-23 06:56:18 +01:00
|
|
|
mount(self.dev_name + "p1", rootfs_dir, 'ext4')
|
2022-05-22 01:03:21 +01:00
|
|
|
else:
|
|
|
|
self.dev_name = create_disk(disk_path, "none", "ext4", '8G')
|
2021-07-06 01:52:10 +01:00
|
|
|
# Use chown to allow executing user to access it
|
|
|
|
run('sudo', 'chown', getpass.getuser(), self.dev_name)
|
2022-05-22 01:03:21 +01:00
|
|
|
if self.external_sources:
|
|
|
|
run('sudo', 'chown', getpass.getuser(), rootfs_dir)
|
2021-07-06 01:52:10 +01:00
|
|
|
else:
|
2022-05-23 13:32:00 +01:00
|
|
|
rootfs_dir = self.tmp_dir
|
2021-08-05 13:16:19 +01:00
|
|
|
|
2022-05-22 01:03:21 +01:00
|
|
|
if self.external_sources:
|
2023-01-13 13:55:39 +00:00
|
|
|
source_manifest = self.get_source_manifest()
|
|
|
|
self.get_packages(source_manifest)
|
|
|
|
|
2022-05-23 06:56:18 +01:00
|
|
|
copytree(self.cache_dir, os.path.join(rootfs_dir, "distfiles"))
|
2022-04-13 00:08:29 +01:00
|
|
|
|
2022-05-23 14:00:07 +01:00
|
|
|
# Unmount tmp/mnt if it was mounted
|
2022-05-23 06:56:18 +01:00
|
|
|
if create_disk_image and self.external_sources:
|
2022-05-23 13:32:00 +01:00
|
|
|
umount(rootfs_dir)
|