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