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