From a307467a95b989d9b8670f231e3bebd2864ba249 Mon Sep 17 00:00:00 2001 From: Adam Talbot Date: Fri, 23 Jun 2023 12:40:26 +0100 Subject: [PATCH] Add 'info' to set of commands Additions: - info added to supported commands - Add example yml file in tests/ Changes: - the stdout is logged to console to see current status. --- tests/info.yml | 1 + tw_pywrap/cli.py | 12 +++++++----- tw_pywrap/helper.py | 14 +++++++++++++- tw_pywrap/tower.py | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) create mode 100644 tests/info.yml diff --git a/tests/info.yml b/tests/info.yml new file mode 100644 index 0000000..4ab1e57 --- /dev/null +++ b/tests/info.yml @@ -0,0 +1 @@ +info: \ No newline at end of file diff --git a/tw_pywrap/cli.py b/tw_pywrap/cli.py index 6fe994b..18a63b5 100644 --- a/tw_pywrap/cli.py +++ b/tw_pywrap/cli.py @@ -52,6 +52,7 @@ def __init__(self, tw, list_for_add_method): def handle_block(self, block, args): # Handles a block of commands by calling the appropriate function. block_handler_map = { + "info": helper.handle_info, "teams": helper.handle_teams, "participants": helper.handle_participants, "compute-envs": helper.handle_compute_envs, @@ -95,11 +96,12 @@ def main(): # Returns a dict that maps block names to lists of command line arguments. cmd_args_dict = helper.parse_all_yaml(options.config, list(data.keys())) - for block, args_list in cmd_args_dict.items(): - for args in args_list: - # Run the methods for each block - block_manager.handle_block(block, args) - time.sleep(3) + # Flatten commands to list of tuples + cmd_sets = [(block, arg) for block, args in cmd_args_dict.items() for arg in args] + for block, args in cmd_sets: + # Run the methods for each block + block_manager.handle_block(block, args) + time.sleep(3) if __name__ == "__main__": diff --git a/tw_pywrap/helper.py b/tw_pywrap/helper.py index 3376fb1..ba0afaa 100644 --- a/tw_pywrap/helper.py +++ b/tw_pywrap/helper.py @@ -3,10 +3,12 @@ Including handling methods for each block in the YAML file, and parsing methods for each block in the YAML file. """ + +import json import yaml + from tw_pywrap import tower from tw_pywrap import utils -import json # TODO: rename these functions to be more descriptive @@ -23,6 +25,11 @@ def parse_yaml_block(file_path, block_name): cmd_args_list = [] # Iterate over each item in the block. + # If no commands supplied just run `tw ` + if block is None: + logging.warn(f"No arguments applied to {block_name}") + block = [{}] # List of empty dict + for item in block: cmd_args_list.append(parse_block(block_name, item)) @@ -200,6 +207,11 @@ def handle_add_block(tw, block, args): method("add", *args) +def handle_info(tw, args): + method = getattr(tw, "info") + method(*args) + + def handle_teams(tw, args): cmd_args, members_cmd_args = args tw.teams("add", *cmd_args) diff --git a/tw_pywrap/tower.py b/tw_pywrap/tower.py index b76860e..92b6456 100644 --- a/tw_pywrap/tower.py +++ b/tw_pywrap/tower.py @@ -58,7 +58,7 @@ def _tw_run(self, cmd, *args, **kwargs): process = subprocess.Popen(full_cmd, stdout=subprocess.PIPE, shell=True) stdout, _ = process.communicate() stdout = stdout.decode("utf-8").strip() - + logging.info(stdout) return stdout # Allow any 'tw' subcommand to be called as a method.