Bug 1609972 [wpt PR 21238] - Deflake WPT test: required_csp-header.html, a=testonly
[gecko.git] / mach
blobf61a3a1c0d9aa692ad0e2c81d7e3372982cf829a
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 py2commands="
14 addtest
15 analyze
16 android
17 android-emulator
18 artifact
19 awsy-test
20 bootstrap
21 browsertime
22 build
23 build-backend
24 buildsymbols
25 busted
26 cargo
27 check-spidermonkey
28 clang-format
29 compileflags
30 configure
31 cppunittest
32 cramtest
33 crashtest
34 devtools-css-db
35 doctor
36 empty-makefiles
37 file-info
38 firefox-ui-functional
39 fluent-migration-test
40 geckodriver
41 geckodriver-test
42 geckoview-junit
43 gradle
44 gtest
45 ide
46 import-pr
47 install
48 jit-test
49 jsapi-tests
50 jsshell-bench
51 jstestbrowser
52 jstests
53 marionette-test
54 mochitest
55 mozbuild-reference
56 mozharness
57 mozregression
58 package
59 package-multi-locale
60 pastebin
61 power
62 prettier-format
63 puppeteer-test
64 python
65 python-test
66 raptor
67 raptor-test
68 reftest
69 release
70 release-history
71 remote
72 repackage
73 resource-usage
74 robocop
75 run
76 rusttests
77 show-log
78 static-analysis
79 talos-test
80 taskcluster-build-image
81 taskcluster-load-image
82 taskgraph
83 telemetry-tests-client
84 test
85 test-info
86 tps-build
87 try
88 valgrind-test
89 visualmetrics
90 warnings-list
91 warnings-summary
92 watch
93 web-platform-tests
94 web-platform-tests-update
95 webidl-example
96 webidl-parser-test
97 webrtc-gtest
98 wpt
99 wpt-manifest-update
100 wpt-metadata-merge
101 wpt-metadata-summary
102 wpt-serve
103 wpt-test-paths
104 wpt-unittest
105 wpt-update
106 xpcshell-test
109 run_py() {
110 # Try to run a specific Python interpreter. Fall back to the system
111 # default Python if the specific interpreter couldn't be found.
112 py_executable="$1"
113 shift
114 if which "$py_executable" > /dev/null
115 then
116 exec "$py_executable" "$0" "$@"
117 elif [ "$py_executable" = "python2.7" ]; then
118 exec python "$0" "$@"
119 else
120 echo "This mach command requires $py_executable, which wasn't found on the system!"
121 exit 1
125 first_arg=$1
126 if [ "$first_arg" = "help" ]; then
127 # When running `./mach help <command>`, the correct Python for <command>
128 # needs to be used.
129 first_arg=$2
130 elif [ "$first_arg" = "mach-completion" ]; then
131 # When running `./mach mach-completion /path/to/mach <command>`, the
132 # correct Python for <command> needs to be used.
133 first_arg=$3
136 if [ -z "$first_arg" ]; then
137 # User ran `./mach` or `./mach help`, use Python 3.
138 run_py python3 "$@"
141 case "${first_arg}" in
142 "-"*)
143 # We have global arguments which are tricky to parse from this shell
144 # script. So invoke `mach` with a special --print-command argument to
145 # return the name of the command. This adds extra overhead when using
146 # global arguments, but global arguments are an edge case and this hack
147 # is only needed temporarily for the Python 3 migration. We use Python
148 # 2.7 because using Python 3 hits this error in build tasks:
149 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
150 command=`run_py python2.7 --print-command "$@" | tail -n1`
153 # In the common case, the first argument is the command.
154 command=${first_arg};
156 esac
158 # Check for the mach subcommand in the Python 2 commands list and run it
159 # with the correct interpreter.
160 case " $(echo $py2commands) " in
161 *\ $command\ *)
162 run_py python2.7 "$@"
165 run_py python3 "$@"
167 esac
169 # Run Python 3 for everything else.
170 run_py python3 "$@"
173 from __future__ import absolute_import, print_function, unicode_literals
175 import os
176 import sys
178 def ancestors(path):
179 while path:
180 yield path
181 (path, child) = os.path.split(path)
182 if child == "":
183 break
185 def load_mach(dir_path, mach_path):
186 if sys.version_info < (3, 5):
187 import imp
188 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
189 else:
190 import importlib.util
191 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
192 mach_bootstrap = importlib.util.module_from_spec(spec)
193 spec.loader.exec_module(mach_bootstrap)
195 return mach_bootstrap.bootstrap(dir_path)
198 def check_and_get_mach(dir_path):
199 bootstrap_paths = (
200 'build/mach_bootstrap.py',
201 # test package bootstrap
202 'tools/mach_bootstrap.py',
204 for bootstrap_path in bootstrap_paths:
205 mach_path = os.path.join(dir_path, bootstrap_path)
206 if os.path.isfile(mach_path):
207 return load_mach(dir_path, mach_path)
208 return None
211 def setdefaultenv(key, value):
212 """Compatibility shim to ensure the proper string type is used with
213 os.environ for the version of Python being used.
215 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
217 if sys.version_info[0] == 2:
218 if isinstance(key, unicode):
219 key = key.encode(encoding)
220 if isinstance(value, unicode):
221 value = value.encode(encoding)
222 else:
223 if isinstance(key, bytes):
224 key = key.decode(encoding)
225 if isinstance(value, bytes):
226 value = value.decode(encoding)
228 os.environ.setdefault(key, value)
231 def get_mach():
232 # Check whether the current directory is within a mach src or obj dir.
233 for dir_path in ancestors(os.getcwd()):
234 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
235 config_status_path = os.path.join(dir_path, 'config.status')
236 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
237 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
238 import json
239 info = json.load(open(mozinfo_path))
240 if 'mozconfig' in info:
241 # If the MOZCONFIG environment variable is not already set, set it
242 # to the value from mozinfo.json. This will tell the build system
243 # to look for a config file at the path in $MOZCONFIG rather than
244 # its default locations.
245 setdefaultenv('MOZCONFIG', info['mozconfig'])
247 if 'topsrcdir' in info:
248 # Continue searching for mach_bootstrap in the source directory.
249 dir_path = info['topsrcdir']
251 mach = check_and_get_mach(dir_path)
252 if mach:
253 return mach
255 # If we didn't find a source path by scanning for a mozinfo.json, check
256 # whether the directory containing this script is a source directory. We
257 # follow symlinks so mach can be run even if cwd is outside the srcdir.
258 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
260 def main(args):
261 mach = get_mach()
262 if not mach:
263 print('Could not run mach: No mach source directory found.')
264 sys.exit(1)
265 sys.exit(mach.run(args))
268 if __name__ == '__main__':
269 main(sys.argv[1:])