jw-pkg/scripts/python-tools.sh
Jan Lindemann 45f8e8cc89
All checks were successful
CI / Packaging - Kali Linux (pull_request) Successful in 4m14s
CI / Packaging - OpenSUSE Tumbleweed (pull_request) Successful in 4m20s
CI / Packaging test (pull_request) Successful in 0s
CI / Packaging - Kali Linux (push) Successful in 3m45s
CI / Packaging - OpenSUSE Tumbleweed (push) Successful in 3m52s
CI / Packaging test (push) Successful in 0s
test + python-tools.sh: Fix toplevel "make all" fallout

After a pipeline change, CI now runs "make all" in a repo's root, which uncovers two problems:

1. The help integration test only succeeded as long CI didn't run "make all" before "make test". That way, the checked out repository lacked the generated __init__.py files needed for some modular subcommands to be fully loaded, and hence, the test should have failed. The entire machinery only worked because the subcommands in question are not not essential to building jw-pkg itself: "secrets" and "posix". So, this commit adapts the help integration test to the new reality.

2. Regarding python-tools.sh: Commit 55060486 satisfies yapf in some places of the source code, but in others not anylonger. So patch python-tools.sh's newline handling again.

While not thematically similar, both fixes get baked into one commit to satisfy the requirement that every single commit needs to pass "make clean all check test" individually.

Signed-off-by: Jan Lindemann <jan@janware.com>
2026-06-25 18:28:36 +02:00

139 lines
2.3 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 -A seen=()
local f dst_type
if [ "$sed_extract_cmd" ]; then
for f in $files; do
test -d $f && continue
local base=${f##*/}
base=${base%.py}
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
echo "from `module_path $base` import $src_type as $dst_type"
__add_seen $dst_type
done
done
fi
local submodule
for submodule in $submodules; do
echo "from . import $submodule as $submodule"
__add_seen $submodule
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
if [ ${#seen[@]} -eq 0 ]; then
echo "__all__ = []"
else
echo
echo "__all__ = ["
for dst_type in ${!seen[@]}; do
echo " \"$dst_type\","
done
echo "]"
fi
echo "# << $del <<"
}
# --------------------- here we go
myname=`basename $0`
submodules=""
eval set -- `getopt -l 'symbol-filter:,extract-filter:,module:,submodules:' -o 'he:m:' "$@"`
while [ "$1" != -- ]; do
case $1 in
-e|--extract-filter)
sed_extract_cmd="$2"
shift
;;
--symbol-filter)
sed_symbol_filter_cmd="$2"
shift
;;
-m|--module)
module=$2
shift
;;
-m|--submodules)
submodules="$submodules $2"
shift
;;
-h|--help)
usage 0
;;
*)
echo "Unknown argument $1"
usage 1
;;
esac
shift
done
shift
cmd=cmd_${1//-/_}
shift
eval $cmd $*