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