From 87c34f482210111d375eb8edb3dc00bee847bf11 Mon Sep 17 00:00:00 2001 From: jbirddog <100367399+jbirddog@users.noreply.github.com> Date: Fri, 28 Apr 2023 10:54:24 -0400 Subject: [PATCH 1/4] Support connectors that return logs (#230) --- .../spiffworkflow_backend/services/service_task_service.py | 7 +++++++ .../integration/test_debug_controller.py | 1 - 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spiffworkflow-backend/src/spiffworkflow_backend/services/service_task_service.py b/spiffworkflow-backend/src/spiffworkflow_backend/services/service_task_service.py index 8500ecb5..2754e8ec 100644 --- a/spiffworkflow-backend/src/spiffworkflow_backend/services/service_task_service.py +++ b/spiffworkflow-backend/src/spiffworkflow_backend/services/service_task_service.py @@ -91,6 +91,13 @@ class ServiceTaskDelegate: json_parse_error = e parsed_response = {} + if "spiff__logs" in parsed_response: + for log in parsed_response["spiff__logs"]: + current_app.logger.info(f"Log from connector {name}: {log}") + if "api_response" in parsed_response: + parsed_response = parsed_response["api_response"] + response_text = json.dumps(parsed_response) + if proxied_response.status_code >= 300: message = ServiceTaskDelegate.get_message_for_status(proxied_response.status_code) error = f"Received an unexpected response from service {name} : {message}" diff --git a/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_debug_controller.py b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_debug_controller.py index 84fcafb3..d9136020 100644 --- a/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_debug_controller.py +++ b/spiffworkflow-backend/tests/spiffworkflow_backend/integration/test_debug_controller.py @@ -4,7 +4,6 @@ from tests.spiffworkflow_backend.helpers.base_test import BaseTest class TestDebugController(BaseTest): - def test_test_raise_error( self, app: Flask, From 64356e8796ab2848163da6bb48dae9f8dbb2a345 Mon Sep 17 00:00:00 2001 From: burnettk Date: Fri, 28 Apr 2023 22:29:46 -0400 Subject: [PATCH 2/4] scripts to find and process dead code --- .../bin/codemod/remove_all_unused_functions | 30 +++++++++++++++++ .../codemod/update_file_to_remove_function.py | 33 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 spiffworkflow-backend/bin/codemod/remove_all_unused_functions create mode 100644 spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py diff --git a/spiffworkflow-backend/bin/codemod/remove_all_unused_functions b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions new file mode 100755 index 00000000..fcc384ae --- /dev/null +++ b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions @@ -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" diff --git a/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py b/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py new file mode 100644 index 00000000..9362fabb --- /dev/null +++ b/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py @@ -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) From 247c26054106eea482fd1ea767e56f117661cb76 Mon Sep 17 00:00:00 2001 From: burnettk Date: Fri, 28 Apr 2023 22:37:17 -0400 Subject: [PATCH 3/4] iterate on function crusher script --- .../bin/codemod/remove_all_unused_functions | 11 ++++++++++- .../bin/codemod/update_file_to_remove_function.py | 2 +- .../bin/import_tickets_for_command_line.py | 11 ----------- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/spiffworkflow-backend/bin/codemod/remove_all_unused_functions b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions index fcc384ae..2d51f00d 100755 --- a/spiffworkflow-backend/bin/codemod/remove_all_unused_functions +++ b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions @@ -18,10 +18,19 @@ 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}" + echo "possible 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}')" + 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 codemod <<< "$file_name" >/dev/null 2>&1; then + echo "skipping ${file_name} because it's a codemod file" + continue + fi 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!" diff --git a/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py b/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py index 9362fabb..82d571fa 100644 --- a/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py +++ b/spiffworkflow-backend/bin/codemod/update_file_to_remove_function.py @@ -13,7 +13,7 @@ def remove_function(filename: str, function_name: str) -> None: bowler_query = (Query(filename) .select_function(function_name) .modify(remove_statement) - .execute(write=True)) + .execute(write=True, silent=True, interactive=False)) if len(bowler_query.exceptions) > 0: print(f"Failed to remove function {function_name} from {filename}.") diff --git a/spiffworkflow-backend/bin/import_tickets_for_command_line.py b/spiffworkflow-backend/bin/import_tickets_for_command_line.py index db7e35be..1392c111 100644 --- a/spiffworkflow-backend/bin/import_tickets_for_command_line.py +++ b/spiffworkflow-backend/bin/import_tickets_for_command_line.py @@ -11,17 +11,6 @@ from spiffworkflow_backend.services.process_instance_processor import ( from spiffworkflow_backend.services.process_instance_service import ( ProcessInstanceService, ) - - -def print_process_instance_count(process_model_identifier_ticket: str) -> None: - """Print process instance count.""" - process_instances = ProcessInstanceModel.query.filter_by( - process_model_identifier=process_model_identifier_ticket - ).all() - process_instance_count = len(process_instances) - print(f"process_instance_count: {process_instance_count}") - - def main(): """Main.""" app = get_hacked_up_app_for_script() From 1ea36658cca4c29473b3ef7c7c9a8ab0b233873a Mon Sep 17 00:00:00 2001 From: burnettk Date: Fri, 28 Apr 2023 22:48:02 -0400 Subject: [PATCH 4/4] start a list of file patterns to ignore --- .../bin/codemod/remove_all_unused_functions | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spiffworkflow-backend/bin/codemod/remove_all_unused_functions b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions index 2d51f00d..b0686eda 100755 --- a/spiffworkflow-backend/bin/codemod/remove_all_unused_functions +++ b/spiffworkflow-backend/bin/codemod/remove_all_unused_functions @@ -16,6 +16,8 @@ fi list_of_unused_things="$(dead | grep -E '^[a-z].*is never read')" +filename_patterns_to_ignore="(codemod|migrations/versions|conftest.py|noxfile.py)" + while read -r line; do function_name="$(echo "$line" | awk '{print $1}')" echo "possible function_name: ${function_name}" @@ -25,8 +27,8 @@ while read -r line; do echo "skipping $function_name in ${file_name} because it's not a function" continue fi - if grep -E codemod <<< "$file_name" >/dev/null 2>&1; then - echo "skipping ${file_name} because it's a codemod file" + 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)" continue fi echo "trying to remove $function_name from: ${file_name}"