From 87c384c509b83c0dda9989290885663923255cd7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dennis=20Gl=C3=A4ser?= <dennis.glaeser@iws.uni-stuttgart.de>
Date: Mon, 31 May 2021 08:58:31 +0200
Subject: [PATCH] [bin][runcommand] make it possible to suppress traceback

The commands (e.g. pipeline queries via API) may contain sensible
information that we don't want to display e.g. in the output of jobs
from the GitLab CI
---
 bin/util/common.py | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

diff --git a/bin/util/common.py b/bin/util/common.py
index 1a2596fea7..0fbee3ff4d 100644
--- a/bin/util/common.py
+++ b/bin/util/common.py
@@ -5,23 +5,27 @@ import subprocess
 
 
 # execute a command and retrieve the output
-def runCommand(command):
+def runCommand(command, suppressTraceBack=False, errorMessage=''):
     try:
         return subprocess.run(command,
                               shell=True, check=True,
                               text=True, capture_output=True).stdout
     except Exception:
-        print()
-        print("An error occurred during subprocess run:")
-        print("-- command: {}".format(command))
-        print("-- folder: {}".format(os.getcwd()))
-        print("-- error: {}".format(sys.exc_info()[1]))
-        if "git " in command:
-            print()
-            print("It seems that a git command failed. Please check:\n"
-                  "    -- is the module registered as git repository?\n"
-                  "    -- is upstream defined for the branch?\n")
-        raise
+        if suppressTraceBack:
+            sys.excepthook(Exception, Exception(errorMessage), None)
+        elif not errorMessage:
+            print("An error occurred during subprocess run:")
+            print("-- command: {}".format(command))
+            print("-- folder: {}".format(os.getcwd()))
+            print("-- error: {}".format(sys.exc_info()[1]))
+            if "git " in command:
+                print()
+                print("It seems that a git command failed. Please check:\n"
+                      "    -- is the module registered as git repository?\n"
+                      "    -- is upstream defined for the branch?\n")
+            raise
+        else:
+            raise Exception(errorMessage)
 
 
 # decorator to call function from within the given path
-- 
GitLab