Skip to content

Commit 52a6302

Browse files
authored
Merge pull request #25 from desultory/clean_dict
Clean dict checks
2 parents 836cd49 + 51f2a73 commit 52a6302

File tree

16 files changed

+112
-90
lines changed

16 files changed

+112
-90
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "ugrd"
7-
version = "1.14.3"
7+
version = "1.16.0"
88
authors = [
99
{ name="Desultory", email="[email protected]" },
1010
]
@@ -18,7 +18,7 @@ classifiers = [
1818
]
1919

2020
dependencies = [
21-
"zenlib >= 2.1.2",
21+
"zenlib >= 2.2.0",
2222
"pycpio >= 1.2.1"
2323
]
2424

src/ugrd/base/base.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
__author__ = 'desultory'
2-
__version__ = '4.5.2'
2+
__version__ = '4.6.1'
33

44
from importlib.metadata import version
55
from pathlib import Path
66

7-
from zenlib.util import check_dict
7+
from zenlib.util import contains
88

99

10-
@check_dict('validate', value=True)
11-
@check_dict('hostonly', value=True)
10+
@contains('hostonly')
1211
def _validate_init_target(self) -> None:
1312
if not self['init_target'].exists():
14-
raise FileNotFoundError('init_target not found at: %s', self['init_target'])
13+
raise FileNotFoundError('init_target not found at: %s' % self['init_target'])
1514

1615

1716
def _process_init_target(self, target: Path) -> None:
@@ -22,18 +21,20 @@ def _process_init_target(self, target: Path) -> None:
2221
_validate_init_target(self)
2322

2423

25-
@check_dict('init_target', unset=True, message='init_target already set.')
24+
@contains('init_target', 'init_target is already set, skipping autodetection.', log_level=30)
2625
def _process_autodetect_init(self, state) -> None:
27-
from shutil import which
2826
dict.__setitem__(self, 'autodetect_init', state)
29-
if not state:
30-
return
3127

28+
29+
@contains('autodetect_init', log_level=30)
30+
def autodetect_init(self) -> None:
31+
""" Autodetects the init_target. """
32+
from shutil import which
3233
if init := which('init'):
3334
self.logger.info('Detected init at: %s', init)
3435
self['init_target'] = init
3536
else:
36-
raise FileNotFoundError('init_target is not specified and coud not be detected.')
37+
raise FileNotFoundError('init_target is not specified and could not be detected.')
3738

3839

3940
def _find_init(self) -> str:
@@ -49,7 +50,7 @@ def _find_init(self) -> str:
4950
'return 1']
5051

5152

52-
@check_dict('init_target', not_empty=True, raise_exception=True, message='init_target must be set.')
53+
@contains('init_target', 'init_target must be set.', raise_exception=True)
5354
def do_switch_root(self) -> str:
5455
"""
5556
Should be the final statement, switches root.

src/ugrd/base/base.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ autodetect_init = true
1010
[imports.config_processing]
1111
"ugrd.base.base" = [ "_process_init_target", "_process_autodetect_init" ]
1212

13+
[imports.build_pre]
14+
"ugrd.base.base" = [ "autodetect_init" ]
15+
1316
[imports.init_final]
1417
"ugrd.base.base" = [ "do_switch_root" ]
1518

src/ugrd/base/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
__author__ = 'desultory'
2-
__version__ = '3.3.1'
2+
__version__ = '3.4.0'
33

44
from pathlib import Path
55
from typing import Union
66

7-
from zenlib.util import check_dict, NoDupFlatList
7+
from zenlib.util import contains, unset, NoDupFlatList
88

99

10-
@check_dict('clean', value=True, log_level=30, message="Skipping cleaning build directory")
10+
@contains('clean', "Skipping cleaning build directory", log_level=30)
1111
def clean_build_dir(self) -> None:
1212
""" Cleans the build directory. """
1313
from shutil import rmtree
@@ -119,7 +119,7 @@ def deploy_symlinks(self) -> None:
119119
self._symlink(symlink_parameters['source'], symlink_parameters['target'])
120120

121121

122-
@check_dict('mknod_cpio', value=False, log_level=20, message="Skipping real device node creation with mknod, as mknod_cpio is specified.")
122+
@unset('mknod_cpio', "Skipping real device node creation with mknod, as mknod_cpio is not specified.", log_level=20)
123123
def deploy_nodes(self) -> None:
124124
""" Generates specified device nodes. """
125125
from os import makedev, mknod
@@ -140,7 +140,7 @@ def deploy_nodes(self) -> None:
140140
raise e
141141

142142

143-
@check_dict('find_libgcc', value=True, log_level=20, message="Skipping libgcc_s dependency resolution.")
143+
@contains('find_libgcc', "Skipping libgcc_s dependency resolution", log_level=20)
144144
def find_libgcc(self) -> None:
145145
"""
146146
Finds libgcc.so, adds a 'dependencies' item for it.

