Bug 1676898 [wpt PR 26509] - Move wpt/css/composite-bgcolor-animation to wpt/css...
[gecko.git] / mach
blob66b88e32963009ebac02eb5114037caab023cc86
1 #!/bin/sh
2 # This Source Code Form is subject to the terms of the Mozilla Public
3 # License, v. 2.0. If a copy of the MPL was not distributed with this
4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 # The beginning of this script is both valid POSIX shell and valid Python,
7 # such that the script starts with the shell and is reexecuted with
8 # the right Python.
10 # Embeds a shell script inside a Python triple quote. This pattern is valid
11 # shell because `''':'`, `':'` and `:` are all equivalent, and `:` is a no-op.
12 ''':'
13 # Commands that are to be run with Python 2.
14 py2commands="
15 awsy-test
16 firefox-ui-functional
17 geckodriver
18 geckodriver-test
19 jsshell-bench
20 marionette-test
21 jstests
22 mozharness
23 raptor
24 raptor-test
25 telemetry-tests-client
26 test
27 web-platform-tests
28 web-platform-tests-update
29 wpt
30 wpt-metadata-merge
31 wpt-serve
32 wpt-update
35 # Commands that are to be run with the system Python 3 instead of the
36 # virtualenv.
37 nativecmds="
38 bootstrap
39 create-mach-environment
40 install-moz-phab
43 run_py() {
44 # Try to run a specific Python interpreter.
45 py_executable="$1"
46 shift
47 if command -v "$py_executable" > /dev/null
48 then
49 exec "$py_executable" "$0" "$@"
50 else
51 echo "This mach command requires $py_executable, which wasn't found on the system!"
52 case "$py_executable" in
53 python2.7|python3) ;;
55 echo "Consider running 'mach bootstrap' or 'mach create-mach-environment' to create the mach virtualenvs, or set MACH_USE_SYSTEM_PYTHON to use the system Python installation over a virtualenv."
57 esac
58 exit 1
62 get_command() {
63 # Parse the name of the mach command out of the arguments. This is necessary
64 # in the presence of global mach arguments that come before the name of the
65 # command, e.g. `mach -v build`. We dispatch to the correct Python
66 # interpreter depending on the command.
67 while true; do
68 case $1 in
69 -v|--verbose) shift;;
70 -l|--log-file)
71 if [ "$#" -lt 2 ]
72 then
73 echo
74 break
75 else
76 shift 2
79 --log-interval) shift;;
80 --log-no-times) shift;;
81 -h) shift;;
82 --debug-command) shift;;
83 --settings)
84 if [ "$#" -lt 2 ]
85 then
86 echo
87 break
88 else
89 shift 2
92 # When running `./mach help <command>`, the correct Python for <command>
93 # needs to be used.
94 help) echo $2; break;;
95 # When running `./mach mach-completion /path/to/mach <command>`, the
96 # correct Python for <command> needs to be used.
97 mach-completion) echo $3; break;;
98 "") echo; break;;
99 *) echo $1; break;;
100 esac
101 done
104 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
105 command=$(get_command "$@")
107 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
108 # python{2.7,3} executables and not the virtualenv locations.
109 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
110 then
111 case "$OSTYPE" in
112 cygwin|msys|win32) bin_path=Scripts;;
113 *) bin_path=bin;;
114 esac
115 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
116 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
117 else
118 py2executable=python2.7
119 py3executable=python3
122 # Check whether we need to run with the native Python 3 interpreter.
123 case " $(echo $nativecmds) " in
124 *\ $command\ *)
125 run_py python3 "$@"
127 esac
129 # Check for the mach subcommand in the Python 2 commands list and run it
130 # with the correct interpreter.
131 case " $(echo $py2commands) " in
132 *\ $command\ *)
133 run_py "$py2executable" "$@"
136 if [ -z ${MACH_PY2} ]
137 then
138 run_py "$py3executable" "$@"
139 else
140 if [ $command != "python-test" ]
141 then
142 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
143 exit 1
145 run_py "$py2executable" "$@"
148 esac
150 # Run Python 3 for everything else.
151 run_py "$py3executable" "$@"
154 from __future__ import absolute_import, print_function, unicode_literals
156 import os
157 import sys
159 def ancestors(path):
160 while path:
161 yield path
162 (path, child) = os.path.split(path)
163 if child == "":
164 break
166 def load_mach(dir_path, mach_path):
167 if sys.version_info < (3, 5):
168 import imp
169 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
170 else:
171 import importlib.util
172 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
173 mach_bootstrap = importlib.util.module_from_spec(spec)
174 spec.loader.exec_module(mach_bootstrap)
176 return mach_bootstrap.bootstrap(dir_path)
179 def check_and_get_mach(dir_path):
180 bootstrap_paths = (
181 'build/mach_bootstrap.py',
182 # test package bootstrap
183 'tools/mach_bootstrap.py',
185 for bootstrap_path in bootstrap_paths:
186 mach_path = os.path.join(dir_path, bootstrap_path)
187 if os.path.isfile(mach_path):
188 return load_mach(dir_path, mach_path)
189 return None
192 def setdefaultenv(key, value):
193 """Compatibility shim to ensure the proper string type is used with
194 os.environ for the version of Python being used.
196 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
198 if sys.version_info[0] == 2:
199 if isinstance(key, unicode):
200 key = key.encode(encoding)
201 if isinstance(value, unicode):
202 value = value.encode(encoding)
203 else:
204 if isinstance(key, bytes):
205 key = key.decode(encoding)
206 if isinstance(value, bytes):
207 value = value.decode(encoding)
209 os.environ.setdefault(key, value)
212 def get_mach():
213 # Check whether the current directory is within a mach src or obj dir.
214 for dir_path in ancestors(os.getcwd()):
215 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
216 config_status_path = os.path.join(dir_path, 'config.status')
217 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
218 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
219 import json
220 info = json.load(open(mozinfo_path))
221 if 'mozconfig' in info:
222 # If the MOZCONFIG environment variable is not already set, set it
223 # to the value from mozinfo.json. This will tell the build system
224 # to look for a config file at the path in $MOZCONFIG rather than
225 # its default locations.
226 setdefaultenv('MOZCONFIG', info['mozconfig'])
228 if 'topsrcdir' in info:
229 # Continue searching for mach_bootstrap in the source directory.
230 dir_path = info['topsrcdir']
232 mach = check_and_get_mach(dir_path)
233 if mach:
234 return mach
236 # If we didn't find a source path by scanning for a mozinfo.json, check
237 # whether the directory containing this script is a source directory. We
238 # follow symlinks so mach can be run even if cwd is outside the srcdir.
239 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
241 def main(args):
242 mach = get_mach()
243 if not mach:
244 print('Could not run mach: No mach source directory found.')
245 sys.exit(1)
246 sys.exit(mach.run(args))
249 if __name__ == '__main__':
250 main(sys.argv[1:])