From 2712c240b4bd8c47baa1e1023f671e4536a7963c Mon Sep 17 00:00:00 2001 From: Frederik Dyrmose Date: Sun, 22 Oct 2023 16:58:36 +0200 Subject: [PATCH 1/2] Fix error with stdout and stderr together in shell command when runnign git_date update --- ci/ext.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/ci/ext.py b/ci/ext.py index 20e954292..8c9e69356 100644 --- a/ci/ext.py +++ b/ci/ext.py @@ -39,8 +39,7 @@ import textwrap import time import xbuild - - +import typing #------------------------------------------------------------------------------- # Version handling #------------------------------------------------------------------------------- @@ -439,15 +438,40 @@ def get_meta(): #------------------------------------------------------------------------------- # Build info file #------------------------------------------------------------------------------- - -def shell_cmd(cmd, strict=False): +def shell_cmd( + cmd: typing.List[str], + strict: bool = False, +) -> str: try: - return subprocess.check_output(cmd, universal_newlines=True, - stderr=subprocess.STDOUT).strip() - except subprocess.CalledProcessError as e: + process = subprocess.Popen( + cmd, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + universal_newlines=True + ) + stdout, stderr = process.communicate() + + if process.returncode != 0: + # Command failed + if strict: + error_message = ( + f"Command `{' '.join(cmd)}` failed with code " + f"{process.returncode}: {stderr}" + ) + raise SystemExit(error_message) + else: + return "" + + # If the command succeeds, return stdout + return stdout.strip() + + except Exception as e: if strict: - raise SystemExit("Command `%s` failed with code %d: %s" - % (" ".join(cmd), e.returncode, e.output)) + error_message = ( + f"Command `{' '.join(cmd)}` encountered an error: {str(e)}" + ) + raise SystemExit(error_message) + return "" @@ -485,6 +509,7 @@ def generate_build_info(mode=None, strict=False): strict=strict) git_diff = shell_cmd(["git", "diff", "HEAD", "--stat", "--no-color"], strict=strict) + # Reformat the `git_date` as a UTC time string if git_date: git_date = time.strftime("%Y-%m-%d %H:%M:%S", From 192daad2ec4dad9d369da876c6b5099f5a6a67f9 Mon Sep 17 00:00:00 2001 From: Frederik Dyrmose Date: Sun, 22 Oct 2023 17:47:10 +0200 Subject: [PATCH 2/2] Ensure that CalledProcessError is handled correctly --- ci/ext.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ci/ext.py b/ci/ext.py index 8c9e69356..d8a596dcc 100644 --- a/ci/ext.py +++ b/ci/ext.py @@ -464,7 +464,8 @@ def shell_cmd( # If the command succeeds, return stdout return stdout.strip() - + except subprocess.CalledProcessError as e: + return "" except Exception as e: if strict: error_message = (