14 from distutils.sysconfig import get_python_lib
16 from common import get_meson
18 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
19 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
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)
27 def get_subprocess_env(options):
28 env = os.environ.copy()
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")
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/')))
54 lib_path_envvar = 'PATH'
55 elif platform.system() == 'Darwin':
56 lib_path_envvar = 'DYLD_LIBRARY_PATH'
58 lib_path_envvar = 'LD_LIBRARY_PATH'
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',
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'))
72 targets_s = subprocess.check_output([sys.executable, meson, 'introspect', options.builddir, '--targets'])
73 targets = json.loads(targets_s.decode())
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":
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))
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))
98 prepend_env_var(env, 'PATH', p)
102 prepend_env_var(env, "MONO_PATH", p)
105 encoding_targets = 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))
123 prepend_env_var(env, 'GST_PRESET_PATH', p)
125 for t in encoding_targets:
126 prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
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,
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)
142 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
144 return (hasattr(sys, 'real_prefix') or
145 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
147 def python_env(options, unset_env=False):
149 Setup our overrides_hack.py as sitecustomize.py script in user
150 site-packages if unset_env=False, else unset, previously set
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):
160 sitepackages = get_python_lib()
162 sitepackages = site.getusersitepackages()
167 sitecustomize = os.path.join(sitepackages, "sitecustomize.py")
168 overrides_hack = os.path.join(gst_python_path, "testsuite", "overrides_hack.py")
169 mesonconfig = os.path.join(gst_python_path, "testsuite", "mesonconfig.py")
170 mesonconfig_link = os.path.join(sitepackages, "mesonconfig.py")
173 if os.path.exists(sitecustomize):
174 if os.path.realpath(sitecustomize) == overrides_hack:
175 print("Customize user site script already linked to the GStreamer one")
178 old_sitecustomize = os.path.join(sitepackages,
179 "old.sitecustomize.gstuninstalled.py")
180 shutil.move(sitecustomize, old_sitecustomize)
181 elif not os.path.exists(sitepackages):
182 os.makedirs(sitepackages)
184 os.symlink(overrides_hack, sitecustomize)
185 os.symlink(mesonconfig, mesonconfig_link)
186 return os.path.realpath(sitecustomize) == overrides_hack
188 if not os.path.realpath(sitecustomize) == overrides_hack:
191 os.remove(sitecustomize)
192 os.remove (mesonconfig_link)
193 old_sitecustomize = os.path.join(sitepackages,
194 "old.sitecustomize.gstuninstalled.py")
196 if os.path.exists(old_sitecustomize):
197 shutil.move(old_sitecustomize, sitecustomize)
202 if __name__ == "__main__":
203 parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
205 parser.add_argument("--builddir",
206 default=os.path.join(SCRIPTDIR, "build"),
207 help="The meson build directory")
208 parser.add_argument("--srcdir",
210 help="The top level source directory")
211 parser.add_argument("--gst-version", default="master",
212 help="The GStreamer major version")
213 options, args = parser.parse_known_args()
215 if not os.path.exists(options.builddir):
216 print("GStreamer not built in %s\n\nBuild it and try again" %
220 if not os.path.exists(options.srcdir):
221 print("The specified source dir does not exist" %
227 args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
229 args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
230 if "bash" in args[0]:
231 bashrc = os.path.expanduser('~/.bashrc')
232 if os.path.exists(bashrc):
233 tmprc = tempfile.NamedTemporaryFile(mode='w')
234 with open(bashrc, 'r') as src:
235 shutil.copyfileobj(src, tmprc)
236 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
238 # Let the GC remove the tmp file
239 args.append("--rcfile")
240 args.append(tmprc.name)
241 python_set = python_env(options)
243 exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
244 env=get_subprocess_env(options)))
245 except subprocess.CalledProcessError as e:
249 python_env(options, unset_env=True)