Bug 1648429 [wpt PR 24338] - Python 3: port tests in service-workers [part 5], a...
[gecko.git] / mach
blob22988e1b57d2b00e4bec8b1b17f0bc07b7306799
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 py2commands="
14 android
15 awsy-test
16 check-spidermonkey
17 clang-format
18 cramtest
19 crashtest
20 devtools-css-db
21 firefox-ui-functional
22 geckodriver
23 geckodriver-test
24 geckoview-junit
25 hazards
26 jsapi-tests
27 jsshell-bench
28 marionette-test
29 jstestbrowser
30 jstests
31 mochitest
32 mozharness
33 prettier-format
34 python-test
35 raptor
36 raptor-test
37 reftest
38 release
39 talos-test
40 taskcluster-load-image
41 taskgraph
42 telemetry-tests-client
43 test
44 tps-build
45 visualmetrics
46 web-platform-tests
47 web-platform-tests-update
48 webidl-example
49 webidl-parser-test
50 wpt
51 wpt-manifest-update
52 wpt-metadata-merge
53 wpt-metadata-summary
54 wpt-serve
55 wpt-test-paths
56 wpt-unittest
57 wpt-update
58 xpcshell-test
61 run_py() {
62 # Try to run a specific Python interpreter. Fall back to the system
63 # default Python if the specific interpreter couldn't be found.
64 py_executable="$1"
65 shift
66 if which "$py_executable" > /dev/null
67 then
68 exec "$py_executable" "$0" "$@"
69 elif [ "$py_executable" = "python2.7" ]; then
70 exec python "$0" "$@"
71 else
72 echo "This mach command requires $py_executable, which wasn't found on the system!"
73 exit 1
77 first_arg=$1
78 if [ "$first_arg" = "help" ]; then
79 # When running `./mach help <command>`, the correct Python for <command>
80 # needs to be used.
81 first_arg=$2
82 elif [ "$first_arg" = "mach-completion" ]; then
83 # When running `./mach mach-completion /path/to/mach <command>`, the
84 # correct Python for <command> needs to be used.
85 first_arg=$3
88 if [ -z "$first_arg" ]; then
89 # User ran `./mach` or `./mach help`, use Python 3.
90 run_py python3 "$@"
93 case "${first_arg}" in
94 "-"*)
95 # We have global arguments which are tricky to parse from this shell
96 # script. So invoke `mach` with a special --print-command argument to
97 # return the name of the command. This adds extra overhead when using
98 # global arguments, but global arguments are an edge case and this hack
99 # is only needed temporarily for the Python 3 migration. We use Python
100 # 2.7 because using Python 3 hits this error in build tasks:
101 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
102 command=`run_py python2.7 --print-command "$@" | tail -n1`
105 # In the common case, the first argument is the command.
106 command=${first_arg};
108 esac
110 # Check for the mach subcommand in the Python 2 commands list and run it
111 # with the correct interpreter.
112 case " $(echo $py2commands) " in
113 *\ $command\ *)
114 run_py python2.7 "$@"
117 run_py python3 "$@"
119 esac
121 # Run Python 3 for everything else.
122 run_py python3 "$@"
125 from __future__ import absolute_import, print_function, unicode_literals
127 import os
128 import sys
130 def ancestors(path):
131 while path:
132 yield path
133 (path, child) = os.path.split(path)
134 if child == "":
135 break
137 def load_mach(dir_path, mach_path):
138 if sys.version_info < (3, 5):
139 import imp
140 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
141 else:
142 import importlib.util
143 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
144 mach_bootstrap = importlib.util.module_from_spec(spec)
145 spec.loader.exec_module(mach_bootstrap)
147 return mach_bootstrap.bootstrap(dir_path)
150 def check_and_get_mach(dir_path):
151 bootstrap_paths = (
152 'build/mach_bootstrap.py',
153 # test package bootstrap
154 'tools/mach_bootstrap.py',
156 for bootstrap_path in bootstrap_paths:
157 mach_path = os.path.join(dir_path, bootstrap_path)
158 if os.path.isfile(mach_path):
159 return load_mach(dir_path, mach_path)
160 return None
163 def setdefaultenv(key, value):
164 """Compatibility shim to ensure the proper string type is used with
165 os.environ for the version of Python being used.
167 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
169 if sys.version_info[0] == 2:
170 if isinstance(key, unicode):
171 key = key.encode(encoding)
172 if isinstance(value, unicode):
173 value = value.encode(encoding)
174 else:
175 if isinstance(key, bytes):
176 key = key.decode(encoding)
177 if isinstance(value, bytes):
178 value = value.decode(encoding)
180 os.environ.setdefault(key, value)
183 def get_mach():
184 # Check whether the current directory is within a mach src or obj dir.
185 for dir_path in ancestors(os.getcwd()):
186 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
187 config_status_path = os.path.join(dir_path, 'config.status')
188 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
189 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
190 import json
191 info = json.load(open(mozinfo_path))
192 if 'mozconfig' in info:
193 # If the MOZCONFIG environment variable is not already set, set it
194 # to the value from mozinfo.json. This will tell the build system
195 # to look for a config file at the path in $MOZCONFIG rather than
196 # its default locations.
197 setdefaultenv('MOZCONFIG', info['mozconfig'])
199 if 'topsrcdir' in info:
200 # Continue searching for mach_bootstrap in the source directory.
201 dir_path = info['topsrcdir']
203 mach = check_and_get_mach(dir_path)
204 if mach:
205 return mach
207 # If we didn't find a source path by scanning for a mozinfo.json, check
208 # whether the directory containing this script is a source directory. We
209 # follow symlinks so mach can be run even if cwd is outside the srcdir.
210 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
212 def main(args):
213 mach = get_mach()
214 if not mach:
215 print('Could not run mach: No mach source directory found.')
216 sys.exit(1)
217 sys.exit(mach.run(args))
220 if __name__ == '__main__':
221 main(sys.argv[1:])