Use mesonintrospect to set library path
[platform/upstream/gstreamer.git] / gst-uninstalled.py
1 #!/usr/bin/env python3
2
3 import argparse
4 import json
5 import os
6 import platform
7 import re
8 import site
9 import shutil
10 import subprocess
11 import tempfile
12
13 from common import get_meson
14
15 SCRIPTDIR = os.path.abspath(os.path.dirname(__file__))
16
17
18 def prepend_env_var(env, var, value):
19     env[var] = os.pathsep + value + os.pathsep + env.get(var, "")
20     env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
21
22
23 def get_subprocess_env(options):
24     env = os.environ.copy()
25
26     PATH = env.get("PATH", "")
27     subprojects_path = os.path.join(options.builddir, "subprojects")
28     for proj in os.listdir(subprojects_path):
29         projpath = os.path.join(subprojects_path, proj)
30         if not os.path.exists(projpath):
31             print("Subproject %s does not exist in %s.,\n"
32                   " Make sure to build everything properly "
33                   "and try again." % (proj, projpath))
34             exit(1)
35
36         toolsdir = os.path.join(projpath, "tools")
37         if os.path.exists(toolsdir):
38             prepend_env_var(env, "PATH", toolsdir)
39
40         prepend_env_var(env, "GST_PLUGIN_PATH", projpath)
41
42     prepend_env_var(env, "GST_PLUGIN_PATH", os.path.join(SCRIPTDIR, 'subprojects',
43                                                          'gst-python', 'plugin'))
44     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR)
45     env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
46         "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
47     env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
48         "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
49     env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
50         "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
51     prepend_env_var(env, "PATH", os.path.normpath(
52         "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
53     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
54     env["PATH"] += os.pathsep + PATH
55     env["GST_VERSION"] = options.gst_version
56     env["GST_ENV"] = 'gst-' + options.gst_version
57     env["GST_PLUGIN_SYSTEM_PATH"] = ""
58     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
59         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
60     env["GST_PTP_HELPER"] = os.path.normpath(
61         "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
62     env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
63
64     filename = "meson.build"
65     sharedlib_reg = re.compile(r'\.so|\.dylib|\.dll')
66     typelib_reg = re.compile(r'.*\.typelib$')
67
68     if os.name is 'nt':
69         lib_path_envvar = 'PATH'
70     elif platform.system() == 'Darwin':
71         lib_path_envvar = 'DYLD_LIBRARY_PATH'
72     else:
73         lib_path_envvar = 'LD_LIBRARY_PATH'
74
75     meson, mesonconf, mesonintrospect = get_meson()
76     targets_s = subprocess.check_output([mesonintrospect, options.builddir, '--targets'])
77     targets = json.loads(targets_s.decode())
78     for target in targets:
79         filename = target['filename']
80         root = os.path.dirname(filename)
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":
86                 continue
87
88             if "lib/gstreamer-1.0" in os.path.normpath(target.get('install_filename')):
89                 continue
90
91             prepend_env_var(env, lib_path_envvar,
92                             os.path.join(options.builddir, root))
93
94     return env
95
96
97 def python_env(options, unset_env=False):
98     """
99     Setup our overrides_hack.py as sitecustomize.py script in user
100     site-packages if unset_env=False, else unset, previously set
101     env.
102     """
103     subprojects_path = os.path.join(options.builddir, "subprojects")
104     gst_python_path = os.path.join(SCRIPTDIR, "subprojects", "gst-python")
105     if not os.path.exists(os.path.join(subprojects_path, "gst-python")) or \
106             not os.path.exists(gst_python_path):
107         return False
108
109     sitepackages = site.getusersitepackages()
110     if not sitepackages:
111         return False
112
113     sitecustomize = os.path.join(sitepackages, "sitecustomize.py")
114     overrides_hack = os.path.join(gst_python_path, "testsuite", "overrides_hack.py")
115
116     if not unset_env:
117         if os.path.exists(sitecustomize):
118             if os.path.realpath(sitecustomize) == overrides_hack:
119                 print("Customize user site script already linked to the GStreamer one")
120                 return False
121
122             old_sitecustomize = os.path.join(sitepackages,
123                                             "old.sitecustomize.gstuninstalled.py")
124             shutil.move(sitecustomize, old_sitecustomize)
125         elif not os.path.exists(sitepackages):
126             os.makedirs(sitepackages)
127
128         os.symlink(overrides_hack, sitecustomize)
129         return os.path.realpath(sitecustomize) == overrides_hack
130     else:
131         if not os.path.realpath(sitecustomize) == overrides_hack:
132             return False
133
134         os.remove(sitecustomize)
135         old_sitecustomize = os.path.join(sitepackages,
136                                             "old.sitecustomize.gstuninstalled.py")
137
138         if os.path.exists(old_sitecustomize):
139             shutil.move(old_sitecustomize, sitecustomize)
140
141         return True
142
143
144 if __name__ == "__main__":
145     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
146
147     parser.add_argument("--builddir",
148                         default=os.path.join(SCRIPTDIR, "build"),
149                         help="The meson build directory")
150     parser.add_argument("--gst-version", default="master",
151                         help="The GStreamer major version")
152     options, args = parser.parse_known_args()
153
154     if not os.path.exists(options.builddir):
155         print("GStreamer not built in %s\n\nBuild it and try again" %
156               options.builddir)
157         exit(1)
158
159     if not args:
160         if os.name is 'nt':
161             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
162         else:
163             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
164         if "bash" in args[0]:
165             bashrc = os.path.expanduser('~/.bashrc')
166             if os.path.exists(bashrc):
167                 tmprc = tempfile.NamedTemporaryFile(mode='w')
168                 with open(bashrc, 'r') as src:
169                     shutil.copyfileobj(src, tmprc)
170                 tmprc.write('\nexport PS1="[gst-%s] $PS1"' % options.gst_version)
171                 tmprc.flush()
172                 # Let the GC remove the tmp file
173                 args.append("--rcfile")
174                 args.append(tmprc.name)
175     python_set = python_env(options)
176     try:
177         exit(subprocess.call(args, env=get_subprocess_env(options)))
178     except subprocess.CalledProcessError as e:
179         exit(e.returncode)
180     finally:
181         if python_set:
182             python_env(options, unset_env=True)