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