scripts: Add a script to check that all repos are clean
[platform/upstream/gstreamer.git] / scripts / 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 if os.name is 'nt':
14     import ctypes
15     from ctypes import wintypes
16     _GetShortPathNameW = ctypes.windll.kernel32.GetShortPathNameW
17     _GetShortPathNameW.argtypes = [wintypes.LPCWSTR, wintypes.LPWSTR, wintypes.DWORD]
18     _GetShortPathNameW.restype = wintypes.DWORD
19
20 def win32_get_short_path_name(long_name):
21     """
22     Gets the short path name of a given long path.
23     http://stackoverflow.com/a/23598461/200291
24     """
25     output_buf_size = 0
26     while True:
27         output_buf = ctypes.create_unicode_buffer(output_buf_size)
28         needed = _GetShortPathNameW(long_name, output_buf, output_buf_size)
29         if output_buf_size >= needed:
30             return output_buf.value
31         else:
32             output_buf_size = needed
33
34 class Colors:
35     HEADER = '\033[95m'
36     OKBLUE = '\033[94m'
37     OKGREEN = '\033[92m'
38     WARNING = '\033[93m'
39     FAIL = '\033[91m'
40     ENDC = '\033[0m'
41
42     force_disable = False
43
44     def _windows_ansi():
45         from ctypes import windll, byref
46         from ctypes.wintypes import DWORD
47
48         kernel = windll.kernel32
49         stdout = kernel.GetStdHandle(-11)
50         mode = DWORD()
51         if not kernel.GetConsoleMode(stdout, byref(mode)):
52             return False
53         # Try setting ENABLE_VIRTUAL_TERMINAL_PROCESSING (0x4)
54         # If that fails (returns 0), we disable colors
55         return kernel.SetConsoleMode(stdout, mode.value | 0x4) or os.environ.get('ANSICON')
56
57     @classmethod
58     def can_enable(cls):
59         if not os.isatty(sys.stdout.fileno()):
60             return False
61         if platform.system().lower() == 'windows':
62             return cls._windows_ansi()
63         return os.environ.get('TERM') != 'dumb'
64
65     @classmethod
66     def disable(cls):
67         cls.HEADER = ''
68         cls.OKBLUE = ''
69         cls.OKGREEN = ''
70         cls.WARNING = ''
71         cls.FAIL = ''
72         cls.ENDC = ''
73
74     @classmethod
75     def enable(cls):
76         if cls.force_disable:
77             return
78
79         cls.HEADER = '\033[95m'
80         cls.OKBLUE = '\033[94m'
81         cls.OKGREEN = '\033[92m'
82         cls.WARNING = '\033[93m'
83         cls.FAIL = '\033[91m'
84         cls.ENDC = '\033[0m'
85
86
87
88 def git(*args, repository_path='.'):
89     return subprocess.check_output(["git"] + list(args), cwd=repository_path,
90                                    stdin=subprocess.DEVNULL,
91                                    stderr=subprocess.STDOUT).decode()
92
93 def accept_command(commands):
94     """Search @commands and returns the first found absolute path."""
95     for command in commands:
96         command = shutil.which(command)
97         if command:
98             return command
99     return None
100
101 def get_meson():
102     meson = os.path.join(ROOTDIR, 'meson', 'meson.py')
103     if os.path.exists(meson):
104         return [sys.executable, meson]
105
106     mesonintrospect = os.environ.get('MESONINTROSPECT', '')
107     for comp in shlex.split (mesonintrospect):
108         # mesonintrospect might look like "/usr/bin/python /somewhere/meson introspect",
109         # let's not get tricked
110         if 'python' in os.path.basename (comp):
111             continue
112         if os.path.exists(comp):
113             if comp.endswith('.py'):
114                 return [sys.executable, comp]
115             else:
116                 return [comp]
117
118     meson = accept_command(['meson.py'])
119     if meson:
120         return [sys.executable, meson]
121     meson = accept_command(['meson'])
122     if meson:
123         return [meson]
124     raise RuntimeError('Could not find Meson')