From 85edf4c42d644a989d0e91decd4d2bb5925ae123 Mon Sep 17 00:00:00 2001 From: Andrew Ammerlaan Date: Tue, 23 Apr 2024 20:50:17 +0200 Subject: [PATCH] ecleankernel/layout: do not hardcode os id/name Signed-off-by: Andrew Ammerlaan --- .github/workflows/ci.yml | 4 ++-- ecleankernel/layout/blspec.py | 7 +++++- ecleankernel/layout/std.py | 13 +++++++---- pyproject.toml | 3 +++ test/test_layout_std.py | 43 +++++++++++++++++++---------------- tox.ini | 1 + 6 files changed, 45 insertions(+), 26 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5139da9..0143d96 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,7 +19,7 @@ jobs: with: python-version: ${{ matrix.python-version }} - name: Install dependencies - run: python -m pip install tox + run: python -m pip install tox distro - name: Test using tox run: tox -e py qa: @@ -31,6 +31,6 @@ jobs: with: python-version: "3.10" - name: Install tox - run: pip install tox + run: pip install tox distro - name: Test using tox run: tox -e qa diff --git a/ecleankernel/layout/blspec.py b/ecleankernel/layout/blspec.py index eb1a256..2db4ded 100644 --- a/ecleankernel/layout/blspec.py +++ b/ecleankernel/layout/blspec.py @@ -2,6 +2,7 @@ # (c) 2020-2023 Michał Górny # Released under the terms of the 2-clause BSD license. +import distro import logging import os import typing @@ -138,8 +139,12 @@ def find_kernels(self, # Not an UKI continue + distro_id = distro.id() + if not distro_id: + distro_id = 'linux' + ver = basename.removeprefix(f"{self.kernel_id}-" - ).removeprefix("gentoo-") + ).removeprefix(f"{distro_id}-") if basename == ver: # Not our UKI continue diff --git a/ecleankernel/layout/std.py b/ecleankernel/layout/std.py index fe1d670..b8ebccd 100644 --- a/ecleankernel/layout/std.py +++ b/ecleankernel/layout/std.py @@ -2,6 +2,7 @@ # (c) 2011-2020 Michał Górny # Released under the terms of the 2-clause BSD license. +import distro import itertools import os import os.path @@ -70,12 +71,16 @@ def find_kernels(self, kernels: typing.Dict[str, typing.Dict[str, Kernel]] = {} other_files: typing.List[typing.Tuple[GenericFile, str]] = [] + distro_name = distro.name() + if not distro_name: + distro_name = 'Linux' + def find_std_files() -> typing.Iterator: for directory in (self.root / 'boot', - self.root / 'boot/EFI/EFI/Gentoo', - self.root / 'boot/efi/EFI/Gentoo', - self.root / 'boot/EFI/Gentoo', - self.root / 'efi/EFI/Gentoo'): + self.root / f"boot/EFI/EFI/{distro_name}", + self.root / f"boot/efi/EFI/{distro_name}", + self.root / f"boot/EFI/{distro_name}", + self.root / f"efi/EFI/{distro_name}"): try: for file in os.listdir(directory): yield directory / file diff --git a/pyproject.toml b/pyproject.toml index b59a42f..8a84a8b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,6 +18,9 @@ classifiers = [ "Topic :: System :: Installation/Setup" ] requires-python = ">=3.9" +dependencies = [ + "distro" +] [project.optional-dependencies] test = ["pytest"] diff --git a/test/test_layout_std.py b/test/test_layout_std.py index a960656..597dde6 100644 --- a/test/test_layout_std.py +++ b/test/test_layout_std.py @@ -3,6 +3,7 @@ # Released under the terms of the 2-clause BSD license. import io +import distro import os import tempfile import typing @@ -22,6 +23,10 @@ TEST_DATA_DIR = Path(__file__).parent / 'data' +distro_name = distro.name() +if not distro_name: + distro_name = 'Linux' + def kernel_paths(kd: typing.List[Kernel] ) -> typing.Iterable[typing.Tuple[ @@ -68,11 +73,11 @@ def create_layout(self) -> tempfile.TemporaryDirectory: """EFI Stub""" test_spec += [ - 'efi/EFI/Gentoo/vmlinuz-1.2.1.efi', - 'efi/EFI/Gentoo/vmlinuz-1.2.1.png', - 'efi/EFI/Gentoo/System.map-1.2.1', - 'efi/EFI/Gentoo/config-1.2.1', - 'efi/EFI/Gentoo/initramfs-1.2.1.img', + f'efi/EFI/{distro_name}/vmlinuz-1.2.1.efi', + f'efi/EFI/{distro_name}/vmlinuz-1.2.1.png', + f'efi/EFI/{distro_name}/System.map-1.2.1', + f'efi/EFI/{distro_name}/config-1.2.1', + f'efi/EFI/{distro_name}/initramfs-1.2.1.img', ] test_spec += [ @@ -93,7 +98,7 @@ def create_layout(self) -> tempfile.TemporaryDirectory: td = make_test_files(test_spec) path = Path(td.name) boot = path / 'boot' - efistub = path / 'efi/EFI/Gentoo/' + efistub = path / f'efi/EFI/{distro_name}/' modules = path / 'lib/modules' write_bzImage(efistub / 'vmlinuz-1.2.1.efi', b'1.2.1 test') @@ -127,10 +132,10 @@ def assert_kernels(self, 'boot/initrd-1.2.3.img.old': k123old, 'boot/vmlinuz-1.2.2': k122, 'boot/System.map-1.2.2': k122, - 'efi/EFI/Gentoo/vmlinuz-1.2.1.efi': k121, - 'efi/EFI/Gentoo/System.map-1.2.1': k121, - 'efi/EFI/Gentoo/config-1.2.1': k121, - 'efi/EFI/Gentoo/initramfs-1.2.1.img': k121, + f'efi/EFI/{distro_name}/vmlinuz-1.2.1.efi': k121, + f'efi/EFI/{distro_name}/System.map-1.2.1': k121, + f'efi/EFI/{distro_name}/config-1.2.1': k121, + f'efi/EFI/{distro_name}/initramfs-1.2.1.img': k121, 'boot/config-1.2.4': k124, 'lib/modules/1.2.1/test.ko': k121, 'lib/modules/1.2.2/test.ko': k122, @@ -155,7 +160,7 @@ def test_find_modules(self) -> None: with self.create_layout() as td: path = Path(td) boot = path / 'boot' - efistub = path / 'efi/EFI/Gentoo/' + efistub = path / f'efi/EFI/{distro_name}/' modules = path / 'lib/modules' self.assertEqual( @@ -210,7 +215,7 @@ def test_exclude_config(self) -> None: with self.create_layout() as td: path = Path(td) boot = path / 'boot' - efistub = path / 'efi/EFI/Gentoo/' + efistub = path / f'efi/EFI/{distro_name}/' modules = path / 'lib/modules' self.assertEqual( @@ -262,7 +267,7 @@ def test_exclude_modules(self) -> None: with self.create_layout() as td: path = Path(td) boot = path / 'boot' - efistub = path / 'efi/EFI/Gentoo/' + efistub = path / f'efi/EFI/{distro_name}/' modules = path / 'lib/modules' self.assertEqual( @@ -313,7 +318,7 @@ def test_exclude_build(self) -> None: with self.create_layout() as td: path = Path(td) boot = path / 'boot' - efistub = path / 'efi/EFI/Gentoo/' + efistub = path / f'efi/EFI/{distro_name}/' modules = path / 'lib/modules' self.assertEqual( @@ -417,11 +422,11 @@ def test_main_list_kernels(self, - modules: {td}/lib/modules/1.2.2 - build: {td}/lib/modules/1.2.2/../../../usr/src/linux other 1.2.1 [1.2.1] -- systemmap: {td}/efi/EFI/Gentoo/System.map-1.2.1 -- config: {td}/efi/EFI/Gentoo/config-1.2.1 -- initramfs: {td}/efi/EFI/Gentoo/initramfs-1.2.1.img -- vmlinuz: {td}/efi/EFI/Gentoo/vmlinuz-1.2.1.efi -- misc: {td}/efi/EFI/Gentoo/vmlinuz-1.2.1.png +- systemmap: {td}/efi/EFI/{distro_name}/System.map-1.2.1 +- config: {td}/efi/EFI/{distro_name}/config-1.2.1 +- initramfs: {td}/efi/EFI/{distro_name}/initramfs-1.2.1.img +- vmlinuz: {td}/efi/EFI/{distro_name}/vmlinuz-1.2.1.efi +- misc: {td}/efi/EFI/{distro_name}/vmlinuz-1.2.1.png - modules: {td}/lib/modules/1.2.1 - build: {td}/lib/modules/1.2.1/../../../usr/src/linux'''.lstrip()) self.assert_kernels(Path(td)) diff --git a/tox.ini b/tox.ini index 7e18a2b..3eff77a 100644 --- a/tox.ini +++ b/tox.ini @@ -14,6 +14,7 @@ basepython = python3 ignore_errors = True skip_install = True deps = + distro mypy pycodestyle pyflakes