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" $py_profile_command_args "$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;;
64 py_profile_command
="1"
79 return ${py_profile_command}
82 state_dir
=${MOZBUILD_STATE_PATH:-~/.mozbuild}
83 command=$
(get_command
"$@")
86 if [ ${py_profile_command} -eq 0 ]
88 py_profile_command_args
=""
90 # We would prefer to use an array variable here, but we're limited to POSIX.
91 # None of our arguments have quoting or spaces so we can safely interpolate
93 py_profile_command_args
="-m cProfile -o mach_profile_${command}.cProfile"
94 echo "Running with --profile-command. To visualize, use snakeviz:"
95 echo "$HOME/.mozbuild/_virtualenvs/mach/bin/python -m pip install snakeviz"
96 echo "$HOME/.mozbuild/_virtualenvs/mach/bin/python -m snakeviz mach_profile_${command}.cProfile"
99 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
100 # python 3 executables and not the virtualenv locations.
101 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
104 cygwin|msys|win32
) bin_path
=Scripts
;;
107 py3executable
=$state_dir/_virtualenvs
/mach
/$bin_path/python
109 py3executable
=python3
112 # Check whether we need to run with the native Python 3 interpreter.
113 case " $(echo $nativecmds) " in
119 # # Use the mach virtualenv's Python 3 for the rest of the commands.
120 run_py
"$py3executable" "$@"
123 from __future__ import absolute_import, print_function, unicode_literals
129 def load_mach(dir_path, mach_path):
130 import importlib.util
131 spec = importlib.util.spec_from_file_location('mach_initialize
', mach_path)
132 mach_initialize = importlib.util.module_from_spec(spec)
133 spec.loader.exec_module(mach_initialize)
134 return mach_initialize.initialize(dir_path)
137 def check_and_get_mach(dir_path):
139 'build
/mach_initialize.py
',
140 # test package initialize
141 'tools
/mach_initialize.py
',
143 for initialize_path in initialize_paths:
144 mach_path = os.path.join(dir_path, initialize_path)
145 if os.path.isfile(mach_path):
146 return load_mach(dir_path, mach_path)
151 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
152 # used when a python subprocess is created. This is an issue when we want
153 # to run using our virtualenv python executables.
154 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
155 # application code (mach) is started.
156 # https://github.com/python/cpython/pull/9516
157 os.environ.pop("__PYVENV_LAUNCHER__", None)
159 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
161 print('Could not run mach
: No mach
source directory found.
')
163 sys.exit(mach.run(args))
166 if __name__ == '__main__
':