nle: composition: Avoid running query before being constructed
[platform/upstream/gstreamer.git] / scripts / common.py
index 5cea742..ec0cc70 100644 (file)
@@ -5,12 +5,13 @@ import shutil
 import argparse
 import platform
 import subprocess
+import uuid
 
 
 ROOTDIR = os.path.abspath(os.path.dirname(__file__))
 
 
-if os.name is 'nt':
+if os.name == 'nt':
     import ctypes
     from ctypes import wintypes
     _GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
@@ -31,6 +32,33 @@ def win32_get_short_path_name(long_name):
         else:
             output_buf_size = needed
 
+
+def get_wine_shortpath(winecmd, wine_paths):
+    seen = set()
+    wine_paths += [p for p in wine_paths if not (p in seen or seen.add(p))]
+
+    getShortPathScript = '%s.bat' % str(uuid.uuid4()).lower()[:5]
+    with open(getShortPathScript, mode='w') as f:
+        f.write("@ECHO OFF\nfor %%x in (%*) do (\n echo|set /p=;%~sx\n)\n")
+        f.flush()
+    try:
+        with open(os.devnull, 'w') as stderr:
+            wine_path = subprocess.check_output(
+                winecmd +
+                ['cmd', '/C', getShortPathScript] + wine_paths,
+                stderr=stderr).decode('utf-8')
+    except subprocess.CalledProcessError as e:
+        print("Could not get short paths: %s" % e)
+        wine_path = ';'.join(wine_paths)
+    finally:
+        os.remove(getShortPathScript)
+    if len(wine_path) > 2048:
+        raise AssertionError('WINEPATH size {} > 2048'
+                                ' this will cause random failure.'.format(
+                                    len(wine_path)))
+    return wine_path
+
+
 class Colors:
     HEADER = '\033[95m'
     OKBLUE = '\033[94m'
@@ -85,10 +113,17 @@ class Colors:
 
 
 
-def git(*args, repository_path='.'):
-    return subprocess.check_output(["git"] + list(args), cwd=repository_path,
-                                   stdin=subprocess.DEVNULL,
-                                   stderr=subprocess.STDOUT).decode()
+def git(*args, repository_path='.', fatal=True):
+    try:
+        ret = subprocess.check_output(["git"] + list(args), cwd=repository_path,
+                                      stdin=subprocess.DEVNULL,
+                                      stderr=subprocess.STDOUT).decode()
+    except subprocess.CalledProcessError as e:
+        if fatal:
+            raise e
+        print("Non-fatal error running git {}:\n{}".format(' '.join(args), e))
+        return None
+    return ret
 
 def accept_command(commands):
     """Search @commands and returns the first found absolute path."""