jw-pkg/scripts/python-tools.sh
Jan Lindemann 9039dc7e40
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 3m8s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 3m22s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 3m9s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 3m14s
CI / Packaging test (push) Successful in 0s
python-tools.sh, py-mod.mk: Use --symbol-filter

Letting python-tools.sh rewrite symbols is more robust than rewriting an entire __init__.py with PY_INIT_FILTER in the including Makefile. The latter can break in non-obvious ways if python-tools.sh changes __init__.py's format.

Make python-tools.sh support --symbol-filter to remedy that. The option takes an sed script which should expect a string of two non-whitespace tokens: The module from which the symbol is imported, and the name of the symbol in that module. It's output will then be used as the symbol to be exported from __init__.py.

Also, support the PY_SYMBOL_FILTER variable in py-mod.mk. If it's defined, it is used for --symbol-filter.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-12 07:33:07 +02:00

123 lines
2 KiB
Bash

#!/bin/bash
log()
{
echo "$myname: $*" >&2
}
fatal()
{
echo "$myname: Fatal: $*" >&2
exit 1
}
usage()
{
cat <<-EOT
usage $myname [-e sed-extract-command] [-m module] file.py ...
EOT
[ "$1" ] && exit $1
}
module_path()
{
if [ "$module" = "." ]; then
echo .$1
elif [ "$module" ]; then
echo $module.$1
else
echo $1
fi
}
cmd_create_init()
{
__add_seen() {
local type="$1"
[[ -n "${seen[$type]+x}" ]] && fatal "Duplicate symbol: $type"
seen["$type"]=1
}
local import_submodules=0
local files="$*"
local del="-------------------------- generated by $myname"
echo "# >> $del >>"
echo "# ruff: noqa: E501"
echo "from pkgutil import extend_path"
echo ""
echo "__path__ = extend_path(__path__, __name__)"
echo
local f
local -A seen=()
local dst_type
for f in $files; do
test -d $f && continue
local base=${f##*/}
base=${base%.py}
if [ "$sed_extract_cmd" ]; then
local src_type types=$(sed "$sed_extract_cmd" $f)
for src_type in $types; do
if [ -z "$sed_symbol_filter_cmd" ]; then
dst_type="$src_type"
else
dst_type=$(echo $base $src_type | sed "$sed_symbol_filter_cmd")
fi
[ "$sed_symbol_filter_cmd" ] && dst_type=$(echo $base $src_type | sed "$sed_symbol_filter_cmd")
echo "from `module_path $base` import $src_type as $dst_type"
__add_seen $dst_type
done
fi
done
if [ "$import_submodules" = 1 ]; then
for f in $files; do
[ -f $f/__init__.py ] || continue
echo "import `module_path $f` as $f"
__add_seen $f
done
fi
echo
echo "__all__ = ["
for dst_type in ${!seen[@]}; do
echo " \"$dst_type\","
done
echo "]"
echo "# << $del <<"
}
# --------------------- here we go
myname=`basename $0`
eval set -- `getopt -l 'symbol-filter:' -o 'he:m:' "$@"`
while [ "$1" != -- ]; do
case $1 in
-e)
sed_extract_cmd="$2"
shift
;;
--symbol-filter)
sed_symbol_filter_cmd="$2"
shift
;;
-m)
module=$2
shift
;;
-h)
usage 0;;
*)
echo unknown argument $1
usage 1;;
esac
shift
done
shift
cmd=cmd_${1//-/_}
shift
eval $cmd $*