Windows fixes for scripts
[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 subprocess
8
9
10 SCRIPTDIR = os.path.abspath(os.path.dirname(__file__))
11
12
13 def prepend_env_var(env, var, value):
14     env[var] = os.pathsep + value + os.pathsep + env.get(var, "")
15     env[var] = env[var].replace(os.pathsep + os.pathsep, os.pathsep).strip(os.pathsep)
16
17
18 def set_prompt_var(options, env):
19     ps1 = env.get("PS1")
20     if ps1:
21         env["PS1"] = "[gst-%s] %s" % (options.gst_version, ps1)
22
23     prompt = env.get("PROMPT")
24     if prompt:
25         env["PROMPT"] = "[gst-%s] %s" % (options.gst_version, prompt)
26
27
28 def get_subprocess_env(options):
29     env = os.environ.copy()
30
31     PATH = env.get("PATH", "")
32     subprojects_path = os.path.join(options.builddir, "subprojects")
33     for proj in os.listdir(subprojects_path):
34         projpath = os.path.join(subprojects_path, proj)
35         if not os.path.exists(projpath):
36             print("Subproject %s does not exist in %s.,\n"
37                   " Make sure to build everything properly "
38                   "and try again." % (proj, projpath))
39             exit(1)
40
41         envvars_file = os.path.join(projpath, os.path.basename(projpath) + "-uninstalled-envvars.py")
42         if os.path.exists(envvars_file):
43             envvars_env = {"envvars": {}}
44             with open(envvars_file) as f:
45                 code = compile(f.read(), envvars_file, 'exec')
46                 exec(code, None, envvars_env)
47             for var, value in envvars_env["envvars"].items():
48                 if var.startswith("+"):
49                     prepend_env_var(env, var, value.strip("+"))
50                 else:
51                     env[var] = value
52
53         toolsdir = os.path.join(projpath, "tools")
54         if os.path.exists(toolsdir):
55             prepend_env_var(env, "PATH", toolsdir)
56
57         prepend_env_var(env, "GST_PLUGIN_PATH", projpath)
58
59     env["CURRENT_GST"] = os.path.normpath(SCRIPTDIR + "/subprojects")
60     env["GST_VALIDATE_SCENARIOS_PATH"] = os.path.normpath(
61         "%s/subprojects/gst-devtools/validate/data/scenarios" % SCRIPTDIR)
62     env["GST_VALIDATE_PLUGIN_PATH"] = os.path.normpath(
63         "%s/subprojects/gst-devtools/validate/plugins" % options.builddir)
64     env["GST_VALIDATE_APPS_DIR"] = os.path.normpath(
65         "%s/subprojects/gst-editing-services/tests/validate" % SCRIPTDIR)
66     prepend_env_var(env, "PATH", os.path.normpath(
67         "%s/subprojects/gst-devtools/validate/tools" % options.builddir))
68     prepend_env_var(env, "PATH", os.path.join(SCRIPTDIR, 'meson'))
69     env["PATH"] += os.pathsep + PATH
70     env["GST_VERSION"] = options.gst_version
71     env["GST_PLUGIN_SYSTEM_PATH"] = ""
72     env["GST_PLUGIN_SCANNER"] = os.path.normpath(
73         "%s/subprojects/gstreamer/libs/gst/helpers/gst-plugin-scanner" % options.builddir)
74     env["GST_PTP_HELPER"] = os.path.normpath(
75         "%s/subprojects/gstreamer/libs/gst/helpers/gst-ptp-helper" % options.builddir)
76     env["GST_REGISTRY"] = os.path.normpath(options.builddir + "/registry.dat")
77     prepend_env_var(env, 'PYTHONPATH', ':'.join(site.getsitepackages()))
78     env["PYTHONPATH"] = env["PYTHONPATH"] + ':' + os.path.normpath(
79         options.builddir + '/subprojects/gst-python')
80
81     filename = "meson.build"
82     sharedlib_reg = re.compile(r'\.so$|\.dylib$')
83     typelib_reg = re.compile(r'.*\.typelib$')
84     for root, dirnames, filenames in os.walk(os.path.join(options.builddir,
85                                                           'subprojects')):
86         has_typelib = False
87         has_shared = False
88         for filename in filenames:
89             if typelib_reg.search(filename) and not has_typelib:
90                 has_typelib = True
91                 prepend_env_var(env, "GI_TYPELIB_PATH",
92                                 os.path.join(options.builddir, root))
93                 if has_shared:
94                     break
95             elif sharedlib_reg.search(filename) and not has_shared:
96                 has_shared = True
97                 prepend_env_var(env, "LD_LIBRARY_PATH",
98                                 os.path.join(options.builddir, root))
99                 prepend_env_var(env, "DYLD_LIBRARY_PATH",
100                                 os.path.join(options.builddir, root))
101                 if has_typelib:
102                     break
103
104
105     set_prompt_var(options, env)
106
107     return env
108
109
110 if __name__ == "__main__":
111     parser = argparse.ArgumentParser(prog="gstreamer-uninstalled")
112
113     parser.add_argument("--builddir",
114                         default=os.path.join(SCRIPTDIR, "build"),
115                         help="The meson build directory")
116     parser.add_argument("--gst-version", default="master",
117                         help="The GStreamer major version")
118     options, args = parser.parse_known_args()
119
120     if not os.path.exists(options.builddir):
121         print("GStreamer not built in %s\n\nBuild it and try again" %
122               options.builddir)
123         exit(1)
124
125     if not args:
126         if os.name is 'nt':
127             args = [os.environ.get("COMSPEC", r"C:\WINDOWS\system32\cmd.exe")]
128         else:
129             args = [os.environ.get("SHELL", os.path.realpath("/bin/sh"))]
130         if args[0] == "/bin/bash":
131             args.append("--noprofile")
132
133     try:
134         exit(subprocess.call(args, env=get_subprocess_env(options)))
135     except subprocess.CalledProcessError as e:
136         exit(e.returncode)