From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 36EDFC433EF for ; Mon, 10 Jan 2022 03:15:45 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id BB8B8830B7; Mon, 10 Jan 2022 04:15:23 +0100 (CET) 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="SJPpHlX0"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 685C181879; Mon, 10 Jan 2022 04:15:17 +0100 (CET) Received: from mail-il1-x12d.google.com (mail-il1-x12d.google.com [IPv6:2607:f8b0:4864:20::12d]) (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 9B58381B53 for ; Mon, 10 Jan 2022 04:15:11 +0100 (CET) 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-x12d.google.com with SMTP id h30so280161ila.12 for ; Sun, 09 Jan 2022 19:15:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=5RHCSj+cNEsjFqqaXlfoV1mC25DRek5dEEYzyuUIwEc=; b=SJPpHlX0dt0TDBn3VJy9M/9E6S2OY15q7+21zplFhvvcw1rbnFnxo/SRGXrWNWYOEp TNbL480Omp2sz87hsEuAaxwitYHWprq6ciUqNrO1EwjzPyXmjJNrj9RQdmwMxO+kmE+L LEXpjvvo8PcSpB5ZlQUriDlmHvzb+IupabaBQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=5RHCSj+cNEsjFqqaXlfoV1mC25DRek5dEEYzyuUIwEc=; b=7/0SOT4jX1JAiBCFFwGr5Z6q90TZCA9ECk/PJ2GoK0TECJX6JaJd5FgQ9fSdv3WpCX MO8YZhwmkjcfr9zNoroHosqESkF4kxFbcVdafM2O7ev3CehLntA3EEf7nzvPAaskDxpU fOvXj+Lv0u/cFO+rcCmawQhLxqsRTQKovhulG0Br6nlD6L1fylZe3D+P2VVU5ms+NIrF 2I+Bw6Y1i8ZaX3xnRjoHfkiAOOZVBJhOnIh1tG7pXlsVxAEa2BZjmrJT+dvy5+wK57JT n1/d7ZFVnk6KdT/iYogAB5yLY32hBSnR35jWoBUXLgzkKJUJs/LuvQK1fblLnWiRgAEB PW4g== X-Gm-Message-State: AOAM530sXJLMbzz4ooxLbZFRoFiuW+KG5PbmQHkJZCSSE2udc2GsBTK5 Elot511AZnmgyZFfPboGDJB2yXZq7g5VKA== X-Google-Smtp-Source: ABdhPJw4aqZ9MOfT+93Y+VX2ThtXwlrpLkdiYBTGopWkipReSGdJt+dnzleZiGUM8zpIEBBJjYjhWA== X-Received: by 2002:a05:6e02:1114:: with SMTP id u20mr1873286ilk.33.1641784510269; Sun, 09 Jan 2022 19:15:10 -0800 (PST) Received: from sjg1.roam.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id d16sm3542182ioy.29.2022.01.09.19.15.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Jan 2022 19:15:10 -0800 (PST) From: Simon Glass To: U-Boot Mailing List Cc: Simon Glass Subject: [PATCH v2 05/38] patman: Allow running a tool and returning the full result Date: Sun, 9 Jan 2022 20:13:40 -0700 Message-Id: <20220110031413.1970836-6-sjg@chromium.org> X-Mailer: git-send-email 2.34.1.575.g55b058a8bb-goog In-Reply-To: <20220110031413.1970836-1-sjg@chromium.org> References: <20220110031413.1970836-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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.2 at phobos.denx.de X-Virus-Status: Clean Add a new function which returns the entire result from running a tool, not just stdout. Update Run() to use this and to return stdout on error, if stderr is empty, since some unfortunate tools write their error output to stdout rather than stderr. Move building of the PATH to a separate function. Make the exception catching more specific, to catch just ValueError, since broad exceptions are a pain to debug. Signed-off-by: Simon Glass --- (no changes since v1) tools/patman/tools.py | 56 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/tools/patman/tools.py b/tools/patman/tools.py index 86c4f616206..7af4a52a8fd 100644 --- a/tools/patman/tools.py +++ b/tools/patman/tools.py @@ -313,7 +313,22 @@ def GetTargetCompileTool(name, cross_compile=None): target_name = name return target_name, extra_args -def Run(name, *args, **kwargs): +def get_env_with_path(): + """Get an updated environment with the PATH variable set correctly + + If there are any search paths set, these need to come first in the PATH so + that these override any other version of the tools. + + Returns: + dict: New environment with PATH updated, or None if there are not search + paths + """ + if tool_search_paths: + env = dict(os.environ) + env['PATH'] = ':'.join(tool_search_paths) + ':' + env['PATH'] + return env + +def run_result(name, *args, **kwargs): """Run a tool with some arguments This runs a 'tool', which is a program used by binman to process files and @@ -326,6 +341,7 @@ def Run(name, *args, **kwargs): for_host: True to resolve the command to the version for the host for_target: False to run the command as-is, without resolving it to the version for the compile target + raise_on_error: Raise an error if the command fails (True by default) Returns: CommandResult object @@ -334,10 +350,8 @@ def Run(name, *args, **kwargs): binary = kwargs.get('binary') for_host = kwargs.get('for_host', False) for_target = kwargs.get('for_target', not for_host) - env = None - if tool_search_paths: - env = dict(os.environ) - env['PATH'] = ':'.join(tool_search_paths) + ':' + env['PATH'] + raise_on_error = kwargs.get('raise_on_error', True) + env = get_env_with_path() if for_target: name, extra_args = GetTargetCompileTool(name) args = tuple(extra_args) + args @@ -349,11 +363,12 @@ def Run(name, *args, **kwargs): result = command.RunPipe([all_args], capture=True, capture_stderr=True, env=env, raise_on_error=False, binary=binary) if result.return_code: - raise ValueError("Error %d running '%s': %s" % - (result.return_code,' '.join(all_args), - result.stderr)) - return result.stdout - except: + if raise_on_error: + raise ValueError("Error %d running '%s': %s" % + (result.return_code,' '.join(all_args), + result.stderr or result.stdout)) + return result + except ValueError: if env and not PathHasFile(env['PATH'], name): msg = "Please install tool '%s'" % name package = packages.get(name) @@ -362,6 +377,27 @@ def Run(name, *args, **kwargs): raise ValueError(msg) raise +def Run(name, *args, **kwargs): + """Run a tool with some arguments + + This runs a 'tool', which is a program used by binman to process files and + perhaps produce some output. Tools can be located on the PATH or in a + search path. + + Args: + name: Command name to run + args: Arguments to the tool + for_host: True to resolve the command to the version for the host + for_target: False to run the command as-is, without resolving it + to the version for the compile target + + Returns: + CommandResult object + """ + result = run_result(name, *args, **kwargs) + if result is not None: + return result.stdout + def Filename(fname): """Resolve a file path to an absolute path. -- 2.34.1.575.g55b058a8bb-goog