mirror of
https://github.com/sartography/spiff-arena.git
synced 2025-02-23 06:38:24 +00:00
scripts to find and process dead code
This commit is contained in:
parent
070f203a13
commit
b88f9e9664
30
spiffworkflow-backend/bin/codemod/remove_all_unused_functions
Executable file
30
spiffworkflow-backend/bin/codemod/remove_all_unused_functions
Executable file
@ -0,0 +1,30 @@
|
|||||||
|
#!/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')"
|
||||||
|
|
||||||
|
while read -r line; do
|
||||||
|
function_name="$(echo "$line" | awk '{print $1}')"
|
||||||
|
echo "function_name: ${function_name}"
|
||||||
|
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}')"
|
||||||
|
echo "trying to remove $function_name from: ${file_name}"
|
||||||
|
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"
|
@ -0,0 +1,33 @@
|
|||||||
|
from bowler import Query
|
||||||
|
from bowler.types import Leaf
|
||||||
|
|
||||||
|
# This came about because vulture (actually dead, from the list of Similar programs at https://pypi.org/project/vulture/)
|
||||||
|
# actually found unused stuff, and I wanted to remove it.
|
||||||
|
# See also https://github.com/craigds/decrapify
|
||||||
|
|
||||||
|
def remove_function(filename: str, function_name: str) -> None:
|
||||||
|
|
||||||
|
def remove_statement(node, capture, filename):
|
||||||
|
node.remove()
|
||||||
|
|
||||||
|
bowler_query = (Query(filename)
|
||||||
|
.select_function(function_name)
|
||||||
|
.modify(remove_statement)
|
||||||
|
.execute(write=True))
|
||||||
|
|
||||||
|
if len(bowler_query.exceptions) > 0:
|
||||||
|
print(f"Failed to remove function {function_name} from {filename}.")
|
||||||
|
raise Exception(bowler_query.exceptions[0])
|
||||||
|
|
||||||
|
print(f"Function {function_name} successfully removed from {filename}.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(description="Remove a function from a Python file while preserving comments.")
|
||||||
|
parser.add_argument("filename", help="the file to modify")
|
||||||
|
parser.add_argument("function_name", help="the name of the function to remove")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
remove_function(args.filename, args.function_name)
|
Loading…
x
Reference in New Issue
Block a user