Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | #!/bin/sh |
michael@0 | 2 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 5 | |
michael@0 | 6 | # The beginning of this script is both valid shell and valid python, |
michael@0 | 7 | # such that the script starts with the shell and is reexecuted with |
michael@0 | 8 | # the right python. |
michael@0 | 9 | '''which' python2.7 > /dev/null && exec python2.7 "$0" "$@" || exec python "$0" "$@" |
michael@0 | 10 | ''' |
michael@0 | 11 | |
michael@0 | 12 | from __future__ import print_function, unicode_literals |
michael@0 | 13 | |
michael@0 | 14 | import os |
michael@0 | 15 | import sys |
michael@0 | 16 | |
michael@0 | 17 | def ancestors(path): |
michael@0 | 18 | while path: |
michael@0 | 19 | yield path |
michael@0 | 20 | (path, child) = os.path.split(path) |
michael@0 | 21 | if child == "": |
michael@0 | 22 | break |
michael@0 | 23 | |
michael@0 | 24 | def load_mach(topsrcdir): |
michael@0 | 25 | sys.path[0:0] = [os.path.join(topsrcdir, "build")] |
michael@0 | 26 | import mach_bootstrap |
michael@0 | 27 | return mach_bootstrap.bootstrap(topsrcdir) |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | def check_and_run_mach(dir_path, args): |
michael@0 | 31 | # If we find the mach bootstrap module, we are in the srcdir. |
michael@0 | 32 | mach_path = os.path.join(dir_path, 'build/mach_bootstrap.py') |
michael@0 | 33 | if os.path.isfile(mach_path): |
michael@0 | 34 | mach = load_mach(dir_path) |
michael@0 | 35 | sys.exit(mach.run(args)) |
michael@0 | 36 | |
michael@0 | 37 | |
michael@0 | 38 | def main(args): |
michael@0 | 39 | # Check whether the current directory is within a mach src or obj dir. |
michael@0 | 40 | for dir_path in ancestors(os.getcwd()): |
michael@0 | 41 | # If we find a "mozinfo.json" file, we are in the objdir. |
michael@0 | 42 | mozinfo_path = os.path.join(dir_path, 'mozinfo.json') |
michael@0 | 43 | if os.path.isfile(mozinfo_path): |
michael@0 | 44 | import json |
michael@0 | 45 | info = json.load(open(mozinfo_path)) |
michael@0 | 46 | if 'mozconfig' in info and 'MOZCONFIG' not in os.environ: |
michael@0 | 47 | # If the MOZCONFIG environment variable is not already set, set it |
michael@0 | 48 | # to the value from mozinfo.json. This will tell the build system |
michael@0 | 49 | # to look for a config file at the path in $MOZCONFIG rather than |
michael@0 | 50 | # its default locations. |
michael@0 | 51 | # |
michael@0 | 52 | # Note: subprocess requires native strings in os.environ on Windows |
michael@0 | 53 | os.environ[b'MOZCONFIG'] = str(info['mozconfig']) |
michael@0 | 54 | |
michael@0 | 55 | if 'topsrcdir' in info: |
michael@0 | 56 | # Continue searching for mach_bootstrap in the source directory. |
michael@0 | 57 | dir_path = info['topsrcdir'] |
michael@0 | 58 | |
michael@0 | 59 | check_and_run_mach(dir_path, args) |
michael@0 | 60 | |
michael@0 | 61 | # If we didn't find a source path by scanning for a mozinfo.json, check |
michael@0 | 62 | # whether the directory containing this script is a source directory. |
michael@0 | 63 | check_and_run_mach(os.path.dirname(__file__), args) |
michael@0 | 64 | |
michael@0 | 65 | print('Could not run mach: No mach source directory found.') |
michael@0 | 66 | sys.exit(1) |
michael@0 | 67 | |
michael@0 | 68 | |
michael@0 | 69 | if __name__ == '__main__': |
michael@0 | 70 | main(sys.argv[1:]) |