Bug 1627632 [wpt PR 22714] - Python 3: Improve webdriver tests compatibility with...
[gecko.git] / mach
blobd81b32cbedddd1e89e24874e57a2c2e59d9bf003
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 analyze
15 android
16 android-emulator
17 artifact
18 awsy-test
19 browsertime
20 buildsymbols
21 cargo
22 check-spidermonkey
23 clang-format
24 compileflags
25 cppunittest
26 cramtest
27 crashtest
28 devtools-css-db
29 doctor
30 empty-makefiles
31 file-info
32 firefox-ui-functional
33 geckodriver
34 geckodriver-test
35 geckoview-junit
36 gradle
37 gtest
38 hazards
39 ide
40 install
41 jsapi-tests
42 jsshell-bench
43 jstestbrowser
44 jstests
45 marionette-test
46 mochitest
47 mozbuild-reference
48 mozharness
49 package
50 package-multi-locale
51 pastebin
52 power
53 prettier-format
54 puppeteer-test
55 python
56 python-test
57 raptor
58 raptor-test
59 reftest
60 release
61 release-history
62 remote
63 repackage
64 resource-usage
65 robocop
66 run
67 rusttests
68 show-log
69 static-analysis
70 talos-test
71 taskcluster-build-image
72 taskcluster-load-image
73 taskgraph
74 telemetry-tests-client
75 test
76 test-info
77 tps-build
78 try
79 valgrind-test
80 visualmetrics
81 warnings-list
82 web-platform-tests
83 web-platform-tests-update
84 webidl-example
85 webidl-parser-test
86 webrtc-gtest
87 wpt
88 wpt-manifest-update
89 wpt-metadata-merge
90 wpt-metadata-summary
91 wpt-serve
92 wpt-test-paths
93 wpt-unittest
94 wpt-update
95 xpcshell-test
98 run_py() {
99 # Try to run a specific Python interpreter. Fall back to the system
100 # default Python if the specific interpreter couldn't be found.
101 py_executable="$1"
102 shift
103 if which "$py_executable" > /dev/null
104 then
105 exec "$py_executable" "$0" "$@"
106 elif [ "$py_executable" = "python2.7" ]; then
107 exec python "$0" "$@"
108 else
109 echo "This mach command requires $py_executable, which wasn't found on the system!"
110 exit 1
114 first_arg=$1
115 if [ "$first_arg" = "help" ]; then
116 # When running `./mach help <command>`, the correct Python for <command>
117 # needs to be used.
118 first_arg=$2
119 elif [ "$first_arg" = "mach-completion" ]; then
120 # When running `./mach mach-completion /path/to/mach <command>`, the
121 # correct Python for <command> needs to be used.
122 first_arg=$3
125 if [ -z "$first_arg" ]; then
126 # User ran `./mach` or `./mach help`, use Python 3.
127 run_py python3 "$@"
130 case "${first_arg}" in
131 "-"*)
132 # We have global arguments which are tricky to parse from this shell
133 # script. So invoke `mach` with a special --print-command argument to
134 # return the name of the command. This adds extra overhead when using
135 # global arguments, but global arguments are an edge case and this hack
136 # is only needed temporarily for the Python 3 migration. We use Python
137 # 2.7 because using Python 3 hits this error in build tasks:
138 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
139 command=`run_py python2.7 --print-command "$@" | tail -n1`
142 # In the common case, the first argument is the command.
143 command=${first_arg};
145 esac
147 # Check for the mach subcommand in the Python 2 commands list and run it
148 # with the correct interpreter.
149 case " $(echo $py2commands) " in
150 *\ $command\ *)
151 run_py python2.7 "$@"
154 run_py python3 "$@"
156 esac
158 # Run Python 3 for everything else.
159 run_py python3 "$@"
162 from __future__ import absolute_import, print_function, unicode_literals
164 import os
165 import sys
167 def ancestors(path):
168 while path:
169 yield path
170 (path, child) = os.path.split(path)
171 if child == "":
172 break
174 def load_mach(dir_path, mach_path):
175 if sys.version_info < (3, 5):
176 import imp
177 mach_bootstrap = imp.load_source('mach_bootstrap', mach_path)
178 else:
179 import importlib.util
180 spec = importlib.util.spec_from_file_location('mach_bootstrap', mach_path)
181 mach_bootstrap = importlib.util.module_from_spec(spec)
182 spec.loader.exec_module(mach_bootstrap)
184 return mach_bootstrap.bootstrap(dir_path)
187 def check_and_get_mach(dir_path):
188 bootstrap_paths = (
189 'build/mach_bootstrap.py',
190 # test package bootstrap
191 'tools/mach_bootstrap.py',
193 for bootstrap_path in bootstrap_paths:
194 mach_path = os.path.join(dir_path, bootstrap_path)
195 if os.path.isfile(mach_path):
196 return load_mach(dir_path, mach_path)
197 return None
200 def setdefaultenv(key, value):
201 """Compatibility shim to ensure the proper string type is used with
202 os.environ for the version of Python being used.
204 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
206 if sys.version_info[0] == 2:
207 if isinstance(key, unicode):
208 key = key.encode(encoding)
209 if isinstance(value, unicode):
210 value = value.encode(encoding)
211 else:
212 if isinstance(key, bytes):
213 key = key.decode(encoding)
214 if isinstance(value, bytes):
215 value = value.decode(encoding)
217 os.environ.setdefault(key, value)
220 def get_mach():
221 # Check whether the current directory is within a mach src or obj dir.
222 for dir_path in ancestors(os.getcwd()):
223 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
224 config_status_path = os.path.join(dir_path, 'config.status')
225 mozinfo_path = os.path.join(dir_path, 'mozinfo.json')
226 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
227 import json
228 info = json.load(open(mozinfo_path))
229 if 'mozconfig' in info:
230 # If the MOZCONFIG environment variable is not already set, set it
231 # to the value from mozinfo.json. This will tell the build system
232 # to look for a config file at the path in $MOZCONFIG rather than
233 # its default locations.
234 setdefaultenv('MOZCONFIG', info['mozconfig'])
236 if 'topsrcdir' in info:
237 # Continue searching for mach_bootstrap in the source directory.
238 dir_path = info['topsrcdir']
240 mach = check_and_get_mach(dir_path)
241 if mach:
242 return mach
244 # If we didn't find a source path by scanning for a mozinfo.json, check
245 # whether the directory containing this script is a source directory. We
246 # follow symlinks so mach can be run even if cwd is outside the srcdir.
247 return check_and_get_mach(os.path.dirname(os.path.realpath(__file__)))
249 def main(args):
250 mach = get_mach()
251 if not mach:
252 print('Could not run mach: No mach source directory found.')
253 sys.exit(1)
254 sys.exit(mach.run(args))
257 if __name__ == '__main__':
258 main(sys.argv[1:])