Bug 1637473 [wpt PR 23557] - De-flaky pointerevents/pointerevent_capture_mouse.html...
[gecko.git] / mach
blob2e213af9fb6b9f5eb836c15a9ce379fd40dc88d4
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 android
15 awsy-test
16 browsertime
17 check-spidermonkey
18 clang-format
19 cppunittest
20 cramtest
21 crashtest
22 devtools-css-db
23 firefox-ui-functional
24 geckodriver
25 geckodriver-test
26 geckoview-junit
27 gradle
28 gtest
29 hazards
30 jsapi-tests
31 jsshell-bench
32 jstestbrowser
33 jstests
34 marionette-test
35 mochitest
36 mozharness
37 pastebin
38 power
39 prettier-format
40 puppeteer-test
41 python
42 python-test
43 raptor
44 raptor-test
45 reftest
46 release
47 rusttests
48 static-analysis
49 talos-test
50 taskcluster-build-image
51 taskcluster-load-image
52 taskgraph
53 telemetry-tests-client
54 test
55 tps-build
56 visualmetrics
57 web-platform-tests
58 web-platform-tests-update
59 webidl-example
60 webidl-parser-test
61 wpt
62 wpt-manifest-update
63 wpt-metadata-merge
64 wpt-metadata-summary
65 wpt-serve
66 wpt-test-paths
67 wpt-unittest
68 wpt-update
69 xpcshell-test
72 run_py() {
73 # Try to run a specific Python interpreter. Fall back to the system
74 # default Python if the specific interpreter couldn't be found.
75 py_executable="$1"
76 shift
77 if which "$py_executable" > /dev/null
78 then
79 exec "$py_executable" "$0" "$@"
80 elif [ "$py_executable" = "python2.7" ]; then
81 exec python "$0" "$@"
82 else
83 echo "This mach command requires $py_executable, which wasn't found on the system!"
84 exit 1
88 first_arg=$1
89 if [ "$first_arg" = "help" ]; then
90 # When running `./mach help <command>`, the correct Python for <command>
91 # needs to be used.
92 first_arg=$2
93 elif [ "$first_arg" = "mach-completion" ]; then
94 # When running `./mach mach-completion /path/to/mach <command>`, the
95 # correct Python for <command> needs to be used.
96 first_arg=$3
99 if [ -z "$first_arg" ]; then
100 # User ran `./mach` or `./mach help`, use Python 3.
101 run_py python3 "$@"
104 case "${first_arg}" in
105 "-"*)
106 # We have global arguments which are tricky to parse from this shell
107 # script. So invoke `mach` with a special --print-command argument to
108 # return the name of the command. This adds extra overhead when using
109 # global arguments, but global arguments are an edge case and this hack
110 # is only needed temporarily for the Python 3 migration. We use Python
111 # 2.7 because using Python 3 hits this error in build tasks:
112 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
113 command=`run_py python2.7 --print-command "$@" | tail -n1`
116 # In the common case, the first argument is the command.
117 command=${first_arg};
119 esac
121 # Check for the mach subcommand in the Python 2 commands list and run it
122 # with the correct interpreter.
123 case " $(echo $py2commands) " in
124 *\ $command\ *)
125 run_py python2.7 "$@"
128 run_py python3 "$@"
130 esac
132 # Run Python 3 for everything else.
133 run_py python3 "$@"
136 from __future__ import absolute_import, print_function, unicode_literals
138 import os
139 import sys
141 def ancestors(path):
142 while path:
143 yield path
144 (path, child) = os.path.split(path)
145 if child == "":
146 break
148 def load_mach(dir_path, mach_path):
149 if sys.version_info < (3, 5):
150 import imp
151 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
152 else:
153 import importlib.util
154 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
155 mach_bootstrap = importlib.util.module_from_spec(spec)
156 spec.loader.exec_module(mach_bootstrap)
158 return mach_bootstrap.bootstrap(dir_path)
161 def check_and_get_mach(dir_path):
162 bootstrap_paths = (
163 'build/mach_bootstrap.py',
164 # test package bootstrap
165 'tools/mach_bootstrap.py',
167 for bootstrap_path in bootstrap_paths:
168 mach_path = os.path.join(dir_path, bootstrap_path)
169 if os.path.isfile(mach_path):
170 return load_mach(dir_path, mach_path)
171 return None
174 def setdefaultenv(key, value):
175 """Compatibility shim to ensure the proper string type is used with
176 os.environ for the version of Python being used.
178 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
180 if sys.version_info[0] == 2:
181 if isinstance(key, unicode):
182 key = key.encode(encoding)
183 if isinstance(value, unicode):
184 value = value.encode(encoding)
185 else:
186 if isinstance(key, bytes):
187 key = key.decode(encoding)
188 if isinstance(value, bytes):
189 value = value.decode(encoding)
191 os.environ.setdefault(key, value)
194 def get_mach():
195 # Check whether the current directory is within a mach src or obj dir.
196 for dir_path in ancestors(os.getcwd()):
197 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
198 config_status_path = os.path.join(dir_path, 'config.status')
199 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
200 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
201 import json
202 info = json.load(open(mozinfo_path))
203 if 'mozconfig' in info:
204 # If the MOZCONFIG environment variable is not already set, set it
205 # to the value from mozinfo.json. This will tell the build system
206 # to look for a config file at the path in $MOZCONFIG rather than
207 # its default locations.
208 setdefaultenv('MOZCONFIG', info['mozconfig'])
210 if 'topsrcdir' in info:
211 # Continue searching for mach_bootstrap in the source directory.
212 dir_path = info['topsrcdir']
214 mach = check_and_get_mach(dir_path)
215 if mach:
216 return mach
218 # If we didn't find a source path by scanning for a mozinfo.json, check
219 # whether the directory containing this script is a source directory. We
220 # follow symlinks so mach can be run even if cwd is outside the srcdir.
221 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
223 def main(args):
224 mach = get_mach()
225 if not mach:
226 print('Could not run mach: No mach source directory found.')
227 sys.exit(1)
228 sys.exit(mach.run(args))
231 if __name__ == '__main__':
232 main(sys.argv[1:])