Bug 1698048 - Move browser/themes/shared/reader/readerMode.svg to the browser/themes...
[gecko.git] / mach
blobd41d77b00e53b86f6c82d99742d99f15c41a8b40
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="
15 mozharness
16 raptor
17 raptor-test
18 telemetry-tests-client
19 test
22 # Commands that are to be run with the system Python 3 instead of the
23 # virtualenv.
24 nativecmds="
25 bootstrap
26 create-mach-environment
27 install-moz-phab
30 run_py() {
31 # Try to run a specific Python interpreter.
32 py_executable="$1"
33 shift
34 if command -v "$py_executable" > /dev/null
35 then
36 exec "$py_executable" "$0" "$@"
37 else
38 echo "This mach command requires $py_executable, which wasn't found on the system!"
39 case "$py_executable" in
40 python2.7|python3) ;;
42 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."
44 esac
45 exit 1
49 get_command() {
50 # Parse the name of the mach command out of the arguments. This is necessary
51 # in the presence of global mach arguments that come before the name of the
52 # command, e.g. `mach -v build`. We dispatch to the correct Python
53 # interpreter depending on the command.
54 while true; do
55 case $1 in
56 -v|--verbose) shift;;
57 -l|--log-file)
58 if [ "$#" -lt 2 ]
59 then
60 echo
61 break
62 else
63 shift 2
66 --log-interval) shift;;
67 --log-no-times) shift;;
68 -h) shift;;
69 --debug-command) shift;;
70 --settings)
71 if [ "$#" -lt 2 ]
72 then
73 echo
74 break
75 else
76 shift 2
79 # When running `./mach help <command>`, the correct Python for <command>
80 # needs to be used.
81 help) echo $2; break;;
82 # When running `./mach mach-completion /path/to/mach <command>`, the
83 # correct Python for <command> needs to be used.
84 mach-completion) echo $3; break;;
85 "") echo; break;;
86 *) echo $1; break;;
87 esac
88 done
91 state_dir=${MOZBUILD_STATE_PATH:-~/.mozbuild}
92 command=$(get_command "$@")
94 # If MACH_USE_SYSTEM_PYTHON or MOZ_AUTOMATION are set, always use the
95 # python{2.7,3} executables and not the virtualenv locations.
96 if [ -z ${MACH_USE_SYSTEM_PYTHON} ] && [ -z ${MOZ_AUTOMATION} ]
97 then
98 case "$OSTYPE" in
99 cygwin|msys|win32) bin_path=Scripts;;
100 *) bin_path=bin;;
101 esac
102 py2executable=$state_dir/_virtualenvs/mach_py2/$bin_path/python
103 py3executable=$state_dir/_virtualenvs/mach/$bin_path/python
104 else
105 py2executable=python2.7
106 py3executable=python3
109 # Check whether we need to run with the native Python 3 interpreter.
110 case " $(echo $nativecmds) " in
111 *\ $command\ *)
112 run_py python3 "$@"
114 esac
116 # Check for the mach subcommand in the Python 2 commands list and run it
117 # with the correct interpreter.
118 case " $(echo $py2commands) " in
119 *\ $command\ *)
120 run_py "$py2executable" "$@"
123 if [ -z ${MACH_PY2} ]
124 then
125 run_py "$py3executable" "$@"
126 else
127 if [ $command != "python-test" ]
128 then
129 echo "MACH_PY2 is only valid for mach python-test; please unset MACH_PY2 to continue."
130 exit 1
132 run_py "$py2executable" "$@"
135 esac
137 # Run Python 3 for everything else.
138 run_py "$py3executable" "$@"
141 from __future__ import absolute_import, print_function, unicode_literals
143 import os
144 import sys
146 def load_mach(dir_path, mach_path):
147 if sys.version_info < (3, 5):
148 import imp
149 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
150 else:
151 import importlib.util
152 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
153 mach_bootstrap = importlib.util.module_from_spec(spec)
154 spec.loader.exec_module(mach_bootstrap)
156 return mach_bootstrap.bootstrap(dir_path)
159 def check_and_get_mach(dir_path):
160 bootstrap_paths = (
161 'build/mach_bootstrap.py',
162 # test package bootstrap
163 'tools/mach_bootstrap.py',
165 for bootstrap_path in bootstrap_paths:
166 mach_path = os.path.join(dir_path, bootstrap_path)
167 if os.path.isfile(mach_path):
168 return load_mach(dir_path, mach_path)
169 return None
172 def main(args):
173 # XCode python sets __PYVENV_LAUNCHER__, which overrides the executable
174 # used when a python subprocess is created. This is an issue when we want
175 # to run using our virtualenv python executables.
176 # In future Python relases, __PYVENV_LAUNCHER__ will be cleared before
177 # application code (mach) is started.
178 # https://github.com/python/cpython/pull/9516
179 os.environ.pop("__PYVENV_LAUNCHER__", None)
181 mach = check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
182 if not mach:
183 print('Could not run mach: No mach source directory found.')
184 sys.exit(1)
185 sys.exit(mach.run(args))
188 if __name__ == '__main__':
189 main(sys.argv[1:])