11 ROOTDIR = os.path.abspath(os.path.dirname(__file__))
16 from ctypes import wintypes
17 _GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
18 _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
19 _GetShortPathNameW.restype = wintypes.DWORD
21 def win32_get_short_path_name(long_name):
23 Gets the short path name of a given long path.
24 http://stackoverflow.com/a/23598461/200291
28 output_buf = ctypes.create_unicode_buffer(output_buf_size)
29 needed = _GetShortPathNameW(long_name, output_buf, output_buf_size)
30 if output_buf_size >= needed:
31 return output_buf.value
33 output_buf_size = needed
36 def get_wine_shortpath(winecmd, wine_paths):
38 wine_paths += [p for p in wine_paths if not (p in seen or seen.add(p))]
40 getShortPathScript = '%s.bat' % str(uuid.uuid4()).lower()[:5]
41 with open(getShortPathScript, mode='w') as f:
42 f.write("@ECHO OFF\nfor %%x in (%*) do (\n echo|set /p=;%~sx\n)\n")
45 with open(os.devnull, 'w') as stderr:
46 wine_path = subprocess.check_output(
48 ['cmd', '/C', getShortPathScript] + wine_paths,
49 stderr=stderr).decode('utf-8')
50 except subprocess.CalledProcessError as e:
51 print("Could not get short paths: %s" % e)
52 wine_path = ';'.join(wine_paths)
54 os.remove(getShortPathScript)
55 if len(wine_path) > 2048:
56 raise AssertionError('WINEPATH size {} > 2048'
57 ' this will cause random failure.'.format(
73 from ctypes import windll, byref
74 from ctypes.wintypes import DWORD
76 kernel = windll.kernel32
77 stdout = kernel.GetStdHandle(-11)
79 if not kernel.GetConsoleMode(stdout, byref(mode)):
81 # Try setting ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
82 # If that fails (returns 0), we disable colors
83 return kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON')
87 if not os.isatty(sys.stdout.fileno()):
89 if platform.system().lower() == 'windows':
90 return cls._windows_ansi()
91 return os.environ.get('TERM') != 'dumb'
104 if cls.force_disable:
107 cls.HEADER = '\033[95m'
108 cls.OKBLUE = '\033[94m'
109 cls.OKGREEN = '\033[92m'
110 cls.WARNING = '\033[93m'
111 cls.FAIL = '\033[91m'
116 def git(*args, repository_path='.', fatal=True):
118 ret = subprocess.check_output(["git"] + list(args), cwd=repository_path,
119 stdin=subprocess.DEVNULL,
120 stderr=subprocess.STDOUT).decode()
121 except subprocess.CalledProcessError as e:
124 print("Non-fatal error running git {}:\n{}".format(' '.join(args), e))
128 def accept_command(commands):
129 """Search @commands and returns the first found absolute path."""
130 for command in commands:
131 command = shutil.which(command)
137 meson = os.path.join(ROOTDIR, 'meson', 'meson.py')
138 if os.path.exists(meson):
139 return [sys.executable, meson]
141 mesonintrospect = os.environ.get('MESONINTROSPECT', '')
142 for comp in shlex.split (mesonintrospect):
143 # mesonintrospect might look like "/usr/bin/python /somewhere/meson introspect",
144 # let's not get tricked
145 if 'python' in os.path.basename (comp):
147 if os.path.exists(comp):
148 if comp.endswith('.py'):
149 return [sys.executable, comp]
153 meson = accept_command(['meson.py'])
155 return [sys.executable, meson]
156 meson = accept_command(['meson'])
159 raise RuntimeError('Could not find Meson')