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