From fb85e3fea38c66e7fc6b26eae36b87e45024bd19 Mon Sep 17 00:00:00 2001 From: Kai Hermann Date: Tue, 1 Aug 2023 22:59:21 +0200 Subject: [PATCH] Fix edm init --config and prettify logging (#29) * Support --config parameter in edm init again using the main handler * Extend and prettify logging if a git_rev is given instead of a git_tag * Use workspace path if provided in edm init --------- Signed-off-by: Kai-Uwe Hermann --- dependency_manager/src/edm_tool/__init__.py | 2 +- dependency_manager/src/edm_tool/edm.py | 50 ++++++++++++++++----- 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/dependency_manager/src/edm_tool/__init__.py b/dependency_manager/src/edm_tool/__init__.py index bbc9c65..0150cc7 100644 --- a/dependency_manager/src/edm_tool/__init__.py +++ b/dependency_manager/src/edm_tool/__init__.py @@ -4,7 +4,7 @@ # """Everest Dependency Manager.""" from edm_tool import edm -__version__ = "0.5.4" +__version__ = "0.5.5" def get_parser(): diff --git a/dependency_manager/src/edm_tool/edm.py b/dependency_manager/src/edm_tool/edm.py index b20eb64..2915ebd 100755 --- a/dependency_manager/src/edm_tool/edm.py +++ b/dependency_manager/src/edm_tool/edm.py @@ -133,21 +133,32 @@ def prettify(lst: list, indent: int) -> str: return output -def pretty_print(lst: list, indent: int): +def pretty_print(lst: list, indent: int, log_level: int): """Debug log every list element with the given indentation.""" space = " " * indent for out in lst: if out and out != "\n": - log.debug(f"{space}{out}") + if log_level == logging.DEBUG: + log.debug(f"{space}{out}") + elif log_level == logging.INFO: + log.info(f"{space}{out}") + elif log_level == logging.WARNING: + log.warning(f"{space}{out}") + elif log_level == logging.ERROR: + log.error(f"{space}{out}") + elif log_level == logging.CRITICAL: + log.critical(f"{space}{out}") + else: + log.info(f"{space}{out}") -def pretty_print_process(c: subprocess.CompletedProcess, indent: int): +def pretty_print_process(c: subprocess.CompletedProcess, indent: int, log_level: int): """Pretty print stdout and stderr of a CompletedProcess object.""" stdout = c.stdout.decode("utf-8").split("\n") - pretty_print(stdout, indent) + pretty_print(stdout, indent, log_level) stderr = c.stderr.decode("utf-8").split("\n") - pretty_print(stderr, indent) + pretty_print(stderr, indent, log_level) def pattern_matches(string: str, patterns: list) -> bool: @@ -459,9 +470,9 @@ def checkout_rev(cls, checkout_dir: Path, rev: str): try: result = subprocess.run(["git", "-C", checkout_dir, "checkout", rev], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) - pretty_print_process(result, 4) + pretty_print_process(result, 4, logging.DEBUG) except subprocess.CalledProcessError as result: - pretty_print_process(result, 4) + pretty_print_process(result, 4, logging.DEBUG) class EDM: @@ -815,10 +826,11 @@ def clone_dependency_repo(git: str, git_tag: str, checkout_dir: Path) -> None: try: result = subprocess.run(git_clone_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) - pretty_print_process(result, 4) + pretty_print_process(result, 4, logging.DEBUG) except subprocess.CalledProcessError as e: - error_message = f" Error while cloning git repository during local dependency checkout: {str(e.stderr.decode())}" - log.error(error_message) + error_message = " Error while cloning git repository during local dependency checkout:" + log.warning(error_message) + pretty_print(e.stderr.decode().strip().split("\n"), 6, logging.WARNING) raise LocalDependencyCheckoutError(error_message) from e log.info(f"Setting up dependency \"{Color.GREEN}{name}{Color.CLEAR}\" in workspace") @@ -826,6 +838,7 @@ def clone_dependency_repo(git: str, git_tag: str, checkout_dir: Path) -> None: log.debug(f" git-tag: \"{git_tag}\"") log.debug(f" git-rev: \"{git_rev}\"") log.debug(f" local directory: \"{checkout_dir}\"") + git_tag_is_git_rev = False if checkout_dir.exists(): log.debug(f" ... the directory for dependency \"{name}\" already exists at \"{checkout_dir}\".") # check if git is dirty @@ -845,20 +858,25 @@ def clone_dependency_repo(git: str, git_tag: str, checkout_dir: Path) -> None: # maybe the given tag was actually a rev? if not git_rev: # assume git_tag is git_rev - log.info(f"No git_rev given, but git_tag \"{git_tag}\" might be a git_rev, trying to checkout this rev.") + log.info(f" No git_rev given, but git_tag \"{git_tag}\" might be a git_rev, trying to checkout this rev.") git_rev = git_tag git_tag = None clone_dependency_repo(git, git_tag, checkout_dir) + git_tag_is_git_rev = True elif git_rev and git_tag: - log.info(f"Both git_rev and git_tag given, but git_tag \"{git_tag}\" might be a git_rev, trying to checkout git_rev \"{git_rev}\" instead.") + log.info(f" Both git_rev and git_tag given, but git_tag \"{git_tag}\" might be a git_rev," + f" trying to checkout git_rev \"{git_rev}\" instead.") git_tag = None clone_dependency_repo(git, git_tag, checkout_dir) + git_tag_is_git_rev = True else: raise e if git_rev is not None: log.debug(f" Checking out requested git rev \"{git_rev}\"") GitInfo.checkout_rev(checkout_dir, git_rev) + if git_tag_is_git_rev: + log.info(f" Successfully checked out git_rev \"{git_rev}\" of dependency \"{Color.GREEN}{name}{Color.CLEAR}\"") return {"name": name, "path": checkout_dir, "git_tag": git_tag} @@ -936,6 +954,10 @@ def init_handler(args): """Handler for the edm init subcommand""" working_dir = Path(args.working_dir).expanduser().resolve() + if args.workspace: + log.info(f"Using provided workspace path \"{args.workspace}\"") + working_dir = Path(args.workspace) + config_path = working_dir / "workspace-config.yaml" if args.list: @@ -945,6 +967,10 @@ def init_handler(args): if args.release: log.info(f"Checking if requested EVerest release \"{args.release}\" is available...") + elif args.config: + log.info(f"Using supplied config \"{args.config}\"") + main_handler(args) + return else: log.info("No release specified, checking for most recent stable version...")