Skip to content

Commit

Permalink
ecleankernel/layout: do not hardcode os id/name
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Ammerlaan <andrewammerlaan@gentoo.org>
  • Loading branch information
Nowa-Ammerlaan committed May 7, 2024
1 parent dc4e11f commit 75bdc6b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 29 deletions.
5 changes: 4 additions & 1 deletion ecleankernel/layout/blspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# (c) 2020-2023 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

import distro
import logging
import os
import typing
Expand Down Expand Up @@ -136,8 +137,10 @@ def find_kernels(self,
# Not an UKI
continue

distro_id = distro.id() or "linux"

ver = basename.removeprefix(f"{self.kernel_id}-"
).removeprefix("gentoo-")
).removeprefix(f"{distro_id}-")
if basename == ver:
# Not our UKI
continue
Expand Down
11 changes: 7 additions & 4 deletions ecleankernel/layout/std.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# (c) 2011-2020 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

import distro
import itertools
import os
import os.path
Expand Down Expand Up @@ -70,12 +71,14 @@ def find_kernels(self,
kernels: typing.Dict[str, typing.Dict[str, Kernel]] = {}
other_files: typing.List[typing.Tuple[GenericFile, str]] = []

distro_name = distro.name() or "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
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ classifiers = [
"Topic :: System :: Installation/Setup"
]
requires-python = ">=3.9"
dependencies = [
"distro"
]

[project.optional-dependencies]
test = ["pytest"]
Expand Down
20 changes: 12 additions & 8 deletions test/test_layout_blspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# (c) 2020 Michał Górny <mgorny@gentoo.org>
# Released under the terms of the 2-clause BSD license.

import distro
import os
import tempfile
import unittest
Expand All @@ -22,6 +23,8 @@
from test.test_file import write_bzImage
from test.test_layout_std import make_test_files, kernel_paths

distro_id = distro.id() or "linux"


class BlSpecLayoutTests(unittest.TestCase):
maxDiff = None
Expand All @@ -43,7 +46,7 @@ def create_layout(self,
test_spec = [
f"boot/{subdir}/EFI/Linux/{entry}-1.2.6.efi",
f"boot/{subdir}/EFI/Linux/{entry}-1.2.6.png",
f"boot/{subdir}/EFI/Linux/{entry}-1.2.5.efi",
f"boot/{subdir}/EFI/Linux/{distro_id}-1.2.5.efi",
f"boot/{subdir}/{entry}/1.2.5/initrd",
f"boot/{subdir}/{entry}/1.2.5/linux",
f"boot/{subdir}/{entry}/1.2.4/initrd",
Expand Down Expand Up @@ -80,7 +83,8 @@ def create_layout(self,
with open(path / "etc/kernel/entry-token", "w") as f:
f.write(f"{self.entry_token}\n")
write_bzImage(bootsub / f"EFI/Linux/{entry}-1.2.6.efi", b'1.2.6 test')
write_bzImage(bootsub / f"EFI/Linux/{entry}-1.2.5.efi", b'1.2.5 test')
write_bzImage(bootsub / f"EFI/Linux/{distro_id}-1.2.5.efi",
b'1.2.5 test')
write_bzImage(bootsub / f"{entry}/1.2.5/linux", b'1.2.5 test')
write_bzImage(bootsub / f"{entry}/1.2.3/linux", b'1.2.3 test')
write_bzImage(bootsub / f"{entry}/1.2.2/linux", b'1.2.2 test')
Expand All @@ -106,7 +110,7 @@ def assert_kernels(self,
subdir = 'EFI/' if efi_subdir else ''
files = {
f'boot/{subdir}/EFI/Linux/{self.machine_id}-1.2.6.efi': k126,
f'boot/{subdir}/EFI/Linux/{self.machine_id}-1.2.5.efi': k125,
f'boot/{subdir}/EFI/Linux/{distro_id}-1.2.5.efi': k125,
f'boot/{subdir}{self.machine_id}/1.2.5/initrd': k125,
f'boot/{subdir}{self.machine_id}/1.2.5/linux': k125,
f'boot/{subdir}{self.machine_id}/1.2.5': k125,
Expand Down Expand Up @@ -235,7 +239,7 @@ def test_find_modules(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -305,7 +309,7 @@ def test_exclude_misc(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
Expand Down Expand Up @@ -369,7 +373,7 @@ def test_exclude_modules(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
],
Expand Down Expand Up @@ -432,7 +436,7 @@ def test_exclude_build(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
],
'1.2.5'),
Expand Down Expand Up @@ -498,7 +502,7 @@ def test_find_modules_EFI(self) -> None:
],
'1.2.5'),
('1.2.5',
[KernelImage(ukipath / f"{self.machine_id}-1.2.5.efi"),
[KernelImage(ukipath / f"{distro_id}-1.2.5.efi"),
ModuleDirectory(modules / '1.2.5'),
GenericFile(modules / '1.2.5/../../../usr/src/linux',
KFT.BUILD),
Expand Down
31 changes: 17 additions & 14 deletions test/test_layout_std.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Released under the terms of the 2-clause BSD license.

import io
import distro
import os
import tempfile
import typing
Expand All @@ -22,6 +23,8 @@

TEST_DATA_DIR = Path(__file__).parent / 'data'

distro_name = distro.name() or "Linux"


def kernel_paths(kd: typing.List[Kernel]
) -> typing.Iterable[typing.Tuple[
Expand Down Expand Up @@ -68,11 +71,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 += [
Expand All @@ -93,7 +96,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')
Expand Down Expand Up @@ -127,10 +130,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,
Expand All @@ -155,7 +158,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(
Expand Down Expand Up @@ -210,7 +213,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(
Expand Down Expand Up @@ -262,7 +265,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(
Expand Down Expand Up @@ -313,7 +316,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(
Expand Down
5 changes: 3 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ skip_missing_interpreters = True
isolated_build = True

[testenv]
deps =
mypy
extras =
test
commands =
pytest -vv {posargs:test}
mypy {posargs:ecleankernel test}

[testenv:qa]
basepython = python3
ignore_errors = True
skip_install = True
deps =
mypy
pycodestyle
pyflakes
commands =
mypy {posargs:ecleankernel test}
pyflakes {posargs:ecleankernel test}
pycodestyle {posargs:ecleankernel test}

Expand Down

0 comments on commit 75bdc6b

Please sign in to comment.