uninstalled: Also support GES overrides
[platform/upstream/gstreamer.git] / gst-uninstalled.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import contextlib
5 import json
6 import os
7 import platform
8 import re
9 import site
10 import shutil
11 import subprocess
12 import sys
13 import tempfile
14
15 from distutils.sysconfig import get_python_lib
16
17 from common import get_meson
18
19 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
20 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
21 # Use '_build' as the builddir instead of 'build'
22 DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, 'build')
23 if not os.path.exists(DEFAULT_BUILDDIR):
24     DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, '_build')
25
26
27 def prepend_env_var(env, var, value):
28     env[var] = os.pathsep + value + os.pathsep + env.get(var, "")
29     env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
30
31
32 def get_subprocess_env(options):
33     env = os.environ.copy()
34
35     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
36     env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
37         "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
38     env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
39         "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
40     env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
41         "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
42     prepend_env_var(env, "PATH", os.path.normpath(
43         "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
44     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
45     env["GST_VERSION"] = options.gst_version
46     env["GST_ENV"] = 'gst-' + options.gst_version
47     env["GST_PLUGIN_SYSTEM_PATH"] = ""
48     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
49         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
50     env["GST_PTP_HELPER"] = os.path.normpath(
51         "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
52     env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
53
54     sharedlib_reg = re.compile(r'\.so|\.dylib|\.dll')
55     typelib_reg = re.compile(r'.*\.typelib$')
56     pluginpath_reg = re.compile(r'lib.*' + re.escape(os.path.normpath('/gstreamer-1.0/')))
57
58     if os.name is 'nt':
59         lib_path_envvar = 'PATH'
60     elif platform.system() == 'Darwin':
61         lib_path_envvar = 'DYLD_LIBRARY_PATH'
62     else:
63         lib_path_envvar = 'LD_LIBRARY_PATH'
64
65     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
66                                                         'gst-python', 'plugin'))
67     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(PREFIX_DIR, 'lib',
68                                                         'gstreamer-1.0'))
69     prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'))
70     prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib'))
71     prepend_env_var(env, "GST_VALIDATE_SCENARIOS_PATH", os.path.join(
72         PREFIX_DIR, 'share', 'gstreamer-1.0', 'validate', 'scenarios'))
73     prepend_env_var(env, "GI_TYPELIB_PATH", os.path.join(PREFIX_DIR, 'lib',
74                                                          'lib', 'girepository-1.0'))
75     prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(PREFIX_DIR, 'lib', 'pkgconfig'))
76     setup_python_env(options, env)
77
78     meson = get_meson()
79     targets_s = subprocess.check_output([sys.executable, meson, 'introspect', options.builddir, '--targets'])
80     targets = json.loads(targets_s.decode())
81     paths = set()
82     mono_paths = set()
83     for target in targets:
84         filename = target['filename']
85         root = os.path.dirname(filename)
86         if filename.endswith('.dll'):
87             mono_paths.add(os.path.join(options.builddir, root))
88         if typelib_reg.search(filename):
89             prepend_env_var(env, "GI_TYPELIB_PATH",
90                             os.path.join(options.builddir, root))
91         elif sharedlib_reg.search(filename):
92             if target.get('type') != "shared library":
93                 continue
94
95             if target.get('installed') and pluginpath_reg.search(os.path.normpath(target.get('install_filename'))):
96                 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, root))
97                 continue
98
99             prepend_env_var(env, lib_path_envvar,
100                             os.path.join(options.builddir, root))
101         elif target.get('type') == 'executable' and target.get('installed'):
102             paths.add(os.path.join(options.builddir, root))
103
104     for p in paths:
105         prepend_env_var(env, 'PATH', p)
106
107     if os.name != 'nt':
108         for p in mono_paths:
109             prepend_env_var(env, "MONO_PATH", p)
110
111     presets = set()
112     encoding_targets = set()
113     pkg_dirs = set()
114     if '--installed' in subprocess.check_output([sys.executable, meson, 'introspect', '-h']).decode():
115         installed_s = subprocess.check_output([sys.executable, meson, 'introspect',
116                                                options.builddir, '--installed'])
117         for path, installpath in json.loads(installed_s.decode()).items():
118             if path.endswith('.prs'):
119                 presets.add(os.path.dirname(path))
120             elif path.endswith('.gep'):
121                 encoding_targets.add(
122                     os.path.abspath(os.path.join(os.path.dirname(path), '..')))
123             elif path.endswith('.pc'):
124                 # Is there a -uninstalled pc file for this file?
125                 uninstalled = "{0}-uninstalled.pc".format(path[:-3])
126                 if os.path.exists(uninstalled):
127                     pkg_dirs.add(os.path.dirname(path))
128
129         for p in presets:
130             prepend_env_var(env, 'GST_PRESET_PATH', p)
131
132         for t in encoding_targets:
133             prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
134
135         for pkg_dir in pkg_dirs:
136             prepend_env_var(env, "PKG_CONFIG_PATH", pkg_dir)
137     prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(options.builddir,
138                                                          'subprojects',
139                                                          'gst-plugins-good',
140                                                          'pkgconfig'))
141
142     mesonpath = os.path.join(SCRIPTDIR, "meson")
143     if os.path.join(mesonpath):
144         # Add meson/ into PYTHONPATH if we are using a local meson
145         prepend_env_var(env, 'PYTHONPATH', mesonpath)
146
147     return env
148
149 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
150 def in_venv():
151     return (hasattr(sys, 'real_prefix') or
152             (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
153
154 def setup_python_env(options, env):
155     some_overrides_built = False
156     for override_path in [os.path.join("subprojects", "gst-editing-services", "bindings", "python", "gi", "overrides"),
157                           os.path.join("subprojects", "gst-python", "gi", "overrides")]:
158         if os.path.exists(os.path.join(SCRIPTDIR, override_path)):
159             some_overrides_built = True
160         else:
161             continue
162
163         prepend_env_var(env, "PYGOBJECT_OVERRIDES_PATH", os.path.join(SCRIPTDIR, override_path))
164         prepend_env_var(env, "PYGOBJECT_OVERRIDES_PATH", os.path.join(options.builddir, override_path))
165
166     if not some_overrides_built:
167         return
168
169     if in_venv ():
170         sitepackages = get_python_lib()
171     else:
172         sitepackages = site.getusersitepackages()
173
174     sitecustomize = os.path.join(
175         subprocess.check_output([sys.executable, '-c', 'import site; print(site.USER_SITE)'],
176         env={"PYTHONUSERBASE": PREFIX_DIR}).decode().strip("\n"), "sitecustomize.py")
177
178     custom_user_sitepackage = os.path.dirname(sitecustomize)
179     os.makedirs(custom_user_sitepackage, exist_ok=True)
180     with open(sitecustomize, "w") as f:
181          f.write("""import os
182 import gi.overrides
183
184 for override_path in os.environ.get("PYGOBJECT_OVERRIDES_PATH", "").split(os.pathsep):
185     gi.overrides.__path__.insert(0, override_path)
186 """)
187
188     env["PYTHONUSERBASE"] = PREFIX_DIR
189     if sitepackages:
190         prepend_env_var(env, "PYTHONPATH", sitepackages)
191
192 if __name__ == "__main__":
193     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
194
195     parser.add_argument("--builddir",
196                         default=DEFAULT_BUILDDIR,
197                         help="The meson build directory")
198     parser.add_argument("--srcdir",
199                         default=SCRIPTDIR,
200                         help="The top level source directory")
201     parser.add_argument("--gst-version", default="master",
202                         help="The GStreamer major version")
203     options, args = parser.parse_known_args()
204
205     if not os.path.exists(options.builddir):
206         print("GStreamer not built in %s\n\nBuild it and try again" %
207               options.builddir)
208         exit(1)
209     options.builddir = os.path.abspath(options.builddir)
210
211     if not os.path.exists(options.srcdir):
212         print("The specified source dir does not exist" %
213               options.srcdir)
214         exit(1)
215
216     if not args:
217         if os.name is 'nt':
218             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
219         else:
220             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
221         if "bash" in args[0]:
222             bashrc = os.path.expanduser('~/.bashrc')
223             if os.path.exists(bashrc):
224                 tmprc = tempfile.NamedTemporaryFile(mode='w')
225                 with open(bashrc, 'r') as src:
226                     shutil.copyfileobj(src, tmprc)
227                 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
228                 tmprc.flush()
229                 # Let the GC remove the tmp file
230                 args.append("--rcfile")
231                 args.append(tmprc.name)
232     try:
233         exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
234                              env=get_subprocess_env(options)))
235     except subprocess.CalledProcessError as e:
236         exit(e.returncode)