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