jw.pkg: Fix "make check" static code check fallout
The previous commits have put rules for linting and formatting via ruff, yapf, mypy and pyright into place. They are checked with the make check target, and this commit adds the fixes for the target to succeed.
It does some refactoring where type checking dug up dirty bits, and also adds lots of churn in the Python code. To a good deal, that's owed to mere formatting changes. It would have been better to seperate those from syntax and refactoring fixes into multiple commits, so that the interesting changes don't drown in the formatting nose. However, that would have been a lot of additional work only to be thrown away by later commits, hence this commit has a big diff in one piece. The size of the diff is regrettable but hopefully a one-off: What it buys is automatic format checking for CI and predictble formats for smaller diffs in the future.
Rules that "make check" enforces are, in the following order
- Syntax checkers:
- ruff check . - mypy . - pyright- Format check:
- yapf --diff --recursive .The refactoring includes:
- Turn the Result class into a more elaborate object, capable of doing more heavy lifting around stderr and stdout decoding, summarizing outcome, and matching error strings.Aside from fixing broken type checks, this also removes lots of boilerplate calling code which is currently used for handling possible call outcome scenarios. Trying to access an inexistent, decoded string should raise a meaningful exception by itself now, which removes lots of code with case distinctions.- Fix Cmd type hierarchy:
- Add the AbstractCmd class above Cmd. This is necessary because the checker rightfully complains it can't instantiate a Cmd instance where constructor arguments were needed. They never were, but the type used at the instantiating code's location in jw.pkg.App so claims.- Lots of sub- and sub-subcommands are derived from the base class of the invoking command. That provides some properties shared across the ancestor hierarchy of a command, but is semantically unsound. Fix that by introducing jw.pkg.BaseCmd class as a place to provide basic helpers shared across all commands used in a jw.pkg.App's context, and derive all command classes from that afresh. The parent command is still reachable via a common parent property.Formatting changes are conforming to PEP-8, mostly, with minor tweaks. All in all they include the following changes.
- Remove # -*- coding: utf-8 -*-
The line was needed by Python 2 which is not supported anylonger. For Python 3, the default encoding is UTF-8, anyway.- Allow to run "make py-format" without having it produce any changes. It's basically "yapf --in-place --recursive ." with some code style settings, see conf/topdir/pyproject.toml. The settings may be debatable. I've had custom tweaks in place on that target, too, but then again, IDEs would have more hassle to integrate that.- Introduce a 88 character line length limit
- One import per line, reshuffle them semantically, see [tool.isort] in pyproject.toml.- Hide imports needed for type-checking only behind
if TYPE_CHECKING- Spaces around assignments accounts for much churn. Having having no spaces in inline parameter list assignments and default parameter values would arguably be more compact where it's useful. On the other hand, I have not found a code formatter which allows spaces around assignments in parameter lists broken into one per line and that's often better than a wall of text.- Add two spaces before # export, as this seems to be mandated by PEP-8- Use single quotes by default
Signed-off-by: Jan Lindemann <jan@janware.com>
|
|
@ -1,53 +1,65 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...Distro import Distro as Base
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Iterable
|
||||
|
||||
from ...base import Result
|
||||
from ...Package import Package
|
||||
|
||||
class Distro(Base):
|
||||
|
||||
async def pacman(self, args: list[str], verbose: bool=True, sudo: bool=True) -> Result:
|
||||
async def pacman(
|
||||
self, args: list[str], verbose: bool = True, sudo: bool = True
|
||||
) -> Result:
|
||||
cmd = ['/usr/bin/pacman']
|
||||
if not self.interactive:
|
||||
cmd.extend(['--noconfirm'])
|
||||
cmd.extend(args)
|
||||
if sudo:
|
||||
return await self.sudo(cmd, verbose=verbose)
|
||||
return await self.run(cmd, verbose=verbose)
|
||||
return await self.sudo(cmd, verbose = verbose)
|
||||
return await self.run(cmd, verbose = verbose)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def _ref(self) -> None:
|
||||
raise NotImplementedError('distro refresh is not yet implemented for Arch-like distributions')
|
||||
raise NotImplementedError(
|
||||
'distro refresh is not yet implemented for Arch-like distributions'
|
||||
)
|
||||
|
||||
async def _dup(self, download_only: bool) -> None:
|
||||
args = ['-Su']
|
||||
if args.download_only:
|
||||
if download_only:
|
||||
args.append('-w')
|
||||
return await self.pacman(args)
|
||||
await self.pacman(args)
|
||||
|
||||
async def _reboot_required(self, verbose: bool) -> bool:
|
||||
raise NotImplementedError('distro reboot-required is not yet implemented for Arch-like distributions')
|
||||
raise NotImplementedError(
|
||||
'distro reboot-required is not yet implemented for Arch-like distributions'
|
||||
)
|
||||
|
||||
async def _select_by_name(self, names: Iterable[str]) -> Iterable[Package]:
|
||||
raise NotImplementedError('distro select is not yet implemented for Arch-like distributions')
|
||||
raise NotImplementedError(
|
||||
'distro select is not yet implemented for Arch-like distributions'
|
||||
)
|
||||
|
||||
async def _install(self, names: Iterable[str], only_update: bool) -> None:
|
||||
if only_update:
|
||||
raise NotImplementedError('--only-update is not yet implemented for pacman')
|
||||
args = ['-S', '--needed']
|
||||
args.extend(args.packages)
|
||||
args.extend(names)
|
||||
await self.pacman(args)
|
||||
|
||||
async def _delete(self, names: Iterable[str]) -> None:
|
||||
raise NotImplementedError('distro delete not yet implemented for Arch-like distributions')
|
||||
raise NotImplementedError(
|
||||
'distro delete not yet implemented for Arch-like distributions'
|
||||
)
|
||||
|
||||
async def _pkg_files(self, name: str) -> Iterable[str]:
|
||||
raise NotImplementedError('distro pkg ls yet implemented for Arch-like distributions')
|
||||
raise NotImplementedError(
|
||||
'distro pkg ls yet implemented for Arch-like distributions'
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,47 +1,52 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
import os
|
||||
|
||||
from ...log import *
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...Distro import Distro as Base
|
||||
from ...pm.dpkg import run_dpkg, run_dpkg_query, query_packages, list_files
|
||||
from ...log import NOTICE, log
|
||||
from ...pm.dpkg import list_files, query_packages, run_dpkg
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Iterable
|
||||
|
||||
from ...base import Result
|
||||
from ...Package import Package
|
||||
|
||||
class Distro(Base):
|
||||
|
||||
async def apt_get(self, args: list[str], verbose: bool=True, sudo: bool=True):
|
||||
async def apt_get(
|
||||
self, args: list[str], verbose: bool = True, sudo: bool = True
|
||||
) -> Result:
|
||||
cmd = ['/usr/bin/apt-get']
|
||||
mod_env_cmd = None
|
||||
if not self.interactive:
|
||||
cmd.extend(['--yes', '--quiet'])
|
||||
mod_env_cmd = { 'DEBIAN_FRONTEND': 'noninteractive' }
|
||||
mod_env_cmd = {'DEBIAN_FRONTEND': 'noninteractive'}
|
||||
cmd.extend(args)
|
||||
if sudo:
|
||||
return await self.sudo(cmd, verbose=verbose, mod_env_cmd=mod_env_cmd)
|
||||
return await self.run(cmd, verbose=verbose)
|
||||
return (
|
||||
await
|
||||
self.sudo(cmd, verbose = verbose, mod_env_cmd = mod_env_cmd, throw = True)
|
||||
if sudo else await self.run(cmd, verbose = verbose)
|
||||
)
|
||||
|
||||
async def dpkg(self, *args, **kwargs):
|
||||
return await run_dpkg(*args, ec=self.ctx, **kwargs)
|
||||
async def dpkg(self, *args, **kwargs) -> str:
|
||||
kwargs.setdefault('ec', self.ctx)
|
||||
return await run_dpkg(*args, **kwargs)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def _ref(self) -> None:
|
||||
return await self.apt_get(['update'])
|
||||
await self.apt_get(['update'])
|
||||
|
||||
async def _dup(self, download_only: bool) -> None:
|
||||
args: list[str] = []
|
||||
if download_only:
|
||||
args.append('--download-only')
|
||||
args.append('upgrade')
|
||||
return await self.apt_get(args)
|
||||
await self.apt_get(args)
|
||||
|
||||
async def _reboot_required(self, verbose: bool) -> bool:
|
||||
reboot_required = '/run/reboot_required'
|
||||
|
|
@ -56,11 +61,11 @@ class Distro(Base):
|
|||
print(content.strip())
|
||||
return True
|
||||
if verbose:
|
||||
log(NOTICE, f'No. {reboot_required} doesn\'t exist.')
|
||||
log(NOTICE, f"No. {reboot_required} doesn't exist.")
|
||||
return False
|
||||
|
||||
async def _select_by_name(self, names: Iterable[str]) -> Iterable[Package]:
|
||||
return await query_packages(names, ec=self.ctx)
|
||||
return await query_packages(names, ec = self.ctx)
|
||||
|
||||
async def _install(self, names: Iterable[str], only_update: bool) -> None:
|
||||
args = ['install']
|
||||
|
|
@ -68,10 +73,10 @@ class Distro(Base):
|
|||
args.append('--only-upgrade')
|
||||
args.append('--no-install-recommends')
|
||||
args.extend(names)
|
||||
return await self.apt_get(args)
|
||||
await self.apt_get(args)
|
||||
|
||||
async def _delete(self, names: Iterable[str]) -> None:
|
||||
return await self.dpkg(['-P', *names], sudo=True)
|
||||
await self.dpkg(['-P', *names], sudo = True)
|
||||
|
||||
async def _pkg_files(self, name: str) -> Iterable[str]:
|
||||
return await list_files(name, ec=self.ctx)
|
||||
return await list_files(name, ec = self.ctx)
|
||||
|
|
|
|||
|
|
@ -1,62 +1,71 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from ...Distro import Distro as Base
|
||||
from ...pm.rpm import run_rpm, query_packages, list_files
|
||||
from ...pm.rpm import list_files, query_packages, run_rpm
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from typing import Iterable
|
||||
|
||||
from ...base import Result
|
||||
from ...ExecContext import ExecContext
|
||||
from ...Package import Package
|
||||
|
||||
class Distro(Base):
|
||||
|
||||
async def zypper(self, args: list[str], verbose: bool=True, sudo: bool=True) -> Result:
|
||||
async def zypper(
|
||||
self, args: list[str], verbose: bool = True, sudo: bool = True
|
||||
) -> Result:
|
||||
cmd = ['/usr/bin/zypper']
|
||||
if not self.interactive:
|
||||
cmd.extend(['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks'])
|
||||
cmd.extend(
|
||||
['--non-interactive', '--gpg-auto-import-keys', '--no-gpg-checks']
|
||||
)
|
||||
cmd.extend(args)
|
||||
if sudo:
|
||||
return await self.sudo(cmd, verbose=verbose)
|
||||
return await self.run(cmd, verbose=verbose)
|
||||
return (
|
||||
await self.sudo(cmd, verbose = verbose)
|
||||
if sudo else await self.run(cmd, verbose = verbose)
|
||||
)
|
||||
|
||||
async def rpm(self, *args, **kwargs) -> Result:
|
||||
return await run_rpm(*args, ec=self.ctx, **kwargs)
|
||||
async def rpm(self, *args, ec: ExecContext | None = None, **kwargs) -> str:
|
||||
if ec is None:
|
||||
ec = self.ctx
|
||||
kwargs['ec'] = ec
|
||||
return await run_rpm(*args, **kwargs)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
async def _ref(self) -> None:
|
||||
return await self.zypper(['refresh'])
|
||||
await self.zypper(['refresh'])
|
||||
|
||||
async def _dup(self, download_only: bool) -> None:
|
||||
args = ['dup', '--force-resolution', '--auto-agree-with-licenses']
|
||||
if download_only:
|
||||
args.append('--download-only')
|
||||
return await self.zypper(args)
|
||||
await self.zypper(args)
|
||||
|
||||
async def _reboot_required(self, verbose: bool) -> bool:
|
||||
opts = []
|
||||
if not verbose:
|
||||
pass
|
||||
#opts.append('--quiet')
|
||||
# opts.append('--quiet')
|
||||
opts.append('needs-rebooting')
|
||||
stdout, stderr, ret = await self.zypper(opts, sudo=False, verbose=verbose)
|
||||
ret = await self.zypper(opts, sudo = False, verbose = verbose)
|
||||
if ret != 0:
|
||||
return True
|
||||
return False
|
||||
|
||||
async def _select_by_name(self, names: Iterable[str]) -> Iterable[Package]:
|
||||
return await query_packages(names, ec=self.ctx)
|
||||
return await query_packages(names, ec = self.ctx)
|
||||
|
||||
async def _install(self, names: Iterable[str], only_update: bool) -> None:
|
||||
cmd = 'update' if only_update else 'install'
|
||||
return await self.zypper([cmd, *names])
|
||||
await self.zypper([cmd, *names])
|
||||
|
||||
async def _delete(self, names: Iterable[str]) -> None:
|
||||
return await self.rpm(['-e', *names], sudo=True)
|
||||
await self.rpm(['-e', *names], sudo = True)
|
||||
|
||||
async def _pkg_files(self, name: str) -> Iterable[str]:
|
||||
return await list_files(name, ec=self.ctx)
|
||||
return await list_files(name, ec = self.ctx)
|
||||
|
|
|
|||