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.
14 # Commands that are to be run with the system Python 3 instead of the
18 create-mach-environment
23 # Try to run a specific Python interpreter.
26 if command -v "$py_executable" > /dev
/null
28 exec "$py_executable" "$0" "$@"
30 echo "This mach command requires $py_executable, which wasn't found on the system!"
31 case "$py_executable" in
34 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."
42 # Parse the name of the mach command out of the arguments. This is necessary
43 # in the presence of global mach arguments that come before the name of the
44 # command, e.g. `mach -v build`. We dispatch to the correct Python
45 # interpreter depending on the command.
58 --no-interactive) shift;;
59 --log-interval) shift;;
60 --log-no-times) shift;;
62 --debug-command) shift;;
78 state_dir
=${MOZBUILD_STATE_PATH:-~/.mozbuild}
79 command=$
(get_command
"$@")
81 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
82 # python 3 executables and not the virtualenv locations.
83 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
86 cygwin|msys|win32
) bin_path
=Scripts
;;
89 py3executable
=$state_dir/_virtualenvs
/mach
/$bin_path/python
94 # Check whether we need to run with the native Python 3 interpreter.
95 case " $(echo $nativecmds) " in
101 # # Use the mach virtualenv's Python 3 for the rest of the commands.
102 run_py
"$py3executable" "$@"
105 from __future__ import absolute_import, print_function, unicode_literals
110 def load_mach(dir_path, mach_path):
111 if sys.version_info < (3, 5):
113 mach_bootstrap = imp.load_source('mach_bootstrap
', mach_path)
115 import importlib.util
116 spec = importlib.util.spec_from_file_location('mach_bootstrap
', mach_path)
117 mach_bootstrap = importlib.util.module_from_spec(spec)
118 spec.loader.exec_module(mach_bootstrap)
120 return mach_bootstrap.bootstrap(dir_path)
123 def check_and_get_mach(dir_path):
125 'build
/mach_bootstrap.py
',
126 # test package bootstrap
127 'tools
/mach_bootstrap.py
',
129 for bootstrap_path in bootstrap_paths:
130 mach_path = os.path.join(dir_path, bootstrap_path)
131 if os.path.isfile(mach_path):
132 return load_mach(dir_path, mach_path)
137 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
138 # used when a python subprocess is created. This is an issue when we want
139 # to run using our virtualenv python executables.
140 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
141 # application code (mach) is started.
142 # https://github.com/python/cpython/pull/9516
143 os.environ.pop("__PYVENV_LAUNCHER__", None)
145 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
147 print('Could not run mach
: No mach
source directory found.
')
149 sys.exit(mach.run(args))
152 if __name__ == '__main__
':