Skip to content

Commit

Permalink
shed_tools exit code 1 on error
Browse files Browse the repository at this point in the history
and add --log_level argument
  • Loading branch information
bernt-matthias committed May 16, 2024
1 parent 25d7d94 commit 6bf19fd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 67 deletions.
6 changes: 1 addition & 5 deletions src/ephemeris/_idc_data_managers_to_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@ def main():
disable_external_library_logging()
parser = _parser()
args = parser.parse_args()
log = setup_global_logger(name=__name__, log_file=args.log_file)
if args.verbose:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log = setup_global_logger(name=__name__, log_file=args.log_file, log_level=args.log_level)
write_shed_install_conf(args.data_managers_conf, args.shed_install_output_conf)


Expand Down
6 changes: 1 addition & 5 deletions src/ephemeris/_idc_split_data_manager_genomes.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,11 +314,7 @@ def main():
disable_external_library_logging()
parser = _parser()
args = parser.parse_args()
log = setup_global_logger(name=__name__, log_file=args.log_file)
if args.verbose:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log = setup_global_logger(name=__name__, log_file=args.log_file, log_level=args.log_level)

if args.complete_check_cvmfs:
is_build_complete = CVMFSPublishIsComplete(get_cvmfs_publish_records(args))
Expand Down
14 changes: 11 additions & 3 deletions src/ephemeris/common_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,30 @@ def add_verbosity_argument(parser_or_group):
parser_or_group.add_argument("-v", "--verbose", help="Increase output verbosity.", action="store_true")


def add_log_file_argument(parser_or_group):
def add_log_file_arguments(parser_or_group):
parser_or_group.add_argument(
"--log-file",
"--log_file",
dest="log_file",
help="Where the log file should be stored. " "Default is a file in your system's temp folder",
help="Where the log file should be stored. " "Default is a output to screen",
default=None,
)
parser_or_group.add_argument(
"--log-level",
"--log_level",
dest="log_level",
choices=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
help="Level of debugging",
default='INFO',
)


def get_common_args(login_required=True, log_file=False):
parser = argparse.ArgumentParser(add_help=False)
general_group = parser.add_argument_group("General options")
add_verbosity_argument(general_group)
if log_file:
add_log_file_argument(general_group)
add_log_file_arguments(general_group)

con_group = parser.add_argument_group("Galaxy connection")
default_galaxy = os.environ.get("EPHEMERIS_GALAXY") or "http://localhost:8080"
Expand Down
53 changes: 9 additions & 44 deletions src/ephemeris/ephemeris_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,6 @@
import tempfile


class ProgressConsoleHandler(logging.StreamHandler):
"""
A handler class which allows the cursor to stay on
one line for selected messages
"""

on_same_line = False

def emit(self, record):
try:
msg = self.format(record)
stream = self.stream
same_line = hasattr(record, "same_line")
if self.on_same_line and not same_line:
stream.write("\r\n")
stream.write(msg)
if same_line:
stream.write(".")
self.on_same_line = True
else:
stream.write("\r\n")
self.on_same_line = False
self.flush()
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
self.handleError(record)


def disable_external_library_logging():
# Omit (most of the) logging by external libraries
logging.getLogger("bioblend").setLevel(logging.ERROR)
Expand All @@ -41,23 +12,17 @@ def disable_external_library_logging():
pass


def setup_global_logger(name, log_file=None):
def setup_global_logger(name, log_file=None, log_level='DEBUG'):
formatter = logging.Formatter("%(asctime)s %(levelname)-5s - %(message)s")
progress = ProgressConsoleHandler()
console = logging.StreamHandler()
console.setFormatter(formatter)


logger = logging.getLogger(name)
logger.setLevel(logging.DEBUG)
logger.addHandler(progress)
logger.setLevel(getattr(logging, log_level))

if not log_file:
# delete = false is chosen here because it is always nice to have a log file
# ready if you need to debug. Not having the "if only I had set a log file"
# moment after the fact.
temp = tempfile.NamedTemporaryFile(prefix="ephemeris_", delete=False)
log_file = temp.name
file_handler = logging.FileHandler(log_file)
logger.addHandler(file_handler)
logger.info(f"Storing log file in: {log_file}")
handler = logging.StreamHandler()
else:
handler = logging.FileHandler(log_file)

