gstreamer/gst-uninstalled.py
Martin Kelly 4525078098 uninstalled: use usercustomize, not sitecustomize
Currently, gst-uninstalled is using sitecustomize.py for adding gi
override tweaks. However, if the standard Python libraries come before
this file in sys.path, then sitecustomize.py will never be run because
the "import sitecustomize" done in site.py will use the standard Python
libraries instead. This can be seen by running "import sitecustomize;
print(sitecustomize.__file__)" inside the uninstalled environment, as
well as by checking gi.override.__path__ and seeing that the tweaks are
missing (and the overrides are misbehaving).

Switch to using usercustomize.py, which has no match in the standard
libraries and thus will be correctly imported.

https://bugzilla.gnome.org/show_bug.cgi?id=797011
2018-09-13 07:05:33 -03:00

239 lines
9.7 KiB
Python
Executable file

#!/usr/bin/env python3
import argparse
import contextlib
import json
import os
import platform
import re
import site
import shutil
import subprocess
import sys
import tempfile
from distutils.sysconfig import get_python_lib
from common import get_meson
from common import git
SCRIPTDIR = os.path.dirname(os.path.realpath(__file__))
PREFIX_DIR = os.path.join(SCRIPTDIR, 'prefix')
# Use '_build' as the builddir instead of 'build'
DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, 'build')
if not os.path.exists(DEFAULT_BUILDDIR):
DEFAULT_BUILDDIR = os.path.join(SCRIPTDIR, '_build')
def prepend_env_var(env, var, value):
env[var] = os.pathsep + value + os.pathsep + env.get(var, "")
env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
def get_subprocess_env(options, gst_version):
env = os.environ.copy()
env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
"%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
"%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
"%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
prepend_env_var(env, "PATH", os.path.normpath(
"%s/subprojects/gst-devtools/validate/tools" % options.builddir))
prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
env["GST_VERSION"] = gst_version
env["GST_ENV"] = 'gst-' + gst_version
env["GST_PLUGIN_SYSTEM_PATH"] = ""
env["GST_PLUGIN_SCANNER"] = os.path.normpath(
"%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
env["GST_PTP_HELPER"] = os.path.normpath(
"%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
sharedlib_reg = re.compile(r'\.so|\.dylib|\.dll')
typelib_reg = re.compile(r'.*\.typelib$')
pluginpath_reg = re.compile(r'lib.*' + re.escape(os.path.normpath('/gstreamer-1.0/')))
if os.name is 'nt':
lib_path_envvar = 'PATH'
elif platform.system() == 'Darwin':
lib_path_envvar = 'DYLD_LIBRARY_PATH'
else:
lib_path_envvar = 'LD_LIBRARY_PATH'
prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
'gst-python', 'plugin'))
prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(PREFIX_DIR, 'lib',
'gstreamer-1.0'))
prepend_env_var(env, "PATH", os.path.join(PREFIX_DIR, 'bin'))
prepend_env_var(env, lib_path_envvar, os.path.join(PREFIX_DIR, 'lib'))
prepend_env_var(env, "GST_VALIDATE_SCENARIOS_PATH", os.path.join(
PREFIX_DIR, 'share', 'gstreamer-1.0', 'validate', 'scenarios'))
prepend_env_var(env, "GI_TYPELIB_PATH", os.path.join(PREFIX_DIR, 'lib',
'lib', 'girepository-1.0'))
prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(PREFIX_DIR, 'lib', 'pkgconfig'))
setup_python_env(options, env)
meson = get_meson()
targets_s = subprocess.check_output(meson + ['introspect', options.builddir, '--targets'])
targets = json.loads(targets_s.decode())
paths = set()
mono_paths = set()
for target in targets:
filename = target['filename']
root = os.path.dirname(filename)
if filename.endswith('.dll'):
mono_paths.add(os.path.join(options.builddir, root))
if typelib_reg.search(filename):
prepend_env_var(env, "GI_TYPELIB_PATH",
os.path.join(options.builddir, root))
elif sharedlib_reg.search(filename):
if target.get('type') != "shared library":
continue
if target.get('installed') and pluginpath_reg.search(os.path.normpath(target.get('install_filename'))):
prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(options.builddir, root))
continue
prepend_env_var(env, lib_path_envvar,
os.path.join(options.builddir, root))
elif target.get('type') == 'executable' and target.get('installed'):
paths.add(os.path.join(options.builddir, root))
for p in paths:
prepend_env_var(env, 'PATH', p)
if os.name != 'nt':
for p in mono_paths:
prepend_env_var(env, "MONO_PATH", p)
presets = set()
encoding_targets = set()
pkg_dirs = set()
if '--installed' in subprocess.check_output(meson + ['introspect', '-h']).decode():
installed_s = subprocess.check_output(meson + ['introspect', options.builddir, '--installed'])
for path, installpath in json.loads(installed_s.decode()).items():
if path.endswith('.prs'):
presets.add(os.path.dirname(path))
elif path.endswith('.gep'):
encoding_targets.add(
os.path.abspath(os.path.join(os.path.dirname(path), '..')))
elif path.endswith('.pc'):
# Is there a -uninstalled pc file for this file?
uninstalled = "{0}-uninstalled.pc".format(path[:-3])
if os.path.exists(uninstalled):
pkg_dirs.add(os.path.dirname(path))
for p in presets:
prepend_env_var(env, 'GST_PRESET_PATH', p)
for t in encoding_targets:
prepend_env_var(env, 'GST_ENCODING_TARGET_PATH', t)
for pkg_dir in pkg_dirs:
prepend_env_var(env, "PKG_CONFIG_PATH", pkg_dir)
prepend_env_var(env, "PKG_CONFIG_PATH", os.path.join(options.builddir,
'subprojects',
'gst-plugins-good',
'pkgconfig'))
mesonpath = os.path.join(SCRIPTDIR, "meson")
if os.path.join(mesonpath):
# Add meson/ into PYTHONPATH if we are using a local meson
prepend_env_var(env, 'PYTHONPATH', mesonpath)
return env
# https://stackoverflow.com/questions/1871549/determine-if-python-is-running-inside-virtualenv
def in_venv():
return (hasattr(sys, 'real_prefix') or
(hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix))
def setup_python_env(options, env):
some_overrides_built = False
for override_path in [os.path.join("subprojects", "gst-editing-services", "bindings", "python", "gi", "overrides"),
os.path.join("subprojects", "gst-python", "gi", "overrides")]:
if os.path.exists(os.path.join(SCRIPTDIR, override_path)):
some_overrides_built = True
else:
continue
prepend_env_var(env, "PYGOBJECT_OVERRIDES_PATH", os.path.join(SCRIPTDIR, override_path))
prepend_env_var(env, "PYGOBJECT_OVERRIDES_PATH", os.path.join(options.builddir, override_path))
if not some_overrides_built:
return
if in_venv ():
sitepackages = get_python_lib()
else:
sitepackages = site.getusersitepackages()
usercustomize = os.path.join(
subprocess.check_output([sys.executable, '-c', 'import site; print(site.USER_SITE)'],
env={"PYTHONUSERBASE": PREFIX_DIR}).decode().strip("\n"), "usercustomize.py")
custom_user_sitepackage = os.path.dirname(usercustomize)
os.makedirs(custom_user_sitepackage, exist_ok=True)
with open(usercustomize, "w") as f:
f.write("""import os
import gi.overrides
for override_path in os.environ.get("PYGOBJECT_OVERRIDES_PATH", "").split(os.pathsep):
gi.overrides.__path__.insert(0, override_path)
""")
env["PYTHONUSERBASE"] = PREFIX_DIR
if sitepackages:
prepend_env_var(env, "PYTHONPATH", sitepackages)
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
parser.add_argument("--builddir",
default=DEFAULT_BUILDDIR,
help="The meson build directory")
parser.add_argument("--srcdir",
default=SCRIPTDIR,
help="The top level source directory")
options, args = parser.parse_known_args()
if not os.path.exists(options.builddir):
print("GStreamer not built in %s\n\nBuild it and try again" %
options.builddir)
exit(1)
options.builddir = os.path.abspath(options.builddir)
if not os.path.exists(options.srcdir):
print("The specified source dir does not exist" %
options.srcdir)
exit(1)
# The following incantation will retrieve the current branch name.
gst_version = git("rev-parse", "--symbolic-full-name", "--abbrev-ref", "HEAD",
repository_path=options.srcdir).strip('\n')
if not args:
if os.name is 'nt':
args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
else:
args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
if "bash" in args[0]:
bashrc = os.path.expanduser('~/.bashrc')
if os.path.exists(bashrc):
tmprc = tempfile.NamedTemporaryFile(mode='w')
with open(bashrc, 'r') as src:
shutil.copyfileobj(src, tmprc)
tmprc.write('\nexport PS1="[gst-%s] $PS1"' % gst_version)
tmprc.flush()
# Let the GC remove the tmp file
args.append("--rcfile")
args.append(tmprc.name)
try:
exit(subprocess.call(args, cwd=options.srcdir, close_fds=False,
env=get_subprocess_env(options, gst_version)))
except subprocess.CalledProcessError as e:
exit(e.returncode)