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.
27 telemetry-tests-client
30 web-platform-tests-update
37 # Commands that are to be run with the system Python 3 instead of the
41 create-mach-environment
46 # Try to run a specific Python interpreter.
49 if command -v "$py_executable" > /dev
/null
51 exec "$py_executable" "$0" "$@"
53 echo "This mach command requires $py_executable, which wasn't found on the system!"
54 case "$py_executable" in
57 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."
65 # Parse the name of the mach command out of the arguments. This is necessary
66 # in the presence of global mach arguments that come before the name of the
67 # command, e.g. `mach -v build`. We dispatch to the correct Python
68 # interpreter depending on the command.
81 --log-interval) shift;;
82 --log-no-times) shift;;
84 --debug-command) shift;;
94 # When running `./mach help <command>`, the correct Python for <command>
96 help) echo $2; break;;
97 # When running `./mach mach-completion /path/to/mach <command>`, the
98 # correct Python for <command> needs to be used.
99 mach-completion
) echo $3; break;;
106 state_dir
=${MOZBUILD_STATE_PATH:-~/.mozbuild}
107 command=$
(get_command
"$@")
109 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
110 # python{2.7,3} executables and not the virtualenv locations.
111 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
114 cygwin|msys|win32
) bin_path
=Scripts
;;
117 py2executable
=$state_dir/_virtualenvs
/mach_py
2/$bin_path/python
118 py3executable
=$state_dir/_virtualenvs
/mach
/$bin_path/python
120 py2executable
=python2.7
121 py3executable
=python3
124 # Check whether we need to run with the native Python 3 interpreter.
125 case " $(echo $nativecmds) " in
131 # Check for the mach subcommand in the Python 2 commands list and run it
132 # with the correct interpreter.
133 case " $(echo $py2commands) " in
135 run_py
"$py2executable" "$@"
138 if [ -z ${MACH_PY2} ]
140 run_py
"$py3executable" "$@"
142 if [ $command != "python-test" ]
144 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
147 run_py
"$py2executable" "$@"
152 # Run Python 3 for everything else.
153 run_py
"$py3executable" "$@"
156 from __future__ import absolute_import, print_function, unicode_literals
164 (path, child) = os.path.split(path)
168 def load_mach(dir_path, mach_path):
169 if sys.version_info < (3, 5):
171 mach_bootstrap = imp.load_source('mach_bootstrap
', mach_path)
173 import importlib.util
174 spec = importlib.util.spec_from_file_location('mach_bootstrap
', mach_path)
175 mach_bootstrap = importlib.util.module_from_spec(spec)
176 spec.loader.exec_module(mach_bootstrap)
178 return mach_bootstrap.bootstrap(dir_path)
181 def check_and_get_mach(dir_path):
183 'build
/mach_bootstrap.py
',
184 # test package bootstrap
185 'tools
/mach_bootstrap.py
',
187 for bootstrap_path in bootstrap_paths:
188 mach_path = os.path.join(dir_path, bootstrap_path)
189 if os.path.isfile(mach_path):
190 return load_mach(dir_path, mach_path)
194 def setdefaultenv(key, value):
195 """Compatibility shim to ensure the proper string type is used with
196 os.environ for the version of Python being used.
198 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
200 if sys.version_info[0] == 2:
201 if isinstance(key, unicode):
202 key = key.encode(encoding)
203 if isinstance(value, unicode):
204 value = value.encode(encoding)
206 if isinstance(key, bytes):
207 key = key.decode(encoding)
208 if isinstance(value, bytes):
209 value = value.decode(encoding)
211 os.environ.setdefault(key, value)
215 # Check whether the current directory is within a mach src or obj dir.
216 for dir_path in ancestors(os.getcwd()):
217 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
218 config_status_path = os.path.join(dir_path, 'config.status
')
219 mozinfo_path = os.path.join(dir_path, 'mozinfo.json
')
220 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
222 info = json.load(open(mozinfo_path))
223 if 'mozconfig
' in info:
224 # If the MOZCONFIG environment variable is not already set, set it
225 # to the value from mozinfo.json. This will tell the build system
226 # to look for a config file at the path in $MOZCONFIG rather than
227 # its default locations.
228 setdefaultenv('MOZCONFIG
', info['mozconfig
'])
230 if 'topsrcdir
' in info:
231 # Continue searching for mach_bootstrap in the source directory.
232 dir_path = info['topsrcdir
']
234 mach = check_and_get_mach(dir_path)
238 # If we didn't
find a
source path by scanning
for a mozinfo.json
, check
239 # whether the directory containing this script is a source directory. We
240 # follow symlinks so mach can be run even if cwd is outside the srcdir.
241 return check_and_get_mach
(os.path.
dirname(os.path.realpath
(__file__
)))
246 print
('Could not run mach: No mach source directory found.')
248 sys.
exit(mach.run
(args
))
251 if __name__
== '__main__':