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
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.
13 # Commands that are to be run with Python 2.
35 taskcluster-load-image
36 telemetry-tests-client
41 web-platform-tests-update
52 # Commands that are to be run with the system Python 3 instead of the
56 create-mach-environment
61 # Try to run a specific Python interpreter.
64 if command -v "$py_executable" > /dev
/null
66 exec "$py_executable" "$0" "$@"
68 echo "This mach command requires $py_executable, which wasn't found on the system!"
69 case "$py_executable" in
72 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."
80 # Parse the name of the mach command out of the arguments. This is necessary
81 # in the presence of global mach arguments that come before the name of the
82 # command, e.g. `mach -v build`. We dispatch to the correct Python
83 # interpreter depending on the command.
96 --log-interval) shift;;
97 --log-no-times) shift;;
99 --debug-command) shift;;
109 # When running `./mach help <command>`, the correct Python for <command>
111 help) echo $2; break;;
112 # When running `./mach mach-completion /path/to/mach <command>`, the
113 # correct Python for <command> needs to be used.
114 mach-completion
) echo $3; break;;
121 state_dir
=${MOZBUILD_STATE_PATH:-~/.mozbuild}
122 command=$
(get_command
"$@")
124 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
125 # python{2.7,3} executables and not the virtualenv locations.
126 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
129 cygwin|msys|win32
) bin_path
=Scripts
;;
132 py2executable
=$state_dir/_virtualenvs
/mach_py
2/$bin_path/python
133 py3executable
=$state_dir/_virtualenvs
/mach
/$bin_path/python
135 py2executable
=python2.7
136 py3executable
=python3
139 # Check whether we need to run with the native Python 3 interpreter.
140 case " $(echo $nativecmds) " in
146 # Check for the mach subcommand in the Python 2 commands list and run it
147 # with the correct interpreter.
148 case " $(echo $py2commands) " in
150 run_py
"$py2executable" "$@"
153 if [ -z ${MACH_PY2} ]
155 run_py
"$py3executable" "$@"
157 if [ $command != "python-test" ]
159 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
162 run_py
"$py2executable" "$@"
167 # Run Python 3 for everything else.
168 run_py
"$py3executable" "$@"
171 from __future__ import absolute_import, print_function, unicode_literals
179 (path, child) = os.path.split(path)
183 def load_mach(dir_path, mach_path):
184 if sys.version_info < (3, 5):
186 mach_bootstrap = imp.load_source('mach_bootstrap
', mach_path)
188 import importlib.util
189 spec = importlib.util.spec_from_file_location('mach_bootstrap
', mach_path)
190 mach_bootstrap = importlib.util.module_from_spec(spec)
191 spec.loader.exec_module(mach_bootstrap)
193 return mach_bootstrap.bootstrap(dir_path)
196 def check_and_get_mach(dir_path):
198 'build
/mach_bootstrap.py
',
199 # test package bootstrap
200 'tools
/mach_bootstrap.py
',
202 for bootstrap_path in bootstrap_paths:
203 mach_path = os.path.join(dir_path, bootstrap_path)
204 if os.path.isfile(mach_path):
205 return load_mach(dir_path, mach_path)
209 def setdefaultenv(key, value):
210 """Compatibility shim to ensure the proper string type is used with
211 os.environ for the version of Python being used.
213 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
215 if sys.version_info[0] == 2:
216 if isinstance(key, unicode):
217 key = key.encode(encoding)
218 if isinstance(value, unicode):
219 value = value.encode(encoding)
221 if isinstance(key, bytes):
222 key = key.decode(encoding)
223 if isinstance(value, bytes):
224 value = value.decode(encoding)
226 os.environ.setdefault(key, value)
230 # Check whether the current directory is within a mach src or obj dir.
231 for dir_path in ancestors(os.getcwd()):
232 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
233 config_status_path = os.path.join(dir_path, 'config.status
')
234 mozinfo_path = os.path.join(dir_path, 'mozinfo.json
')
235 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
237 info = json.load(open(mozinfo_path))
238 if 'mozconfig
' in info:
239 # If the MOZCONFIG environment variable is not already set, set it
240 # to the value from mozinfo.json. This will tell the build system
241 # to look for a config file at the path in $MOZCONFIG rather than
242 # its default locations.
243 setdefaultenv('MOZCONFIG
', info['mozconfig
'])
245 if 'topsrcdir
' in info:
246 # Continue searching for mach_bootstrap in the source directory.
247 dir_path = info['topsrcdir
']
249 mach = check_and_get_mach(dir_path)
253 # If we didn't
find a
source path by scanning
for a mozinfo.json
, check
254 # whether the directory containing this script is a source directory. We
255 # follow symlinks so mach can be run even if cwd is outside the srcdir.
256 return check_and_get_mach
(os.path.
dirname(os.path.realpath
(__file__
)))
261 print
('Could not run mach: No mach source directory found.')
263 sys.
exit(mach.run
(args
))
266 if __name__
== '__main__':