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