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