jw-pkg/src/python/jw/pkg/lib/pm/rpm.py
Jan Lindemann fc6f2fbb65
python-tools.sh: Fix __init__.py linter complaints

The __init__.py files as gnerated by python-tools.sh contain multiple issues, fix them:

- Make the machinery fail if the same type name is imported from different modules
- Support relative imports from .Module import Module instead of having to use the entire module path as import source

- Import types explicitly re-exported with "as":

from .Module import Module as Module
Otherwise ruff will regard the type as "imported but not used"
- Add "# ruff: noqa: E501" near the top. The import lines can get long and are beyond manual control (except for renaming the modules themselves, that is). This can cause ruff to fail, so get it to accept long lines in __init__.py. The style violation doesn't make much of a difference in generated code, anyway, because nobody reads that. Plus what's happening in the code isn't rocket science, so good style wouldn't help much with understanding, either.

This promptly digs up two symbol name conflicts lib.pm.dpkg and lib.pm.rpm. Fix them along with this commit to keep it from breaking the build.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-05-31 17:51:51 +02:00

45 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import annotations
from typing import Iterable, TYPE_CHECKING
if TYPE_CHECKING:
from ..ExecContext import ExecContext
from ..util import run_cmd, run_sudo
from ..base import InputMode
from ..Package import Package, meta_tags
_meta_map: dict[str, str]|None = None
def meta_map():
global _meta_map
if _meta_map is None:
_meta_map = Package.order_tags({
'name': 'Name',
'vendor': 'Vendor',
'packager': 'Packager',
'url': 'URL',
'maintainer': None, # RPM doesn't have a maintainer field
})
return _meta_map
async def run_rpm(args: list[str], sudo: bool=False, ec: ExecContext=None, mode: InputMode=InputMode.OptInteractive, **kwargs): # export
cmd = ['/usr/bin/rpm']
cmd.extend(args)
if sudo:
return await run_sudo(cmd, ec=ec, cmd_input=mode, **kwargs)
return await run_cmd(cmd, ec=ec, cmd_input=mode, **kwargs)
async def query_packages(names: Iterable[str] = [], ec: ExecContext=None) -> Iterable[Package]:
fmt_str = '|'.join([(f'%{{{tag}}}' if tag else '') for tag in meta_map().values()]) + r'\n'
opts = ['-q', '--queryformat', fmt_str]
if not names:
opts.append('-a')
specs, stderr, status = await run_rpm([*opts, *names], throw=True, sudo=False, mode=InputMode.NonInteractive, ec=ec)
return Package.parse_specs_str(specs.decode())
async def list_files(pkg: str, ec: ExecContext=None) -> list[str]:
stdout, stderr, status = await run_rpm(['-ql', pkg], throw=True, sudo=False, mode=InputMode.NonInteractive, ec=ec)
return stdout.decode().splitlines()