16 from distutils.sysconfig import get_python_lib
18 from common import get_meson
19 from common import git
21 SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
22 PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
23 # Use '_build' as the builddir instead of 'build'
24 DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, 'build')
25 if not os.path.exists(DEFAULT_BUILDDIR):
26 DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, '_build')
30 if isinstance(o, str):
32 if isinstance(o, list):
34 raise AssertionError('Object {!r} must be a string or a list'.format(o))
37 if isinstance(o, str):
39 if isinstance(o, list):
42 raise AssertionError('Did not expect object {!r} to have more than one element'.format(o))
43 raise AssertionError('Object {!r} must be a string or a list'.format(o))
45 def prepend_env_var(env, var, value):
46 env[var] = os.pathsep + value + os.pathsep + env.get(var, "")
47 env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
50 def get_subprocess_env(options, gst_version):
51 env = os.environ.copy()
53 env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
54 env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
55 "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
56 env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
57 "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
58 env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
59 "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
60 prepend_env_var(env, "PATH", os.path.normpath(
61 "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
62 prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
63 env["GST_VERSION"] = gst_version
64 env["GST_ENV"] = 'gst-' + gst_version
65 env["GST_PLUGIN_SYSTEM_PATH"] = ""
66 env["GST_PLUGIN_SCANNER"] = os.path.normpath(
67 "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
68 env["GST_PTP_HELPER"] = os.path.normpath(
69 "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
70 env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
72 sharedlib_reg = re.compile(r'\.so|\.dylib|\.dll')
73 typelib_reg = re.compile(r'.*\.typelib$')
74 pluginpath_reg = re.compile(r'lib.*' + re.escape(os.path.normpath('/gstreamer-1.0/')))
77 lib_path_envvar = 'PATH'
78 elif platform.system() == 'Darwin':
79 lib_path_envvar = 'DYLD_LIBRARY_PATH'
81 lib_path_envvar = 'LD_LIBRARY_PATH'
83 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
84 'gst-python', 'plugin'))
85 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(PREFIX_DIR, 'lib',
87 prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'))
88 prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib'))
89 prepend_env_var(env, "GST_VALIDATE_SCENARIOS_PATH", os.path.join(
90 PREFIX_DIR, 'share', 'gstreamer-1.0', 'validate', 'scenarios'))
91 prepend_env_var(env, "GI_TYPELIB_PATH", os.path.join(PREFIX_DIR, 'lib',
92 'lib', 'girepository-1.0'))
93 prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(PREFIX_DIR, 'lib', 'pkgconfig'))
96 targets_s = subprocess.check_output(meson + ['introspect', options.builddir, '--targets'])
97 targets = json.loads(targets_s.decode())
100 srcdir_path = pathlib.Path(options.srcdir)
101 for target in targets:
102 filenames = listify(target['filename'])
103 for filename in filenames:
104 root = os.path.dirname(filename)
105 if srcdir_path / "subprojects/gst-devtools/validate/plugins" in (srcdir_path / root).parents:
107 if filename.endswith('.dll'):
108 mono_paths.add(os.path.join(options.builddir, root))
109 if typelib_reg.search(filename):
110 prepend_env_var(env, "GI_TYPELIB_PATH",
111 os.path.join(options.builddir, root))
112 elif sharedlib_reg.search(filename):
113 if not target['type'].startswith('shared'):
115 if target['installed']:
116 if pluginpath_reg.search(os.path.normpath(stringify(target['install_filename']))):
117 prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, root))
120 prepend_env_var(env, lib_path_envvar,
121 os.path.join(options.builddir, root))
122 elif target['type'] == 'executable' and target['installed']:
123 paths.add(os.path.join(options.builddir, root))
126 prepend_env_var(env, 'PATH', p)
130 prepend_env_var(env, "MONO_PATH", p)
133 encoding_targets = set()
135 python_dirs = set(["%s/subprojects/gstreamer/libs/gst/helpers/" % options.srcdir])
136 if '--installed' in subprocess.check_output(meson + ['introspect', '-h']).decode():
137 installed_s = subprocess.check_output(meson + ['introspect', options.builddir, '--installed'])
138 for path, installpath in json.loads(installed_s.decode()).items():
139 installpath_parts = pathlib.Path(installpath).parts
140 path_parts = pathlib.Path(path).parts
142 # We want to add all python modules to the PYTHONPATH
143 # in a manner consistent with the way they would be imported:
144 # For example if the source path /home/meh/foo/bar.py
145 # is to be installed in /usr/lib/python/site-packages/foo/bar.py,
146 # we want to add /home/meh to the PYTHONPATH.
147 # This will only work for projects where the paths to be installed
148 # mirror the installed directory layout, for example if the path
149 # is /home/meh/baz/bar.py and the install path is
150 # /usr/lib/site-packages/foo/bar.py , we will not add anything
151 # to PYTHONPATH, but the current approach works with pygobject
152 # and gst-python at least.
153 if 'site-packages' in installpath_parts:
154 install_subpath = os.path.join(*installpath_parts[installpath_parts.index('site-packages') + 1:])
155 if path.endswith(install_subpath):
156 python_dirs.add(path[:len (install_subpath) * -1])
158 if path.endswith('.prs'):
159 presets.add(os.path.dirname(path))
160 elif path.endswith('.gep'):
161 encoding_targets.add(
162 os.path.abspath(os.path.join(os.path.dirname(path), '..')))
163 elif path.endswith('.pc'):
164 # Is there a -uninstalled pc file for this file?
165 uninstalled = "{0}-uninstalled.pc".format(path[:-3])
166 if os.path.exists(uninstalled):
167 pkg_dirs.add(os.path.dirname(path))
169 if path.endswith('gstomx.conf'):
170 prepend_env_var(env, 'GST_OMX_CONFIG_DIR', os.path.dirname(path))
173 prepend_env_var(env, 'GST_PRESET_PATH', p)
175 for t in encoding_targets:
176 prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
178 for pkg_dir in pkg_dirs:
179 prepend_env_var(env, "PKG_CONFIG_PATH", pkg_dir)
180 prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(options.builddir,
185 for python_dir in python_dirs:
186 prepend_env_var(env, 'PYTHONPATH', python_dir)
188 mesonpath = os.path.join(SCRIPTDIR, "meson")
189 if os.path.join(mesonpath):
190 # Add meson/ into PYTHONPATH if we are using a local meson
191 prepend_env_var(env, 'PYTHONPATH', mesonpath)
195 # https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
197 return (hasattr(sys, 'real_prefix') or
198 (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
200 if __name__ == "__main__":
201 parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
203 parser.add_argument("--builddir",
204 default=DEFAULT_BUILDDIR,
205 help="The meson build directory")
206 parser.add_argument("--srcdir",
208 help="The top level source directory")
209 options, args = parser.parse_known_args()
211 if not os.path.exists(options.builddir):
212 print("GStreamer not built in %s\n\nBuild it and try again" %
215 options.builddir = os.path.abspath(options.builddir)
217 if not os.path.exists(options.srcdir):
218 print("The specified source dir does not exist" %
222 # The following incantation will retrieve the current branch name.
223 gst_version = git("rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD",
224 repository_path=options.srcdir).strip('\n')
228 args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
229 args += ['/k', 'prompt [gst-{}] $P$G'.format(gst_version)]
231 args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
232 if "bash" in args[0]:
233 bashrc = os.path.expanduser('~/.bashrc')
234 if os.path.exists(bashrc):
235 tmprc = tempfile.NamedTemporaryFile(mode='w')
236 with open(bashrc, 'r') as src:
237 shutil.copyfileobj(src, tmprc)
238 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % gst_version)
240 # Let the GC remove the tmp file
241 args.append("--rcfile")
242 args.append(tmprc.name)
244 exit(subprocess.call(args, close_fds=False,
245 env=get_subprocess_env(options, gst_version)))
246 except subprocess.CalledProcessError as e: