From 806af26ced0d3dd6a92b5ebe3f4e366f62e60dee Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Sun, 4 Sep 2022 21:06:54 +0200 Subject: [PATCH] Use 0 as the default value for partition sizes instead of None --- mkosi/__init__.py | 32 ++++++++++++++++---------------- mkosi/backend.py | 16 ++++++++-------- tests/test_init.py | 2 +- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/mkosi/__init__.py b/mkosi/__init__.py index 276e8479e..ea36ae31d 100644 --- a/mkosi/__init__.py +++ b/mkosi/__init__.py @@ -776,19 +776,19 @@ def initialize_partition_table(state: MkosiState, force: bool = False) -> None: for condition, label, size, type_uuid, name, read_only in ( (state.config.bootable, PartitionIdentifier.esp, state.config.esp_size, GPT_ESP, "ESP System Partition", False), - (state.config.bios_size is not None, + (state.config.bios_size > 0, PartitionIdentifier.bios, state.config.bios_size, GPT_BIOS, "BIOS Boot Partition", False), - (state.config.xbootldr_size is not None, + (state.config.xbootldr_size > 0, PartitionIdentifier.xbootldr, state.config.xbootldr_size, GPT_XBOOTLDR, "Boot Loader Partition", False), - (state.config.swap_size is not None, + (state.config.swap_size > 0, PartitionIdentifier.swap, state.config.swap_size, GPT_SWAP, "Swap Partition", False), - (no_btrfs and state.config.home_size is not None, + (no_btrfs and state.config.home_size > 0, PartitionIdentifier.home, state.config.home_size, GPT_HOME, "Home Partition", False), - (no_btrfs and state.config.srv_size is not None, + (no_btrfs and state.config.srv_size > 0, PartitionIdentifier.srv, state.config.srv_size, GPT_SRV, "Server Data Partition", False), - (no_btrfs and state.config.var_size is not None, + (no_btrfs and state.config.var_size > 0, PartitionIdentifier.var, state.config.var_size, GPT_VAR, "Variable Data Partition", False), - (no_btrfs and state.config.tmp_size is not None, + (no_btrfs and state.config.tmp_size > 0, PartitionIdentifier.tmp, state.config.tmp_size, GPT_TMP, "Temporary Data Partition", False), (not is_generated_root(state.config), PartitionIdentifier.root, state.config.root_size, @@ -796,7 +796,7 @@ def initialize_partition_table(state: MkosiState, force: bool = False) -> None: root_partition_description(state.config), state.config.read_only)): - if condition and size is not None: + if condition and size > 0: table.add(label, size, type_uuid, name, read_only=read_only) state.partition_table = table @@ -5873,10 +5873,10 @@ def parse_args_file_group( return create_parser().parse_args(config_files + argv) -def parse_bytes(num_bytes: Optional[str], *, sector_size: int = 512) -> Optional[int]: +def parse_bytes(num_bytes: Optional[str], *, sector_size: int = 512) -> int: """Convert a string for a number of bytes into a number rounding up to sector size.""" if num_bytes is None: - return None + return 0 if num_bytes.endswith("G"): factor = 1024 ** 3 @@ -6515,10 +6515,10 @@ def load_args(args: argparse.Namespace) -> MkosiConfig: args.swap_size = parse_bytes(args.swap_size) args.bios_size = parse_bytes(args.bios_size) - if args.root_size is None: + if args.root_size == 0: args.root_size = 3 * 1024 * 1024 * 1024 - if args.bootable and args.esp_size is None: + if args.bootable and args.esp_size == 0: args.esp_size = 256 * 1024 * 1024 if args.secure_boot_key is not None: @@ -6675,15 +6675,15 @@ def yes_no_or(b: Union[bool, str]) -> str: return b if isinstance(b, str) else yes_no(b) -def format_bytes_or_disabled(sz: Optional[int]) -> str: - if sz is None: +def format_bytes_or_disabled(sz: int) -> str: + if sz == 0: return "(disabled)" return format_bytes(sz) -def format_bytes_or_auto(sz: Optional[int]) -> str: - if sz is None: +def format_bytes_or_auto(sz: int) -> str: + if sz == 0: return "(automatic)" return format_bytes(sz) diff --git a/mkosi/backend.py b/mkosi/backend.py index 880f53230..911d6620c 100644 --- a/mkosi/backend.py +++ b/mkosi/backend.py @@ -525,14 +525,14 @@ class MkosiConfig: nspawn_settings: Optional[Path] base_image: Optional[Path] root_size: int - esp_size: Optional[int] - xbootldr_size: Optional[int] - swap_size: Optional[int] - home_size: Optional[int] - srv_size: Optional[int] - var_size: Optional[int] - tmp_size: Optional[int] - bios_size: Optional[int] + esp_size: int + xbootldr_size: int + swap_size: int + home_size: int + srv_size: int + var_size: int + tmp_size: int + bios_size: int usr_only: bool split_artifacts: bool checksum: bool diff --git a/tests/test_init.py b/tests/test_init.py index 2281e1c8d..7c65ca39e 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -58,7 +58,7 @@ def test_copy_file(tmpdir: Path) -> None: def test_parse_bytes() -> None: - assert mkosi.parse_bytes(None) is None + assert mkosi.parse_bytes(None) == 0 assert mkosi.parse_bytes("1") == 512 assert mkosi.parse_bytes("1000") == 1024 assert mkosi.parse_bytes("1K") == 1024 -- 2.47.2