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.
23 telemetry-tests-client
28 # Commands that are to be run with the system Python 3 instead of the
32 create-mach-environment
37 # Try to run a specific Python interpreter.
40 if command -v "$py_executable" > /dev
/null
42 exec "$py_executable" "$0" "$@"
44 echo "This mach command requires $py_executable, which wasn't found on the system!"
45 case "$py_executable" in
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."
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.
72 --log-interval) shift;;
73 --log-no-times) shift;;
75 --debug-command) shift;;
85 # When running `./mach help <command>`, the correct Python for <command>
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;;
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} ]
105 cygwin|msys|win32
) bin_path
=Scripts
;;
108 py2executable
=$state_dir/_virtualenvs
/mach_py
2/$bin_path/python
109 py3executable
=$state_dir/_virtualenvs
/mach
/$bin_path/python
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
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
126 run_py
"$py2executable" "$@"
129 if [ -z ${MACH_PY2} ]
131 run_py
"$py3executable" "$@"
133 if [ $command != "python-test" ]
135 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
138 run_py
"$py2executable" "$@"
143 # Run Python 3 for everything else.
144 run_py
"$py3executable" "$@"
147 from __future__ import absolute_import, print_function, unicode_literals
152 def load_mach(dir_path, mach_path):
153 if sys.version_info < (3, 5):
155 mach_bootstrap = imp.load_source('mach_bootstrap
', mach_path)
157 import importlib.util
158 spec = importlib.util.spec_from_file_location('mach_bootstrap
', mach_path)
159 mach_bootstrap = importlib.util.module_from_spec(spec)
160 spec.loader.exec_module(mach_bootstrap)
162 return mach_bootstrap.bootstrap(dir_path)
165 def check_and_get_mach(dir_path):
167 'build
/mach_bootstrap.py
',
168 # test package bootstrap
169 'tools
/mach_bootstrap.py
',
171 for bootstrap_path in bootstrap_paths:
172 mach_path = os.path.join(dir_path, bootstrap_path)
173 if os.path.isfile(mach_path):
174 return load_mach(dir_path, mach_path)
179 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
180 # used when a python subprocess is created. This is an issue when we want
181 # to run using our virtualenv python executables.
182 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
183 # application code (mach) is started.
184 # https://github.com/python/cpython/pull/9516
185 os.environ.pop("__PYVENV_LAUNCHER__", None)
187 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
189 print('Could not run mach
: No mach
source directory found.
')
191 sys.exit(mach.run(args))
194 if __name__ == '__main__
':