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
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.
37 taskcluster-load-image
38 telemetry-tests-client
43 web-platform-tests-update
55 # Try to run a specific Python interpreter. Fall back to the system
56 # default Python if the specific interpreter couldn't be found.
59 if which "$py_executable" > /dev
/null
61 exec "$py_executable" "$0" "$@"
62 elif [ "$py_executable" = "python2.7" ]; then
65 echo "This mach command requires $py_executable, which wasn't found on the system!"
71 if [ "$first_arg" = "help" ]; then
72 # When running `./mach help <command>`, the correct Python for <command>
75 elif [ "$first_arg" = "mach-completion" ]; then
76 # When running `./mach mach-completion /path/to/mach <command>`, the
77 # correct Python for <command> needs to be used.
81 if [ -z "$first_arg" ]; then
82 # User ran `./mach` or `./mach help`, use Python 3.
86 case "${first_arg}" in
88 # We have global arguments which are tricky to parse from this shell
89 # script. So invoke `mach` with a special --print-command argument to
90 # return the name of the command. This adds extra overhead when using
91 # global arguments, but global arguments are an edge case and this hack
92 # is only needed temporarily for the Python 3 migration. We use Python
93 # 2.7 because using Python 3 hits this error in build tasks:
94 # https://searchfox.org/mozilla-central/rev/c7e8bc4996f9/build/moz.configure/init.configure#319
95 command=`run_py python2.7 --print-command "$@" | tail -n1`
98 # In the common case, the first argument is the command.
103 # Check for the mach subcommand in the Python 2 commands list and run it
104 # with the correct interpreter.
105 case " $(echo $py2commands) " in
107 run_py python2.7
"$@"
114 # Run Python 3 for everything else.
118 from __future__ import absolute_import, print_function, unicode_literals
126 (path, child) = os.path.split(path)
130 def load_mach(dir_path, mach_path):
131 if sys.version_info < (3, 5):
133 mach_bootstrap = imp.load_source('mach_bootstrap
', mach_path)
135 import importlib.util
136 spec = importlib.util.spec_from_file_location('mach_bootstrap
', mach_path)
137 mach_bootstrap = importlib.util.module_from_spec(spec)
138 spec.loader.exec_module(mach_bootstrap)
140 return mach_bootstrap.bootstrap(dir_path)
143 def check_and_get_mach(dir_path):
145 'build
/mach_bootstrap.py
',
146 # test package bootstrap
147 'tools
/mach_bootstrap.py
',
149 for bootstrap_path in bootstrap_paths:
150 mach_path = os.path.join(dir_path, bootstrap_path)
151 if os.path.isfile(mach_path):
152 return load_mach(dir_path, mach_path)
156 def setdefaultenv(key, value):
157 """Compatibility shim to ensure the proper string type is used with
158 os.environ for the version of Python being used.
160 encoding = "mbcs" if sys.platform == "win32" else "utf-8"
162 if sys.version_info[0] == 2:
163 if isinstance(key, unicode):
164 key = key.encode(encoding)
165 if isinstance(value, unicode):
166 value = value.encode(encoding)
168 if isinstance(key, bytes):
169 key = key.decode(encoding)
170 if isinstance(value, bytes):
171 value = value.decode(encoding)
173 os.environ.setdefault(key, value)
177 # Check whether the current directory is within a mach src or obj dir.
178 for dir_path in ancestors(os.getcwd()):
179 # If we find a "config.status" and "mozinfo.json" file, we are in the objdir.
180 config_status_path = os.path.join(dir_path, 'config.status
')
181 mozinfo_path = os.path.join(dir_path, 'mozinfo.json
')
182 if os.path.isfile(config_status_path) and os.path.isfile(mozinfo_path):
184 info = json.load(open(mozinfo_path))
185 if 'mozconfig
' in info:
186 # If the MOZCONFIG environment variable is not already set, set it
187 # to the value from mozinfo.json. This will tell the build system
188 # to look for a config file at the path in $MOZCONFIG rather than
189 # its default locations.
190 setdefaultenv('MOZCONFIG
', info['mozconfig
'])
192 if 'topsrcdir
' in info:
193 # Continue searching for mach_bootstrap in the source directory.
194 dir_path = info['topsrcdir
']
196 mach = check_and_get_mach(dir_path)
200 # If we didn't
find a
source path by scanning
for a mozinfo.json
, check
201 # whether the directory containing this script is a source directory. We
202 # follow symlinks so mach can be run even if cwd is outside the srcdir.
203 return check_and_get_mach
(os.path.
dirname(os.path.realpath
(__file__
)))
208 print
('Could not run mach: No mach source directory found.')
210 sys.
exit(mach.run
(args
))
213 if __name__
== '__main__':