src/ugrd/base/debug.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__author__ = "desultory"
2-
__version__ = "1.2.1"
2+
__version__ = "1.3.0"
33

4-
from zenlib.util import check_dict
4+
from zenlib.util import contains
55

66

77
def start_shell(self) -> str:
@@ -14,7 +14,7 @@ def start_shell(self) -> str:
1414
'bash -l']
1515

1616

17-
@check_dict('start_shell', value=True, message="Not enabling the debug shell, as the start_shell option is not set.")
17+
@contains('start_shell', 'Not enabling the debug shell, as the start_shell option is not set.', log_level=30)
1818
def enable_debug(self) -> str:
1919
""" Enable debug mode. """
2020
return "setvar debug 1"

src/ugrd/base/keymap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__author__ = 'desultory'
2-
__version__ = '0.2.1'
2+
__version__ = '0.3.0'
33

4-
from zenlib.util import check_dict
4+
from zenlib.util import contains
55

66

77
def _find_keymap_include(self, base_path, included_file, no_recurse=False):
@@ -71,7 +71,7 @@ def _process_keymap_file(self, keymap_file: str) -> str:
7171
dict.__setitem__(self, 'keymap_file', keymap_file.replace('.gz', ''))
7272

7373

74-
@check_dict('keymap_file', raise_exception=True, message="keymap_file must be set to use the keymap module")
74+
@contains('keymap_file', "keymap_file must be set to use the keymap module", raise_exception=True)
7575
def set_keymap(self) -> str:
7676
""" Sets the specified keymap. """
7777
return [f'einfo "Setting keymap: {self["keymap_file"]}"',

src/ugrd/crypto/cryptsetup.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
__author__ = 'desultory'
2-
__version__ = '2.5.0'
2+
__version__ = '2.5.1'
33

4-
from zenlib.util import check_dict
4+
from zenlib.util import contains
55

66

77
_module_name = 'ugrd.crypto.cryptsetup'
88

99
CRYPTSETUP_PARAMETERS = ['key_type', 'partuuid', 'uuid', 'path', 'key_file', 'header_file', 'retries', 'key_command', 'reset_command', 'try_nokey', 'include_key']
1010

1111

12-
@check_dict('cryptsetup', value_arg=1, return_arg=2, contains=True) # Check if the mapped name is defined
1312
def _merge_cryptsetup(self, mapped_name: str, config: dict) -> None:
1413
""" Merges the cryptsetup configuration """
14+
if mapped_name not in self['cryptsetup']:
15+
return config
16+
1517
self.logger.log(5, "Existing cryptsetup configuration: %s" % self['cryptsetup'][mapped_name])
1618
self.logger.debug("[%s] Merging cryptsetup configuration: %s" % (mapped_name, config))
1719
return dict(self['cryptsetup'][mapped_name], **config)
@@ -38,7 +40,7 @@ def _process_cryptsetup_key_types_multi(self, key_type: str, config: dict) -> No
3840
self['cryptsetup_key_types'][key_type] = config
3941

4042

41-
@check_dict('validate', value=True, log_level=30, message="Skipping cryptsetup key validation.")
43+
@contains('validate', "Skipping cryptsetup keyfile validation.", log_level=30)
4244
def _validate_crypysetup_key(self, key_paramters: dict) -> None:
4345
""" Validates the cryptsetup key """
4446
if key_paramters.get('include_key'):
@@ -67,7 +69,7 @@ def _validate_crypysetup_key(self, key_paramters: dict) -> None:
6769
key_copy = parent
6870

6971

70-
@check_dict('validate', value=True, log_level=30, message="Skipping cryptsetup configuration validation.")
72+
@contains('validate', "Skipping cryptsetup configuration validation.", log_level=30)
7173
def _validate_cryptsetup_config(self, mapped_name: str, config: dict) -> None:
7274
self.logger.log(5, "[%s] Validating cryptsetup configuration: %s" % (mapped_name, config))
7375
for parameter in config:
@@ -115,7 +117,7 @@ def _process_cryptsetup_multi(self, mapped_name: str, config: dict) -> None:
115117
self['cryptsetup'][mapped_name] = config
116118

117119

118-
@check_dict('validate', value=True, log_level=30, message="Skipping LUKS source validation.")
120+
@contains('validate', "Skipping cryptsetup configuration validation.", log_level=30)
119121
def _validate_luks_source(self, mapped_name: str) -> None:
120122
""" Checks that a LUKS source device is valid """
121123
for _dm_info in self['_dm_info'].values():

src/ugrd/crypto/smartcard.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
__author__ = 'desultory'
2-
__version__ = '1.0.0'
2+
__version__ = '1.1.0'
33

4+
from zenlib.util import contains
45

5-
from zenlib.util import check_dict
66

7-
8-
@check_dict('sc_public_key', raise_exception=True, message="Smartcard public key file not specified (sc_public_key)")
7+
@contains('sc_public_key', "Smartcard public key file not specified (sc_public_key)", raise_exception=True)
98
def fetch_keys(self) -> None:
9+
""" Adds the GGP public key file to the list of dependencies. """
1010
self.logger.info("Adding GPG public key file to dependencies: %s", self['sc_public_key'])
1111
self['dependencies'] = self['sc_public_key']
1212

1313

14-
@check_dict('sc_public_key', raise_exception=True, message="Smartcard public key file not specified (sc_public_key)")
14+
@contains('sc_public_key', "Smartcard public key file not specified (sc_public_key)", raise_exception=True)
1515
def import_keys(self) -> str:
16-
""" Import GPG public keys. """
16+
""" Import GPG public keys at runtime. """
1717
return f"gpg --import {self['sc_public_key']}"
1818

src/ugrd/fs/btrfs.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
__version__ = '1.7.3'
1+
__version__ = '1.8.1'
22
__author__ = 'desultory'
33

44

5-
from zenlib.util import check_dict
5+
from zenlib.util import contains, unset
66

77

88
class SubvolNotFound(Exception):
@@ -25,16 +25,17 @@ def _get_mount_subvol(self, mountpoint: str) -> list:
2525
raise SubvolNotFound("No subvolume detected.")
2626

2727

28-
@check_dict('validate', value=True, message="Validate is not set, skipping root subvolume validation.")
29-
@check_dict('root_subvol', not_empty=True, message="root_subvol is not set, skipping validation.")
28+
@contains('validate', "validate is not enabled, skipping root subvolume validation.")
3029
def _validate_root_subvol(self) -> None:
3130
""" Validates the root subvolume. """
3231
try:
3332
detected_subvol = _get_mount_subvol(self, '/')
3433
except SubvolNotFound:
35-
raise ValueError("Current root mount is not using a subvolume, but root_subvol is set: %s" % self['root_subvol'])
34+
if self['root_subvol']:
35+
raise ValueError("Current root mount is not using a subvolume, but root_subvol is set: %s" % self['root_subvol'])
3636
except SubvolIsRoot:
37-
raise ValueError("Current root mount is not using a subvolume, but root_subvol is set: %s" % self['root_subvol'])
37+
if self['root_subvol'] != '/':
38+
raise ValueError("Current root mount is not using a subvolume, but root_subvol is set: %s" % self['root_subvol'])
3839

3940
if self['root_subvol'] != detected_subvol:
4041
raise ValueError("[%s] Root subvolume does not match detected subvolume: %s" % (self['root_subvol'], detected_subvol))
@@ -62,10 +63,10 @@ def btrfs_scan(self) -> str:
6263
return 'einfo "$(btrfs device scan)"'
6364

6465

65-
@check_dict('subvol_selector', value=False, log_level=20, message="subvol_selector enabled, skipping.")
66-
@check_dict('autodetect_root_subvol', value=True, message="autodetect_root_subvol not enabled, skipping.")
67-
@check_dict('root_subvol', unset=True, message="root_subvol is set, skipping.")
68-
@check_dict('hostonly', value=True, message="hostonly is not set, skipping.")
66+
@unset('subvol_selector', message="subvol_selector is enabled, skipping.", log_level=20)
67+
@contains('autodetect_root_subvol', "autodetect_root_subvol is not enabled, skipping.", log_level=30)
68+
@unset('root_subvol', message="root_subvol is set, skipping.")
69+
@contains('hostonly', "hostonly is not enabled, skipping.", log_level=30)
6970
def autodetect_root_subvol(self):
7071
""" Detects the root subvolume. """
7172
try:
@@ -78,8 +79,8 @@ def autodetect_root_subvol(self):
7879
self.logger.debug("Root mount is not using a subvolume.")
7980

8081

81-
@check_dict('subvol_selector', value=True, message="subvol_selector not enabled, skipping")
82-
@check_dict('root_subvol', unset=True, message="root_subvol is set, skipping.")
82+
@contains('subvol_selector', message="subvol_selector is not enabled, skipping.")
83+
@unset('root_subvol', message="root_subvol is set, skipping.")
8384
def select_subvol(self) -> str:
8485
""" Returns a bash script to list subvolumes on the root volume. """
8586
# TODO: Figure out a way to make the case prompt more standard
@@ -106,7 +107,7 @@ def select_subvol(self) -> str:
106107
f"umount -l {self['_base_mount_path']}"]
107108

108109

109-
@check_dict('root_subvol', not_empty=True, message="root_subvol is not set, skipping.")
110+
@contains('root_subvol', message="root_subvol is not set, skipping.")
110111
def set_root_subvol(self) -> str:
111112
""" Adds the root_subvol to the root_mount options. """
112113
_validate_root_subvol(self)

src/ugrd/fs/lvm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__author__ = 'desultory'
2-
__version__ = '1.2.0'
2+
__version__ = '1.2.1'
33

4-
from zenlib.util import check_dict
4+
from zenlib.util import contains
55

66

77
def _process_lvm_multi(self, mapped_name: str, config: dict) -> None:
@@ -11,7 +11,7 @@ def _process_lvm_multi(self, mapped_name: str, config: dict) -> None:
1111
self['lvm'][mapped_name] = config
1212

1313

14-
@check_dict('lvm', not_empty=True, log_level=10, message="Skipping LVM initialization, no LVM configurations found.")
14+
@contains('lvm', "Skipping LVM initialization, no LVM configurations found.")
1515
def init_lvm(self) -> None:
1616
""" Returns bash lines to initialize LVM """
1717
return ['einfo "Initializing LVM, module version %s"' % __version__,

0 commit comments

Comments
 (0)