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