From 241d1d43b0cafbab56e96bb23539d39eb4855f81 Mon Sep 17 00:00:00 2001 From: Gustavo Padovan Date: Wed, 15 Jan 2025 17:12:37 -0300 Subject: [PATCH] use kci_err() for error messages across codebase Thi commit changes all secho error messages with fg="red" as parameter to kci_err(). Signed-off-by: Gustavo Padovan --- kcidev/subcommands/bisect.py | 38 ++++++++++++++------------- kcidev/subcommands/checkout.py | 27 ++++++++----------- kcidev/subcommands/maestro_results.py | 10 ++++--- kcidev/subcommands/testretry.py | 6 ++--- 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/kcidev/subcommands/bisect.py b/kcidev/subcommands/bisect.py index 334acc6..784cad6 100644 --- a/kcidev/subcommands/bisect.py +++ b/kcidev/subcommands/bisect.py @@ -12,6 +12,8 @@ import toml from git import Repo +from kcidev.libs.common import * + """ To not lose the state of the bisection, we need to store the state in a file The state file is a json file that contains the following keys: @@ -76,13 +78,13 @@ def git_exec_getcommit(cmd): click.secho("Executing git command: " + " ".join(cmd), fg="green") result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if result.returncode != 0: - click.secho("git command return failed. Error:", fg="red") - click.secho(result.stderr, fg="red") - click.secho(result.stdout, fg="red") + kci_err("git command return failed. Error:") + kci_err(result.stderr) + kci_err(result.stdout) sys.exit(1) lines = result.stdout.split(b"\n") if len(lines) < 2: - click.secho(f"git command answer length failed: {lines}", fg="red") + kci_err(f"git command answer length failed: {lines}") sys.exit(1) # is it last bisect?: "is the first bad commit" if "is the first bad commit" in str(lines[1]): @@ -91,7 +93,7 @@ def git_exec_getcommit(cmd): sys.exit(0) re_commit = re.search(r"\[([a-f0-9]+)\]", str(lines[1])) if not re_commit: - click.secho(f"git command regex failed: {lines}", fg="red") + kci_err(f"git command regex failed: {lines}") sys.exit(1) return re_commit.group(1) @@ -110,7 +112,7 @@ def kcidev_exec(cmd): click.echo(line, nl=False) process.wait() except Exception as e: - click.secho(f"Error executing kci-dev: {e}", fg="red") + kci_err(f"Error executing kci-dev: {e}") sys.exit(1) return process @@ -122,11 +124,11 @@ def init_bisect(state): click.secho("init bisect", fg="green") r = os.system("git bisect start") if r != 0: - click.secho("git bisect start failed", fg="red") + kci_err("git bisect start failed") sys.exit(1) r = os.system("git bisect good " + state["good"]) if r != 0: - click.secho("git bisect good failed", fg="red") + kci_err("git bisect good failed") sys.exit(1) # result = subprocess.run(['ls', '-l'], stdout=subprocess.PIPE) cmd = ["git", "bisect", "bad", state["bad"]] @@ -181,7 +183,7 @@ def bisection_loop(state): try: testret = result.returncode except Exception as e: - click.secho(f"Error executing kci-dev, no returncode: {e}", fg="red") + kci_err(f"Error executing kci-dev, no returncode: {e}") sys.exit if testret == 0: bisect_result = "good" @@ -192,13 +194,13 @@ def bisection_loop(state): # TBD: Retry failed test to make sure it is not a flaky test bisect_result = "skip" else: - click.secho("Maestro failed to execute the test", fg="red") + kci_err("Maestro failed to execute the test") # Internal maestro error, retry procesure return None cmd = ["git", "bisect", bisect_result] commitid = git_exec_getcommit(cmd) if not commitid: - click.secho("git bisect failed, commit return is empty", fg="red") + kci_err("git bisect failed, commit return is empty") sys.exit(1) state["history"].append({commit: bisect_result}) state["next_commit"] = commitid @@ -244,25 +246,25 @@ def bisect( if state is None or ignorestate: state = default_state if not giturl: - click.secho("--giturl is required", fg="red") + kci_err("--giturl is required") return if not branch: - click.secho("--branch is required", fg="red") + kci_err("--branch is required") return if not good: - click.secho("--good is required", fg="red") + kci_err("--good is required") return if not bad: - click.secho("--bad is required", fg="red") + kci_err("--bad is required") return if not job_filter: - click.secho("--job_filter is required", fg="red") + kci_err("--job-filter is required") return if not platform_filter: - click.secho("--platform_filter is required", fg="red") + kci_err("--platform-filter is required") return if not test: - click.secho("--test is required", fg="red") + kci_err("--test is required") return state["giturl"] = giturl diff --git a/kcidev/subcommands/checkout.py b/kcidev/subcommands/checkout.py index 12e4f54..6bc6a9c 100644 --- a/kcidev/subcommands/checkout.py +++ b/kcidev/subcommands/checkout.py @@ -21,13 +21,13 @@ def api_connection(host): def display_api_error(response): - click.secho(f"API response error code: {response.status_code}", fg="red") + kci_err(f"API response error code: {response.status_code}") try: - click.secho(response.json(), fg="red") + kci_err(response.json()) except json.decoder.JSONDecodeError: click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow") except Exception as e: - click.secho(f"API response error: {e}: {response.text}", fg="red") + kci_err(f"API response error: {e}: {response.text}") return @@ -50,7 +50,7 @@ def send_checkout_full(baseurl, token, **kwargs): try: response = requests.post(url, headers=headers, data=jdata, timeout=30) except requests.exceptions.RequestException as e: - click.secho(f"API connection error: {e}", fg="red") + kci_err(f"API connection error: {e}") return if response.status_code != 200: @@ -148,10 +148,7 @@ def watch_jobs(baseurl, token, treeid, job_filter, test): # if test is same as job, dont indicate infra-failure if test job fail if test and test != node["name"]: # if we have a test, and prior job failed, we should indicate that - click.secho( - f"Job {node['name']} failed, test can't be executed", - fg="red", - ) + kci_err(f"Job {node['name']} failed, test can't be executed") sys.exit(2) nodeid = node.get("id") click.secho( @@ -175,7 +172,7 @@ def watch_jobs(baseurl, token, treeid, job_filter, test): sys.exit(0) elif test_result: # ignore null, that means result not ready yet - click.secho(f"Test {test} failed: {test_result}", fg="red") + kci_err(f"Test {test} failed: {test_result}") sys.exit(1) click.echo(f"\rRefresh in 30s...", nl=False) @@ -250,13 +247,13 @@ def checkout( job_filter = None click.secho("No job filter defined. All jobs will be triggered!", fg="yellow") if watch and not job_filter: - click.secho("No job filter defined. Can't watch for a job(s)!", fg="red") + kci_err("No job filter defined. Can't watch for a job(s)!") return if test and not watch: - click.secho("Test option only works with watch option", fg="red") + kci_err("Test option only works with watch option") return if not commit and not tipoftree: - click.secho("No commit or tree/branch latest commit defined", fg="red") + kci_err("No commit or tree/branch latest commit defined") return if tipoftree: click.secho( @@ -264,9 +261,7 @@ def checkout( ) commit = retrieve_tot_commit(giturl, branch) if not commit or len(commit) != 40: - click.secho( - "Unable to retrieve latest commit. Wrong tree/branch?", fg="red" - ) + kci_err("Unable to retrieve latest commit. Wrong tree/branch?") return click.secho(f"Commit to checkout: {commit}", fg="green") resp = send_checkout_full( @@ -286,7 +281,7 @@ def checkout( node = resp.get("node") treeid = node.get("treeid") if not treeid: - click.secho("No treeid returned. Can't watch for a job(s)!", fg="red") + kci_err("No treeid returned. Can't watch for a job(s)!") return click.secho(f"Watching for jobs on treeid: {treeid}", fg="green") if test: diff --git a/kcidev/subcommands/maestro_results.py b/kcidev/subcommands/maestro_results.py index 97d138b..95ffde8 100644 --- a/kcidev/subcommands/maestro_results.py +++ b/kcidev/subcommands/maestro_results.py @@ -9,6 +9,8 @@ import toml from git import Repo +from kcidev.libs.common import * + def api_connection(host): click.secho("api connect: " + host, fg="green") @@ -38,10 +40,10 @@ def get_node(url, nodeid, field): try: response.raise_for_status() except requests.exceptions.HTTPError as ex: - click.secho(ex.response.json().get("detail"), fg="red") + kci_err(ex.response.json().get("detail")) return None except Exception as ex: - click.secho(ex, fg="red") + kci_err(ex) return None print_nodes(response.json(), field) @@ -62,10 +64,10 @@ def get_nodes(url, limit, offset, filter, field): try: response.raise_for_status() except requests.exceptions.HTTPError as ex: - click.secho(ex.response.json().get("detail"), fg="red") + kci_err(ex.response.json().get("detail")) return None except Exception as ex: - click.secho(ex, fg="red") + kci_err(ex) return None nodes = response.json() diff --git a/kcidev/subcommands/testretry.py b/kcidev/subcommands/testretry.py index f6a46ae..79e01c5 100644 --- a/kcidev/subcommands/testretry.py +++ b/kcidev/subcommands/testretry.py @@ -16,9 +16,9 @@ def api_connection(host): def display_api_error(response): - click.secho(f"API response error code: {response.status_code}", fg="red") + kci_err(f"API response error code: {response.status_code}") try: - click.secho(response.json(), fg="red") + kci_err(response.json()) except json.decoder.JSONDecodeError: click.secho(f"No JSON response. Plain text: {response.text}", fg="yellow") return @@ -35,7 +35,7 @@ def send_jobretry(baseurl, jobid, token): try: response = requests.post(url, headers=headers, data=jdata) except requests.exceptions.RequestException as e: - click.secho(f"API connection error: {e}", fg="red") + kci_err(f"API connection error: {e}") return if response.status_code != 200: