From patchwork Thu Aug 10 16:31:53 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1819968 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.a=rsa-sha256 header.s=google header.b=DiReLvbt; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384)) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RMCH95TSFz1yYl for ; Fri, 11 Aug 2023 02:36:21 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 3D0A186962; Thu, 10 Aug 2023 18:32:43 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="DiReLvbt"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 154EC86907; Thu, 10 Aug 2023 18:32:41 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-il1-x12e.google.com (mail-il1-x12e.google.com [IPv6:2607:f8b0:4864:20::12e]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id CF5F08666E for ; Thu, 10 Aug 2023 18:32:38 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-il1-x12e.google.com with SMTP id e9e14a558f8ab-3498811fcf8so3805675ab.3 for ; Thu, 10 Aug 2023 09:32:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1691685157; x=1692289957; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rSJI/9zKCBcwxpsdtFt9IisdpARy2QNg419hvhKr1uM=; b=DiReLvbttrN4eQMFHG9J8yg7Zm+MYOY5EHcBTn+attTEnAXKRrvyBBEzyIproipB0W svn11YWdoidVOBU3XiCHMhC2+D0c5fifniOR3HaDR+q+Nam2sy+SMYVDO2QK+/wxnFYP FvLo0xyWOL4quTSk3Hek/sTm697j8muWQH7Wk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691685157; x=1692289957; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rSJI/9zKCBcwxpsdtFt9IisdpARy2QNg419hvhKr1uM=; b=JCYnieX6JSY3FUkYf6GcJq5lmwxaP/pR/6wn7zaNqfFDbJXyMIeJb3VXfFqI2CUbIa YTEYGZxaASUQxxQKPhmnNdhujiijzr9xaIBEBognkTpAJC+J8Qy2qwmgb6rnNZx9GS0B 1FPCe2JLv0nggYvG6PI610Uf1jO0JuAKwLjK0mIcXBa4CUTw58IuVRmhens9xW0JWJX+ mUoFokIyrkcFLAOYald4AXpsaSHcLoxSd0jukrKWLHkDjQRhevT5axyn8+9Xu6Jsl56q K3SHCD15fZX3w9f8D3NTI47/y3EMHJXLU9W90dnwCPCus6EV8JN/khTdKGAFeKmqcPpg zgWA== X-Gm-Message-State: AOJu0Yxrl6mONaM8qGWDmgfKzuLIvC+z1OPfCpdHd59MYOLBrUEnJNht EC0rV/rdxqV2o0te1XaoylZQlgEoBwneTK0p5UM= X-Google-Smtp-Source: AGHT+IHU5QJMBe0cNaHZCk0S1Uem3s5Iw/zbJVP2L3Sc7toXKb8L1198vdgqK1qbUELChbYnl/3QXg== X-Received: by 2002:a05:6e02:1cac:b0:349:650c:7226 with SMTP id x12-20020a056e021cac00b00349650c7226mr4047173ill.25.1691685157403; Thu, 10 Aug 2023 09:32:37 -0700 (PDT) Received: from kea.bld.corp.google.com ([2620:15c:183:200:e0a2:950d:33f0:fddc]) by smtp.gmail.com with ESMTPSA id u21-20020a02aa95000000b0042b28813816sm487309jai.14.2023.08.10.09.32.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 10 Aug 2023 09:32:37 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Tom Rini , Masahiro Yamada , Simon Glass Subject: [PATCH 21/28] moveconfig: Use u_boot_pylib for terminal colour Date: Thu, 10 Aug 2023 10:31:53 -0600 Message-ID: <20230810163212.2368374-22-sjg@chromium.org> X-Mailer: git-send-email 2.41.0.640.ga95def55d0-goog In-Reply-To: <20230810163212.2368374-1-sjg@chromium.org> References: <20230810163212.2368374-1-sjg@chromium.org> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean Use the existing terminal code to handle ANSI colours. Enable colour by default if the output is going to a terminal. Signed-off-by: Simon Glass --- tools/moveconfig.py | 92 +++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 54 deletions(-) diff --git a/tools/moveconfig.py b/tools/moveconfig.py index f4fb3f46b81..30d8489f7e3 100755 --- a/tools/moveconfig.py +++ b/tools/moveconfig.py @@ -33,6 +33,7 @@ import asteval from buildman import bsettings from buildman import kconfiglib from buildman import toolchain +from u_boot_pylib import terminal SHOW_GNU_MAKE = 'scripts/show-gnu-make' SLEEP_TIME=0.03 @@ -42,23 +43,6 @@ STATE_DEFCONFIG = 1 STATE_AUTOCONF = 2 STATE_SAVEDEFCONFIG = 3 -COLOR_BLACK = '0;30' -COLOR_RED = '0;31' -COLOR_GREEN = '0;32' -COLOR_BROWN = '0;33' -COLOR_BLUE = '0;34' -COLOR_PURPLE = '0;35' -COLOR_CYAN = '0;36' -COLOR_LIGHT_GRAY = '0;37' -COLOR_DARK_GRAY = '1;30' -COLOR_LIGHT_RED = '1;31' -COLOR_LIGHT_GREEN = '1;32' -COLOR_YELLOW = '1;33' -COLOR_LIGHT_BLUE = '1;34' -COLOR_LIGHT_PURPLE = '1;35' -COLOR_LIGHT_CYAN = '1;36' -COLOR_WHITE = '1;37' - AUTO_CONF_PATH = 'include/config/auto.conf' CONFIG_DATABASE = 'moveconfig.db' @@ -189,15 +173,6 @@ def get_all_defconfigs(): return defconfigs -def color_text(color_enabled, color, string): - """Return colored string.""" - if color_enabled: - # LF should not be surrounded by the escape sequence. - # Otherwise, additional whitespace or line-feed might be printed. - return '\n'.join([ '\033[' + color + 'm' + s + '\033[0m' if s else '' - for s in string.split('\n') ]) - return string - def write_file(fname, data): """Write data to a file @@ -385,8 +360,8 @@ class Slot: for faster processing. """ - def __init__(self, toolchains, args, progress, devnull, - make_cmd, reference_src_dir, db_queue): + def __init__(self, toolchains, args, progress, devnull, make_cmd, + reference_src_dir, db_queue, col): """Create a new process slot. Args: @@ -398,6 +373,7 @@ class Slot: reference_src_dir: Determine the true starting config state from this source tree. db_queue: output queue to write config info for the database + col (terminal.Color): Colour object """ self.toolchains = toolchains self.args = args @@ -407,6 +383,7 @@ class Slot: self.make_cmd = (make_cmd, 'O=' + self.build_dir) self.reference_src_dir = reference_src_dir self.db_queue = db_queue + self.col = col self.parser = KconfigParser(args, self.build_dir) self.state = STATE_IDLE self.failed_boards = set() @@ -499,12 +476,11 @@ class Slot: def handle_error(self): """Handle error cases.""" - self.log.append(color_text(self.args.color, COLOR_LIGHT_RED, - 'Failed to process')) + self.log.append(self.col.build(self.col.RED, 'Failed to process', + bright=True)) if self.args.verbose: for line in self.proc.stderr.read().decode().splitlines(): - self.log.append(color_text(self.args.color, COLOR_LIGHT_CYAN, - line)) + self.log.append(self.col.build(self.col.CYAN, line, True)) self.finish(False) def do_defconfig(self): @@ -524,8 +500,9 @@ class Slot: try: tchain = self.toolchains.Select(arch) except ValueError: - self.log.append(color_text(self.args.color, COLOR_YELLOW, - f"Tool chain for '{arch}' is missing: do nothing")) + self.log.append(self.col.build( + self.col.YELLOW, + f"Tool chain for '{arch}' is missing: do nothing")) self.finish(False) return env = tchain.MakeEnvironment(False) @@ -567,8 +544,8 @@ class Slot: updated = not filecmp.cmp(orig_defconfig, new_defconfig) if updated: - self.log.append(color_text(self.args.color, COLOR_LIGHT_BLUE, - 'defconfig updated')) + self.log.append( + self.col.build(self.col.BLUE, 'defconfig updated', bright=True)) if not self.args.dry_run and updated: shutil.move(new_defconfig, orig_defconfig) @@ -614,24 +591,27 @@ class Slots: """Controller of the array of subprocess slots.""" - def __init__(self, toolchains, args, progress, reference_src_dir, db_queue): + def __init__(self, toolchains, args, progress, reference_src_dir, db_queue, + col): """Create a new slots controller. Args: - toolchains: Toolchains object containing toolchains. - args: Program arguments - progress: A progress indicator. - reference_src_dir: Determine the true starting config state from this - source tree. - db_queue: output queue to write config info for the database + toolchains (Toolchains): Toolchains object containing toolchains + args (Namespace): Program arguments + progress (Progress): A progress indicator. + reference_src_dir (str): Determine the true starting config state + from this source tree (None for none) + db_queue (Queue): output queue to write config info for the database + col (terminal.Color): Colour object """ self.args = args self.slots = [] + self.col = col devnull = subprocess.DEVNULL make_cmd = get_make_cmd() for _ in range(args.jobs): self.slots.append(Slot(toolchains, args, progress, devnull, - make_cmd, reference_src_dir, db_queue)) + make_cmd, reference_src_dir, db_queue, col)) def add(self, defconfig): """Add a new subprocess if a vacant slot is found. @@ -683,8 +663,8 @@ class Slots: msg = 'The following boards were not processed due to error:\n' msg += boards msg += f'(the list has been saved in {output_file})\n' - print(color_text(self.args.color, COLOR_LIGHT_RED, - msg), file=sys.stderr) + print(self.col.build(self.col.RED, msg, bright=True), + file=sys.stderr) write_file(output_file, boards) @@ -723,13 +703,14 @@ class ReferenceSource: return self.src_dir -def move_config(toolchains, args, db_queue): +def move_config(toolchains, args, db_queue, col): """Build database or sync config options to defconfig files. Args: - toolchains (Toolchains): Toolchains to use - args (Namespace): Program arguments - db_queue (Queue): Queue for database updates + toolchains (Toolchains): Toolchains to use + args (Namespace): Program arguments + db_queue (Queue): Queue for database updates + col (terminal.Color): Colour object """ if args.force_sync: print('Syncing defconfigs', end=' ') @@ -749,7 +730,7 @@ def move_config(toolchains, args, db_queue): defconfigs = get_all_defconfigs() progress = Progress(len(defconfigs)) - slots = Slots(toolchains, args, progress, reference_src_dir, db_queue) + slots = Slots(toolchains, args, progress, reference_src_dir, db_queue, col) # Main loop to process defconfig files: # Add a new subprocess into a vacant slot. @@ -1495,8 +1476,8 @@ doc/develop/moveconfig.rst for documentation.''' 'implying others') parser.add_argument('-b', '--build-db', action='store_true', default=False, help='build a CONFIG database') - parser.add_argument('-c', '--color', action='store_true', default=False, - help='display the log in color') + parser.add_argument('-C', '--nocolour', action='store_true', default=False, + help="don't display the log in colour") parser.add_argument('-d', '--defconfigs', type=str, help='a file containing a list of defconfigs to move, ' "one per line (for example 'snow_defconfig') " @@ -1541,6 +1522,9 @@ doc/develop/moveconfig.rst for documentation.''' return 1 unittest.main() + col = terminal.Color(terminal.COLOR_NEVER if args.nocolour + else terminal.COLOR_IF_TERMINAL) + if args.scan_source: do_scan_source(os.getcwd(), args.update) return 0 @@ -1591,7 +1575,7 @@ doc/develop/moveconfig.rst for documentation.''' toolchains = toolchain.Toolchains() toolchains.GetSettings() toolchains.Scan(verbose=False) - move_config(toolchains, args, db_queue) + move_config(toolchains, args, db_queue, col) db_queue.join() if args.build_db: