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