handler.setFormatter(formatter)
logger.addHandler(handler)
return logger
6 changes: 1 addition & 5 deletions src/ephemeris/run_data_managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,7 @@ def main(argv=None):
disable_external_library_logging()
parser = _parser()
args = parser.parse_args(argv)
log = setup_global_logger(name=__name__, log_file=args.log_file)
if args.verbose:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log = setup_global_logger(name=__name__, log_file=args.log_file, log_level=args.log_level)
gi = get_galaxy_connection(args, file=args.config, log=log, login_required=True)
config = load_yaml_file(args.config)
data_managers = DataManagers(gi, config)
Expand Down
19 changes: 14 additions & 5 deletions src/ephemeris/shed_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import logging
import os
import re
import sys
import time
from collections import namedtuple
from concurrent.futures import (
Expand Down Expand Up @@ -261,7 +262,8 @@ def install_repositories(
[(t["name"], t.get("changeset_revision", "")) for t in errored_repositories],
)
)
log.info("All repositories have been installed.")
if len(errored_repositories) == 0:
log.info("All repositories have been installed.")
log.info(f"Total run time: {dt.datetime.now() - installation_start}")
return InstallResults(
installed_repositories=installed_repositories,
Expand Down Expand Up @@ -378,6 +380,8 @@ def test_tools(
log.info(f"Failed tool tests ({n_failed}): {[t[0] for t in test_exceptions]}")
log.info(f"Total tool test time: {dt.datetime.now() - tool_test_start}")

return n_failed == 0

def _get_interactor(self, test_user, test_user_api_key):
if test_user_api_key is None:
whoami = self.gi.make_get_request(self.gi.url + "/whoami").json()
Expand Down Expand Up @@ -600,7 +604,7 @@ def log_repository_install_error(repository, start, msg, log):
"""
end = dt.datetime.now()
log.error(
"\t* Error installing a repository (after %s seconds)! Name: %s," "owner: %s, " "revision: %s, error: %s",
"\tError installing a repository (after %s seconds)! Name: %s," "owner: %s, " "revision: %s, error: %s",
str(end - start),
repository.get("name", ""),
repository.get("owner", ""),
Expand Down Expand Up @@ -678,7 +682,7 @@ def args_to_repos(args) -> List[InstallRepoDict]:
def main(argv=None):
disable_external_library_logging()
args = parser().parse_args(argv)
log = setup_global_logger(name=__name__, log_file=args.log_file)
log = setup_global_logger(name=__name__, log_file=args.log_file, log_level=args.log_level)
gi = get_galaxy_connection(args, file=args.tool_list_file, log=log, login_required=True)
install_repository_manager = InstallRepositoryManager(gi)

Expand All @@ -702,14 +706,15 @@ def main(argv=None):
# Start installing/updating and store the results in install_results.
# Or do testing if the action is `test`
install_results = None
test_result = True
if args.action == "update":
install_results = install_repository_manager.update_repositories(repositories=repos, log=log, **kwargs)
elif args.action == "install":
install_results = install_repository_manager.install_repositories(
repos, log=log, force_latest_revision=args.force_latest_revision, **kwargs
)
elif args.action == "test":
install_repository_manager.test_tools(
test_result = install_repository_manager.test_tools(
test_json=args.test_json,
repositories=repos,
log=log,
Expand All @@ -729,7 +734,7 @@ def main(argv=None):
if args.test_existing:
to_be_tested_repositories.extend(install_results.skipped_repositories)
if to_be_tested_repositories:
install_repository_manager.test_tools(
test_result = install_repository_manager.test_tools(
test_json=args.test_json,
repositories=to_be_tested_repositories,
log=log,
Expand All @@ -739,6 +744,10 @@ def main(argv=None):
client_test_config_path=args.client_test_config,
)

if install_results:
if len(install_results.errored_repositories) > 0:
sys.exit(1)
sys.exit(test_result)

if __name__ == "__main__":
main()

0 comments on commit 6bf19fd

Please sign in to comment.