From 5b23a8ea2aa1ae96c13a1a699a1bddb530181530 Mon Sep 17 00:00:00 2001 From: Harsh Vora Date: Mon, 17 Oct 2022 17:00:06 -0700 Subject: [PATCH] Edkrepo: Logger functionality without ui_functions Signed-off-by: Harsh Vora harsh.vora@intel.com --- edkrepo/command_completion_edkrepo.py | 14 +- edkrepo/commands/cache_command.py | 8 +- edkrepo/commands/checkout_pin_command.py | 15 +- edkrepo/commands/clean_command.py | 4 +- edkrepo/commands/clone_command.py | 4 +- edkrepo/commands/combo_command.py | 10 +- edkrepo/commands/create_pin_command.py | 14 +- edkrepo/commands/f2f_cherry_pick_command.py | 68 ++++---- edkrepo/commands/list_pins_command.py | 17 +- edkrepo/commands/list_repos_command.py | 34 ++-- edkrepo/commands/log_command.py | 25 +-- edkrepo/commands/maintenance_command.py | 25 +-- edkrepo/commands/manifest_command.py | 41 ++--- edkrepo/commands/manifest_repos_command.py | 6 +- edkrepo/commands/reset_command.py | 5 +- edkrepo/commands/sparse_command.py | 17 +- edkrepo/commands/status_command.py | 7 +- edkrepo/commands/sync_command.py | 60 +++---- edkrepo/common/common_cache_functions.py | 6 +- edkrepo/common/common_repo_functions.py | 75 ++++----- edkrepo/common/edkrepo_exception.py | 6 + edkrepo/common/edkrepo_version.py | 79 +++++++++ edkrepo/common/humble.py | 14 ++ edkrepo/common/logger.py | 158 ++++++++++++++++++ edkrepo/common/progress_handler.py | 5 +- edkrepo/common/ui_functions.py | 93 ----------- .../manifest_repos_maintenance.py | 16 +- edkrepo/config/config_factory.py | 13 +- edkrepo/edkrepo_cli.py | 62 +++++-- .../edk_manifest_validation.py | 12 +- project_utils/cache.py | 34 ++-- 31 files changed, 589 insertions(+), 358 deletions(-) create mode 100644 edkrepo/common/edkrepo_version.py create mode 100644 edkrepo/common/logger.py delete mode 100644 edkrepo/common/ui_functions.py diff --git a/edkrepo/command_completion_edkrepo.py b/edkrepo/command_completion_edkrepo.py index 8a2c0e6..d236151 100644 --- a/edkrepo/command_completion_edkrepo.py +++ b/edkrepo/command_completion_edkrepo.py @@ -18,16 +18,18 @@ from edkrepo.common.edkrepo_exception import EdkrepoManifestNotFoundException from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import list_available_manifest_repos from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_source_manifest_repo +from edkrepo.common.logger import get_logger from edkrepo.config import config_factory from edkrepo.config.config_factory import get_workspace_manifest +logger = get_logger() def checkout(parsed_args, config): manifest = get_workspace_manifest() - print(' '.join(combinations_in_manifest(manifest))) + logger.info(' '.join(combinations_in_manifest(manifest))) def current_combo(parsed_args, config): manifest = get_workspace_manifest() - print(" [{}]".format(manifest.general_config.current_combo)) + logger.info(" [{}]".format(manifest.general_config.current_combo)) def checkout_pin(parsed_args, config): pins = [] @@ -56,11 +58,11 @@ def checkout_pin(parsed_args, config): pin = ManifestXml(pin_file) parse_output = sys.stdout.getvalue() sys.stdout = stdout - if parsed_args.verbose and parse_output.strip() != '': - print('Pin {} Parsing Errors: {}\n'.format(file, parse_output.strip())) + if parse_output.strip() != '': + logger.info('Pin {} Parsing Errors: {}\n'.format(file, parse_output.strip()), extra={'verbose': parsed_args.verbose}) if pin.project_info.codename == manifest.project_info.codename: pins.append(file) - print(' '.join(pins)) + logger.info(' '.join(pins), extra={'verbose': parsed_args.verbose}) # To add command completions for a new command, add an entry to this dictionary. command_completions = { @@ -91,7 +93,7 @@ def main(): except Exception as e: if parsed_args.verbose: traceback.print_exc() - print("Error: {}".format(str(e))) + logger.error("Error: {}".format(str(e)), extra={'verbose':parsed_args.verbose}) return 1 return 0 diff --git a/edkrepo/commands/cache_command.py b/edkrepo/commands/cache_command.py index 048ea18..b5bd199 100644 --- a/edkrepo/commands/cache_command.py +++ b/edkrepo/commands/cache_command.py @@ -15,6 +15,7 @@ from edkrepo.commands.humble.cache_humble import UNABLE_TO_LOAD_MANIFEST, UNABLE_TO_PARSE_MANIFEST from edkrepo.common.common_cache_functions import add_missing_cache_repos from edkrepo.common.common_cache_functions import get_repo_cache_obj +from edkrepo.common.logger import get_logger from edkrepo.common.edkrepo_exception import EdkrepoCacheException from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_project_in_all_indices from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import pull_all_manifest_repos @@ -57,6 +58,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() # Process enable disable requests if args.disable: config['user_cfg_file'].set_caching_state(False) @@ -65,7 +67,7 @@ def run_command(self, args, config): # Get the current state now that we have processed enable/disable cache_state = config['user_cfg_file'].caching_state - ui_functions.print_info_msg(CACHE_ENABLED.format(cache_state)) + logger.info(CACHE_ENABLED.format(cache_state)) if not cache_state: return @@ -90,14 +92,14 @@ def run_command(self, args, config): # Display all the cache information if args.info: - ui_functions.print_info_msg(CACHE_INFO) + logger.info(CACHE_INFO) info = cache_obj.get_cache_info(args.verbose) for item in info: ui_functions.print_info_msg(CACHE_INFO_LINE.format(item.path, item.remote, item.url)) # Do an update if requested if args.update: - ui_functions.print_info_msg(CACHE_FETCH) + logger.info(CACHE_FETCH) cache_obj.update_cache(verbose=True) # Close the cache repos diff --git a/edkrepo/commands/checkout_pin_command.py b/edkrepo/commands/checkout_pin_command.py index df234c8..166e77d 100644 --- a/edkrepo/commands/checkout_pin_command.py +++ b/edkrepo/commands/checkout_pin_command.py @@ -14,6 +14,7 @@ from edkrepo.commands.edkrepo_command import EdkrepoCommand, OverrideArgument, SourceManifestRepoArgument import edkrepo.commands.arguments.checkout_pin_args as arguments import edkrepo.commands.humble.checkout_pin_humble as humble +from edkrepo.common.logger import get_logger from edkrepo.common.common_cache_functions import get_repo_cache_obj from edkrepo.common.common_repo_functions import sparse_checkout_enabled, reset_sparse_checkout, sparse_checkout from edkrepo.common.common_repo_functions import check_dirty_repos, checkout_repos, combinations_in_manifest @@ -50,6 +51,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() workspace_path = get_workspace_path() manifest = get_workspace_manifest() @@ -74,15 +76,14 @@ def run_command(self, args, config): self.__pin_matches_project(pin, manifest, workspace_path) sparse_enabled = sparse_checkout_enabled(workspace_path, manifest_sources) if sparse_enabled: - ui_functions.print_info_msg(SPARSE_RESET, header = False) + logger.info(SPARSE_RESET) reset_sparse_checkout(workspace_path, manifest_sources) submodule_combo = pin.general_config.current_combo try: deinit_full(workspace_path, manifest, args.verbose) except Exception as e: - ui_functions.print_error_msg(SUBMODULE_DEINIT_FAILED, header = False) - if args.verbose: - ui_functions.print_error_msg(e, header = False) + logger.error(SUBMODULE_DEINIT_FAILED) + logger.error(e, extra={'verbose':args.verbose}) pin_repo_sources = pin.get_repo_sources(pin.general_config.current_combo) try: checkout_repos(args.verbose, args.override, pin_repo_sources, workspace_path, manifest) @@ -94,7 +95,7 @@ def run_command(self, args, config): cache_path = cache_obj.get_cache_path(SUBMODULE_CACHE_REPO_NAME) maintain_submodules(workspace_path, pin, submodule_combo, args.verbose, cache_path) if sparse_enabled: - ui_functions.print_info_msg(SPARSE_CHECKOUT, header = False) + logger.info(SPARSE_CHECKOUT, header = False) sparse_checkout(workspace_path, pin_repo_sources, manifest) def __get_pin_path(self, args, workspace_path, manifest_repo_path, manifest): @@ -115,13 +116,13 @@ def __get_pin_path(self, args, workspace_path, manifest_repo_path, manifest): else: raise EdkrepoInvalidParametersException(humble.NOT_FOUND) - def __pin_matches_project(self, pin, manifest, workspace_path): + def __pin_matches_project(self, logger, pin, manifest, workspace_path): if pin.project_info.codename != manifest.project_info.codename: raise EdkrepoProjectMismatchException(humble.MANIFEST_MISMATCH) elif not set(pin.remotes).issubset(set(manifest.remotes)): raise EdkrepoProjectMismatchException(humble.MANIFEST_MISMATCH) elif pin.general_config.current_combo not in combinations_in_manifest(manifest): - ui_functions.print_warning_msg(humble.COMBO_NOT_FOUND.format(pin.general_config.current_combo), header = False) + logger.warning(humble.COMBO_NOT_FOUND.format(pin.general_config.current_combo)) combo_name = pin.general_config.current_combo pin_sources = pin.get_repo_sources(combo_name) pin_root_remote = {source.root:source.remote_name for source in pin_sources} diff --git a/edkrepo/commands/clean_command.py b/edkrepo/commands/clean_command.py index 4c108c7..9008f91 100644 --- a/edkrepo/commands/clean_command.py +++ b/edkrepo/commands/clean_command.py @@ -16,6 +16,7 @@ import edkrepo.commands.arguments.clean_args as arguments from edkrepo.config.config_factory import get_workspace_path, get_workspace_manifest import edkrepo.common.ui_functions as ui_functions +from edkrepo.common.logger import get_logger class CleanCommand(EdkrepoCommand): def __init__(self): @@ -45,6 +46,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() workspace_path = get_workspace_path() manifest = get_workspace_manifest() manifest_config = manifest.general_config @@ -57,4 +59,4 @@ def run_command(self, args, config): n=(not args.force), q=(args.quiet and args.force)) if result: - ui_functions.print_info_msg(result, header = False) + logger.info(result) diff --git a/edkrepo/commands/clone_command.py b/edkrepo/commands/clone_command.py index 5c99a18..a5483b4 100644 --- a/edkrepo/commands/clone_command.py +++ b/edkrepo/commands/clone_command.py @@ -29,6 +29,7 @@ from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import list_available_manifest_repos from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_source_manifest_repo from edkrepo.common.workspace_maintenance.humble.manifest_repos_maintenance_humble import PROJ_NOT_IN_REPO, SOURCE_MANIFEST_REPO_NOT_FOUND +from edkrepo.common.logger import get_logger import edkrepo.common.ui_functions as ui_functions from edkrepo_manifest_parser.edk_manifest import CiIndexXml, ManifestXml from project_utils.submodule import maintain_submodules @@ -74,6 +75,7 @@ def get_metadata(self): def run_command(self, args, config): + logger = get_logger() pull_all_manifest_repos(config['cfg_file'], config['user_cfg_file'], False) name_or_manifest = args.ProjectNameOrManifestFile @@ -192,5 +194,5 @@ def run_command(self, args, config): # Command line disables sparse checkout use_sparse = False if use_sparse: - ui_functions.print_info_msg(SPARSE_CHECKOUT) + logger.info(SPARSE_CHECKOUT) sparse_checkout(workspace_dir, repo_sources_to_clone, manifest) diff --git a/edkrepo/commands/combo_command.py b/edkrepo/commands/combo_command.py index 538bd7e..d7ad5fe 100644 --- a/edkrepo/commands/combo_command.py +++ b/edkrepo/commands/combo_command.py @@ -13,6 +13,7 @@ import edkrepo.commands.arguments.combo_args as arguments import edkrepo.common.ui_functions as ui_functions from edkrepo.config.config_factory import get_workspace_manifest +from edkrepo.common.logger import get_logger class ComboCommand(EdkrepoCommand): @@ -33,6 +34,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() manifest = get_workspace_manifest() combo_archive = [] combo_list = [c.name for c in manifest.combinations] @@ -43,13 +45,13 @@ def run_command(self, args, config): combo_list.append(manifest.general_config.current_combo) for combo in sorted(combo_list): if combo == manifest.general_config.current_combo: - print("* {}{}{}".format(Fore.GREEN, combo, Fore.RESET)) + logger.info("* {}{}{}".format(Fore.GREEN, combo, Fore.RESET)) elif combo in combo_archive: - print(" {}{}{}{}".format(Fore.YELLOW, Style.BRIGHT, combo, Style.RESET_ALL)) + logger.info(" {}{}{}{}".format(Fore.YELLOW, Style.BRIGHT, combo, Style.RESET_ALL)) else: - ui_functions.print_info_msg(" {}".format(combo), header=False) + logger.info(" {}".format(combo)) if args.verbose: sources = manifest.get_repo_sources(combo) length = len(max([source.root for source in sources], key=len)) for source in sources: - ui_functions.print_info_msg(" {} : {}".format(source.root.ljust(length), source.branch), header=False) + logger.info(" {} : {}".format(source.root.ljust(length), source.branch)) diff --git a/edkrepo/commands/create_pin_command.py b/edkrepo/commands/create_pin_command.py index ff69d01..d9dfc54 100644 --- a/edkrepo/commands/create_pin_command.py +++ b/edkrepo/commands/create_pin_command.py @@ -23,6 +23,7 @@ from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_source_manifest_repo from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import list_available_manifest_repos from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import pull_workspace_manifest_repo +from edkrepo.common.logger import get_logger from edkrepo.config.config_factory import get_workspace_manifest, get_workspace_path from edkrepo_manifest_parser.edk_manifest import ManifestXml import edkrepo.common.ui_functions as ui_functions @@ -54,7 +55,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): - + logger = get_logger() workspace_path = get_workspace_path() manifest = get_workspace_manifest() @@ -73,7 +74,7 @@ def run_command(self, args, config): repo_sources = manifest.get_repo_sources(manifest.general_config.current_combo) # get the repo sources and commit ids for the pin - ui_functions.print_info_msg(GENERATING_PIN_DATA.format(manifest.project_info.codename, manifest.general_config.current_combo), header = False) + logger.info(GENERATING_PIN_DATA.format(manifest.project_info.codename, manifest.general_config.current_combo)) updated_repo_sources = [] for repo_source in repo_sources: local_repo_path = os.path.join(workspace_path, repo_source.root) @@ -81,15 +82,14 @@ def run_command(self, args, config): raise EdkrepoWorkspaceCorruptException(MISSING_REPO.format(repo_source.root)) repo = Repo(local_repo_path) commit_id = repo.head.commit.hexsha - if args.verbose: - ui_functions.print_info_msg(GENERATING_REPO_DATA.format(repo_source.root), header = False) - ui_functions.print_info_msg(BRANCH.format(repo_source.branch), header = False) - ui_functions.print_info_msg(COMMIT.format(commit_id), header = False) + logger.info(GENERATING_REPO_DATA.format(repo_source.root), extra={'verbose':args.verbose}) + logger.info(BRANCH.format(repo_source.branch), extra={'verbose':args.verbose}) + logger.info(COMMIT.format(commit_id), extra={'verbose':args.verbose}) updated_repo_source = repo_source._replace(commit=commit_id) updated_repo_sources.append(updated_repo_source) # create the pin - ui_functions.print_info_msg(WRITING_PIN_FILE.format(pin_file_name), header = False) + logger.info(WRITING_PIN_FILE.format(pin_file_name)) manifest.generate_pin_xml(args.Description, manifest.general_config.current_combo, updated_repo_sources, filename=pin_file_name) diff --git a/edkrepo/commands/f2f_cherry_pick_command.py b/edkrepo/commands/f2f_cherry_pick_command.py index f6b21ac..38413dd 100644 --- a/edkrepo/commands/f2f_cherry_pick_command.py +++ b/edkrepo/commands/f2f_cherry_pick_command.py @@ -21,6 +21,7 @@ from colorama import Fore from edkrepo.common.common_repo_functions import sparse_checkout_enabled, get_full_path +from edkrepo.common.logger import get_logger from edkrepo.commands.edkrepo_command import EdkrepoCommand from edkrepo.common.edkrepo_exception import EdkrepoAbortCherryPickException, EdkrepoInvalidParametersException, EdkrepoWorkspaceInvalidException from edkrepo.common.edkrepo_exception import EdkrepoNotFoundException, EdkrepoGitException @@ -92,8 +93,9 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() if args.list_templates: - _list_templates() + _list_templates(logger) return continue_operation = vars(args)['continue'] commit_ish = vars(args)['commit-ish'] @@ -115,7 +117,7 @@ def run_command(self, args, config): except EdkrepoAbortCherryPickException: return - _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cherry_pick_info) + _complete_cherry_pick(args, logger, continue_operation, repo_info, commit_info, cherry_pick_info) def _start_new_cherry_pick(args, json_path): (cherry_pick_operations, repo_path) = _parse_arguments(args) @@ -127,7 +129,7 @@ def _start_new_cherry_pick(args, json_path): repo_info = RepoInfo(repo_path, json_path, repo) return (repo_info, cherry_pick_operations) -def _prep_new_cherry_pick(args, repo, commit_ish, config, cherry_pick_operations): +def _prep_new_cherry_pick(args, logger, repo, commit_ish, config, cherry_pick_operations): # Check for staged, unstaged, and untracked files # Require everything be totally clean before attempting Folder to Folder voodoo @@ -219,7 +221,7 @@ def _abort_cherry_pick(repo, json_path, original_branch, original_head, f2f_src_ repo.git.branch('-D', f2f_cherry_pick_src) return -def _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cherry_pick_info): +def _complete_cherry_pick(args, logger, continue_operation, repo_info, commit_info, cherry_pick_info): # Unpack namedtuples (repo_path, json_path, repo) = (repo_info.repo_path, repo_info.json_path, repo_info.repo) @@ -254,13 +256,13 @@ def _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cher if not cherry_pick_operations_template: cherry_pick_operations_template = cherry_pick_operations if continue_operation: - print(humble.F2F_CHERRY_PICK_NUM_COMMITS_CONTINUE.format(len(todo_commits))) + logger.info(humble.F2F_CHERRY_PICK_NUM_COMMITS_CONTINUE.format(len(todo_commits))) else: - print(humble.F2F_CHERRY_PICK_NUM_COMMITS_NEW.format(len(todo_commits))) + logger.info(humble.F2F_CHERRY_PICK_NUM_COMMITS_NEW.format(len(todo_commits))) while todo_commits: source_commit = todo_commits[0] - print(humble.F2F_CHERRY_PICK_ESTIMATE_REMAINING_OPERATIONS.format(len(todo_commits), len(todo_commits) * len(cherry_pick_operations_template))) - print(humble.F2F_CHERRY_PICK_CURRENT_COMMIT.format(source_commit)) + logger.info(humble.F2F_CHERRY_PICK_ESTIMATE_REMAINING_OPERATIONS.format(len(todo_commits), len(todo_commits) * len(cherry_pick_operations_template))) + logger.info(humble.F2F_CHERRY_PICK_CURRENT_COMMIT.format(source_commit)) if not continue_operation: # Now that the change delta is known, we can optimize the cherry pick operation cherry_pick_operations = _optimize_f2f_cherry_pick_operations(cherry_pick_operations_template, repo, source_commit) @@ -270,16 +272,16 @@ def _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cher # Inform the user w.r.t. what is about to happen if num_cherry_picks == 1: - print(humble.F2F_CHERRY_PICK_NUM_CHERRY_PICKS_SINGULAR.format(len(cherry_pick_operations))) + logger.warning(humble.F2F_CHERRY_PICK_NUM_CHERRY_PICKS.format(len(cherry_pick_operations))) else: - print(humble.F2F_CHERRY_PICK_NUM_CHERRY_PICKS_PLURAL.format(len(cherry_pick_operations))) + logger.info(humble.F2F_CHERRY_PICK_NUM_CHERRY_PICKS_PLURAL.format(len(cherry_pick_operations))) for index in range(len(cherry_pick_operations)): - print(humble.F2F_CHERRY_PICK_CHERRY_PICK_NUM.format(index + 1)) + logger.info(humble.F2F_CHERRY_PICK_CHERRY_PICK_NUM.format(index + 1)) for folder in cherry_pick_operations[index]: - print("{} -> {} -> {}".format(folder.source, folder.intermediate, folder.destination)) + logger.info("{} -> {} -> {}".format(folder.source, folder.intermediate, folder.destination)) if len(folder.source_excludes) > 0: - print(humble.F2F_CHERRY_PICK_CHERRY_PICK_EXCLUDE_LIST.format(repr(folder.source_excludes))) - print() + logger.info(humble.F2F_CHERRY_PICK_CHERRY_PICK_EXCLUDE_LIST.format(repr(folder.source_excludes))) + logger.info('') if continue_operation: # # Finish up the current cherry pick operation now that merge conflicts are resolved @@ -367,7 +369,7 @@ def _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cher start_commit = str(repo.commit('HEAD~{}'.format(num_cherry_picks))) end_commit = str(repo.commit('HEAD')) commit_message = repo.commit('HEAD').message - print(commit_message) + logger.info(commit_message) f2f_cherry_pick_squash = get_unique_branch_name('f2f-cherry-pick-squash', repo) try: squash_commits(start_commit, end_commit, f2f_cherry_pick_squash, commit_message, repo, False) @@ -378,8 +380,8 @@ def _complete_cherry_pick(args, continue_operation, repo_info, commit_info, cher if f2f_cherry_pick_squash in repo.heads: repo.git.branch('-D', f2f_cherry_pick_squash) # Current source_commit is successful, let user know and move the commit to the completed list - print() - print(humble.F2F_CHERRY_PICK_SUCCESSFUL) + logger.info() + logger.info(humble.F2F_CHERRY_PICK_SUCCESSFUL) todo_commits.remove(source_commit) complete_commits.append(source_commit) finally: @@ -495,7 +497,7 @@ def strip_commit_message(commit, repo, source_commit=None, append_sha=False): del os.environ['GIT_EDITOR'] del os.environ['COMMIT_MESSAGE'] -def _perform_cherry_pick(commit, repo, verbose): +def _perform_cherry_pick(commit, logger, repo, verbose): merge_conflict = False p = Popen(['git', 'cherry-pick', commit], stdin=PIPE, stdout=PIPE, stderr=STDOUT) stdout = p.communicate()[0] @@ -506,12 +508,12 @@ def _perform_cherry_pick(commit, repo, verbose): merge_conflict = True stdout = stdout.replace("hint: and commit the result with 'git commit'",'') sys.stdout.write(stdout) - print() - print(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE1) - print(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE2) - print() - print(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE3) - print(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE4) + logger.info('') + logger.warning(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE1) + logger.warning(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE2) + logger.info('') + logger.warning(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE3) + logger.warning(humble.F2F_CHERRY_PICK_MERGE_CONFLICT_LINE4) else: sys.stdout.write(stdout) raise EdkrepoGitException(humble.F2F_CHERRY_PICK_GIT_FAILURE.format(p.returncode)) @@ -775,7 +777,7 @@ def _restore_f2f_cherry_pick_state(repo_path): data['in_progress_commit']['source_commit'], data['append_sha'], cp_operations_template, data['complete_commits'], data['todo_commits'], data['squash']) -def _init_f2f_cherry_pick_operations(cherry_pick_operations, repo, src_commit, dest_commit, config): +def _init_f2f_cherry_pick_operations(cherry_pick_operations, logger, repo, src_commit, dest_commit, config): repo_path = repo.working_tree_dir # Normalize all the paths used_common_folder_paths = [] @@ -793,10 +795,10 @@ def _init_f2f_cherry_pick_operations(cherry_pick_operations, repo, src_commit, d source = os.path.relpath(source, repo_path).replace(os.sep, '/') destination = os.path.relpath(destination, repo_path).replace(os.sep, '/') if not git_path_exists(source, src_commit, repo): - print(humble.F2F_CHERRY_PICK_PATH_NOT_EXIST.format(source, src_commit)) + logger.warning(humble.F2F_CHERRY_PICK_PATH_NOT_EXIST.format(source, src_commit)) continue if not git_path_exists(destination, dest_commit, repo): - print(humble.F2F_CHERRY_PICK_PATH_NOT_EXIST.format(destination, dest_commit)) + logger.warning(humble.F2F_CHERRY_PICK_PATH_NOT_EXIST.format(destination, dest_commit)) continue source_excludes = [] for exclude in folder.source_excludes: @@ -943,18 +945,18 @@ def _path_in_changed_files(path, changed_files): file_path = os.path.dirname(file_path) return False -def _list_templates(): +def _list_templates(logger): manifest = get_workspace_manifest() f2f_templates = manifest.folder_to_folder_mappings for template in f2f_templates: - print('{}Template {}<-->{}{}'.format(Fore.MAGENTA, template.project1, template.project2, Fore.RESET)) + logger.info('{}Template {}<-->{}{}'.format(Fore.MAGENTA, template.project1, template.project2, Fore.RESET)) for folder in template.folders: - print("{}<-->{}".format(folder.project1_folder, folder.project2_folder)) + logger.info("{}<-->{}".format(folder.project1_folder, folder.project2_folder)) if len(folder.excludes) > 0: - print("Excludes:") + logger.info("Excludes:") for exclude in folder.excludes: - print('\t{}'.format(exclude.path)) - print() + logger.info('\t{}'.format(exclude.path)) + logger.info('') def _parse_arguments(args): cherry_pick_operations = [] diff --git a/edkrepo/commands/list_pins_command.py b/edkrepo/commands/list_pins_command.py index 3f07a3b..bd9b916 100644 --- a/edkrepo/commands/list_pins_command.py +++ b/edkrepo/commands/list_pins_command.py @@ -27,6 +27,7 @@ import edkrepo.commands.arguments.list_pins_args as arguments import edkrepo.commands.humble.list_pins_humble as humble from edkrepo.common.common_repo_functions import find_less +from edkrepo.common.logger import get_logger from edkrepo_manifest_parser.edk_manifest import ManifestXml, CiIndexXml class ListPinsCommand(EdkrepoCommand): @@ -53,6 +54,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() less_path, use_less = find_less() if use_less: output_string = '' @@ -84,12 +86,12 @@ def run_command(self, args, config): manifest_directory = config['user_cfg_file'].manifest_repo_abs_path(manifest_repo) manifest = ManifestXml(manifest_path) if manifest.general_config.pin_path is None: - print(humble.NO_PIN_FOLDER) + logger.info(humble.NO_PIN_FOLDER) return pin_folder = os.path.normpath(os.path.join(manifest_directory, manifest.general_config.pin_path)) if args.verbose: if not use_less: - print(humble.PIN_FOLDER.format(pin_folder)) + logger.info(humble.PIN_FOLDER.format(pin_folder)) else: output_string = (humble.PIN_FOLDER.format(pin_folder)) for dirpath, _, filenames in os.walk(pin_folder): @@ -106,13 +108,12 @@ def run_command(self, args, config): sys.stdout = stdout if pin.project_info.codename == manifest.project_info.codename: if not use_less: - print('Pin File: {}'.format(file)) - if args.verbose and not args.description: - print('Parsing Errors: {}\n'.format(parse_output.strip())) - elif args.verbose and args.description: - print('Parsing Errors: {}'.format(parse_output.strip())) + logger.info('Pin File: {}'.format(file)) + if not args.description: + logger.info('Parsing Errors: {}\n'.format(parse_output.strip()), extra={'verbose':args.verbose}) if args.description: - print('Description: {}\n'.format(pin.project_info.description)) + logger.info('Parsing Errors: {}'.format(parse_output.strip()), extra={'verbose':args.verbose}) + logger.info('Description: {}\n'.format(pin.project_info.description)) elif use_less: output_string = separator.join((output_string, 'Pin File: {}'.format(file))) if args.verbose and not args.description: diff --git a/edkrepo/commands/list_repos_command.py b/edkrepo/commands/list_repos_command.py index 1451af4..56271f0 100644 --- a/edkrepo/commands/list_repos_command.py +++ b/edkrepo/commands/list_repos_command.py @@ -20,6 +20,7 @@ from edkrepo.common.edkrepo_exception import EdkrepoInvalidParametersException, EdkrepoManifestInvalidException from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import pull_all_manifest_repos from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import list_available_manifest_repos +from edkrepo.common.logger import get_logger from edkrepo.config.tool_config import CI_INDEX_FILE_NAME from edkrepo_manifest_parser.edk_manifest import CiIndexXml, ManifestXml @@ -48,9 +49,10 @@ def get_metadata(self): return metadata def run_command(self, args, config): - print() + logger = get_logger() + logger.info("") pull_all_manifest_repos(config['cfg_file'], config['user_cfg_file']) - print() + logger.info("") cfg_manifest_repos, user_config_manifest_repos, conflicts = list_available_manifest_repos(config['cfg_file'], config['user_cfg_file']) @@ -63,13 +65,12 @@ def run_command(self, args, config): for manifest_repo in cfg_manifest_repos: # Get path to global manifest file global_manifest_directory = config['cfg_file'].manifest_repo_abs_path(manifest_repo) - if args.verbose: - print(humble.MANIFEST_DIRECTORY) - print(global_manifest_directory) - print() + logger.info(humble.MANIFEST_DIRECTORY, extra={'verbose':args.verbose}) + logger.info(global_manifest_directory, extra={'verbose':args.verbose}) + logger.info("", extra={'verbose':args.verbose}) #Create a dictionary containing all the manifests listed in the CiIndex.xml file index_path = os.path.join(global_manifest_directory, CI_INDEX_FILE_NAME) - print(index_path) + logger.info(index_path) ci_index_xml = CiIndexXml(index_path) config_manifest_repos_project_list = ci_index_xml.project_list if args.archived: @@ -88,10 +89,9 @@ def run_command(self, args, config): for manifest_repo in user_config_manifest_repos: # Get path to global manifest file global_manifest_directory = config['user_cfg_file'].manifest_repo_abs_path(manifest_repo) - if args.verbose: - print(humble.MANIFEST_DIRECTORY) - print(global_manifest_directory) - print() + logger.info(humble.MANIFEST_DIRECTORY, extra={'verbose':args.verbose}) + logger.info(global_manifest_directory, extra={'verbose':args.verbose}) + logger.info("", extra={'verbose':args.verbose}) #Create a dictionary containing all the manifests listed in the CiIndex.xml file index_path = os.path.join(global_manifest_directory, CI_INDEX_FILE_NAME) ci_index_xml = CiIndexXml(index_path) @@ -130,7 +130,7 @@ def run_command(self, args, config): #Determine the names of the repositories self.generate_repo_names(repo_urls, manifests, args.archived) - print(humble.REPOSITORIES) + logger.info(humble.REPOSITORIES) #If the user provided a list of repositories to view, check to make sure #at least one repository will be shown, if not provide an error @@ -142,8 +142,8 @@ def run_command(self, args, config): if args.repos and repo_name not in args.repos: continue repo = self.repo_names[repo_name][0] - print(humble.REPO_NAME_AND_URL.format(repo_name, repo)) - print(humble.BRANCHES) + logger.info(humble.REPO_NAME_AND_URL.format(repo_name, repo)) + logger.info(humble.BRANCHES) #Determine the list of branches that used by any branch combination in any manifest branches = set() @@ -167,7 +167,7 @@ def run_command(self, args, config): #For each interesting branch in the current git repository... for branch in branches: - print(humble.BRANCH_FORMAT_STRING.format(branch)) + logger.info(humble.BRANCH_FORMAT_STRING.format(branch)) #Determine the branch combinations that use that branch for project_name in manifests: @@ -201,9 +201,9 @@ def run_command(self, args, config): #Print the branch combination name, if this is the default branch combination, #then print it in green color with *'s around it if default_combo == combo: - print(humble.DEFAULT_COMBO_FORMAT_STRING.format(project_name_print, combo)) + logger.info(humble.DEFAULT_COMBO_FORMAT_STRING.format(project_name_print, combo)) else: - print(humble.COMBO_FORMAT_STRING.format(project_name_print, combo)) + logger.info(humble.COMBO_FORMAT_STRING.format(project_name_print, combo)) def get_repo_url(self, repo_url): if repo_url[-4:].lower() == '.git': diff --git a/edkrepo/commands/log_command.py b/edkrepo/commands/log_command.py index aee7d60..55ce167 100644 --- a/edkrepo/commands/log_command.py +++ b/edkrepo/commands/log_command.py @@ -18,7 +18,7 @@ from edkrepo.commands.edkrepo_command import EdkrepoCommand import edkrepo.commands.arguments.log_args as arguments from edkrepo.common.common_repo_functions import sort_commits, find_less -import edkrepo.common.ui_functions as ui_functions +from edkrepo.common.logger import get_logger from edkrepo.config.config_factory import get_workspace_path, get_workspace_manifest class LogCommand(EdkrepoCommand): @@ -45,11 +45,12 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() if args.number: try: args.number = int(args.number) except ValueError: - print("Error: \'{}\' is not an integer".format(args.number)) + logger.error("Error: \'{}\' is not an integer".format(args.number)) return workspace_path = get_workspace_path() @@ -70,12 +71,12 @@ def run_command(self, args, config): Fore.CYAN, os.path.basename(commit.repo.working_dir), Fore.RESET, - ui_functions.safe_str(commit.summary)) + logger.safe_str(commit.summary)) if use_less: output_string = separator.join((output_string, oneline)) else: - print(oneline) + logger.info(oneline) else: time_string = datetime.utcfromtimestamp(commit.authored_date - commit.author_tz_offset).strftime("%c") time_zone_string = "{}{:04.0f}".format("-" if commit.author_tz_offset > 0 else "+", @@ -88,21 +89,21 @@ def run_command(self, args, config): author_string = "Author: {} <{}>".format(commit.author.name, commit.author.email) date_string = "Date: {} {}".format(time_string, time_zone_string) if use_less: - output_string = separator.join((output_string, hexsha_string, ui_functions.safe_str(author_string), date_string)) + output_string = separator.join((output_string, hexsha_string, logger.safe_str(author_string), date_string)) commit_string = "" for line in commit.message.splitlines(): - commit_string = separator.join((commit_string, ui_functions.safe_str(" {}".format(line)))) + commit_string = separator.join((commit_string, logger.safe_str(" {}".format(line)))) output_string = separator.join((output_string, commit_string, separator)) else: - print(hexsha_string) - ui_functions.print_safe(author_string) - print(date_string) - print("") + logger.info(hexsha_string) + logger.print_safe(author_string) + logger.info(date_string) + logger.info("") for line in commit.message.splitlines(): - ui_functions.print_safe(" {}".format(line)) - print("") + logger.print_safe(" {}".format(line)) + logger.info("") if less_path: less_output = subprocess.Popen([str(less_path), '-F', '-R', '-S', '-X', '-K'], stdin=subprocess.PIPE, stdout=sys.stdout, universal_newlines=True) less_output.communicate(input=output_string) diff --git a/edkrepo/commands/maintenance_command.py b/edkrepo/commands/maintenance_command.py index 5534b3f..203d9d5 100644 --- a/edkrepo/commands/maintenance_command.py +++ b/edkrepo/commands/maintenance_command.py @@ -17,6 +17,7 @@ from edkrepo.commands.arguments import maintenance_args as arguments from edkrepo.commands.humble import maintenance_humble as humble from edkrepo.common.workspace_maintenance.git_config_maintenance import clean_git_globalconfig, set_long_path_support +from edkrepo.common.logger import get_logger from edkrepo.common.edkrepo_exception import EdkrepoWorkspaceInvalidException from edkrepo.config.config_factory import get_workspace_path, get_workspace_manifest from edkrepo_manifest_parser.edk_manifest import ManifestXml @@ -37,15 +38,15 @@ def get_metadata(self): return metadata def run_command(self, args, config): - + logger = get_logger() # Configure git long path support - ui_functions.print_info_msg(humble.LONGPATH_CONFIG, header = False) + logger.info(humble.LONGPATH_CONFIG) set_long_path_support() - print() + logger.info("") # Remove unneeded instead of entries from git global config - ui_functions.print_info_msg(humble.CLEAN_INSTEAD_OFS, header = False) - print() + logger.info(humble.CLEAN_INSTEAD_OFS) + logger.info("") # If in a valid workspace run the following for each repo: # git reflog --expire, git gc, git remote prune origin @@ -53,8 +54,8 @@ def run_command(self, args, config): workspace_path = get_workspace_path() except EdkrepoWorkspaceInvalidException: workspace_path = None - ui_functions.print_error_msg(humble.NO_WOKKSPACE, header = False) - print() + logger.error(humble.NO_WOKKSPACE) + logger.info("") if workspace_path: manifest = get_workspace_manifest() @@ -62,11 +63,11 @@ def run_command(self, args, config): for repo_to_maintain in repos_to_maintain: local_repo_path = os.path.join(workspace_path, repo_to_maintain.root) repo = Repo(local_repo_path) - ui_functions.print_info_msg(humble.REPO_MAINTENANCE.format(repo_to_maintain.root), header = False) - ui_functions.print_info_msg(humble.REFLOG_EXPIRE, header = False) + logger.info(humble.REPO_MAINTENANCE.format(repo_to_maintain.root)) + logger.info(humble.REFLOG_EXPIRE) repo.git.reflog('expire', '--expire=now', '--all') - ui_functions.print_info_msg(humble.GC_AGGRESSIVE, header = False) + logger.info(humble.GC_AGGRESSIVE) repo.git.gc('--aggressive', '--prune=now') - ui_functions.print_info_msg(humble.REMOTE_PRUNE, header = False) + logger.info(humble.REMOTE_PRUNE) repo.git.remote('prune', 'origin') - print() + logger.info("") diff --git a/edkrepo/commands/manifest_command.py b/edkrepo/commands/manifest_command.py index 9b2fbf6..9b3f2c9 100644 --- a/edkrepo/commands/manifest_command.py +++ b/edkrepo/commands/manifest_command.py @@ -20,6 +20,7 @@ from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import pull_all_manifest_repos from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_source_manifest_repo from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import find_project_in_single_index +from edkrepo.common.logger import get_logger from edkrepo.config.config_factory import get_workspace_manifest from edkrepo_manifest_parser.edk_manifest import CiIndexXml, ManifestXml import edkrepo.common.ui_functions as ui_functions @@ -43,7 +44,8 @@ def get_metadata(self): return metadata def run_command(self, args, config): - print() + logger = get_logger() + logger.info("") cfg_file = config['cfg_file'] user_cfg = config['user_cfg_file'] cfg_man_repos, user_cfg_man_repos, conflicts = list_available_manifest_repos(cfg_file, user_cfg) @@ -73,13 +75,12 @@ def run_command(self, args, config): for repo in man_repos.keys(): - print() - ui_functions.print_info_msg("Manifest directory:", header = False) - ui_functions.print_info_msg(repo, header = False) - if args.verbose: - ui_functions.print_info_msg('Manifest directory path:', header = False) - ui_functions.print_info_msg(man_repos[repo][0], header = False) - print() + logger.info("") + logger.info("Manifest directory:") + logger.info(repo) + logger.info('Manifest directory path:', extra={'verbose': args.verbose}) + logger.info(man_repos[repo][0], extra={'verbose': args.verbose}) + logger.info("") ci_index_xml = CiIndexXml(man_repos[repo][1]) @@ -87,28 +88,28 @@ def run_command(self, args, config): try: validate_manifest_repo(man_repos[repo][0], args.verbose, args.archived) except: - print() + logger.info("") - ui_functions.print_info_msg("Projects:", header = False) + logger.info("Projects:") for project in sorted(ci_index_xml.project_list): if (project == current_project and src_man_repo == repo) or (not src_man_repo and project == current_project): - ui_functions.print_info_msg(project, header = False) + logger.info(project) else: - ui_functions.print_warning_msg(project, header = False) + logger.warning(project) if args.verbose: - ui_functions.print_info_msg(" -> {}".format(ci_index_xml.get_project_xml(project)), header = False) + logger.info(" -> {}".format(ci_index_xml.get_project_xml(project))) proj_manifest = ManifestXml(find_project_in_single_index(project, ci_index_xml, man_repos[repo][0])[1]) - ui_functions.print_info_msg(" -> DevLead: {}".format(' '.join(x for x in proj_manifest.project_info.dev_leads)), header = False) + logger.info(" -> DevLead: {}".format(' '.join(x for x in proj_manifest.project_info.dev_leads))) if args.archived: - print() - ui_functions.print_info_msg("Archived Projects:", header = False) + logger.info("") + logger.info("Archived Projects:") for project in sorted(ci_index_xml.archived_project_list): if project == current_project: - ui_functions.print_info_msg(project, header = False) + logger.info(project) else: - ui_functions.print_warning_msg(project, header = False) + logger.warning(project) if args.verbose: - ui_functions.print_info_msg(" -> {}".format(ci_index_xml.get_project_xml(project)), header = False) + logger.info(" -> {}".format(ci_index_xml.get_project_xml(project))) proj_manifest = ManifestXml(find_project_in_single_index(project, ci_index_xml, man_repos[repo][0])[1]) - ui_functions.print_info_msg(" -> DevLead: {}".format(' '.join(x for x in proj_manifest.project_info.dev_leads)), header = False) + logger.info(" -> DevLead: {}".format(' '.join(x for x in proj_manifest.project_info.dev_leads))) diff --git a/edkrepo/commands/manifest_repos_command.py b/edkrepo/commands/manifest_repos_command.py index 8af254c..da8474b 100644 --- a/edkrepo/commands/manifest_repos_command.py +++ b/edkrepo/commands/manifest_repos_command.py @@ -14,6 +14,7 @@ import edkrepo.commands.humble.manifest_repos_humble as humble from edkrepo.common.edkrepo_exception import EdkrepoInvalidParametersException from edkrepo.common.workspace_maintenance.manifest_repos_maintenance import list_available_manifest_repos +from edkrepo.common.logger import get_logger import edkrepo.common.ui_functions as ui_functions @@ -72,13 +73,14 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() cfg_repos, user_cfg_repos, conflicts = list_available_manifest_repos(config['cfg_file'], config['user_cfg_file']) if args.action == 'list': for repo in cfg_repos: - ui_functions.print_info_msg(humble.CFG_LIST_ENTRY.format(repo), header = False) + logger.info(humble.CFG_LIST_ENTRY.format(repo)) for repo in user_cfg_repos: - ui_functions.print_info_msg(humble.USER_CFG_LIST_ENTRY.format(repo), header = False) + logger.info(humble.USER_CFG_LIST_ENTRY.format(repo)) elif (args.action == ('add' or 'remove')) and not args.name: diff --git a/edkrepo/commands/reset_command.py b/edkrepo/commands/reset_command.py index fe7f5c0..bc38c5f 100644 --- a/edkrepo/commands/reset_command.py +++ b/edkrepo/commands/reset_command.py @@ -25,6 +25,7 @@ from edkrepo.commands.edkrepo_command import EdkrepoCommand import edkrepo.commands.arguments.reset_args as arguments from edkrepo.config.config_factory import get_workspace_path, get_workspace_manifest +from edkrepo.common.logger import get_logger import edkrepo.common.ui_functions as ui_functions class ResetCommand(EdkrepoCommand): def __init__(self): @@ -43,6 +44,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() workspace_path = get_workspace_path() manifest = get_workspace_manifest() manifest_config = manifest.general_config @@ -50,6 +52,5 @@ def run_command(self, args, config): for repo_to_reset in repo_sources_to_reset: local_repo_path = os.path.join(workspace_path, repo_to_reset.root) repo = Repo(local_repo_path) - if args.verbose: - ui_functions.print_info_msg("{}Resetting {}".format("Hard " if args.hard else "", repo_to_reset.root)) + logger.info("{}Resetting {}".format("Hard " if args.hard else "", repo_to_reset.root), extra={'verbose': args.verbose}) repo.head.reset(working_tree=args.hard) diff --git a/edkrepo/commands/sparse_command.py b/edkrepo/commands/sparse_command.py index 3adaa42..6dac15d 100644 --- a/edkrepo/commands/sparse_command.py +++ b/edkrepo/commands/sparse_command.py @@ -16,7 +16,7 @@ from edkrepo.common.humble import SPARSE_ENABLE_DISABLE, SPARSE_NO_CHANGE, SPARSE_ENABLE, SPARSE_DISABLE from edkrepo.common.humble import SPARSE_STATUS, SPARSE_CHECKOUT_STATUS from edkrepo.common.humble import SPARSE_BY_DEFAULT_STATUS, SPARSE_ENABLED_REPOS -import edkrepo.common.ui_functions as ui_functions +from edkrepo.common.logger import get_logger class SparseCommand(EdkrepoCommand): @@ -41,6 +41,7 @@ def get_metadata(self): def run_command(self, args, config): # Collect workspace/repo data + logger = get_logger() workspace_path = get_workspace_path() manifest = get_workspace_manifest() current_combo = manifest.general_config.current_combo @@ -59,17 +60,17 @@ def run_command(self, args, config): check_dirty_repos(manifest, workspace_path) if args.enable and not sparse_enabled: - ui_functions.print_info_msg(SPARSE_ENABLE, header = False) + logger.info(SPARSE_ENABLE) sparse_checkout(workspace_path, repo_list, manifest) elif args.disable and sparse_enabled: - ui_functions.print_info_msg(SPARSE_DISABLE, header = False) + logger.info(SPARSE_DISABLE) reset_sparse_checkout(workspace_path, repo_list, True) else: # Display the current status of the project - ui_functions.print_info_msg(SPARSE_STATUS, header = False) - ui_functions.print_info_msg(SPARSE_CHECKOUT_STATUS.format(sparse_enabled), header = False) + logger.info(SPARSE_STATUS) + logger.info(SPARSE_CHECKOUT_STATUS.format(sparse_enabled)) if sparse_settings is not None: - ui_functions.print_info_msg(SPARSE_BY_DEFAULT_STATUS.format(sparse_settings.sparse_by_default), header = False) - ui_functions.print_info_msg(SPARSE_ENABLED_REPOS.format(current_combo), header = False) + logger.info(SPARSE_BY_DEFAULT_STATUS.format(sparse_settings.sparse_by_default)) + logger.info(SPARSE_ENABLED_REPOS.format(current_combo)) for repo in [x for x in repo_list if x.sparse]: - ui_functions.print_info_msg('- {}: {}'.format(repo.root, repo.remote_url), header = False) \ No newline at end of file + logger.info('- {}: {}'.format(repo.root, repo.remote_url)) \ No newline at end of file diff --git a/edkrepo/commands/status_command.py b/edkrepo/commands/status_command.py index 6a04bec..4325abf 100644 --- a/edkrepo/commands/status_command.py +++ b/edkrepo/commands/status_command.py @@ -15,7 +15,7 @@ import edkrepo.commands.arguments.status_args as arguments import edkrepo.commands.humble.status_humble as humble from edkrepo.config.config_factory import get_workspace_path, get_workspace_manifest -import edkrepo.common.ui_functions as ui_functions +from edkrepo.common.logger import get_logger class StatusCommand(EdkrepoCommand): @@ -30,12 +30,13 @@ def get_metadata(self): metadata['arguments'] = args return metadata def run_command(self, args, config): + logger = get_logger() workspace_path = get_workspace_path() initial_manifest = get_workspace_manifest() current_combo = initial_manifest.general_config.current_combo current_sources = initial_manifest.get_repo_sources(current_combo) - ui_functions.print_info_msg(humble.STATUS_CURRENT_COMBO.format(current_combo), header = False) + logger.info(humble.STATUS_CURRENT_COMBO.format(current_combo)) for current_repo in current_sources: local_repo_path = os.path.join(workspace_path, current_repo.root) repo = Repo(local_repo_path) - ui_functions.print_info_msg("{}: {}\n".format(current_repo.root, repo.git.status()), header = False) \ No newline at end of file + logger.info("{}: {}\n".format(current_repo.root, repo.git.status())) \ No newline at end of file diff --git a/edkrepo/commands/sync_command.py b/edkrepo/commands/sync_command.py index fe0a763..6d997ec 100644 --- a/edkrepo/commands/sync_command.py +++ b/edkrepo/commands/sync_command.py @@ -35,6 +35,7 @@ from edkrepo.common.humble import SYNC_REBASE_CALC_FAIL, SYNC_MOVE_FAILED from edkrepo.common.workspace_maintenance.humble.manifest_repos_maintenance_humble import SOURCE_MANIFEST_REPO_NOT_FOUND from edkrepo.common.pathfix import get_actual_path, expanduser +from edkrepo.common.logger import get_logger from edkrepo.common.common_cache_functions import get_repo_cache_obj from edkrepo.common.common_repo_functions import clone_repos, sparse_checkout_enabled from edkrepo.common.common_repo_functions import reset_sparse_checkout, sparse_checkout, verify_single_manifest @@ -55,7 +56,6 @@ from edkrepo.config.tool_config import SUBMODULE_CACHE_REPO_NAME from edkrepo_manifest_parser.edk_manifest import CiIndexXml, ManifestXml from project_utils.submodule import deinit_submodules, maintain_submodules -import edkrepo.common.ui_functions as ui_functions class SyncCommand(EdkrepoCommand): @@ -87,6 +87,7 @@ def get_metadata(self): return metadata def run_command(self, args, config): + logger = get_logger() workspace_path = get_workspace_path() initial_manifest = get_workspace_manifest() current_combo = initial_manifest.general_config.current_combo @@ -113,7 +114,7 @@ def run_command(self, args, config): update_editor_config(config, global_manifest_directory) if not args.update_local_manifest: - self.__check_for_new_manifest(args, config, initial_manifest, workspace_path, global_manifest_directory) + self.__check_for_new_manifest(args, logger, config, initial_manifest, workspace_path, global_manifest_directory) check_dirty_repos(initial_manifest, workspace_path) # Determine if sparse checkout needs to be disabled for this operation @@ -125,12 +126,12 @@ def run_command(self, args, config): elif args.update_local_manifest: sparse_reset_required = True if sparse_enabled and sparse_reset_required: - ui_functions.print_info_msg(SPARSE_RESET, header = False) + logger.info(SPARSE_RESET) reset_sparse_checkout(workspace_path, initial_sources) # Get the latest manifest if requested if args.update_local_manifest: # NOTE: hyphens in arg name replaced with underscores due to argparse - self.__update_local_manifest(args, config, initial_manifest, workspace_path, global_manifest_directory) + self.__update_local_manifest(args, logger, config, initial_manifest, workspace_path, global_manifest_directory) manifest = get_workspace_manifest() if args.update_local_manifest: try: @@ -179,9 +180,9 @@ def run_command(self, args, config): #The new branch may not exist in the heads list yet if it is a new branch repo.git.checkout(repo_to_sync.branch) if not args.fetch: - ui_functions.print_info_msg(SYNCING.format(repo_to_sync.root, repo.active_branch), header = False) + logger.info(SYNCING.format(repo_to_sync.root, repo.active_branch)) else: - ui_functions.print_info_msg(FETCHING.format(repo_to_sync.root, repo.active_branch), header = False) + logger.info(FETCHING.format(repo_to_sync.root, repo.active_branch)) try: repo.remotes.origin.fetch() except GitCommandError as e: @@ -201,7 +202,7 @@ def run_command(self, args, config): if has_primary_repo_remote(repo, args.verbose): fetch_from_primary_repo(repo, repo_to_sync, args.verbose) if not args.override and not repo.is_ancestor(ancestor_rev='HEAD', rev='origin/{}'.format(repo_to_sync.branch)): - ui_functions.print_info_msg(SYNC_COMMITS_ON_TARGET.format(repo_to_sync.branch, repo_to_sync.root), header = False) + logger.info(SYNC_COMMITS_ON_TARGET.format(repo_to_sync.branch, repo_to_sync.root)) local_commits = True sync_error = True if not args.fetch and (not local_commits or args.override): @@ -209,7 +210,7 @@ def run_command(self, args, config): # Check to see if mirror is up to date if not in_sync_with_primary(repo, repo_to_sync, args.verbose): - ui_functions.print_info_msg(MIRROR_BEHIND_PRIMARY_REPO, header = False) + logger.info(MIRROR_BEHIND_PRIMARY_REPO) # Switch back to the initially active branch before exiting repo.heads[initial_active_branch.name].checkout() @@ -221,23 +222,22 @@ def run_command(self, args, config): branch_origin = next(itertools.islice(repo.iter_commits(), commit_count, commit_count + 1)) behind_count = int(repo.git.rev_list('--count', '{}..{}'.format(branch_origin.hexsha, latest_sha))) if behind_count: - ui_functions.print_info_msg(SYNC_NEEDS_REBASE.format( + logger.info(SYNC_NEEDS_REBASE.format( behind_count=behind_count, target_remote='origin', target_branch=repo_to_sync.branch, local_branch=initial_active_branch.name, - repo_folder=repo_to_sync.root), header = False) + repo_folder=repo_to_sync.root)) except: - ui_functions.print_error_msg(SYNC_REBASE_CALC_FAIL, header = False) - elif args.verbose: - ui_functions.print_warning_msg(NO_SYNC_DETACHED_HEAD.format(repo_to_sync.root), header = False) + logger.error(SYNC_REBASE_CALC_FAIL) + logger.warning(NO_SYNC_DETACHED_HEAD.format(repo_to_sync.root), extra={'verbose':args.verbose}) # Update commit message templates if global_manifest_directory is not None: update_repo_commit_template(workspace_path, repo, repo_to_sync, config, global_manifest_directory) if sync_error: - ui_functions.print_error_msg(SYNC_ERROR, header = False) + logger.error(SYNC_ERROR) # Initialize submodules if not args.skip_submodule: @@ -249,10 +249,10 @@ def run_command(self, args, config): # Restore sparse checkout state if sparse_enabled: - ui_functions.print_info_msg(SPARSE_CHECKOUT, header = False) + logger.info(SPARSE_CHECKOUT) sparse_checkout(workspace_path, repo_sources_to_sync, manifest) - def __update_local_manifest(self, args, config, initial_manifest, workspace_path, global_manifest_directory): + def __update_local_manifest(self, args, logger, config, initial_manifest, workspace_path, global_manifest_directory): #if the manifest repository for the current manifest was not found then there is no project with the manifest #specified project name in the index file for any of the manifest repositories if global_manifest_directory is None: @@ -377,21 +377,21 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path for source in sources_to_move: old_dir = os.path.join(workspace_path, source.root) new_dir = generate_name_for_obsolete_backup(old_dir) - ui_functions.print_warning_msg(SYNC_SOURCE_MOVE_WARNING.format(source.root, new_dir), header = False) + logger.warning(SYNC_SOURCE_MOVE_WARNING.format(source.root, new_dir)) new_dir = os.path.join(workspace_path, new_dir) try: shutil.move(old_dir, new_dir) except: - ui_functions.print_error_msg(SYNC_MOVE_FAILED.format(initial_dir=source.root, new_dir=new_dir), header = False) + logger.error(SYNC_MOVE_FAILED.format(initial_dir=source.root, new_dir=new_dir)) raise # Tell the user about any Git repositories that are no longer used. if len(sources_to_remove) > 0: - ui_functions.print_warning_msg(SYNC_REMOVE_WARNING, header = False) + logger.warning(SYNC_REMOVE_WARNING) for source in sources_to_remove: path_to_source = os.path.join(workspace_path, source.root) - ui_functions.print_warning_msg(path_to_source, header = False) + logger.warning(path_to_source) if len(sources_to_remove) > 0: - ui_functions.print_warning_msg(SYNC_REMOVE_LIST_END_FORMATTING, header = False) + logger.warning(SYNC_REMOVE_LIST_END_FORMATTING) # Clone any new Git repositories clone_repos(args, workspace_path, sources_to_clone, new_manifest_to_check.repo_hooks, config, new_manifest_to_check) # Make a list of and only checkout repos that were newly cloned. Sync keeps repos on their initial active branches @@ -402,12 +402,12 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path for source in sources_to_clone: if source.root == new_source.root: repos_to_checkout.append(source) - repos_to_checkout.extend(self.__check_combo_sha_tag_branch(workspace_path, initial_common, new_common)) + repos_to_checkout.extend(self.__check_combo_sha_tag_branch(logger, workspace_path, initial_common, new_common)) if repos_to_checkout: checkout_repos(args.verbose, args.override, repos_to_checkout, workspace_path, new_manifest_to_check) #remove the old manifest file and copy the new one - ui_functions.print_info_msg(UPDATING_MANIFEST, header = False) + logger.info(UPDATING_MANIFEST) local_manifest_path = os.path.join(local_manifest_dir, 'Manifest.xml') os.remove(local_manifest_path) shutil.copy(global_manifest_path, local_manifest_path) @@ -422,7 +422,7 @@ def __update_local_manifest(self, args, config, initial_manifest, workspace_path except EdkrepoManifestNotFoundException: pass - def __check_combo_sha_tag_branch(self, workspace_path, initial_sources, new_sources): + def __check_combo_sha_tag_branch(self, logger, workspace_path, initial_sources, new_sources): # Checks for changes in the defined SHAs, Tags or branches in the checked out combo. Returns # a list of repos to checkout. Checks to see if user is on appropriate SHA, tag or branch and # throws and exception if not. @@ -434,23 +434,23 @@ def __check_combo_sha_tag_branch(self, workspace_path, initial_sources, new_sour repo = Repo(local_repo_path) if initial_source.commit and initial_source.commit != new_source.commit: if repo.head.object.hexsha != initial_source.commit: - ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False) + logger.info(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root)) repos_to_checkout.append(new_source) break elif initial_source.tag and initial_source.tag != new_source.tag: tag_sha = repo.git.rev_list('-n 1', initial_source.tag) #according to gitpython docs must change - to _ if tag_sha != repo.head.object.hexsha: - ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False) + logger.info(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root)) repos_to_checkout.append(new_source) break elif initial_source.branch and initial_source.branch != new_source.branch: if repo.active_branch.name != initial_source.branch: - ui_functions.print_info_msg(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root), header = False) + logger.info(SYNC_BRANCH_CHANGE_ON_LOCAL.format(initial_source.branch, new_source.branch, initial_source.root)) repos_to_checkout.append(new_source) break return repos_to_checkout - def __check_for_new_manifest(self, args, config, initial_manifest, workspace_path, global_manifest_directory): + def __check_for_new_manifest(self, args, logger, config, initial_manifest, workspace_path, global_manifest_directory): #if the manifest repository for the current manifest was not found then there is no project with the manifest #specified project name in the index file for any of the manifest repositories if global_manifest_directory is None: @@ -471,8 +471,8 @@ def __check_for_new_manifest(self, args, config, initial_manifest, workspace_pat global_manifest_path = os.path.join(global_manifest_directory, os.path.normpath(ci_index_xml_rel_path)) global_manifest = ManifestXml(global_manifest_path) if not initial_manifest.equals(global_manifest, True): - ui_functions.print_warning_msg(SYNC_MANIFEST_DIFF_WARNING, header = False) - ui_functions.print_info_msg(SYNC_MANIFEST_UPDATE, header = False) + logger.warning(SYNC_MANIFEST_DIFF_WARNING) + logger.info(SYNC_MANIFEST_UPDATE) def __check_submodule_config(self, workspace_path, manifest, repo_sources): gitconfigpath = os.path.normpath(expanduser("~/.gitconfig")) diff --git a/edkrepo/common/common_cache_functions.py b/edkrepo/common/common_cache_functions.py index 84bd3ed..c9ebaa4 100644 --- a/edkrepo/common/common_cache_functions.py +++ b/edkrepo/common/common_cache_functions.py @@ -12,7 +12,9 @@ from edkrepo.config.config_factory import get_edkrepo_global_data_directory from edkrepo.config.tool_config import SUBMODULE_CACHE_REPO_NAME from project_utils.cache import RepoCache +from edkrepo.common.logger import get_logger +logger = get_logger() def get_global_cache_directory(config): if config['user_cfg_file'].caching_state: @@ -30,12 +32,12 @@ def get_repo_cache_obj(config): def add_missing_cache_repos(cache_obj, manifest, verbose=False): - print('Adding and fetching new remotes... (this could take a while)') + logger.info('Adding and fetching new remotes... (this could take a while)') for remote in manifest.remotes: cache_obj.add_repo(url=remote.url, verbose=verbose) alt_submodules = manifest.submodule_alternate_remotes if alt_submodules: - print('Adding and fetching new submodule remotes... (this could also take a while)') + logger.info('Adding and fetching new submodule remotes... (this could also take a while)') cache_obj.add_repo(name=SUBMODULE_CACHE_REPO_NAME, verbose=verbose) for alt in alt_submodules: cache_obj.add_remote(alt.alternate_url, SUBMODULE_CACHE_REPO_NAME, verbose) diff --git a/edkrepo/common/common_repo_functions.py b/edkrepo/common/common_repo_functions.py index de2a110..c5bb505 100644 --- a/edkrepo/common/common_repo_functions.py +++ b/edkrepo/common/common_repo_functions.py @@ -52,6 +52,7 @@ from edkrepo.common.humble import VERIFY_GLOBAL, VERIFY_ARCHIVED, VERIFY_PROJ, VERIFY_PROJ_FAIL from edkrepo.common.humble import VERIFY_PROJ_NOT_IN_INDEX, VERIFY_GLOBAL_FAIL from edkrepo.common.humble import SUBMODULE_DEINIT_FAILED +from edkrepo.common.logger import get_logger from edkrepo.common.pathfix import get_actual_path, expanduser from project_utils.sparse import BuildInfo, process_sparse_checkout from edkrepo.config.config_factory import get_workspace_path @@ -76,6 +77,7 @@ CLEAR_LINE = '\x1b[K' DEFAULT_REMOTE_NAME = 'origin' PRIMARY_REMOTE_NAME = 'primary' +logger = get_logger() def clone_repos(args, workspace_dir, repos_to_clone, project_client_side_hooks, config, manifest, cache_obj=None): @@ -85,9 +87,9 @@ def clone_repos(args, workspace_dir, repos_to_clone, project_client_side_hooks, cache_path = None if cache_obj is not None: cache_path = cache_obj.get_cache_path(local_repo_url) - ui_functions.print_info_msg("Cloning from: " + str(local_repo_url), header = False) + logger.info("Cloning from: " + str(local_repo_url)) if cache_path is not None: - ui_functions.print_info_msg('+ Using cache at {}'.format(cache_path)) + logger.info('+ Using cache at {}'.format(cache_path)) repo = Repo.clone_from(local_repo_url, local_repo_path, progress=GitProgressHandler(), reference_if_able=cache_path, @@ -108,12 +110,12 @@ def clone_repos(args, workspace_dir, repos_to_clone, project_client_side_hooks, # order of importance is 1)commit 2)tag 3)branch with only the higest priority being checked # out if repo_to_clone.commit: - if args.verbose and (repo_to_clone.branch or repo_to_clone.tag): - ui_functions.print_info_msg(MULTIPLE_SOURCE_ATTRIBUTES_SPECIFIED.format(repo_to_clone.root)) + if repo_to_clone.branch or repo_to_clone.tag: + logger.info(MULTIPLE_SOURCE_ATTRIBUTES_SPECIFIED.format(repo_to_clone.root), extra={'verbose': args.verbose}) repo.git.checkout(repo_to_clone.commit) elif repo_to_clone.tag and repo_to_clone.commit is None: - if args.verbose and repo_to_clone.branch: - ui_functions.print_info_msg(TAG_AND_BRANCH_SPECIFIED.format(repo_to_clone.root)) + if repo_to_clone.branch: + logger.info(TAG_AND_BRANCH_SPECIFIED.format(repo_to_clone.root), extra={'verbose': args.verbose}) repo.git.checkout(repo_to_clone.tag) elif repo_to_clone.branch and (repo_to_clone.commit is None and repo_to_clone.tag is None): if repo_to_clone.branch not in repo.remotes['origin'].refs: @@ -151,7 +153,7 @@ def clone_repos(args, workspace_dir, repos_to_clone, project_client_side_hooks, # Check to see if mirror is in sync with primary repo if not in_sync_with_primary(repo, repo_to_clone, args.verbose): - ui_functions.print_warning_msg(MIRROR_BEHIND_PRIMARY_REPO) + logger.warning(MIRROR_BEHIND_PRIMARY_REPO) def write_included_config(remotes, submodule_alt_remotes, repo_directory): included_configs = [] @@ -317,7 +319,7 @@ def sparse_checkout(workspace_dir, repo_list, manifest): try: process_sparse_checkout(workspace_dir, repo_list, current_combo, manifest) except RuntimeError as msg: - print(msg) + logger.error(msg) def check_dirty_repos(manifest, workspace_path): @@ -350,26 +352,25 @@ def checkout_repos(verbose, override, repos_to_checkout, workspace_path, manifes raise EdkrepoUncommitedChangesException(CHECKOUT_UNCOMMITED_CHANGES) check_branches(repos_to_checkout, workspace_path) for repo_to_checkout in repos_to_checkout: - if verbose: - if repo_to_checkout.branch is not None and repo_to_checkout.commit is None: - print(CHECKING_OUT_BRANCH.format(repo_to_checkout.branch, repo_to_checkout.root)) - elif repo_to_checkout.commit is not None: - print(CHECKING_OUT_COMMIT.format(repo_to_checkout.commit, repo_to_checkout.root)) + if repo_to_checkout.branch is not None and repo_to_checkout.commit is None: + logger.info(CHECKING_OUT_BRANCH.format(repo_to_checkout.branch, repo_to_checkout.root)) + elif repo_to_checkout.commit is not None: + logger.info(CHECKING_OUT_COMMIT.format(repo_to_checkout.commit, repo_to_checkout.root)) local_repo_path = os.path.join(workspace_path, repo_to_checkout.root) repo = Repo(local_repo_path) # Checkout the repo onto the correct branch/commit/tag if multiple attributes are provided in # the source section for the manifest the order of priority is the followiwng 1)commit # 2) tag 3)branch with the highest priority attribute provided beinng checked out if repo_to_checkout.commit: - if verbose and (repo_to_checkout.branch or repo_to_checkout.tag): - print(MULTIPLE_SOURCE_ATTRIBUTES_SPECIFIED.format(repo_to_checkout.root)) + if repo_to_checkout.branch or repo_to_checkout.tag: + logger.info(MULTIPLE_SOURCE_ATTRIBUTES_SPECIFIED.format(repo_to_checkout.root), extra={'verbose': verbose}) if override: repo.git.checkout(repo_to_checkout.commit, '--force') else: repo.git.checkout(repo_to_checkout.commit) elif repo_to_checkout.tag and repo_to_checkout.commit is None: - if verbose and (repo_to_checkout.branch): - print(TAG_AND_BRANCH_SPECIFIED.format(repo_to_checkout.root)) + if repo_to_checkout.branch: + logger.info(TAG_AND_BRANCH_SPECIFIED.format(repo_to_checkout.root), extra={'verbose': verbose}) if override: repo.git.checkout(repo_to_checkout.tag, '--force') else: @@ -391,19 +392,19 @@ def checkout_repos(verbose, override, repos_to_checkout, workspace_path, manifes raise EdkrepoManifestInvalidException(MISSING_BRANCH_COMMIT) def validate_manifest_repo(manifest_repo, verbose=False, archived=False): - print(VERIFY_GLOBAL) + logger.info(VERIFY_GLOBAL) if archived: - print(VERIFY_ARCHIVED) + logger.info(VERIFY_ARCHIVED) manifest_validation_data = validate_manifestrepo(manifest_repo, archived) manifest_repo_error = get_manifest_validation_status(manifest_validation_data) if manifest_repo_error: - print(VERIFY_GLOBAL_FAIL) + logger.info(VERIFY_GLOBAL_FAIL) if verbose: print_manifest_errors(manifest_validation_data) def verify_single_manifest(cfg_file, manifest_repo, manifest_path, verbose=False): manifest = ManifestXml(manifest_path) - print(VERIFY_PROJ.format(manifest.project_info.codename)) + logger.info(VERIFY_PROJ.format(manifest.project_info.codename)) index_path = os.path.join(cfg_file.manifest_repo_abs_path(manifest_repo), CI_INDEX_FILE_NAME) proj_val_data = validate_manifestfiles([manifest_path]) proj_val_error = get_manifest_validation_status(proj_val_data) @@ -420,14 +421,14 @@ def sort_commits(manifest, workspace_path, max_commits=None): for repo_to_log in repo_sources_to_log: local_repo_path = os.path.join(workspace_path, repo_to_log.root) repo = Repo(local_repo_path) - print("Processing {} log...".format(repo_to_log.root), end='\r') + logger.info("Processing {} log...".format(repo_to_log.root), end='\r') if max_commits: commit_generator = repo.iter_commits(max_count=max_commits) else: commit_generator = repo.iter_commits() for commit in commit_generator: commit_dictionary[commit] = commit.committed_date - print(CLEAR_LINE, end='') + logger.info(CLEAR_LINE, end='') sorted_commit_list = sorted(commit_dictionary, key=commit_dictionary.get, reverse=True) if max_commits: @@ -483,7 +484,7 @@ def checkout(combination, verbose=False, override=False, log=None, cache_obj=Non if sparse_settings is not None: sparse_enabled = False if sparse_enabled or sparse_diff: - print(SPARSE_RESET) + logger.info(SPARSE_RESET) reset_sparse_checkout(workspace_path, current_repos) # Deinit all submodules due to the potential for issues when switching @@ -492,11 +493,10 @@ def checkout(combination, verbose=False, override=False, log=None, cache_obj=Non try: deinit_full(workspace_path, manifest, verbose) except Exception as e: - print(SUBMODULE_DEINIT_FAILED) - if verbose: - print(e) + logger.error(SUBMODULE_DEINIT_FAILED, extra={'header': True}) + logger.warning(e, extra={'verbose': verbose}) - print(CHECKING_OUT_COMBO.format(combo)) + logger.info(CHECKING_OUT_COMBO.format(combo)) try: checkout_repos(verbose, override, repo_sources, workspace_path, manifest) @@ -506,9 +506,8 @@ def checkout(combination, verbose=False, override=False, log=None, cache_obj=Non if combination_is_in_manifest(combo, manifest): manifest.write_current_combo(combo) except: - if verbose: - traceback.print_exc() - print (CHECKOUT_COMBO_UNSUCCESSFULL.format(combo)) + logger.warning(traceback.format_exc(), extra={'verbose': verbose}) + logger.error(CHECKOUT_COMBO_UNSUCCESSFULL.format(combo)) # Return to the initial combo, since there was an issue with cheking out the selected combo checkout_repos(verbose, override, initial_repo_sources, workspace_path, manifest) finally: @@ -517,7 +516,7 @@ def checkout(combination, verbose=False, override=False, log=None, cache_obj=Non cache_path = cache_obj.get_cache_path(SUBMODULE_CACHE_REPO_NAME) maintain_submodules(workspace_path, manifest, submodule_combo, verbose, cache_path) if sparse_enabled or sparse_diff: - print(SPARSE_CHECKOUT) + logger.info(SPARSE_CHECKOUT) sparse_checkout(workspace_path, current_repos, manifest) def get_latest_sha(repo, branch, remote_or_url='origin'): @@ -552,7 +551,7 @@ def update_repo_commit_template(workspace_dir, repo, repo_info, config, global_m if gitglobalconfig.has_option(section='commit', option='template'): gitglobalconfig.get_value(section='commit', option='template') global_template_in_use = True - print(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) + logger.warning(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) # Apply the template based on current manifest with repo.config_writer() as cw: @@ -561,16 +560,16 @@ def update_repo_commit_template(workspace_dir, repo, repo_info, config, global_m current_template = cw.get_value(section='commit', option='template').replace('"', '') if not current_template.startswith(os.path.normpath(global_manifest_directory).replace('\\', '/')): if os.path.isfile(current_template): - print(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) + logger.warning(COMMIT_TEMPLATE_CUSTOM_VALUE.format(repo_info.remote_name)) return else: - print(COMMIT_TEMPLATE_NOT_FOUND.format(current_template)) - print(COMMIT_TEMPLATE_RESETTING_VALUE) + logger.warning(COMMIT_TEMPLATE_NOT_FOUND.format(current_template)) + logger.info(COMMIT_TEMPLATE_RESETTING_VALUE) if repo_info.remote_name in templates: template_path = os.path.normpath(os.path.join(global_manifest_directory, templates[repo_info.remote_name])) if not os.path.isfile(template_path): - print(COMMIT_TEMPLATE_NOT_FOUND.format(template_path)) + logger.warning(COMMIT_TEMPLATE_NOT_FOUND.format(template_path)) return template_path = template_path.replace('\\', '/') # Convert to git approved path cw.set_value(section='commit', option='template', value='"{}"'.format(template_path)) @@ -617,7 +616,7 @@ def check_single_remote_connection(remote_url): Checks the connection to a single remote using git ls-remote remote_url -q invoked via subprocess instead of gitpython to ensure that ssh errors are caught and handled properly on both git bash and windows command line""" - print(CHECKING_CONNECTION.format(remote_url)) + logger.info(CHECKING_CONNECTION.format(remote_url)) check_output = subprocess.Popen('git ls-remote {} -q'.format(remote_url), shell=True) check_output.communicate() diff --git a/edkrepo/common/edkrepo_exception.py b/edkrepo/common/edkrepo_exception.py index 0d89838..b7aae57 100644 --- a/edkrepo/common/edkrepo_exception.py +++ b/edkrepo/common/edkrepo_exception.py @@ -105,3 +105,9 @@ def __init__(self, message): class EdkrepoAbortCherryPickException(EdkrepoException): def __init__(self, message): super().__init__(message, 134) +class EdkrepoVersionException(EdkrepoException): + def __init__(self, message): + super().__init__(message, 135) +class EdkrepoLogsRemoveException(EdkrepoException): + def __init__(self, message): + super().__init__(message, 136) \ No newline at end of file diff --git a/edkrepo/common/edkrepo_version.py b/edkrepo/common/edkrepo_version.py new file mode 100644 index 0000000..1363588 --- /dev/null +++ b/edkrepo/common/edkrepo_version.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 +# +## @file +# edkrepo_version.py +# +# Copyright (c) 2018 - 2020, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# + +import re + +from edkrepo.common.edkrepo_exception import EdkrepoVersionException + +class EdkrepoVersion(): + """ + Initialize, describe and provide comparision operators for an edkrepo version number. + """ + def __init__(self, version_string): + version_pattern = re.compile(r'(\d+).(\d+).(\d+)') + valid_version = re.fullmatch(version_pattern, version_string) + if valid_version is None: + raise EdkrepoVersionException('{} is not a valid version number.'.format(version_string)) + self.major = int(valid_version.group(1)) + self.minor = int(valid_version.group(2)) + self.patch = int(valid_version.group(3)) + + def __eq__(self, other): + if self.major != other.major: + return False + elif self.minor != other.minor: + return False + elif self.patch != other.patch: + return False + else: + return True + + def __ne__(self, other): + return not self.__eq__(other) + + def __lt__(self, other): + if self.major < other.major: + return True + elif self.major == other.major and self.minor < other.minor: + return True + elif self.minor == other.minor and self.patch < other.patch: + return True + else: + return False + + def __le__(self, other): + if self.__lt__(other) or self.__eq__(other): + return True + else: + return False + + def __gt__(self, other): + if self.major > other.major: + return True + elif self.major == other.major and self.minor > other.minor: + return True + elif self.minor == other.minor and self.patch > other.patch: + return True + else: + return False + + def __ge__(self, other): + if self.__gt__(other) or self.__eq__(other): + return True + else: + return False + + def version_string(self): + return '{}.{}.{}'.format(self.major, self.minor, self.patch) + + def __str__(self): + return self.version_string() + + def __repr__(self): + return "edkrepo_version: '{}'".format(self.version_string()) \ No newline at end of file diff --git a/edkrepo/common/humble.py b/edkrepo/common/humble.py index 29d5511..9e6af0b 100644 --- a/edkrepo/common/humble.py +++ b/edkrepo/common/humble.py @@ -163,3 +163,17 @@ # Common submodule error messages SUBMODULE_DEINIT_FAILED = 'Warning: Unable to remove all submodule content' + +# Logging messages +COMMAND = "Command: {}" +GIT_VERSION = "Git Version: {}" +LFS_VERSION = "LFS Version: {}" +EDKREPO_VERSION = "edkrepo Version: {}" +PYTHON_VERSION = "Python Version: {}" +ENVIRONMENT_VARIABLES = "Environment Variables: {}" +GIT_CONFIG = "Git Config: {}" +LINE_BREAK = "\n\n" + "-"*50 + +# Logging errors +REMOVE_LOG_FAILED = "Failed to clear logs at {}" +DISK_SPACE_ERROR = "Due to unavailability of disk space, edkrepo cannot generate log files. Clear some space for it to work." diff --git a/edkrepo/common/logger.py b/edkrepo/common/logger.py new file mode 100644 index 0000000..79bfcce --- /dev/null +++ b/edkrepo/common/logger.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +# +## @file +# logger.py +# +# Copyright (c) 2020-2021, Intel Corporation. All rights reserved.
+# SPDX-License-Identifier: BSD-2-Clause-Patent +# +import os +import re +import git +import sys +import errno +import string +import logging +from datetime import date + +from colorama import init, Fore + +from edkrepo.common.humble import LINE_BREAK, DISK_SPACE_ERROR +from edkrepo.common.pathfix import expanduser + + +# If you find yourself repeatedly sending reset sequences to turn off color changes at the end of +# every print, then init(autoreset=True) will automate that (from https://pypi.org/project/colorama/) +init(autoreset=True) + +class CustomFormatter(logging.Formatter): + def __init__(self): + self.FORMATS = { + logging.INFO: Fore.WHITE, + logging.WARNING: Fore.YELLOW, + logging.ERROR: Fore.RED + } + + def format(self, record): + ''' + Overriding the format method of logging.Formatter to check if the message is normal or verbose + (if none of those, set it to normal by default), whether it requires safe processing or header + and, colors the ouput message according to the message level (info, warning, error) + ''' + format_ = "%(message)s\n" + if not hasattr(record, 'normal') and not hasattr(record, 'verbose'): + record.normal = True + + if hasattr(record, 'safe') and record.safe: + safe_str = '' + for char in record.msg: + if char not in string.printable: + char = '?' + safe_str = ''.join((safe_str, str(char))) + record.msg = safe_str if safe_str != '' else record.msg + + if hasattr(record,'header') and record.header: + format_ = "%(levelname)s: %(message)s\n" + + color = self.FORMATS.get(record.levelno) + formatter = logging.Formatter("{}{}".format(color, format_)) + return formatter.format(record) + +class CustomHandler(logging.StreamHandler): + def __init__(self): + super().__init__() + + def write(self, stream, msg): + try: + stream.write(msg) + except OSError as e: + if e.errno == errno.ENOSPC: + logger.info(DISK_SPACE_ERROR) + + def emit(self, record): + ''' + Overriding the emit method of the StreamHandler class to decide what to print in console + based on the presence of 'verbose' or 'normal' attribute and their boolean values. + ''' + try: + msg = self.format(record) + stream = self.stream + if hasattr(record, 'verbose') and record.verbose: + self.write(stream, msg) + if hasattr(record, 'normal') and record.normal: + self.write(stream, msg) + except: + self.handleError(record) + +class FileFormatter(logging.Formatter): + def __init__(self): + super().__init__() + + def format(self, record): + ''' + Overriding the format method of FileFormatter class to remove the color codes from messages + and not format the blank lines and line breaks between commands in file + ''' + if record.msg != "" and record.msg != LINE_BREAK: + msg = logging.Formatter.format(self, record) + colorCodeEscape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') # regex for all 7-bit ANSI C1 escape sequences + plainMsg = colorCodeEscape.sub('', msg) + return plainMsg + return record.msg + + +def get_logger(): + return logger + +def get_formatted_git_output(output_data, verbose=False): + """ + Displays output from GitPython git commands + + output_data - Output from the git.execute method + verbose - Enable verbose messages + """ + out, verbose_out = "", [] + if verbose and output_data[0]: + verbose_out.append(output_data[0]) + if output_data[1]: + out += output_data[1] + if verbose and output_data[2]: + verbose_out.append(output_data[2]) + return out, verbose_out + +def init_color_console(force_color_output): + config = git.GitConfigParser(os.path.normpath(expanduser("~/.gitconfig"))) + config_color = config.get("color", "ui", fallback="auto") + strip = not sys.stdout.isatty() + convert = sys.stdout.isatty() + if force_color_output or config_color == "always": + strip = False + elif config_color == "false": + strip = True + convert = False + if os.name == 'posix': + # Never convert on Linux. Setting it to False seems to cause problems. + convert=None + init(strip=strip, convert=convert, autoreset=True) + return strip, convert + +def initiate_file_logs(path): + ''' + This method takes in the path to store log files, creates a file handler, sets a custom formatting for our + log files and adds it to the logger object. Because of this, every message gets logged in log files. + ''' + fileHandler = logging.FileHandler("{}/{}.log".format(path, file_name)) + formatter = FileFormatter("%(asctime)s.%(msecs)03d %(levelname)s: %(message)s", datefmt='%Y-%m-%d %H:%M:%S') + fileHandler.setFormatter(formatter) + logger.addHandler(fileHandler) + + +file_name = date.today().strftime("%Y/%m/%d_logs").split('/') +file_name = '-'.join(file_name) + +logger = logging.getLogger('log') +logger.setLevel(logging.INFO) + +consoleHandler = CustomHandler() +consoleHandler.setFormatter(CustomFormatter()) +logger.addHandler(consoleHandler) diff --git a/edkrepo/common/progress_handler.py b/edkrepo/common/progress_handler.py index 077823e..ebaa04a 100644 --- a/edkrepo/common/progress_handler.py +++ b/edkrepo/common/progress_handler.py @@ -8,7 +8,8 @@ # from git import RemoteProgress - +from edkrepo.common.logger import get_logger +logger = get_logger() class GitProgressHandler(RemoteProgress): def __init__(self): super().__init__() @@ -16,4 +17,4 @@ def __init__(self): def update(self, *args): self.__max_line_len = max(self.__max_line_len, len(self._cur_line)) - print(self._cur_line.ljust(self.__max_line_len), end="\r") + logger.info(self._cur_line.ljust(self.__max_line_len), end="\r") diff --git a/edkrepo/common/ui_functions.py b/edkrepo/common/ui_functions.py deleted file mode 100644 index ccf4d58..0000000 --- a/edkrepo/common/ui_functions.py +++ /dev/null @@ -1,93 +0,0 @@ -#!/usr/bin/env python3 -# -## @file -# ui_functions.py -# -# Copyright (c) 2017 - 2022, Intel Corporation. All rights reserved.
-# SPDX-License-Identifier: BSD-2-Clause-Patent -# - -import os -import sys -import string - -import git -import colorama - -from colorama import Fore -from colorama import Style -from colorama import init - -from edkrepo.common.pathfix import expanduser - -def init_color_console(force_color_output): - config = git.GitConfigParser(os.path.normpath(expanduser("~/.gitconfig"))) - config_color = config.get("color", "ui", fallback="auto") - strip = not sys.stdout.isatty() - convert = sys.stdout.isatty() - if force_color_output or config_color == "always": - strip = False - elif config_color == "false": - strip = True - convert = False - if os.name == 'posix': - # Never convert on Linux. Setting it to False seems to cause problems. - convert=None - colorama.init(strip=strip, convert=convert, autoreset=True) - return strip, convert - - -def display_git_output(output_data, verbose=False): - """ - Displays output from GitPython git commands - - output_data - Output from the git.execute method - verbose - Enable verbose messages - """ - if verbose and output_data[0]: - print_info_msg(output_data[0]) - if output_data[1]: - print_info_msg(output_data[1]) - if verbose and output_data[2]: - print_info_msg(output_data[2]) - -def print_info_msg(info_msg, header=True): - """ - Displays informational message with (default) or without header. - """ - if header: - info_msg_formatted = "Info: {}".format(info_msg) - else: - info_msg_formatted = "{}".format(info_msg) - print(info_msg_formatted) - -def print_warning_msg(warning_msg, header=True): - """ - Displays warning message with (default) or without header. - """ - if header: - warning_msg_formatted = "{}{}Warning: {}{}{}".format(Style.BRIGHT, Fore.YELLOW, Style.RESET_ALL, Fore.YELLOW, warning_msg) - else: - warning_msg_formatted = "{}{}".format(Fore.YELLOW, warning_msg) - print(warning_msg_formatted) - -def print_error_msg(error_msg, header=True): - """ - Displays error message with (default) or without header. - """ - if header: - error_msg_formatted = "{}{}Error: {}{}{}".format(Style.BRIGHT, Fore.RED, Style.RESET_ALL, Fore.RED, error_msg) - else: - error_msg_formatted = "{}{}".format(Fore.RED, error_msg) - print(error_msg_formatted) - -def print_safe(input_string): - print(safe_str(input_string)) - -def safe_str(input_string): - safe_str = '' - for char in input_string: - if char not in string.printable: - char = '?' - safe_str = ''.join((safe_str, str(char))) - return safe_str diff --git a/edkrepo/common/workspace_maintenance/manifest_repos_maintenance.py b/edkrepo/common/workspace_maintenance/manifest_repos_maintenance.py index 86f1d2b..66ad877 100644 --- a/edkrepo/common/workspace_maintenance/manifest_repos_maintenance.py +++ b/edkrepo/common/workspace_maintenance/manifest_repos_maintenance.py @@ -19,10 +19,12 @@ from edkrepo.common.edkrepo_exception import EdkrepoUncommitedChangesException, EdkrepoInvalidParametersException from edkrepo.common.edkrepo_exception import EdkrepoManifestNotFoundException from edkrepo.common.progress_handler import GitProgressHandler +from edkrepo.common.logger import get_logger import edkrepo.common.workspace_maintenance.humble.manifest_repos_maintenance_humble as humble from edkrepo.common.workspace_maintenance.workspace_maintenance import generate_name_for_obsolete_backup from edkrepo.common.workspace_maintenance.workspace_maintenance import case_insensitive_single_match from edkrepo_manifest_parser.edk_manifest import CiIndexXml, ManifestXml +logger = get_logger() def pull_single_manifest_repo(url, branch, local_path, reset_hard=False): ''' @@ -34,7 +36,7 @@ def pull_single_manifest_repo(url, branch, local_path, reset_hard=False): local_path = os.path.join(cfg.get_edkrepo_global_data_directory(), local_path) # Clone the repository if it does not exist locally if not os.path.exists(local_path): - print(humble.CLONE_SINGLE_MAN_REPO.format(local_path, url)) + logger.info(humble.CLONE_SINGLE_MAN_REPO.format(local_path, url)) repo = Repo.clone_from(url, local_path, progress=GitProgressHandler(), branch=branch) # Sync the repository if it exists locally else: @@ -44,10 +46,10 @@ def pull_single_manifest_repo(url, branch, local_path, reset_hard=False): raise EdkrepoUncommitedChangesException(humble.SINGLE_MAN_REPO_DIRTY.format(local_path)) elif repo.is_dirty(untracked_files=True) and reset_hard: repo.git.reset('--hard') - print(humble.SYNC_SINGLE_MAN_REPO.format(local_path)) + logger.info(humble.SYNC_SINGLE_MAN_REPO.format(local_path)) if repo.active_branch.name != branch: - print(humble.SINGLE_MAN_REPO_NOT_CFG_BRANCH.format(repo.active_branch.name, local_path)) - print(humble.SINGLE_MAN_REPO_CHECKOUT_CFG_BRANCH.format(branch)) + logger.info(humble.SINGLE_MAN_REPO_NOT_CFG_BRANCH.format(repo.active_branch.name, local_path)) + logger.info(humble.SINGLE_MAN_REPO_CHECKOUT_CFG_BRANCH.format(branch)) repo.git.checkout(branch) repo.remotes.origin.pull() # If the URL specified for this manifest repo has moved back up the existing @@ -55,9 +57,9 @@ def pull_single_manifest_repo(url, branch, local_path, reset_hard=False): else: new_path = generate_name_for_obsolete_backup(local_path) new_path = os.path.join(os.path.dirname(local_path), new_path) - print(humble.SINGLE_MAN_REPO_MOVED.format(new_path)) + logger.info(humble.SINGLE_MAN_REPO_MOVED.format(new_path)) shutil.move(local_path, new_path) - print (humble.CLONE_SINGLE_MAN_REPO.format(local_path, url)) + logger.info(humble.CLONE_SINGLE_MAN_REPO.format(local_path, url)) repo = Repo.clone_from(url, local_path, progress=GitProgressHandler(), branch=branch) def pull_all_manifest_repos(edkrepo_cfg, edkrepo_user_cfg, reset_hard=False): @@ -70,7 +72,7 @@ def pull_all_manifest_repos(edkrepo_cfg, edkrepo_user_cfg, reset_hard=False): conflicts = [] cfg_man_repos, user_cfg_man_repos, conflicts = list_available_manifest_repos(edkrepo_cfg, edkrepo_user_cfg) for conflict in conflicts: - print(humble.CONFLICT_NO_CLONE.format(conflict)) + logger.info(humble.CONFLICT_NO_CLONE.format(conflict)) for repo in cfg_man_repos: pull_single_manifest_repo(edkrepo_cfg.get_manifest_repo_url(repo), edkrepo_cfg.get_manifest_repo_branch(repo), diff --git a/edkrepo/config/config_factory.py b/edkrepo/config/config_factory.py index c68c218..d40f40b 100644 --- a/edkrepo/config/config_factory.py +++ b/edkrepo/config/config_factory.py @@ -226,7 +226,9 @@ def __init__(self): self.prop_list = [ CfgProp('scm', 'mirror_geo', 'geo', 'none', False), CfgProp('send-review', 'max-patch-set', 'max_patch_set', '10', False), - CfgProp('caching', 'enable-caching', 'enable_caching_text', 'false', False)] + CfgProp('caching', 'enable-caching', 'enable_caching_text', 'false', False), + CfgProp('logs', 'logs-path', 'log_path', os.path.join(get_edkrepo_global_data_directory(), 'logs'), False), + CfgProp('logs', 'logs-retention-days', 'logs_retention_days', '30', False)] super().__init__(self.filename, get_edkrepo_global_data_directory(), False) @property @@ -250,6 +252,15 @@ def max_patch_set_int(self): except: raise EdkrepoConfigFileInvalidException(MAX_PATCH_SET_INVALID) + @property + def logs_path(self): + return self.log_path + + @property + def logs_retention_period(self): + return int(self.logs_retention_days) + + def get_workspace_path(): path = os.path.realpath(os.getcwd()) while True: diff --git a/edkrepo/edkrepo_cli.py b/edkrepo/edkrepo_cli.py index 1adb63f..0893733 100644 --- a/edkrepo/edkrepo_cli.py +++ b/edkrepo/edkrepo_cli.py @@ -25,11 +25,17 @@ from edkrepo.commands import command_factory from edkrepo.config import config_factory +from edkrepo.common.edkrepo_version import EdkrepoVersion from edkrepo.common.edkrepo_exception import EdkrepoException, EdkrepoGlobalConfigNotFoundException -from edkrepo.common.edkrepo_exception import EdkrepoWarningException +from edkrepo.common.edkrepo_exception import EdkrepoWarningException, EdkrepoLogsRemoveException from edkrepo.common.edkrepo_exception import EdkrepoConfigFileInvalidException -from edkrepo.common.humble import KEYBOARD_INTERRUPT, GIT_CMD_ERROR +from edkrepo.common.humble import COMMAND, KEYBOARD_INTERRUPT, GIT_CMD_ERROR +from edkrepo.common.humble import LINE_BREAK, PYTHON_VERSION, LFS_VERSION +from edkrepo.common.humble import KEYBOARD_INTERRUPT, GIT_CMD_ERROR, REMOVE_LOG_FAILED +from edkrepo.common.humble import EDKREPO_VERSION, GIT_VERSION, ENVIRONMENT_VARIABLES, GIT_CONFIG from edkrepo.common.pathfix import get_actual_path +from edkrepo.common.logger import get_logger, initiate_file_logs + def generate_command_line(command): parser = argparse.ArgumentParser() @@ -156,6 +162,18 @@ def generate_command_completion_script(script_filename, parser): f.write('if [ -x "$(command -v edkrepo)" ]; then\n') f.write(' complete -F _edkrepo_completions edkrepo\nfi\n') +def clear_logs(config): + config = config["user_cfg_file"] + SECONDS_IN_A_DAY = 86400 + if os.path.exists(config.logs_path): + for file in os.listdir(config.logs_path): + file_path = os.path.join(config.logs_path, file) + if os.stat(file_path).st_mtime < time.time() - config.logs_retention_period * SECONDS_IN_A_DAY: + try: + os.remove(file_path) + except EdkrepoLogsRemoveException as e: + logger.info(REMOVE_LOG_FAILED.format(file_path), extra={'normal': False}) + def main(): start_time = dt.datetime.now() command = command_factory.create_composite_command() @@ -164,10 +182,10 @@ def main(): config["cfg_file"] = config_factory.GlobalConfig() config["user_cfg_file"] = config_factory.GlobalUserConfig() except EdkrepoGlobalConfigNotFoundException as e: - print("Error: {}".format(str(e))) + logger.error("Error: {}".format(str(e))) return e.exit_code except EdkrepoConfigFileInvalidException as e: - print("Error: {}".format(str(e))) + logger.error("Error: {}".format(str(e))) return e.exit_code parser = generate_command_line(command) @@ -179,40 +197,58 @@ def main(): return 0 parsed_args = parser.parse_args() command_name = parsed_args.subparser_name + clear_logs() + initiate_file_logs(config["user_cfg_file"].logs_path) + git_version_output = subprocess.getoutput('git --version') + lfs_version_output = subprocess.getoutput('git-lfs version') + python_version_output = str(sys.version_info.major) + "." + str(sys.version_info.minor) + "." + str(sys.version_info.micro) + edkrepo_version = EdkrepoVersion(pkg_resources.get_distribution('edkrepo').version) + environment_info = json.dumps(dict(os.environ), indent=2) + git_config_values = subprocess.getoutput('git config --list --show-scope --show-origin') + logger.info(COMMAND.format(command_name), extra = {'normal': False}) + logger.info(GIT_VERSION.format(git_version_output), extra = {'normal': False}) + logger.info(LFS_VERSION.format(lfs_version_output), extra = {'normal': False}) + logger.info(PYTHON_VERSION.format(python_version_output), extra = {'normal': False}) + logger.info(EDKREPO_VERSION.format(edkrepo_version.version_string()), extra = {'normal': False}) + logger.info(ENVIRONMENT_VARIABLES.format(environment_info), extra={'normal': False}) + logger.info(GIT_CONFIG.format(git_config_values), extra={'normal': False}) + logger.info(LINE_BREAK, extra={'normal': False}) try: command.run_command(command_name, parsed_args, config) except EdkrepoWarningException as e: - print("Warning: {}".format(str(e))) + logger.warning("Warning: {}".format(str(e))) return e.exit_code except EdkrepoException as e: if parsed_args.verbose: traceback.print_exc() - print("Error: {}".format(str(e))) + logger.error("Error: {}".format(str(e))) return e.exit_code except GitCommandError as e: if parsed_args.verbose: traceback.print_exc() out_str = '' out_str = ' '.join(e.command) - print(GIT_CMD_ERROR.format(out_str)) - print(e.stdout.strip()) - print(e.stderr.strip()) + logger.error(GIT_CMD_ERROR.format(out_str)) + logger.warning(e.stdout.strip()) + logger.warning(e.stderr.strip()) return e.status except KeyboardInterrupt: if parsed_args.verbose: traceback.print_exc() - print(KEYBOARD_INTERRUPT) + logger.warning(KEYBOARD_INTERRUPT) return 1 except Exception as e: if parsed_args.verbose: traceback.print_exc() - print("Error: {}".format(str(e))) + logger.error("Error: {}".format(str(e))) return 1 - if parsed_args.performance: - print('\nExecution Time: {}'.format(dt.datetime.now() - start_time)) + + logger.info('Execution Time: {}'.format(dt.datetime.now() - start_time), extra = {'normal': parsed_args.performance}) + logger.info(LINE_BREAK, extra={'normal': False}) return 0 if __name__ == "__main__": + logger = get_logger() try: sys.exit(main()) except Exception as e: diff --git a/edkrepo_manifest_parser/edk_manifest_validation.py b/edkrepo_manifest_parser/edk_manifest_validation.py index 3dbd2f9..3c98724 100644 --- a/edkrepo_manifest_parser/edk_manifest_validation.py +++ b/edkrepo_manifest_parser/edk_manifest_validation.py @@ -17,6 +17,7 @@ from edkrepo.common.humble import MANIFEST_NAME_INCONSISTENT from edkrepo.common.humble import INDEX_DUPLICATE_NAMES from edkrepo.common.edkrepo_exception import EdkrepoVerificationException +from edkrepo.common.logger import get_logger from edkrepo.common.humble import VERIFY_ERROR_HEADER class ValidateManifest: @@ -120,13 +121,13 @@ def get_manifest_validation_status(manifestfile_validation): return manifest_error def print_manifest_errors(manifestfile_validation): - print(VERIFY_ERROR_HEADER) + logger.info(VERIFY_ERROR_HEADER) for manifestfile in manifestfile_validation.keys(): for result in manifestfile_validation[manifestfile]: if not result[1]: - print ("File name: {} ".format(manifestfile)) - print ("Error type: {} ".format(result[0])) - print("Error message: {} \n".format(result[2])) + logger.error("File name: {} ".format(manifestfile)) + logger.error("Error type: {} ".format(result[0])) + logger.error("Error message: {} \n".format(result[2])) def main(): parser = argparse.ArgumentParser() # Add mutually exclusive arguments group @@ -147,13 +148,14 @@ def main(): # Check status.If any error print errors and raise exception. if not manifest_error: - print("Manifest validation status: PASS \n") + logger.info("Manifest validation status: PASS \n") else: print_manifest_errors(manifestfile_validation) raise EdkrepoVerificationException(("Manifest validation status: FAIL \n")) return 0 if __name__ == '__main__': + logger = get_logger() try: sys.exit(main()) except Exception as e: diff --git a/project_utils/cache.py b/project_utils/cache.py index 8efd411..089a291 100644 --- a/project_utils/cache.py +++ b/project_utils/cache.py @@ -13,6 +13,7 @@ from git import Repo from edkrepo.common.progress_handler import GitProgressHandler +from edkrepo.common.logger import get_logger from project_utils.project_utils_strings import CACHE_ADD_REMOTE, CACHE_ADDING_REPO, CACHE_CHECK_ROOT_DIR from project_utils.project_utils_strings import CACHE_FAILED_TO_CLOSE, CACHE_FAILED_TO_OPEN, CACHE_FETCH_REMOTE from project_utils.project_utils_strings import CACHE_REMOTE_EXISTS, CACHE_REMOVE_REPO, CACHE_REPO_EXISTS @@ -27,6 +28,7 @@ class RepoCache(object): def __init__(self, path): self._cache_root_path = path self._repos = {} + self.logger = get_logger() def _create_name(self, url_or_name): """ @@ -62,11 +64,9 @@ def _get_cache_dirs(self): return [x for x in os.listdir(self._cache_root_path) if os.path.isdir(self._get_repo_path(x))] def _add_and_fetch_remote(self, repo, remote_name, url, verbose=False): - if verbose: - print(CACHE_ADD_REMOTE.format(remote_name, url)) + self.logger.info(CACHE_ADD_REMOTE.format(remote_name, url), extra={'verbose': verbose}) repo.create_remote(remote_name, url) - if verbose: - print(CACHE_FETCH_REMOTE.format(remote_name, url)) + self.logger.info(CACHE_FETCH_REMOTE.format(remote_name, url), extra={'verbose': verbose}) repo.remotes[remote_name].fetch(progress=GitProgressHandler()) def open(self, verbose=False): @@ -77,16 +77,14 @@ def open(self, verbose=False): """ if not self._repos: if not os.path.isdir(self._cache_root_path): - if verbose: - print(CACHE_CHECK_ROOT_DIR.format(self._cache_root_path)) + self.logger.info(CACHE_CHECK_ROOT_DIR.format(self._cache_root_path), extra={'verbose': verbose}) os.makedirs(self._cache_root_path) for dir_name in self._get_cache_dirs(): try: self._repos[dir_name] = self._get_repo(dir_name) except Exception: - if verbose: - print(CACHE_FAILED_TO_OPEN.format(dir_name)) + self.logger.error(CACHE_FAILED_TO_OPEN.format(dir_name), extra={'verbose': verbose}) def close(self, verbose=False): """ @@ -96,8 +94,7 @@ def close(self, verbose=False): try: self._repos[dir_name].close() except Exception: - if verbose: - print(CACHE_FAILED_TO_CLOSE.format(dir_name)) + self.logger.error(CACHE_FAILED_TO_CLOSE.format(dir_name), extra={'verbose': verbose}) self._repos = {} def get_cache_path(self, url_or_name): @@ -144,11 +141,9 @@ def add_repo(self, url=None, name=None, verbose=False): repo_path = self._get_repo_path(dir_name) if dir_name in self._repos: - if verbose: - print(CACHE_REPO_EXISTS.format(dir_name)) + self.logger.info(CACHE_REPO_EXISTS.format(dir_name), extra={'verbose': verbose}) else: - if verbose: - print(CACHE_ADDING_REPO.format(dir_name)) + self.logger.info(CACHE_ADDING_REPO.format(dir_name), extra={'verbose': verbose}) os.makedirs(repo_path) self._repos[dir_name] = Repo.init(repo_path, bare=True) @@ -170,8 +165,7 @@ def remove_repo(self, url=None, name=None, verbose=False): dir_name = self._create_name(url) if dir_name not in self._repos: return - if verbose: - print(CACHE_REMOVE_REPO.format(dir_name)) + self.logger.info(CACHE_REMOVE_REPO.format(dir_name), extra={'verbose': verbose}) self._repos.pop(dir_name).close() shutil.rmtree(os.path.join(self._cache_root_path, dir_name), ignore_errors=True) @@ -182,8 +176,7 @@ def add_remote(self, url, name, verbose=False): raise ValueError repo = self._get_repo(dir_name) if remote_name in repo.remotes: - if verbose: - print(CACHE_REMOTE_EXISTS.format(remote_name)) + self.logger.info(CACHE_REMOTE_EXISTS.format(remote_name), extra={'verbose': verbose}) return self._add_and_fetch_remote(repo, remote_name, url, verbose) @@ -213,11 +206,10 @@ def update_cache(self, url_or_name=None, verbose=False): try: repo = self._get_repo(dir_name) except Exception: - print(CACHE_FAILED_TO_OPEN.format(dir_name)) + self.logger.error(CACHE_FAILED_TO_OPEN.format(dir_name)) continue for remote in repo.remotes: - if verbose: - print(CACHE_FETCH_REMOTE.format(dir_name, remote.url)) + self.logger.info(CACHE_FETCH_REMOTE.format(dir_name, remote.url), extra={'verbose': verbose}) remote.fetch(progress=GitProgressHandler()) def clean_cache(self, verbose=False):