From b8c072e5ee661ded67663d4ab9a30e1e9b7f7599 Mon Sep 17 00:00:00 2001 From: InsanePrawn Date: Sat, 29 Mar 2025 20:25:03 +0100 Subject: [PATCH] fix linter issues --- src/kupferbootstrap/chroot/abstract.py | 6 +++--- src/kupferbootstrap/chroot/device.py | 2 +- src/kupferbootstrap/config/cli.py | 10 +++++----- src/kupferbootstrap/config/profile.py | 2 +- src/kupferbootstrap/config/state.py | 2 +- src/kupferbootstrap/dictscheme.py | 4 ++-- src/kupferbootstrap/exec/file.py | 8 ++++---- src/kupferbootstrap/image/image.py | 22 ++++++++++++---------- src/kupferbootstrap/packages/build.py | 5 +++-- tests/test_integration.py | 2 +- 10 files changed, 33 insertions(+), 30 deletions(-) diff --git a/src/kupferbootstrap/chroot/abstract.py b/src/kupferbootstrap/chroot/abstract.py index 3e5c3a2..6b292f6 100644 --- a/src/kupferbootstrap/chroot/abstract.py +++ b/src/kupferbootstrap/chroot/abstract.py @@ -254,10 +254,10 @@ class Chroot(AbstractChroot): # make sure paths start with '/'. Important: also copies the collection and casts to list, which will be sorted! mounts = [make_abs_path(path) for path in relative_paths] mounts.sort(reverse=True) - for mount in mounts: - if mount == "/proc": + for _mount in mounts: + if _mount == "/proc": continue - self.umount(mount) + self.umount(_mount) if "/proc" in mounts: self.umount("/proc") diff --git a/src/kupferbootstrap/chroot/device.py b/src/kupferbootstrap/chroot/device.py index 0868e52..c0d8074 100644 --- a/src/kupferbootstrap/chroot/device.py +++ b/src/kupferbootstrap/chroot/device.py @@ -84,7 +84,7 @@ def get_device_chroot( **kwargs, ) -> DeviceChroot: name = f"rootfs_{device}-{flavour}" - repos: dict[str, RepoInfo] = ( + repos: dict[str, RepoInfo] = dict( get_kupfer_local(arch).repos if use_local_repos else get_kupfer_https(arch).repos diff --git a/src/kupferbootstrap/config/cli.py b/src/kupferbootstrap/config/cli.py index 779f19e..f073c23 100644 --- a/src/kupferbootstrap/config/cli.py +++ b/src/kupferbootstrap/config/cli.py @@ -57,14 +57,14 @@ def prompt_config( to_check or to_check is zero ) # can't do == due to boolean<->int casting - if type(None) == field_type: + if type(None) is field_type: field_type = str - if field_type == dict: + if field_type is dict: raise Exception( "Dictionaries not supported by config_prompt, this is likely a bug in kupferbootstrap" ) - elif field_type == list: + elif field_type is list: default = list_to_comma_str(default) value_conv = comma_str_to_list else: @@ -83,7 +83,7 @@ def prompt_config( show_choices=show_choices, ) # type: ignore changed = result != ( - original_default if field_type == list else default + original_default if field_type is list else default ) and (true_or_zero(default) or true_or_zero(result)) if changed and echo_changes: print(f'value changed: "{text}" = "{result}"') @@ -411,7 +411,7 @@ def cmd_config_set( key: str = split_pair[0] value: Any = split_pair[1] value_type = type(config_dot_name_get(key, CONFIG_DEFAULTS)) - if value_type != list: + if value_type is not list: value = click.types.convert_type(value_type)(value) else: value = comma_str_to_list(value, default=[]) diff --git a/src/kupferbootstrap/config/profile.py b/src/kupferbootstrap/config/profile.py index f58d1b3..aa47f69 100644 --- a/src/kupferbootstrap/config/profile.py +++ b/src/kupferbootstrap/config/profile.py @@ -95,7 +95,7 @@ def resolve_profile( for key, value in PROFILE_DEFAULTS_DICT.items(): if key not in full.keys(): full[key] = value # type: ignore[literal-required] - if type(value) == list: + if type(value) is list: full[key] = [] # type: ignore[literal-required] full["size_extra_mb"] = int(full["size_extra_mb"] or 0) diff --git a/src/kupferbootstrap/config/state.py b/src/kupferbootstrap/config/state.py index 3574e2d..f29e113 100644 --- a/src/kupferbootstrap/config/state.py +++ b/src/kupferbootstrap/config/state.py @@ -274,7 +274,7 @@ class ConfigStateHolder: raise ConfigLoadException(Exception(m)) ex = self.file_state.exception if ex: - if type(ex) == FileNotFoundError: + if isinstance(ex, FileNotFoundError): ex = Exception( "Config file doesn't exist. Try running `kupferbootstrap config init` first?" ) diff --git a/src/kupferbootstrap/dictscheme.py b/src/kupferbootstrap/dictscheme.py index b938c0f..ed9d1d3 100644 --- a/src/kupferbootstrap/dictscheme.py +++ b/src/kupferbootstrap/dictscheme.py @@ -50,7 +50,7 @@ def resolve_dict_hints(hints: Any) -> Generator[tuple[Any, ...], None, None]: for hint in flatten_hints(hints): t_origin = get_origin(hint) t_args = get_args(hint) - if t_origin == dict: + if t_origin is dict: yield t_args continue if t_origin in [NoneType, Optional, Union, UnionType] and t_args: @@ -294,7 +294,7 @@ class DictScheme(Munch): hints_flat = list(flatten_hints(_hints)) subclass = DictScheme for hint in hints_flat: - if get_origin(hint) == dict: + if get_origin(hint) is dict: _valtype = get_args(hint)[1] _subhints = {n: _valtype for n in v.keys()} break diff --git a/src/kupferbootstrap/exec/file.py b/src/kupferbootstrap/exec/file.py index 62cdad9..3791384 100644 --- a/src/kupferbootstrap/exec/file.py +++ b/src/kupferbootstrap/exec/file.py @@ -71,7 +71,7 @@ def chmod( octal = octal.rjust(4, "0") try: os.chmod(path, mode=octal) # type: ignore - except: + except Exception: cmd = ["chmod", octal, path] result = run_cmd(cmd, switch_user="root" if privileged else None) assert isinstance(result, subprocess.CompletedProcess) @@ -177,7 +177,7 @@ def remove_file(path: str, recursive=False): try: rm = rmtree if recursive else os.unlink rm(path) # type: ignore - except: + except Exception: cmd = ["rm"] + (["-r"] if recursive else []) + [path] rc = run_root_cmd(cmd).returncode if rc: @@ -197,7 +197,7 @@ def makedir( os.makedirs(path, exist_ok=True) else: os.mkdir(path) - except: + except Exception: run_root_cmd(["mkdir"] + (["-p"] if parents else []) + [path]) if mode is not None: chmod(path, mode=mode) @@ -212,7 +212,7 @@ def symlink(source, target): "Create a symlink at `target`, pointing at `source`" try: os.symlink(source, target) - except: + except Exception: result = run_root_cmd(["ln", "-s", source, target]) assert isinstance(result, subprocess.CompletedProcess) if result.returncode: diff --git a/src/kupferbootstrap/image/image.py b/src/kupferbootstrap/image/image.py index c031f6c..a991de7 100644 --- a/src/kupferbootstrap/image/image.py +++ b/src/kupferbootstrap/image/image.py @@ -78,16 +78,17 @@ def get_fs_size(partition: str) -> tuple[int, int]: blocks_cmd.stdout.decode("utf-8") if blocks_cmd.stdout else "" ) try: - fs_blocks = int( - re.search( - "\\nBlock count:[ ]+([0-9]+)\\n", - blocks_text, - flags=re.MULTILINE, - ).group(1) - ) # type: ignore[union-attr] - fs_block_size = int( - re.search("\\nBlock size:[ ]+([0-9]+)\\n", blocks_text).group(1) - ) # type: ignore[union-attr] + re_blocks = re.search( + "\\nBlock count:[ ]+([0-9]+)\\n", + blocks_text, + flags=re.MULTILINE, + ) + assert re_blocks, "Block output should contain block count" + fs_blocks = int(re_blocks.group(1)) + + re_blocksize = re.search("\\nBlock size:[ ]+([0-9]+)\\n", blocks_text) + assert re_blocksize, "Block output should contain block size" + fs_block_size = int(re_blocksize.group(1)) except Exception as ex: logging.debug(f"dumpe2fs stdout:\n {blocks_text}") logging.debug(f"dumpe2fs stderr:\n: {blocks_cmd.stderr}") @@ -133,6 +134,7 @@ def shrink_fs(loop_device: str, file: str, sector_size: int): ), # type: ignore stdin=subprocess.PIPE, ) + assert child_proccess.stdin is not None child_proccess.stdin.write( "\n".join( [ # type: ignore diff --git a/src/kupferbootstrap/packages/build.py b/src/kupferbootstrap/packages/build.py index 2ed1af1..e4a1e59 100644 --- a/src/kupferbootstrap/packages/build.py +++ b/src/kupferbootstrap/packages/build.py @@ -747,7 +747,7 @@ def build_package( failed_deps = [ name for name, res in dep_install.items() - if res.returncode != 0 + if (res.returncode if not isinstance(res, int) else res) != 0 ] # type: ignore[union-attr] if failed_deps: raise Exception( @@ -1092,7 +1092,8 @@ def build_enable_qemu_binfmt( .packages ) pkgfiles = [ - os.path.join(crossrepo[pkg].resolved_url.split("file://")[1]) + # we want this to crash on unresolved URL + os.path.join((crossrepo[pkg].resolved_url or "").split("file://")[1]) for pkg in QEMU_BINFMT_PKGS ] # type: ignore runcmd = run_root_cmd diff --git a/tests/test_integration.py b/tests/test_integration.py index 047ae76..98b5d0d 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -34,7 +34,7 @@ try: integration_enabled = bool( int(os.environ.get(INTEGRATION_TESTS_ENABLE_ENV_VAR, 0)) > 0 ) -except: +except Exception: pass