15 from distutils.sysconfig import get_python_lib
17 from common import get_meson
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')
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)
32 def get_subprocess_env(options):
33 env = os.environ.copy()
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")
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/')))
59 lib_path_envvar = 'PATH'
60 elif platform.system() == 'Darwin':
61 lib_path_envvar = 'DYLD_LIBRARY_PATH'
63 lib_path_envvar = 'LD_LIBRARY_PATH'
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',
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'))
78 targets_s = subprocess.check_output([sys.executable, meson, 'introspect', options.builddir, '--targets'])
79 targets = json.loads(targets_s.decode())
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":
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))
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))
104 prepend_env_var(env, 'PATH', p)
108 prepend_env_var(env, "MONO_PATH", p)
111 encoding_targets = 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))
129 prepend_env_var(env, 'GST_PRESET_PATH', p)
131 for t in encoding_targets:
132 prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
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,
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)
148 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
150 return (hasattr(sys, 'real_prefix') or
151 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
153 def python_env(options, unset_env=False):
155 Setup our overrides_hack.py as sitecustomize.py script in user
156 site-packages if unset_env=False, else unset, previously set
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):
166 sitepackages = get_python_lib()
168 sitepackages = site.getusersitepackages()
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")
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")
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)
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
197 if not os.path.realpath(sitecustomize) == overrides_hack:
200 os.remove(sitecustomize)
201 os.remove (mesonconfig_link)
202 old_sitecustomize = os.path.join(sitepackages,
203 "old.sitecustomize.gstuninstalled.py")
205 if os.path.exists(old_sitecustomize):
206 shutil.move(old_sitecustomize, sitecustomize)
211 if __name__ == "__main__":
212 parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
214 parser.add_argument("--builddir",
215 default=DEFAULT_BUILDDIR,
216 help="The meson build directory")
217 parser.add_argument("--srcdir",
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()
224 if not os.path.exists(options.builddir):
225 print("GStreamer not built in %s\n\nBuild it and try again" %
228 options.builddir = os.path.abspath(options.builddir)
230 if not os.path.exists(options.srcdir):
231 print("The specified source dir does not exist" %
237 args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
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)
248 # Let the GC remove the tmp file
249 args.append("--rcfile")
250 args.append(tmprc.name)
251 python_set = python_env(options)
253 exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
254 env=get_subprocess_env(options)))
255 except subprocess.CalledProcessError as e:
259 python_env(options, unset_env=True)