Skip to content

Commit

Permalink
Adding support for local-check-runner utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichaels-harvard committed Jul 16, 2023
1 parent 57720f7 commit a56d731
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 49 deletions.
101 changes: 57 additions & 44 deletions foursight_core/check_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,18 @@ def _get_check_or_action_function(self, check_or_action_string: str, check_or_ac
"@{check_or_action}_function decorator: {module_name}/{function_name}")
return function

@staticmethod
def get_checks_info(search: str = None) -> list:
checks = []
registry = Decorators.get_registry()
for item in registry:
info = CheckHandler._create_check_or_action_info(registry[item])
if search and search not in info.qualified_name.lower():
continue
if info.is_check:
checks.append(info)
return sorted(checks, key=lambda item: item.qualified_name)

@staticmethod
def get_check_info(check_function_name: str, check_module_name: str = None) -> Optional[namedtuple]:
return CheckHandler._get_check_or_action_info(check_function_name, check_module_name, "check")
Expand All @@ -462,48 +474,6 @@ def get_action_info(action_function_name: str, action_module_name: str = None) -
def _get_check_or_action_info(function_name: str,
module_name: str = None, kind: str = None) -> Optional[namedtuple]:

def create_info(info: dict) -> Optional[namedtuple]:

def unqualified_module_name(module_name: str) -> str:
return module_name.rsplit(".", 1)[-1] if "." in module_name else module_name

def qualified_check_or_action_name(check_or_action_name: str, module_name: str) -> str:
unqualified_module = unqualified_module_name(module_name)
return f"{unqualified_module}/{check_or_action_name}" if unqualified_module else check_or_action_name

Info = namedtuple("CheckInfo", ["kind",
"is_check",
"is_action",
"name",
"qualified_name",
"file",
"line",
"module",
"unqualified_module",
"package",
"github_url",
"args",
"kwargs",
"function",
"associated_action",
"associated_check"])
return Info(info["kind"],
info["kind"] == "check",
info["kind"] == "action",
info["name"],
qualified_check_or_action_name(info["name"], info["module"]),
info["file"],
info["line"],
info["module"],
unqualified_module_name(info["module"]),
info["package"],
info["github_url"],
info["args"],
info["kwargs"],
info["function"],
info.get("action"),
info.get("check"))

function_name = function_name.strip();
if module_name:
module_name = module_name.strip();
Expand All @@ -520,9 +490,52 @@ def qualified_check_or_action_name(check_or_action_name: str, module_name: str)
item = registry[name]
if item["name"] == function_name:
if not module_name:
return create_info(item)
return CheckHandler._create_check_or_action_info(item)
if item["module"].endswith("." + module_name):
return create_info(item)
return CheckHandler._create_check_or_action_info(item)

@staticmethod
def _create_check_or_action_info(info: dict) -> Optional[namedtuple]:

def unqualified_module_name(module_name: str) -> str:
return module_name.rsplit(".", 1)[-1] if "." in module_name else module_name

def qualified_check_or_action_name(check_or_action_name: str, module_name: str) -> str:
unqualified_module = unqualified_module_name(module_name)
return f"{unqualified_module}/{check_or_action_name}" if unqualified_module else check_or_action_name

Info = namedtuple("CheckInfo", ["kind",
"is_check",
"is_action",
"name",
"qualified_name",
"file",
"line",
"module",
"unqualified_module",
"package",
"github_url",
"args",
"kwargs",
"function",
"associated_action",
"associated_check"])
return Info(info["kind"],
info["kind"] == "check",
info["kind"] == "action",
info["name"],
qualified_check_or_action_name(info["name"], info["module"]),
info["file"],
info["line"],
info["module"],
unqualified_module_name(info["module"]),
info["package"],
info["github_url"],
info["args"],
info["kwargs"],
info["function"],
info.get("action"),
info.get("check"))

def init_check_or_action_res(self, connection, check):
"""
Expand Down
20 changes: 15 additions & 5 deletions foursight_core/scripts/local_check_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
args_parser = argparse.ArgumentParser('local_check_runner')
args_parser.add_argument("check_or_action", type=str)
args_parser.add_argument("check_or_action", nargs="?", type=str)
args_parser.add_argument("--env", type=str, default="cgap-supertest")
args_parser.add_argument("--stage", type=str, choices=["dev", "prod"],
default="dev",
Expand All @@ -15,13 +15,16 @@ def main():
args_parser.add_argument("--notimeout", action="store_true")
args_parser.add_argument("--primary", action="store_true")
args_parser.add_argument("--action", action="store_true")
args_parser.add_argument("--list", nargs="?")
args_parser.add_argument("--list", nargs="?", const="all")
args_parser.add_argument("--verbose", action="store_true")
args_parser.add_argument("--quiet", action="store_true")
args_parser.add_argument("--debug", action="store_true")
args = args_parser.parse_args()
if args.notimeout:
args.timeout = 0
if not args.list and not args.check_or_action:
print("A check or action name is required unless the --list option is given.")
exit(1)

# es_checks/elasticsearch_s3_count_diff
# ecs_checks/update_ecs_application_versions
Expand All @@ -37,18 +40,25 @@ def run(args):
# This captured_output thing is just to suppress the mass of (stdout
# and stderr) output from running Foursight; we'd prefer not to have
# this come out of this command-line utility.
with captured_output(False) as captured:
with captured_output() as captured:

PRINT = captured.uncaptured_print

import app
from chalicelib_cgap.app_utils import app_utils_obj as app_utils

# Setup.
handler = app_utils.check_handler

if args.list:
checks = handler.get_checks_info(args.list)
for check in checks:
PRINT(check.qualified_name)
return

# Setup.
app.set_stage(args.stage)
connection = app_utils.init_connection(args.env)
app.set_timeout(args.timeout)
connection = app_utils.init_connection(args.env)

# Run the check.
check_info = handler.get_check_info(args.check_or_action)
Expand Down

0 comments on commit a56d731

Please sign in to comment.