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

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