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