gst-uninstalled: Fix compatibility with meson 0.50
[platform/upstream/gstreamer.git] / common.py
1 import os
2 import sys
3 import shlex
4 import shutil
5 import argparse
6 import platform
7 import subprocess
8
9
10 ROOTDIR = os.path.abspath(os.path.dirname(__file__))
11
12
13 class Colors:
14     HEADER = '\033[95m'
15     OKBLUE = '\033[94m'
16     OKGREEN = '\033[92m'
17     WARNING = '\033[93m'
18     FAIL = '\033[91m'
19     ENDC = '\033[0m'
20
21     force_disable = False
22
23     def _windows_ansi():
24         from ctypes import windll, byref
25         from ctypes.wintypes import DWORD
26
27         kernel = windll.kernel32
28         stdout = kernel.GetStdHandle(-11)
29         mode = DWORD()
30         if not kernel.GetConsoleMode(stdout, byref(mode)):
31             return False
32         # Try setting ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
33         # If that fails (returns 0), we disable colors
34         return kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON')
35
36     @classmethod
37     def can_enable(cls):
38         if not os.isatty(sys.stdout.fileno()):
39             return False
40         if platform.system().lower() == 'windows':
41             return cls._windows_ansi()
42         return os.environ.get('TERM') != 'dumb'
43
44     @classmethod
45     def disable(cls):
46         cls.HEADER = ''
47         cls.OKBLUE = ''
48         cls.OKGREEN = ''
49         cls.WARNING = ''
50         cls.FAIL = ''
51         cls.ENDC = ''
52
53     @classmethod
54     def enable(cls):
55         if cls.force_disable:
56             return
57
58         cls.HEADER = '\033[95m'
59         cls.OKBLUE = '\033[94m'
60         cls.OKGREEN = '\033[92m'
61         cls.WARNING = '\033[93m'
62         cls.FAIL = '\033[91m'
63         cls.ENDC = '\033[0m'
64
65
66
67 def git(*args, repository_path='.'):
68     return subprocess.check_output(["git"] + list(args), cwd=repository_path,
69                                    stdin=subprocess.DEVNULL,
70                                    stderr=subprocess.STDOUT).decode()
71
72 def accept_command(commands):
73     """Search @commands and returns the first found absolute path."""
74     for command in commands:
75         command = shutil.which(command)
76         if command:
77             return command
78     return None
79
80 def get_meson():
81     meson = os.path.join(ROOTDIR, 'meson', 'meson.py')
82     if os.path.exists(meson):
83         return [sys.executable, meson]
84
85     mesonintrospect = os.environ.get('MESONINTROSPECT', '')
86     for comp in shlex.split (mesonintrospect):
87         # mesonintrospect might look like "/usr/bin/python /somewhere/meson introspect",
88         # let's not get tricked
89         if 'python' in os.path.basename (comp):
90             continue
91         if os.path.exists(comp):
92             if comp.endswith('.py'):
93                 return [sys.executable, comp]
94             else:
95                 return [comp]
96
97     meson = accept_command(['meson.py'])
98     if meson:
99         return [sys.executable, meson]
100     meson = accept_command(['meson'])
101     if meson:
102         return [meson]
103     raise RuntimeError('Could not find Meson')