Bug 1684463 - [devtools] Part 2: Split the appending attribute value logic out into...
[gecko.git] / mach
blob73791146a461bdad86d2ffa02c7291e165d0eeff
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 # Commands that are to be run with Python 2.
14 py2commands="
15 awsy-test
16 firefox-ui-functional
17 jsshell-bench
18 marionette-test
19 jstests
20 mozharness
21 raptor
22 raptor-test
23 telemetry-tests-client
24 test
25 wpt-metadata-merge
28 # Commands that are to be run with the system Python 3 instead of the
29 # virtualenv.
30 nativecmds="
31 bootstrap
32 create-mach-environment
33 install-moz-phab
36 run_py() {
37 # Try to run a specific Python interpreter.
38 py_executable="$1"
39 shift
40 if command -v "$py_executable" > /dev/null
41 then
42 exec "$py_executable" "$0" "$@"
43 else
44 echo "This mach command requires $py_executable, which wasn't found on the system!"
45 case "$py_executable" in
46 python2.7|python3) ;;
48 echo "Consider running 'mach bootstrap' or 'mach create-mach-environment' to create the mach virtualenvs, or set MACH_USE_SYSTEM_PYTHON to use the system Python installation over a virtualenv."
50 esac
51 exit 1
55 get_command() {
56 # Parse the name of the mach command out of the arguments. This is necessary
57 # in the presence of global mach arguments that come before the name of the
58 # command, e.g. `mach -v build`. We dispatch to the correct Python
59 # interpreter depending on the command.
60 while true; do
61 case $1 in
62 -v|--verbose) shift;;
63 -l|--log-file)
64 if [ "$#" -lt 2 ]
65 then
66 echo
67 break
68 else
69 shift 2
72 --log-interval) shift;;
73 --log-no-times) shift;;
74 -h) shift;;
75 --debug-command) shift;;
76 --settings)
77 if [ "$#" -lt 2 ]
78 then
79 echo
80 break
81 else
82 shift 2
85 # When running `./mach help <command>`, the correct Python for <command>
86 # needs to be used.
87 help) echo $2; break;;
88 # When running `./mach mach-completion /path/to/mach <command>`, the
89 # correct Python for <command> needs to be used.
90 mach-completion) echo $3; break;;
91 "") echo; break;;
92 *) echo $1; break;;
93 esac
94 done
97 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
98 command=$(get_command "$@")
100 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
101 # python{2.7,3} executables and not the virtualenv locations.
102 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
103 then
104 case "$OSTYPE" in
105 cygwin|msys|win32) bin_path=Scripts;;
106 *) bin_path=bin;;
107 esac
108 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
109 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
110 else
111 py2executable=python2.7
112 py3executable=python3
115 # Check whether we need to run with the native Python 3 interpreter.
116 case " $(echo $nativecmds) " in
117 *\ $command\ *)
118 run_py python3 "$@"
120 esac
122 # Check for the mach subcommand in the Python 2 commands list and run it
123 # with the correct interpreter.
124 case " $(echo $py2commands) " in
125 *\ $command\ *)
126 run_py "$py2executable" "$@"
129 if [ -z ${MACH_PY2} ]
130 then
131 run_py "$py3executable" "$@"
132 else
133 if [ $command != "python-test" ]
134 then
135 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
136 exit 1
138 run_py "$py2executable" "$@"
141 esac
143 # Run Python 3 for everything else.
144 run_py "$py3executable" "$@"
147 from __future__ import absolute_import, print_function, unicode_literals
149 import os
150 import sys
152 def ancestors(path):
153 while path:
154 yield path
155 (path, child) = os.path.split(path)
156 if child == "":
157 break
159 def load_mach(dir_path, mach_path):
160 if sys.version_info < (3, 5):
161 import imp
162 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
163 else:
164 import importlib.util
165 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
166 mach_bootstrap = importlib.util.module_from_spec(spec)
167 spec.loader.exec_module(mach_bootstrap)
169 return mach_bootstrap.bootstrap(dir_path)
172 def check_and_get_mach(dir_path):
173 bootstrap_paths = (
174 'build/mach_bootstrap.py',
175 # test package bootstrap
176 'tools/mach_bootstrap.py',
178 for bootstrap_path in bootstrap_paths:
179 mach_path = os.path.join(dir_path, bootstrap_path)
180 if os.path.isfile(mach_path):
181 return load_mach(dir_path, mach_path)
182 return None
185 def setdefaultenv(key, value):
186 """Compatibility shim to ensure the proper string type is used with
187 os.environ for the version of Python being used.
189 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
191 if sys.version_info[0] == 2:
192 if isinstance(key, unicode):
193 key = key.encode(encoding)
194 if isinstance(value, unicode):
195 value = value.encode(encoding)
196 else:
197 if isinstance(key, bytes):
198 key = key.decode(encoding)
199 if isinstance(value, bytes):
200 value = value.decode(encoding)
202 os.environ.setdefault(key, value)
205 def get_mach():
206 # Check whether the current directory is within a mach src or obj dir.
207 for dir_path in ancestors(os.getcwd()):
208 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
209 config_status_path = os.path.join(dir_path, 'config.status')
210 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
211 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
212 import json
213 info = json.load(open(mozinfo_path))
214 if 'mozconfig' in info:
215 # If the MOZCONFIG environment variable is not already set, set it
216 # to the value from mozinfo.json. This will tell the build system
217 # to look for a config file at the path in $MOZCONFIG rather than
218 # its default locations.
219 setdefaultenv('MOZCONFIG', info['mozconfig'])
221 if 'topsrcdir' in info:
222 # Continue searching for mach_bootstrap in the source directory.
223 dir_path = info['topsrcdir']
225 mach = check_and_get_mach(dir_path)
226 if mach:
227 return mach
229 # If we didn't find a source path by scanning for a mozinfo.json, check
230 # whether the directory containing this script is a source directory. We
231 # follow symlinks so mach can be run even if cwd is outside the srcdir.
232 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
234 def main(args):
235 mach = get_mach()
236 if not mach:
237 print('Could not run mach: No mach source directory found.')
238 sys.exit(1)
239 sys.exit(mach.run(args))
242 if __name__ == '__main__':
243 main(sys.argv[1:])