scripts: Fix missing import in common.py
[platform/upstream/gstreamer.git] / common.py
1 import argparse
2 import os
3 import sys
4 import shutil
5 import subprocess
6 import shlex
7
8
9 ROOTDIR = os.path.abspath(os.path.dirname(__file__))
10
11
12 class Colors:
13     HEADER = '\033[95m'
14     OKBLUE = '\033[94m'
15     OKGREEN = '\033[92m'
16     WARNING = '\033[93m'
17     FAIL = '\033[91m'
18     ENDC = '\033[0m'
19
20     force_disable = False
21
22     @classmethod
23     def disable(cls):
24         cls.HEADER = ''
25         cls.OKBLUE = ''
26         cls.OKGREEN = ''
27         cls.WARNING = ''
28         cls.FAIL = ''
29         cls.ENDC = ''
30
31     @classmethod
32     def enable(cls):
33         if cls.force_disable:
34             return
35
36         cls.HEADER = '\033[95m'
37         cls.OKBLUE = '\033[94m'
38         cls.OKGREEN = '\033[92m'
39         cls.WARNING = '\033[93m'
40         cls.FAIL = '\033[91m'
41         cls.ENDC = '\033[0m'
42
43
44
45 def git(*args, repository_path='.'):
46     return subprocess.check_output(["git"] + list(args), cwd=repository_path,
47                                    stderr=subprocess.STDOUT).decode()
48
49 def accept_command(commands):
50     """Search @commands and returns the first found absolute path."""
51     for command in commands:
52         command = shutil.which(command)
53         if command:
54             return command
55     return None
56
57 def get_meson():
58     meson = os.path.join(ROOTDIR, 'meson', 'meson.py')
59     if os.path.exists(meson):
60         return [sys.executable, meson]
61
62     mesonintrospect = os.environ.get('MESONINTROSPECT', '')
63     for comp in shlex.split (mesonintrospect):
64         # mesonintrospect might look like "/usr/bin/python /somewhere/meson introspect",
65         # let's not get tricked
66         if 'python' in os.path.basename (comp):
67             continue
68         if os.path.exists(comp):
69             if comp.endswith('.py'):
70                 return [sys.executable, comp]
71             else:
72                 return [comp]
73
74     meson = accept_command(['meson.py'])
75     if meson:
76         return [sys.executable, meson]
77     meson = accept_command(['meson'])
78     if meson:
79         return [meson]
80     raise RuntimeError('Could not find Meson')