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