From 1ad594537c25d8c07691b3ca78ba5bbd99d947e4 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 16:39:04 -0800 Subject: [PATCH 1/6] [error-exit-code] Return non-zero if something wrong happens, even if we don't bail immediately --- bin/ka-clone | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/bin/ka-clone b/bin/ka-clone index bcc6374..d59e69a 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -17,6 +17,7 @@ TEMPLATES_DIR = os.path.join( "templates" ) +SHARED_RET_CODE = 0 def _expanded_home_path(path): home = os.path.expanduser("~") # safe for cross platform @@ -168,6 +169,7 @@ def _default_email(): ).decode('utf-8') return kac_email.rstrip() except subprocess.CalledProcessError: + EXIT_CODE = 1 return os.environ['USER'] + "@" + DEFAULT_EMAIL_DOMAIN @@ -295,6 +297,7 @@ def run_make_hooks(): cwd=_top_level_dir()) _cli_log_step_indented_info("Ran make hooks successfully") except subprocess.CalledProcessError as e: + EXIT_CODE = 1 _cli_log_step_indented_info("Failed to run make hooks") print("\nError: {}".format(e.output.decode('utf-8'))) @@ -328,6 +331,7 @@ def _existing_commit_template(cwd): ).decode('utf-8') return template.strip() except subprocess.CalledProcessError: + EXIT_CODE = 1 return None @@ -381,6 +385,7 @@ def link_commit_template(): "The commit template {}".format(tmpl), "is not installed, so we can't link it, skipping..." ) + EXIT_CODE = 1 return all_dirs = _get_submodule_paths() @@ -404,6 +409,7 @@ def _existing_configs(cwd): [line.strip() for line in incl.splitlines()] if incl else [] ) except subprocess.CalledProcessError: + EXIT_CODE = 1 return [] @@ -446,6 +452,7 @@ def link_gitconfig_khan(): tmpl = _expanded_home_path('.gitconfig.khan') if not os.path.isfile(tmpl): + EXIT_CODE = 1 _cli_log_step_indented_info( "The git config file {} is not installed, ".format(tmpl) + "so we can't include it, skipping..." @@ -580,3 +587,5 @@ if __name__ == '__main__': ) os.chdir(_dst) _cli_process_current_dir(args) + + sys.exit(SHARED_RET_CODE) From ad4e69ef2d082f3e4f4f13ecbdba8ff974517686 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 16:42:37 -0800 Subject: [PATCH 2/6] [error-exit-code] fr --- bin/ka-clone | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bin/ka-clone b/bin/ka-clone index d59e69a..07f00e6 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -169,7 +169,7 @@ def _default_email(): ).decode('utf-8') return kac_email.rstrip() except subprocess.CalledProcessError: - EXIT_CODE = 1 + SHARED_RET_CODE = 1 return os.environ['USER'] + "@" + DEFAULT_EMAIL_DOMAIN @@ -297,7 +297,7 @@ def run_make_hooks(): cwd=_top_level_dir()) _cli_log_step_indented_info("Ran make hooks successfully") except subprocess.CalledProcessError as e: - EXIT_CODE = 1 + SHARED_RET_CODE = 1 _cli_log_step_indented_info("Failed to run make hooks") print("\nError: {}".format(e.output.decode('utf-8'))) @@ -331,7 +331,7 @@ def _existing_commit_template(cwd): ).decode('utf-8') return template.strip() except subprocess.CalledProcessError: - EXIT_CODE = 1 + SHARED_RET_CODE = 1 return None @@ -385,7 +385,7 @@ def link_commit_template(): "The commit template {}".format(tmpl), "is not installed, so we can't link it, skipping..." ) - EXIT_CODE = 1 + SHARED_RET_CODE = 1 return all_dirs = _get_submodule_paths() @@ -409,7 +409,7 @@ def _existing_configs(cwd): [line.strip() for line in incl.splitlines()] if incl else [] ) except subprocess.CalledProcessError: - EXIT_CODE = 1 + SHARED_RET_CODE = 1 return [] @@ -452,7 +452,7 @@ def link_gitconfig_khan(): tmpl = _expanded_home_path('.gitconfig.khan') if not os.path.isfile(tmpl): - EXIT_CODE = 1 + SHARED_RET_CODE = 1 _cli_log_step_indented_info( "The git config file {} is not installed, ".format(tmpl) + "so we can't include it, skipping..." From d35e2a5309dfbd5565c6a24cbae046636518e60a Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 16:45:30 -0800 Subject: [PATCH 3/6] [error-exit-code] g --- bin/ka-clone | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bin/ka-clone b/bin/ka-clone index 07f00e6..f9b89cb 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -169,6 +169,7 @@ def _default_email(): ).decode('utf-8') return kac_email.rstrip() except subprocess.CalledProcessError: + global SHARED_RET_CODE SHARED_RET_CODE = 1 return os.environ['USER'] + "@" + DEFAULT_EMAIL_DOMAIN @@ -297,6 +298,7 @@ def run_make_hooks(): cwd=_top_level_dir()) _cli_log_step_indented_info("Ran make hooks successfully") except subprocess.CalledProcessError as e: + global SHARED_RET_CODE SHARED_RET_CODE = 1 _cli_log_step_indented_info("Failed to run make hooks") print("\nError: {}".format(e.output.decode('utf-8'))) @@ -331,6 +333,7 @@ def _existing_commit_template(cwd): ).decode('utf-8') return template.strip() except subprocess.CalledProcessError: + global SHARED_RET_CODE SHARED_RET_CODE = 1 return None @@ -385,6 +388,7 @@ def link_commit_template(): "The commit template {}".format(tmpl), "is not installed, so we can't link it, skipping..." ) + global SHARED_RET_CODE SHARED_RET_CODE = 1 return @@ -409,6 +413,7 @@ def _existing_configs(cwd): [line.strip() for line in incl.splitlines()] if incl else [] ) except subprocess.CalledProcessError: + global SHARED_RET_CODE SHARED_RET_CODE = 1 return [] @@ -452,6 +457,7 @@ def link_gitconfig_khan(): tmpl = _expanded_home_path('.gitconfig.khan') if not os.path.isfile(tmpl): + global SHARED_RET_CODE SHARED_RET_CODE = 1 _cli_log_step_indented_info( "The git config file {} is not installed, ".format(tmpl) + From 0a3dc7417e2150f43a78e71324b8d1ab8fd8be43 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 17:36:07 -0800 Subject: [PATCH 4/6] [error-to-stderr] omg maybe working --- bin/ka-clone | 66 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/bin/ka-clone b/bin/ka-clone index f9b89cb..b3277dd 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -17,7 +17,6 @@ TEMPLATES_DIR = os.path.join( "templates" ) -SHARED_RET_CODE = 0 def _expanded_home_path(path): home = os.path.expanduser("~") # safe for cross platform @@ -169,8 +168,8 @@ def _default_email(): ).decode('utf-8') return kac_email.rstrip() except subprocess.CalledProcessError: - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error('** No git email set') return os.environ['USER'] + "@" + DEFAULT_EMAIL_DOMAIN @@ -298,8 +297,8 @@ def run_make_hooks(): cwd=_top_level_dir()) _cli_log_step_indented_info("Ran make hooks successfully") except subprocess.CalledProcessError as e: - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error('** Failed to run make hooks') _cli_log_step_indented_info("Failed to run make hooks") print("\nError: {}".format(e.output.decode('utf-8'))) @@ -333,8 +332,8 @@ def _existing_commit_template(cwd): ).decode('utf-8') return template.strip() except subprocess.CalledProcessError: - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error('** Failed to get git config --local commit.template') return None @@ -388,8 +387,10 @@ def link_commit_template(): "The commit template {}".format(tmpl), "is not installed, so we can't link it, skipping..." ) - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error( + '** The commit template {} is not installed'.format(tmpl) + ) return all_dirs = _get_submodule_paths() @@ -413,8 +414,8 @@ def _existing_configs(cwd): [line.strip() for line in incl.splitlines()] if incl else [] ) except subprocess.CalledProcessError: - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error('** Failed to get-all git config --local include.path') return [] @@ -457,8 +458,10 @@ def link_gitconfig_khan(): tmpl = _expanded_home_path('.gitconfig.khan') if not os.path.isfile(tmpl): - global SHARED_RET_CODE - SHARED_RET_CODE = 1 + # Send to stderr + logging.error( + '** The git config file {} is not installed'.format(tmpl) + ) _cli_log_step_indented_info( "The git config file {} is not installed, ".format(tmpl) + "so we can't include it, skipping..." @@ -574,8 +577,39 @@ if __name__ == '__main__': parser = _cli_parser() args = parser.parse_args() - logging.basicConfig(format="%(message)s") - logging.getLogger().setLevel(logging.ERROR if args.quiet else logging.INFO) + # logger = logging.getLogger("nameOfTheLogger") + # ConsoleOutputHandler = logging.StreamHandler() + # + # logger.addHandler(ConsoleOutputHandler) + # + # logger.warning("Warning.") + + logger = logging.getLogger() + # logger.setLevel(logging.DEBUG if args.quiet else logging.INFO) + logger.setLevel(logging.DEBUG) + format='%(message)s' + formatter = logging.Formatter(format) + + h1 = logging.NullHandler() if args.quiet else logging.StreamHandler(sys.stdout) + # logging.getLogger().setLevel(logging.DEBUG if args.quiet else logging.INFO) + h1.setLevel(logging.INFO) + h1.addFilter(lambda record: record.levelno <= logging.INFO) + h1.setFormatter(formatter) + + h2 = logging.StreamHandler() + h2.setLevel(logging.WARNING) + h2.setFormatter(formatter) + logger.addHandler(h1) + logger.addHandler(h2) + logger.info("info") + logger.error("error") + + + # logging.basicConfig(stream=sys.stdout, format="%(message)s", level=logging.INFO) + # logging.basicConfig(stream=sys.stderr, format="%(message)s", level=logging.ERROR) + # logging.basicConfig(stream=sys.stdout, level=[logging.DEBUG, logging.INFO]) + # logging.basicConfig(stream=sys.stdout, level=logging.INFO) + # logging.getLogger().setLevel(logging.DEBUG if args.quiet else logging.INFO) if args.repair: if args.src or args.dst: @@ -593,5 +627,3 @@ if __name__ == '__main__': ) os.chdir(_dst) _cli_process_current_dir(args) - - sys.exit(SHARED_RET_CODE) From dc5f881f97ad590cf46eb9024016b2f78242365d Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 17:36:53 -0800 Subject: [PATCH 5/6] [error-to-stderr] clean up --- bin/ka-clone | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/bin/ka-clone b/bin/ka-clone index b3277dd..f5199a6 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -577,21 +577,12 @@ if __name__ == '__main__': parser = _cli_parser() args = parser.parse_args() - # logger = logging.getLogger("nameOfTheLogger") - # ConsoleOutputHandler = logging.StreamHandler() - # - # logger.addHandler(ConsoleOutputHandler) - # - # logger.warning("Warning.") - logger = logging.getLogger() - # logger.setLevel(logging.DEBUG if args.quiet else logging.INFO) logger.setLevel(logging.DEBUG) format='%(message)s' formatter = logging.Formatter(format) h1 = logging.NullHandler() if args.quiet else logging.StreamHandler(sys.stdout) - # logging.getLogger().setLevel(logging.DEBUG if args.quiet else logging.INFO) h1.setLevel(logging.INFO) h1.addFilter(lambda record: record.levelno <= logging.INFO) h1.setFormatter(formatter) @@ -601,15 +592,6 @@ if __name__ == '__main__': h2.setFormatter(formatter) logger.addHandler(h1) logger.addHandler(h2) - logger.info("info") - logger.error("error") - - - # logging.basicConfig(stream=sys.stdout, format="%(message)s", level=logging.INFO) - # logging.basicConfig(stream=sys.stderr, format="%(message)s", level=logging.ERROR) - # logging.basicConfig(stream=sys.stdout, level=[logging.DEBUG, logging.INFO]) - # logging.basicConfig(stream=sys.stdout, level=logging.INFO) - # logging.getLogger().setLevel(logging.DEBUG if args.quiet else logging.INFO) if args.repair: if args.src or args.dst: From c2d6e337c3ec888882febd58f5b3fe44f494f6a1 Mon Sep 17 00:00:00 2001 From: Lilli Szafranski Date: Wed, 8 Jan 2025 17:38:18 -0800 Subject: [PATCH 6/6] [error-to-stderr] lint --- bin/ka-clone | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/ka-clone b/bin/ka-clone index f5199a6..0ff52d6 100755 --- a/bin/ka-clone +++ b/bin/ka-clone @@ -579,10 +579,12 @@ if __name__ == '__main__': logger = logging.getLogger() logger.setLevel(logging.DEBUG) - format='%(message)s' + format = '%(message)s' formatter = logging.Formatter(format) - h1 = logging.NullHandler() if args.quiet else logging.StreamHandler(sys.stdout) + h1 = logging.NullHandler() if args.quiet \ + else logging.StreamHandler(sys.stdout) + h1.setLevel(logging.INFO) h1.addFilter(lambda record: record.levelno <= logging.INFO) h1.setFormatter(formatter)