Fix previous commit putting mesonconfig.py in our custom sitecustomize directory
[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     subprojects_path = os.path.join(options.builddir, "subprojects")
156     gst_python_path = os.path.join(SCRIPTDIR, "subprojects", "gst-python")
157     if not os.path.exists(os.path.join(subprojects_path, "gst-python")) or \
158             not os.path.exists(gst_python_path):
159         return False
160
161     if in_venv ():
162         sitepackages = get_python_lib()
163     else:
164         sitepackages = site.getusersitepackages()
165
166     sitecustomize = os.path.join(
167         subprocess.check_output([sys.executable, '-c', 'import site; print(site.USER_SITE)'],
168         env={"PYTHONUSERBASE": PREFIX_DIR}).decode().strip("\n"), "sitecustomize.py")
169
170     custom_sitecustomize = os.path.dirname(sitecustomize)
171     overrides_hack = os.path.join(gst_python_path, "testsuite", "overrides_hack.py")
172     mesonconfig = os.path.join(gst_python_path, "testsuite", "mesonconfig.py")
173     mesonconfig_link = os.path.join(custom_sitecustomize, "mesonconfig.py")
174
175     os.makedirs(custom_sitecustomize, exist_ok=True)
176     with contextlib.suppress(FileExistsError):
177         os.symlink(overrides_hack, sitecustomize)
178     with contextlib.suppress(FileExistsError):
179         os.symlink(mesonconfig, mesonconfig_link)
180
181     env["PYTHONUSERBASE"] = PREFIX_DIR
182     if sitepackages:
183         prepend_env_var(env, "PYTHONPATH", sitepackages)
184
185 if __name__ == "__main__":
186     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
187
188     parser.add_argument("--builddir",
189                         default=DEFAULT_BUILDDIR,
190                         help="The meson build directory")
191     parser.add_argument("--srcdir",
192                         default=SCRIPTDIR,
193                         help="The top level source directory")
194     parser.add_argument("--gst-version", default="master",
195                         help="The GStreamer major version")
196     options, args = parser.parse_known_args()
197
198     if not os.path.exists(options.builddir):
199         print("GStreamer not built in %s\n\nBuild it and try again" %
200               options.builddir)
201         exit(1)
202     options.builddir = os.path.abspath(options.builddir)
203
204     if not os.path.exists(options.srcdir):
205         print("The specified source dir does not exist" %
206               options.srcdir)
207         exit(1)
208
209     if not args:
210         if os.name is 'nt':
211             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
212         else:
213             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
214         if "bash" in args[0]:
215             bashrc = os.path.expanduser('~/.bashrc')
216             if os.path.exists(bashrc):
217                 tmprc = tempfile.NamedTemporaryFile(mode='w')
218                 with open(bashrc, 'r') as src:
219                     shutil.copyfileobj(src, tmprc)
220                 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
221                 tmprc.flush()
222                 # Let the GC remove the tmp file
223                 args.append("--rcfile")
224                 args.append(tmprc.name)
225     try:
226         exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
227                              env=get_subprocess_env(options)))
228     except subprocess.CalledProcessError as e:
229         exit(e.returncode)