Bug 1670880 [wpt PR 26070] - Remove an old TTWF CSS Masking test, a=testonly
[gecko.git] / mach
blob9469b0c680359a5fbdf2a7f07cbf959ab9fbd763
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 android
16 awsy-test
17 check-spidermonkey
18 cramtest
19 crashtest
20 devtools-css-db
21 firefox-ui-functional
22 geckodriver
23 geckodriver-test
24 jsapi-tests
25 jsshell-bench
26 marionette-test
27 jstestbrowser
28 jstests
29 mozharness
30 prettier-format
31 raptor
32 raptor-test
33 reftest
34 talos-test
35 taskcluster-load-image
36 telemetry-tests-client
37 test
38 tps-build
39 visualmetrics
40 web-platform-tests
41 web-platform-tests-update
42 wpt
43 wpt-manifest-update
44 wpt-metadata-merge
45 wpt-metadata-summary
46 wpt-serve
47 wpt-test-paths
48 wpt-unittest
49 wpt-update
52 # Commands that are to be run with the system Python 3 instead of the
53 # virtualenv.
54 nativecmds="
55 bootstrap
56 create-mach-environment
57 install-moz-phab
60 run_py() {
61 # Try to run a specific Python interpreter.
62 py_executable="$1"
63 shift
64 if command -v "$py_executable" > /dev/null
65 then
66 exec "$py_executable" "$0" "$@"
67 else
68 echo "This mach command requires $py_executable, which wasn't found on the system!"
69 case "$py_executable" in
70 python2.7|python3) ;;
72 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."
74 esac
75 exit 1
79 get_command() {
80 # Parse the name of the mach command out of the arguments. This is necessary
81 # in the presence of global mach arguments that come before the name of the
82 # command, e.g. `mach -v build`. We dispatch to the correct Python
83 # interpreter depending on the command.
84 while true; do
85 case $1 in
86 -v|--verbose) shift;;
87 -l|--log-file)
88 if [ "$#" -lt 2 ]
89 then
90 echo
91 break
92 else
93 shift 2
96 --log-interval) shift;;
97 --log-no-times) shift;;
98 -h) shift;;
99 --debug-command) shift;;
100 --settings)
101 if [ "$#" -lt 2 ]
102 then
103 echo
104 break
105 else
106 shift 2
109 # When running `./mach help <command>`, the correct Python for <command>
110 # needs to be used.
111 help) echo $2; break;;
112 # When running `./mach mach-completion /path/to/mach <command>`, the
113 # correct Python for <command> needs to be used.
114 mach-completion) echo $3; break;;
115 "") echo; break;;
116 *) echo $1; break;;
117 esac
118 done
121 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
122 command=$(get_command "$@")
124 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
125 # python{2.7,3} executables and not the virtualenv locations.
126 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
127 then
128 case "$OSTYPE" in
129 cygwin|msys|win32) bin_path=Scripts;;
130 *) bin_path=bin;;
131 esac
132 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
133 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
134 else
135 py2executable=python2.7
136 py3executable=python3
139 # Check whether we need to run with the native Python 3 interpreter.
140 case " $(echo $nativecmds) " in
141 *\ $command\ *)
142 run_py python3 "$@"
144 esac
146 # Check for the mach subcommand in the Python 2 commands list and run it
147 # with the correct interpreter.
148 case " $(echo $py2commands) " in
149 *\ $command\ *)
150 run_py "$py2executable" "$@"
153 if [ -z ${MACH_PY2} ]
154 then
155 run_py "$py3executable" "$@"
156 else
157 if [ $command != "python-test" ]
158 then
159 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
160 exit 1
162 run_py "$py2executable" "$@"
165 esac
167 # Run Python 3 for everything else.
168 run_py "$py3executable" "$@"
171 from __future__ import absolute_import, print_function, unicode_literals
173 import os
174 import sys
176 def ancestors(path):
177 while path:
178 yield path
179 (path, child) = os.path.split(path)
180 if child == "":
181 break
183 def load_mach(dir_path, mach_path):
184 if sys.version_info < (3, 5):
185 import imp
186 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
187 else:
188 import importlib.util
189 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
190 mach_bootstrap = importlib.util.module_from_spec(spec)
191 spec.loader.exec_module(mach_bootstrap)
193 return mach_bootstrap.bootstrap(dir_path)
196 def check_and_get_mach(dir_path):
197 bootstrap_paths = (
198 'build/mach_bootstrap.py',
199 # test package bootstrap
200 'tools/mach_bootstrap.py',
202 for bootstrap_path in bootstrap_paths:
203 mach_path = os.path.join(dir_path, bootstrap_path)
204 if os.path.isfile(mach_path):
205 return load_mach(dir_path, mach_path)
206 return None
209 def setdefaultenv(key, value):
210 """Compatibility shim to ensure the proper string type is used with
211 os.environ for the version of Python being used.
213 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
215 if sys.version_info[0] == 2:
216 if isinstance(key, unicode):
217 key = key.encode(encoding)
218 if isinstance(value, unicode):
219 value = value.encode(encoding)
220 else:
221 if isinstance(key, bytes):
222 key = key.decode(encoding)
223 if isinstance(value, bytes):
224 value = value.decode(encoding)
226 os.environ.setdefault(key, value)
229 def get_mach():
230 # Check whether the current directory is within a mach src or obj dir.
231 for dir_path in ancestors(os.getcwd()):
232 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
233 config_status_path = os.path.join(dir_path, 'config.status')
234 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
235 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
236 import json
237 info = json.load(open(mozinfo_path))
238 if 'mozconfig' in info:
239 # If the MOZCONFIG environment variable is not already set, set it
240 # to the value from mozinfo.json. This will tell the build system
241 # to look for a config file at the path in $MOZCONFIG rather than
242 # its default locations.
243 setdefaultenv('MOZCONFIG', info['mozconfig'])
245 if 'topsrcdir' in info:
246 # Continue searching for mach_bootstrap in the source directory.
247 dir_path = info['topsrcdir']
249 mach = check_and_get_mach(dir_path)
250 if mach:
251 return mach
253 # If we didn't find a source path by scanning for a mozinfo.json, check
254 # whether the directory containing this script is a source directory. We
255 # follow symlinks so mach can be run even if cwd is outside the srcdir.
256 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
258 def main(args):
259 mach = get_mach()
260 if not mach:
261 print('Could not run mach: No mach source directory found.')
262 sys.exit(1)
263 sys.exit(mach.run(args))
266 if __name__ == '__main__':
267 main(sys.argv[1:])