From 8cf2c50e7774a571528b7044bdfdc5d3a8ebbd2a Mon Sep 17 00:00:00 2001 From: Sergi Blanch Torne Date: Mon, 29 Dec 2025 22:31:21 +0100 Subject: [PATCH] ci,crnm: enhancement within a GitLab job MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit After the migration to use the rich library, the execution within a GitLab job gets dirty: coloring disappeared, the number of columns defaults to 80, and job name padding fails with a broken link in the lines.  This is only a specialization when the tool detects it is running within a job, without changing the behavior in the usual usage of this tool. Signed-off-by: Sergi Blanch Torne Part-of: --- bin/ci/ci_run_n_monitor.py | 13 ++++++++++--- bin/ci/gitlab_gql.py | 7 +++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/bin/ci/ci_run_n_monitor.py b/bin/ci/ci_run_n_monitor.py index 76e112e39af..268d51d963c 100755 --- a/bin/ci/ci_run_n_monitor.py +++ b/bin/ci/ci_run_n_monitor.py @@ -20,6 +20,7 @@ from collections import defaultdict, Counter from concurrent.futures import ThreadPoolExecutor from functools import partial from itertools import chain +from os import getenv from subprocess import check_output, CalledProcessError from typing import Callable, Dict, TYPE_CHECKING, Iterable, Literal, Optional, Tuple, cast @@ -56,7 +57,10 @@ STATUS_COLORS = defaultdict(lambda: "", { COMPLETED_STATUSES = frozenset({"success", "failed"}) RUNNING_STATUSES = frozenset({"created", "pending", "running"}) -console = Console(highlight=False) +if getenv("CI_JOB_ID"): + console = Console(highlight=False, no_color=False, color_system="truecolor", width=120) +else: + console = Console(highlight=False) print = console.print @@ -405,7 +409,7 @@ def print_log( # GitLab's REST API doesn't offer pagination for logs, so we have to refetch it all lines = job.trace().decode().splitlines() for line in lines[printed_lines:]: - print(line) + print(line, markup=False) printed_lines = len(lines) if job.status in COMPLETED_STATUSES: @@ -637,7 +641,10 @@ def __job_duration_record(dict_item: tuple) -> str: def link2print(url: str, text: str, text_pad: int = 0) -> str: text = str(text) text_pad = len(text) if text_pad < 1 else text_pad - return f"[link={url}]{text:{text_pad}}[/link]" + if console.is_terminal: + return f"[link={url}]{text:{text_pad}}[/link]" + else: + return f"{text:{text_pad}}" def main() -> None: diff --git a/bin/ci/gitlab_gql.py b/bin/ci/gitlab_gql.py index 5eb901184d8..d9d1f8cd096 100755 --- a/bin/ci/gitlab_gql.py +++ b/bin/ci/gitlab_gql.py @@ -10,7 +10,7 @@ from collections import OrderedDict from copy import deepcopy from dataclasses import dataclass, field from itertools import accumulate -from os import get_terminal_size +from os import get_terminal_size, getenv from pathlib import Path from subprocess import check_output from textwrap import dedent @@ -41,7 +41,10 @@ Dag = dict[str, DagNode] StageSeq = OrderedDict[str, set[str]] -console = Console(highlight=False) +if getenv("CI_JOB_ID"): + console = Console(highlight=False, no_color=False, color_system="truecolor", width=120) +else: + console = Console(highlight=False) print = console.print