uninstalled: Avoid failling while setting up PyGObject env hack files
[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
77     meson = get_meson()
78     targets_s = subprocess.check_output([sys.executable, meson, 'introspect', options.builddir, '--targets'])
79     targets = json.loads(targets_s.decode())
80     paths = set()
81     mono_paths = set()
82     for target in targets:
83         filename = target['filename']
84         root = os.path.dirname(filename)
85         if filename.endswith('.dll'):
86             mono_paths.add(os.path.join(options.builddir, root))
87         if typelib_reg.search(filename):
88             prepend_env_var(env, "GI_TYPELIB_PATH",
89                             os.path.join(options.builddir, root))
90         elif sharedlib_reg.search(filename):
91             if target.get('type') != "shared library":
92                 continue
93
94             if target.get('installed') and pluginpath_reg.search(os.path.normpath(target.get('install_filename'))):
95                 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, root))
96                 continue
97
98             prepend_env_var(env, lib_path_envvar,
99                             os.path.join(options.builddir, root))
100         elif target.get('type') == 'executable' and target.get('installed'):
101             paths.add(os.path.join(options.builddir, root))
102
103     for p in paths:
104         prepend_env_var(env, 'PATH', p)
105
106     if os.name != 'nt':
107         for p in mono_paths:
108             prepend_env_var(env, "MONO_PATH", p)
109
110     presets = set()
111     encoding_targets = set()
112     pkg_dirs = set()
113     if '--installed' in subprocess.check_output([sys.executable, meson, 'introspect', '-h']).decode():
114         installed_s = subprocess.check_output([sys.executable, meson, 'introspect',
115                                                options.builddir, '--installed'])
116         for path, installpath in json.loads(installed_s.decode()).items():
117             if path.endswith('.prs'):
118                 presets.add(os.path.dirname(path))
119             elif path.endswith('.gep'):
120                 encoding_targets.add(
121                     os.path.abspath(os.path.join(os.path.dirname(path), '..')))
122             elif path.endswith('.pc'):
123                 # Is there a -uninstalled pc file for this file?
124                 uninstalled = "{0}-uninstalled.pc".format(path[:-3])
125                 if os.path.exists(uninstalled):
126                     pkg_dirs.add(os.path.dirname(path))
127
128         for p in presets:
129             prepend_env_var(env, 'GST_PRESET_PATH', p)
130
131         for t in encoding_targets:
132             prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
133
134         for pkg_dir in pkg_dirs:
135             prepend_env_var(env, "PKG_CONFIG_PATH", pkg_dir)
136     prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(options.builddir,
137                                                          'subprojects',
138                                                          'gst-plugins-good',
139                                                          'pkgconfig'))
140
141     mesonpath = os.path.join(SCRIPTDIR, "meson")
142     if os.path.join(mesonpath):
143         # Add meson/ into PYTHONPATH if we are using a local meson
144         prepend_env_var(env, 'PYTHONPATH', mesonpath)
145
146     return env
147
148 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
149 def in_venv():
150     return (hasattr(sys, 'real_prefix') or
151             (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
152
153 def python_env(options, unset_env=False):
154     """
155     Setup our overrides_hack.py as sitecustomize.py script in user
156     site-packages if unset_env=False, else unset, previously set
157     env.
158     """
159     subprojects_path = os.path.join(options.builddir, "subprojects")
160     gst_python_path = os.path.join(SCRIPTDIR, "subprojects", "gst-python")
161     if not os.path.exists(os.path.join(subprojects_path, "gst-python")) or \
162             not os.path.exists(gst_python_path):
163         return False
164
165     if in_venv ():
166         sitepackages = get_python_lib()
167     else:
168         sitepackages = site.getusersitepackages()
169
170     if not sitepackages:
171         return False
172
173     sitecustomize = os.path.join(sitepackages, "sitecustomize.py")
174     overrides_hack = os.path.join(gst_python_path, "testsuite", "overrides_hack.py")
175     mesonconfig = os.path.join(gst_python_path, "testsuite", "mesonconfig.py")
176     mesonconfig_link = os.path.join(sitepackages, "mesonconfig.py")
177
178     if not unset_env:
179         if os.path.exists(sitecustomize):
180             if os.path.realpath(sitecustomize) == overrides_hack:
181                 print("Customize user site script already linked to the GStreamer one")
182                 return False
183
184             old_sitecustomize = os.path.join(sitepackages,
185                                             "old.sitecustomize.gstuninstalled.py")
186             shutil.move(sitecustomize, old_sitecustomize)
187         elif not os.path.exists(sitepackages):
188             os.makedirs(sitepackages)
189
190         with contextlib.suppress(FileNotFoundError):
191             os.remove(sitecustomize)
192             os.remove(mesonconfig_link)
193         os.symlink(overrides_hack, sitecustomize)
194         os.symlink(mesonconfig, mesonconfig_link)
195         return os.path.realpath(sitecustomize) == overrides_hack
196     else:
197         if not os.path.realpath(sitecustomize) == overrides_hack:
198             return False
199
200         os.remove(sitecustomize)
201         os.remove (mesonconfig_link)
202         old_sitecustomize = os.path.join(sitepackages,
203                                             "old.sitecustomize.gstuninstalled.py")
204
205         if os.path.exists(old_sitecustomize):
206             shutil.move(old_sitecustomize, sitecustomize)
207
208         return True
209
210
211 if __name__ == "__main__":
212     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
213
214     parser.add_argument("--builddir",
215                         default=DEFAULT_BUILDDIR,
216                         help="The meson build directory")
217     parser.add_argument("--srcdir",
218                         default=SCRIPTDIR,
219                         help="The top level source directory")
220     parser.add_argument("--gst-version", default="master",
221                         help="The GStreamer major version")
222     options, args = parser.parse_known_args()
223
224     if not os.path.exists(options.builddir):
225         print("GStreamer not built in %s\n\nBuild it and try again" %
226               options.builddir)
227         exit(1)
228     options.builddir = os.path.abspath(options.builddir)
229
230     if not os.path.exists(options.srcdir):
231         print("The specified source dir does not exist" %
232               options.srcdir)
233         exit(1)
234
235     if not args:
236         if os.name is 'nt':
237             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
238         else:
239             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
240         if "bash" in args[0]:
241             bashrc = os.path.expanduser('~/.bashrc')
242             if os.path.exists(bashrc):
243                 tmprc = tempfile.NamedTemporaryFile(mode='w')
244                 with open(bashrc, 'r') as src:
245                     shutil.copyfileobj(src, tmprc)
246                 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
247                 tmprc.flush()
248                 # Let the GC remove the tmp file
249                 args.append("--rcfile")
250                 args.append(tmprc.name)
251     python_set = python_env(options)
252     try:
253         exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
254                              env=get_subprocess_env(options)))
255     except subprocess.CalledProcessError as e:
256         exit(e.returncode)
257     finally:
258         if python_set:
259             python_env(options, unset_env=True)