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>
This commit is contained in:
Jan Lindemann 2026-05-30 09:00:04 +02:00
commit fc6f2fbb65
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
4 changed files with 32 additions and 18 deletions

View file

@ -20,7 +20,7 @@ include $(JWBDIR)/make/py-rules.mk
ifeq ($(PY_UPDATE_INIT_PY),true) ifeq ($(PY_UPDATE_INIT_PY),true)
__init__.py: $(PY_INIT_TMPL) $(filter-out __init__.py,$(PY_SRC_PY)) __init__.py: $(PY_INIT_TMPL) $(filter-out __init__.py,$(PY_SRC_PY))
if [ "$(PY_INIT_TMPL)" ]; then cat "$(PY_INIT_TMPL)" > $@.tmp; else > $@.tmp; fi if [ "$(PY_INIT_TMPL)" ]; then cat "$(PY_INIT_TMPL)" > $@.tmp; else > $@.tmp; fi
/bin/bash +H $(JWB_SCRIPT_DIR)/python-tools.sh create-init -m $(PY_MOD) -e "$(PY_SED_EXTRACT_EXPORT)" \ set -e -o pipefail; /bin/bash $(JWB_SCRIPT_DIR)/python-tools.sh create-init -m . -e "$(PY_SED_EXTRACT_EXPORT)" \
$(filter-out __init__.py,$(PY_ALL_PY)) $(SUBDIRS_TO_ITERATE) | $(PY_INIT_FILTER) | tee -a $@.tmp $(filter-out __init__.py,$(PY_ALL_PY)) $(SUBDIRS_TO_ITERATE) | $(PY_INIT_FILTER) | tee -a $@.tmp
mv $@.tmp $@ mv $@.tmp $@
endif endif

View file

@ -1,5 +1,16 @@
#!/bin/bash #!/bin/bash
log()
{
echo "$myname: $*" >&2
}
fatal()
{
echo "$myname: Fatal: $*" >&2
exit 1
}
usage() usage()
{ {
cat <<-EOT cat <<-EOT
@ -12,33 +23,36 @@ usage()
module_path() module_path()
{ {
if [ "$module" ]; then if [ "$module" = "." ]; then
echo .$1
elif [ "$module" ]; then
echo $module.$1 echo $module.$1
return else
echo $1
fi fi
echo $1
} }
cmd_create_init() cmd_create_init()
{ {
local import_submodules=0 local import_submodules=0
local e f files base extracted module_path local files="$*"
local del="-------------------------- generated by $myname" local del="-------------------------- generated by $myname"
echo "# >> $del >>" echo "# >> $del >>"
echo "# ruff: noqa: E501"
echo "from pkgutil import extend_path" echo "from pkgutil import extend_path"
echo "from typing import Iterable" echo "__path__ = extend_path(__path__, __name__)"
echo "__path__ = extend_path(__path__, __name__) # type: ignore" # was "type Iterable[str]" local f
files="$*" local -A seen=()
for f in $files; do for f in $files; do
test -d $f && continue test -d $f && continue
base=${f##*/} local base=${f##*/}
base=${base%.py} base=${base%.py}
if [ "$sed_extract_command" ]; then if [ "$sed_extract_command" ]; then
#echo running $sed_extract_command on $f local type types=`sed "$sed_extract_command" $f`
extracted=`sed "$sed_extract_command" $f` for type in $types; do
#extracted="$sed_extract_command" echo "from `module_path $base` import $type as $type"
for e in $extracted; do [[ -n "${seen[$type]+x}" ]] && fatal "Duplicate symbol: $type"
echo "from `module_path $base` import $e" seen["$type"]=1
done done
fi fi
done done

View file

@ -39,12 +39,12 @@ async def run_dpkg_query(args: list[str], sudo: bool=False, ec: ExecContext=None
return await run_sudo(cmd) return await run_sudo(cmd)
return (await run_cmd(cmd, ec=ec, cmd_input=InputMode.NonInteractive)).decode() return (await run_cmd(cmd, ec=ec, cmd_input=InputMode.NonInteractive)).decode()
async def query_packages(names: Iterable[str] = [], ec: ExecContext=None) -> Iterable[Package]: # export 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' fmt_str = '|'.join([(f'${{{tag}}}' if tag else '') for tag in meta_map().values()]) + r'\n'
# dpkg-query -W -f='${binary:Package}|${Maintainer}| ... \n' # dpkg-query -W -f='${binary:Package}|${Maintainer}| ... \n'
specs, stderr, status = await run_dpkg_query(['-W', '-f=' + fmt_str, *names], sudo=False, ec=ec) specs, stderr, status = await run_dpkg_query(['-W', '-f=' + fmt_str, *names], sudo=False, ec=ec)
return Package.parse_specs_str(specs) return Package.parse_specs_str(specs)
async def list_files(pkg: str, ec: ExecContext=None) -> list[str]: # export async def list_files(pkg: str, ec: ExecContext=None) -> list[str]:
file_list_str, stderr, status = await run_dpkg(['-L', pkg], sudo=False, ec=ec) file_list_str, stderr, status = await run_dpkg(['-L', pkg], sudo=False, ec=ec)
return file_list_str.splitlines() return file_list_str.splitlines()

View file

@ -32,7 +32,7 @@ async def run_rpm(args: list[str], sudo: bool=False, ec: ExecContext=None, mode:
return await run_sudo(cmd, ec=ec, cmd_input=mode, **kwargs) return await run_sudo(cmd, ec=ec, cmd_input=mode, **kwargs)
return await run_cmd(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]: # export 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' fmt_str = '|'.join([(f'%{{{tag}}}' if tag else '') for tag in meta_map().values()]) + r'\n'
opts = ['-q', '--queryformat', fmt_str] opts = ['-q', '--queryformat', fmt_str]
if not names: if not names:
@ -40,6 +40,6 @@ async def query_packages(names: Iterable[str] = [], ec: ExecContext=None) -> Ite
specs, stderr, status = await run_rpm([*opts, *names], throw=True, sudo=False, mode=InputMode.NonInteractive, ec=ec) specs, stderr, status = await run_rpm([*opts, *names], throw=True, sudo=False, mode=InputMode.NonInteractive, ec=ec)
return Package.parse_specs_str(specs.decode()) return Package.parse_specs_str(specs.decode())
async def list_files(pkg: str, ec: ExecContext=None) -> list[str]: # export 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) stdout, stderr, status = await run_rpm(['-ql', pkg], throw=True, sudo=False, mode=InputMode.NonInteractive, ec=ec)
return stdout.decode().splitlines() return stdout.decode().splitlines()