Skip to content

Commit

Permalink
fix: improve logging and provide informative stdout cmd outputs
Browse files Browse the repository at this point in the history
re #152
  • Loading branch information
ejseqera committed Jul 31, 2024
1 parent 35972db commit e34215e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 18 deletions.
15 changes: 5 additions & 10 deletions seqerakit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def handle_block(self, block, args, destroy=False, dryrun=False):

def main(args=None):
options = parse_args(args if args is not None else sys.argv[1:])
logging.basicConfig(level=options.log_level)
logging.basicConfig(level=getattr(logging, options.log_level.upper()))

# Parse CLI arguments into a list and create a Seqera Platform instance
cli_args_list = options.cli_args.split() if options.cli_args else []
Expand Down Expand Up @@ -186,15 +186,10 @@ def main(args=None):
cmd_args_dict = helper.parse_all_yaml(options.yaml, destroy=options.delete)
for block, args_list in cmd_args_dict.items():
for args in args_list:
try:
# Run the 'tw' methods for each block
block_manager.handle_block(
block, args, destroy=options.delete, dryrun=options.dryrun
)
except (ResourceExistsError, ResourceCreationError) as e:
logging.error(e)
sys.exit(1)
except ValueError as e:
block_manager.handle_block(
block, args, destroy=options.delete, dryrun=options.dryrun
)
except (ResourceExistsError, ResourceCreationError, ValueError) as e:
logging.error(e)
sys.exit(1)

Expand Down
16 changes: 8 additions & 8 deletions seqerakit/seqeraplatform.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
import re
import json

logging.basicConfig(level=logging.DEBUG)


class SeqeraPlatform:
"""
Expand Down Expand Up @@ -99,14 +97,17 @@ def _check_env_vars(self, command):
return " ".join(full_cmd_parts)

# Executes a 'tw' command in a subprocess and returns the output.
def _execute_command(self, full_cmd, to_json=False):
logging.debug(f" Running command: {full_cmd}")
def _execute_command(self, full_cmd, to_json=False, print_stdout=True):
logging.info(f" Running command: {full_cmd}")
process = subprocess.Popen(
full_cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True
)
stdout, _ = process.communicate()
stdout = stdout.decode("utf-8").strip()

if print_stdout:
logging.info(f" Command output: {stdout}")

if "ERROR: " in stdout or process.returncode != 0:
self._handle_command_errors(str(stdout))

Expand All @@ -115,11 +116,9 @@ def _execute_command(self, full_cmd, to_json=False):
def _execute_info_command(self, *args, **kwargs):
# Directly execute 'tw info' command
command = ["info"]
return self._tw_run(command, *args, **kwargs)
return self._tw_run(command, *args, **kwargs, print_stdout=False)

def _handle_command_errors(self, stdout):
logging.error(stdout)

# Check for specific tw cli error patterns and raise custom exceptions
if re.search(
r"ERROR: .*already (exists|a participant)", stdout, flags=re.IGNORECASE
Expand All @@ -134,11 +133,12 @@ def _handle_command_errors(self, stdout):
)

def _tw_run(self, cmd, *args, **kwargs):
print_stout = kwargs.pop("print_stdout", True)
full_cmd = self._construct_command(cmd, *args, **kwargs)
if not full_cmd or self.dryrun:
logging.debug(f"DRYRUN: Running command {full_cmd}")
return
return self._execute_command(full_cmd, kwargs.get("to_json"))
return self._execute_command(full_cmd, kwargs.get("to_json"), print_stout)

# Allow any 'tw' subcommand to be called as a method.
def __getattr__(self, cmd):
Expand Down

0 comments on commit e34215e

Please sign in to comment.