Bug 1649121: part 58) Change `DetermineLastNode` to return the found node. r=masayuki
[gecko.git] / mach
blob9482cdafe01d2933bb3dc58f4e48941f51507c38
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 raptor
33 raptor-test
34 reftest
35 talos-test
36 taskcluster-load-image
37 telemetry-tests-client
38 test
39 tps-build
40 visualmetrics
41 web-platform-tests
42 web-platform-tests-update
43 wpt
44 wpt-manifest-update
45 wpt-metadata-merge
46 wpt-metadata-summary
47 wpt-serve
48 wpt-test-paths
49 wpt-unittest
50 wpt-update
53 run_py() {
54 # Try to run a specific Python interpreter. Fall back to the system
55 # default Python if the specific interpreter couldn't be found.
56 py_executable="$1"
57 shift
58 if which "$py_executable" > /dev/null
59 then
60 exec "$py_executable" "$0" "$@"
61 elif [ "$py_executable" = "python2.7" ]; then
62 exec python "$0" "$@"
63 else
64 echo "This mach command requires $py_executable, which wasn't found on the system!"
65 exit 1
69 first_arg=$1
70 if [ "$first_arg" = "help" ]; then
71 # When running `./mach help <command>`, the correct Python for <command>
72 # needs to be used.
73 first_arg=$2
74 elif [ "$first_arg" = "mach-completion" ]; then
75 # When running `./mach mach-completion /path/to/mach <command>`, the
76 # correct Python for <command> needs to be used.
77 first_arg=$3
80 if [ -z "$first_arg" ]; then
81 # User ran `./mach` or `./mach help`, use Python 3.
82 run_py python3 "$@"
85 case "${first_arg}" in
86 "-"*)
87 # We have global arguments which are tricky to parse from this shell
88 # script. So invoke `mach` with a special --print-command argument to
89 # return the name of the command. This adds extra overhead when using
90 # global arguments, but global arguments are an edge case and this hack
91 # is only needed temporarily for the Python 3 migration. We use Python
92 # 2.7 because using Python 3 hits this error in build tasks:
93 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
94 command=`run_py python2.7 --print-command "$@" | tail -n1`
97 # In the common case, the first argument is the command.
98 command=${first_arg};
100 esac
102 # Check for the mach subcommand in the Python 2 commands list and run it
103 # with the correct interpreter.
104 case " $(echo $py2commands) " in
105 *\ $command\ *)
106 run_py python2.7 "$@"
109 run_py python3 "$@"
111 esac
113 # Run Python 3 for everything else.
114 run_py python3 "$@"
117 from __future__ import absolute_import, print_function, unicode_literals
119 import os
120 import sys
122 def ancestors(path):
123 while path:
124 yield path
125 (path, child) = os.path.split(path)
126 if child == "":
127 break
129 def load_mach(dir_path, mach_path):
130 if sys.version_info < (3, 5):
131 import imp
132 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
133 else:
134 import importlib.util
135 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
136 mach_bootstrap = importlib.util.module_from_spec(spec)
137 spec.loader.exec_module(mach_bootstrap)
139 return mach_bootstrap.bootstrap(dir_path)
142 def check_and_get_mach(dir_path):
143 bootstrap_paths = (
144 'build/mach_bootstrap.py',
145 # test package bootstrap
146 'tools/mach_bootstrap.py',
148 for bootstrap_path in bootstrap_paths:
149 mach_path = os.path.join(dir_path, bootstrap_path)
150 if os.path.isfile(mach_path):
151 return load_mach(dir_path, mach_path)
152 return None
155 def setdefaultenv(key, value):
156 """Compatibility shim to ensure the proper string type is used with
157 os.environ for the version of Python being used.
159 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
161 if sys.version_info[0] == 2:
162 if isinstance(key, unicode):
163 key = key.encode(encoding)
164 if isinstance(value, unicode):
165 value = value.encode(encoding)
166 else:
167 if isinstance(key, bytes):
168 key = key.decode(encoding)
169 if isinstance(value, bytes):
170 value = value.decode(encoding)
172 os.environ.setdefault(key, value)
175 def get_mach():
176 # Check whether the current directory is within a mach src or obj dir.
177 for dir_path in ancestors(os.getcwd()):
178 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
179 config_status_path = os.path.join(dir_path, 'config.status')
180 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
181 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
182 import json
183 info = json.load(open(mozinfo_path))
184 if 'mozconfig' in info:
185 # If the MOZCONFIG environment variable is not already set, set it
186 # to the value from mozinfo.json. This will tell the build system
187 # to look for a config file at the path in $MOZCONFIG rather than
188 # its default locations.
189 setdefaultenv('MOZCONFIG', info['mozconfig'])
191 if 'topsrcdir' in info:
192 # Continue searching for mach_bootstrap in the source directory.
193 dir_path = info['topsrcdir']
195 mach = check_and_get_mach(dir_path)
196 if mach:
197 return mach
199 # If we didn't find a source path by scanning for a mozinfo.json, check
200 # whether the directory containing this script is a source directory. We
201 # follow symlinks so mach can be run even if cwd is outside the srcdir.
202 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
204 def main(args):
205 mach = get_mach()
206 if not mach:
207 print('Could not run mach: No mach source directory found.')
208 sys.exit(1)
209 sys.exit(mach.run(args))
212 if __name__ == '__main__':
213 main(sys.argv[1:])