Bug 1550804 - Add color scheme simulation to the inspector. r=pbro
[gecko.git] / mach
blob5efde8b8df9b61bd5dd495504568db4153c11c0c
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 doc
36 doctor
37 empty-makefiles
38 environment
39 file-info
40 firefox-ui-functional
41 fluent-migration-test
42 geckodriver
43 geckodriver-test
44 geckoview-junit
45 gradle
46 gtest
47 ide
48 import-pr
49 install
50 jsapi-tests
51 jsshell-bench
52 jstestbrowser
53 jstests
54 marionette-test
55 mochitest
56 mozbuild-reference
57 mozharness
58 mozregression
59 package
60 package-multi-locale
61 pastebin
62 power
63 prettier-format
64 puppeteer-test
65 python
66 python-safety
67 python-test
68 raptor
69 raptor-test
70 reftest
71 release
72 release-history
73 remote
74 repackage
75 resource-usage
76 robocop
77 run
78 rusttests
79 show-log
80 static-analysis
81 talos-test
82 taskcluster-build-image
83 taskcluster-load-image
84 taskgraph
85 telemetry-tests-client
86 test
87 test-info
88 tps-build
89 try
90 valgrind-test
91 vendor
92 visualmetrics
93 warnings-list
94 warnings-summary
95 watch
96 web-platform-tests
97 web-platform-tests-update
98 webidl-example
99 webidl-parser-test
100 webrtc-gtest
102 wpt-manifest-update
103 wpt-metadata-merge
104 wpt-metadata-summary
105 wpt-serve
106 wpt-unittest
107 wpt-update
108 xpcshell-test
111 run_py() {
112 # Try to run a specific Python interpreter. Fall back to the system
113 # default Python if the specific interpreter couldn't be found.
114 py_executable="$1"
115 shift
116 if which "$py_executable" > /dev/null
117 then
118 exec "$py_executable" "$0" "$@"
119 elif [ "$py_executable" = "python2.7" ]; then
120 exec python "$0" "$@"
121 else
122 echo "This mach command requires $py_executable, which wasn't found on the system!"
123 exit 1
127 first_arg=$1
128 if [ "$first_arg" = "help" ]; then
129 # When running `./mach help <command>`, the correct Python for <command>
130 # needs to be used.
131 first_arg=$2
134 if [ -z "$first_arg" ]; then
135 # User ran `./mach` or `./mach help`, use Python 3.
136 run_py python3 "$@"
139 case "${first_arg}" in
140 "-"*)
141 # We have global arguments which are tricky to parse from this shell
142 # script. So invoke `mach` with a special --print-command argument to
143 # return the name of the command. This adds extra overhead when using
144 # global arguments, but global arguments are an edge case and this hack
145 # is only needed temporarily for the Python 3 migration. We use Python
146 # 2.7 because using Python 3 hits this error in build tasks:
147 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
148 command=`run_py python2.7 --print-command "$@" | tail -n1`
151 # In the common case, the first argument is the command.
152 command=${first_arg};
154 esac
156 # Check for the mach subcommand in the Python 2 commands list and run it
157 # with the correct interpreter.
158 case " $(echo $py2commands) " in
159 *\ $command\ *)
160 run_py python2.7 "$@"
163 run_py python3 "$@"
165 esac
167 # Run Python 3 for everything else.
168 run_py python3 "$@"
171 from __future__ import absolute_import, print_function, unicode_literals
173 import os
174 import sys
176 def ancestors(path):
177 while path:
178 yield path
179 (path, child) = os.path.split(path)
180 if child == "":
181 break
183 def load_mach(dir_path, mach_path):
184 if sys.version_info < (3, 5):
185 import imp
186 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
187 else:
188 import importlib.util
189 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
190 mach_bootstrap = importlib.util.module_from_spec(spec)
191 spec.loader.exec_module(mach_bootstrap)
193 return mach_bootstrap.bootstrap(dir_path)
196 def check_and_get_mach(dir_path):
197 bootstrap_paths = (
198 'build/mach_bootstrap.py',
199 # test package bootstrap
200 'tools/mach_bootstrap.py',
202 for bootstrap_path in bootstrap_paths:
203 mach_path = os.path.join(dir_path, bootstrap_path)
204 if os.path.isfile(mach_path):
205 return load_mach(dir_path, mach_path)
206 return None
209 def setdefaultenv(key, value):
210 """Compatibility shim to ensure the proper string type is used with
211 os.environ for the version of Python being used.
213 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
215 if sys.version_info[0] == 2:
216 if isinstance(key, unicode):
217 key = key.encode(encoding)
218 if isinstance(value, unicode):
219 value = value.encode(encoding)
220 else:
221 if isinstance(key, bytes):
222 key = key.decode(encoding)
223 if isinstance(value, bytes):
224 value = value.decode(encoding)
226 os.environ.setdefault(key, value)
229 def get_mach():
230 # Check whether the current directory is within a mach src or obj dir.
231 for dir_path in ancestors(os.getcwd()):
232 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
233 config_status_path = os.path.join(dir_path, 'config.status')
234 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
235 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
236 import json
237 info = json.load(open(mozinfo_path))
238 if 'mozconfig' in info:
239 # If the MOZCONFIG environment variable is not already set, set it
240 # to the value from mozinfo.json. This will tell the build system
241 # to look for a config file at the path in $MOZCONFIG rather than
242 # its default locations.
243 setdefaultenv('MOZCONFIG', info['mozconfig'])
245 if 'topsrcdir' in info:
246 # Continue searching for mach_bootstrap in the source directory.
247 dir_path = info['topsrcdir']
249 mach = check_and_get_mach(dir_path)
250 if mach:
251 return mach
253 # If we didn't find a source path by scanning for a mozinfo.json, check
254 # whether the directory containing this script is a source directory. We
255 # follow symlinks so mach can be run even if cwd is outside the srcdir.
256 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
258 def main(args):
259 mach = get_mach()
260 if not mach:
261 print('Could not run mach: No mach source directory found.')
262 sys.exit(1)
263 sys.exit(mach.run(args))
266 if __name__ == '__main__':
267 main(sys.argv[1:])