Bug 1698786: part 7) Add more `MOZ_LOG`ging to `mozInlineSpellWordUtil`. r=masayuki
[gecko.git] / mach
blob019b48bf26129516a580464c4beee7a7a42c893a
1 #!/bin/sh
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
8 # the right Python.
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.
12 ''':'
13 # Commands that are to be run with Python 2.
14 py2commands="
17 # Commands that are to be run with the system Python 3 instead of the
18 # virtualenv.
19 nativecmds="
20 bootstrap
21 create-mach-environment
22 install-moz-phab
25 run_py() {
26 # Try to run a specific Python interpreter.
27 py_executable="$1"
28 shift
29 if command -v "$py_executable" > /dev/null
30 then
31 exec "$py_executable" "$0" "$@"
32 else
33 echo "This mach command requires $py_executable, which wasn't found on the system!"
34 case "$py_executable" in
35 python2.7|python3) ;;
37 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."
39 esac
40 exit 1
44 get_command() {
45 # Parse the name of the mach command out of the arguments. This is necessary
46 # in the presence of global mach arguments that come before the name of the
47 # command, e.g. `mach -v build`. We dispatch to the correct Python
48 # interpreter depending on the command.
49 while true; do
50 case $1 in
51 -v|--verbose) shift;;
52 -l|--log-file)
53 if [ "$#" -lt 2 ]
54 then
55 echo
56 break
57 else
58 shift 2
61 --no-interactive) shift;;
62 --log-interval) shift;;
63 --log-no-times) shift;;
64 -h) shift;;
65 --debug-command) shift;;
66 --settings)
67 if [ "$#" -lt 2 ]
68 then
69 echo
70 break
71 else
72 shift 2
75 # When running `./mach help <command>`, the correct Python for <command>
76 # needs to be used.
77 help) echo $2; break;;
78 # When running `./mach mach-completion /path/to/mach <command>`, the
79 # correct Python for <command> needs to be used.
80 mach-completion) echo $3; break;;
81 "") echo; break;;
82 *) echo $1; break;;
83 esac
84 done
87 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
88 command=$(get_command "$@")
90 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
91 # python{2.7,3} executables and not the virtualenv locations.
92 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
93 then
94 case "$OSTYPE" in
95 cygwin|msys|win32) bin_path=Scripts;;
96 *) bin_path=bin;;
97 esac
98 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
99 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
100 else
101 py2executable=python2.7
102 py3executable=python3
105 # Check whether we need to run with the native Python 3 interpreter.
106 case " $(echo $nativecmds) " in
107 *\ $command\ *)
108 run_py python3 "$@"
110 esac
112 # Check for the mach subcommand in the Python 2 commands list and run it
113 # with the correct interpreter.
114 case " $(echo $py2commands) " in
115 *\ $command\ *)
116 run_py "$py2executable" "$@"
119 if [ -z ${MACH_PY2} ]
120 then
121 run_py "$py3executable" "$@"
122 else
123 if [ $command != "python-test" ]
124 then
125 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
126 exit 1
128 run_py "$py2executable" "$@"
131 esac
133 # Run Python 3 for everything else.
134 run_py "$py3executable" "$@"
137 from __future__ import absolute_import, print_function, unicode_literals
139 import os
140 import sys
142 def load_mach(dir_path, mach_path):
143 if sys.version_info < (3, 5):
144 import imp
145 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
146 else:
147 import importlib.util
148 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
149 mach_bootstrap = importlib.util.module_from_spec(spec)
150 spec.loader.exec_module(mach_bootstrap)
152 return mach_bootstrap.bootstrap(dir_path)
155 def check_and_get_mach(dir_path):
156 bootstrap_paths = (
157 'build/mach_bootstrap.py',
158 # test package bootstrap
159 'tools/mach_bootstrap.py',
161 for bootstrap_path in bootstrap_paths:
162 mach_path = os.path.join(dir_path, bootstrap_path)
163 if os.path.isfile(mach_path):
164 return load_mach(dir_path, mach_path)
165 return None
168 def main(args):
169 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
170 # used when a python subprocess is created. This is an issue when we want
171 # to run using our virtualenv python executables.
172 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
173 # application code (mach) is started.
174 # https://github.com/python/cpython/pull/9516
175 os.environ.pop("__PYVENV_LAUNCHER__", None)
177 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
178 if not mach:
179 print('Could not run mach: No mach source directory found.')
180 sys.exit(1)
181 sys.exit(mach.run(args))
184 if __name__ == '__main__':
185 main(sys.argv[1:])