spiff-arena/spiffworkflow-backend/bin/codemod/remove_all_unused_functions

42 lines
1.5 KiB
Plaintext
Raw Normal View History

2023-04-29 02:29:46 +00:00
#!/usr/bin/env bash
function error_handler() {
>&2 echo "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
exit "$2"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errtrace -o errexit -o nounset
# HELP: remove all unused functions from python files
if ! command -v dead >/dev/null 2>&1; then
echo "dead is not installed. installing..."
echo "pip install dead"
fi
list_of_unused_things="$(dead | grep -E '^[a-z].*is never read')"
filename_patterns_to_ignore="(codemod|migrations/versions|conftest.py|noxfile.py)"
2023-04-29 02:29:46 +00:00
while read -r line; do
function_name="$(echo "$line" | awk '{print $1}')"
2023-04-29 02:37:17 +00:00
echo "possible function_name: ${function_name}"
2023-04-29 02:29:46 +00:00
file_name_and_line_number="$(echo "$line" | perl -p -e "s/.*is never read, defined in //g")"
file_name="$(echo "$file_name_and_line_number" | awk -F ':' '{print $1}')"
2023-04-29 02:37:17 +00:00
if ! grep "def $function_name" "$file_name" >/dev/null 2>&1; then
echo "skipping $function_name in ${file_name} because it's not a function"
continue
fi
if grep -E "$filename_patterns_to_ignore" <<< "$file_name" >/dev/null 2>&1; then
echo "skipping ${file_name} because it's in the list of files to ignore (probably because it is a false positive and actually used)"
2023-04-29 02:37:17 +00:00
continue
fi
2023-04-29 02:29:46 +00:00
echo "trying to remove $function_name from: ${file_name}"
2023-04-29 02:37:17 +00:00
2023-04-29 02:29:46 +00:00
if python bin/codemod/update_file_to_remove_function.py "$file_name" "$function_name"; then
# TODO: run exhaustive tests, and if they pass, report success
echo "function ${function_name} removed from ${file_name}. yay!"
exit 0
fi
done <<< "$list_of_unused_things"