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