Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | # This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | # License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | # file, You can obtain one at http://mozilla.org/MPL/2.0/. |
michael@0 | 4 | |
michael@0 | 5 | from __future__ import print_function, unicode_literals |
michael@0 | 6 | |
michael@0 | 7 | import os |
michael@0 | 8 | import platform |
michael@0 | 9 | import sys |
michael@0 | 10 | import time |
michael@0 | 11 | |
michael@0 | 12 | |
michael@0 | 13 | STATE_DIR_FIRST_RUN = ''' |
michael@0 | 14 | mach and the build system store shared state in a common directory on the |
michael@0 | 15 | filesystem. The following directory will be created: |
michael@0 | 16 | |
michael@0 | 17 | {userdir} |
michael@0 | 18 | |
michael@0 | 19 | If you would like to use a different directory, hit CTRL+c and set the |
michael@0 | 20 | MOZBUILD_STATE_PATH environment variable to the directory you would like to |
michael@0 | 21 | use and re-run mach. For this change to take effect forever, you'll likely |
michael@0 | 22 | want to export this environment variable from your shell's init scripts. |
michael@0 | 23 | '''.lstrip() |
michael@0 | 24 | |
michael@0 | 25 | |
michael@0 | 26 | # TODO Bug 794506 Integrate with the in-tree virtualenv configuration. |
michael@0 | 27 | SEARCH_PATHS = [ |
michael@0 | 28 | 'python/mach', |
michael@0 | 29 | 'python/mozboot', |
michael@0 | 30 | 'python/mozbuild', |
michael@0 | 31 | 'python/mozversioncontrol', |
michael@0 | 32 | 'python/blessings', |
michael@0 | 33 | 'python/configobj', |
michael@0 | 34 | 'python/jsmin', |
michael@0 | 35 | 'python/psutil', |
michael@0 | 36 | 'python/which', |
michael@0 | 37 | 'build/pymake', |
michael@0 | 38 | 'config', |
michael@0 | 39 | 'dom/bindings', |
michael@0 | 40 | 'dom/bindings/parser', |
michael@0 | 41 | 'other-licenses/ply', |
michael@0 | 42 | 'xpcom/idl-parser', |
michael@0 | 43 | 'testing', |
michael@0 | 44 | 'testing/xpcshell', |
michael@0 | 45 | 'testing/marionette/client', |
michael@0 | 46 | 'testing/marionette/client/marionette', |
michael@0 | 47 | 'testing/marionette/transport', |
michael@0 | 48 | 'testing/mozbase/mozcrash', |
michael@0 | 49 | 'testing/mozbase/mozdevice', |
michael@0 | 50 | 'testing/mozbase/mozfile', |
michael@0 | 51 | 'testing/mozbase/mozhttpd', |
michael@0 | 52 | 'testing/mozbase/mozlog', |
michael@0 | 53 | 'testing/mozbase/moznetwork', |
michael@0 | 54 | 'testing/mozbase/mozprocess', |
michael@0 | 55 | 'testing/mozbase/mozprofile', |
michael@0 | 56 | 'testing/mozbase/mozrunner', |
michael@0 | 57 | 'testing/mozbase/mozsystemmonitor', |
michael@0 | 58 | 'testing/mozbase/mozinfo', |
michael@0 | 59 | 'testing/mozbase/moztest', |
michael@0 | 60 | 'testing/mozbase/mozversion', |
michael@0 | 61 | 'testing/mozbase/manifestdestiny', |
michael@0 | 62 | 'xpcom/idl-parser', |
michael@0 | 63 | ] |
michael@0 | 64 | |
michael@0 | 65 | # Individual files providing mach commands. |
michael@0 | 66 | MACH_MODULES = [ |
michael@0 | 67 | 'addon-sdk/mach_commands.py', |
michael@0 | 68 | 'build/valgrind/mach_commands.py', |
michael@0 | 69 | 'dom/bindings/mach_commands.py', |
michael@0 | 70 | 'layout/tools/reftest/mach_commands.py', |
michael@0 | 71 | 'python/mach_commands.py', |
michael@0 | 72 | 'python/mach/mach/commands/commandinfo.py', |
michael@0 | 73 | 'python/mozboot/mozboot/mach_commands.py', |
michael@0 | 74 | 'python/mozbuild/mozbuild/mach_commands.py', |
michael@0 | 75 | 'python/mozbuild/mozbuild/frontend/mach_commands.py', |
michael@0 | 76 | 'testing/mach_commands.py', |
michael@0 | 77 | 'testing/marionette/mach_commands.py', |
michael@0 | 78 | 'testing/mochitest/mach_commands.py', |
michael@0 | 79 | 'testing/xpcshell/mach_commands.py', |
michael@0 | 80 | 'testing/talos/mach_commands.py', |
michael@0 | 81 | 'testing/xpcshell/mach_commands.py', |
michael@0 | 82 | 'tools/docs/mach_commands.py', |
michael@0 | 83 | 'tools/mercurial/mach_commands.py', |
michael@0 | 84 | 'tools/mach_commands.py', |
michael@0 | 85 | ] |
michael@0 | 86 | |
michael@0 | 87 | |
michael@0 | 88 | CATEGORIES = { |
michael@0 | 89 | 'build': { |
michael@0 | 90 | 'short': 'Build Commands', |
michael@0 | 91 | 'long': 'Interact with the build system', |
michael@0 | 92 | 'priority': 80, |
michael@0 | 93 | }, |
michael@0 | 94 | 'post-build': { |
michael@0 | 95 | 'short': 'Post-build Commands', |
michael@0 | 96 | 'long': 'Common actions performed after completing a build.', |
michael@0 | 97 | 'priority': 70, |
michael@0 | 98 | }, |
michael@0 | 99 | 'testing': { |
michael@0 | 100 | 'short': 'Testing', |
michael@0 | 101 | 'long': 'Run tests.', |
michael@0 | 102 | 'priority': 60, |
michael@0 | 103 | }, |
michael@0 | 104 | 'devenv': { |
michael@0 | 105 | 'short': 'Development Environment', |
michael@0 | 106 | 'long': 'Set up and configure your development environment.', |
michael@0 | 107 | 'priority': 50, |
michael@0 | 108 | }, |
michael@0 | 109 | 'build-dev': { |
michael@0 | 110 | 'short': 'Low-level Build System Interaction', |
michael@0 | 111 | 'long': 'Interact with specific parts of the build system.', |
michael@0 | 112 | 'priority': 20, |
michael@0 | 113 | }, |
michael@0 | 114 | 'misc': { |
michael@0 | 115 | 'short': 'Potpourri', |
michael@0 | 116 | 'long': 'Potent potables and assorted snacks.', |
michael@0 | 117 | 'priority': 10, |
michael@0 | 118 | }, |
michael@0 | 119 | 'disabled': { |
michael@0 | 120 | 'short': 'Disabled', |
michael@0 | 121 | 'long': 'These commands are unavailable for your current context, run "mach <command>" to see why.', |
michael@0 | 122 | 'priority': 0, |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | |
michael@0 | 127 | def bootstrap(topsrcdir, mozilla_dir=None): |
michael@0 | 128 | if mozilla_dir is None: |
michael@0 | 129 | mozilla_dir = topsrcdir |
michael@0 | 130 | |
michael@0 | 131 | # Ensure we are running Python 2.7+. We put this check here so we generate a |
michael@0 | 132 | # user-friendly error message rather than a cryptic stack trace on module |
michael@0 | 133 | # import. |
michael@0 | 134 | if sys.version_info[0] != 2 or sys.version_info[1] < 7: |
michael@0 | 135 | print('Python 2.7 or above (but not Python 3) is required to run mach.') |
michael@0 | 136 | print('You are running Python', platform.python_version()) |
michael@0 | 137 | sys.exit(1) |
michael@0 | 138 | |
michael@0 | 139 | # Global build system and mach state is stored in a central directory. By |
michael@0 | 140 | # default, this is ~/.mozbuild. However, it can be defined via an |
michael@0 | 141 | # environment variable. We detect first run (by lack of this directory |
michael@0 | 142 | # existing) and notify the user that it will be created. The logic for |
michael@0 | 143 | # creation is much simpler for the "advanced" environment variable use |
michael@0 | 144 | # case. For default behavior, we educate users and give them an opportunity |
michael@0 | 145 | # to react. We always exit after creating the directory because users don't |
michael@0 | 146 | # like surprises. |
michael@0 | 147 | try: |
michael@0 | 148 | import mach.main |
michael@0 | 149 | except ImportError: |
michael@0 | 150 | sys.path[0:0] = [os.path.join(mozilla_dir, path) for path in SEARCH_PATHS] |
michael@0 | 151 | import mach.main |
michael@0 | 152 | |
michael@0 | 153 | def populate_context(context, key=None): |
michael@0 | 154 | if key is None: |
michael@0 | 155 | return |
michael@0 | 156 | if key == 'state_dir': |
michael@0 | 157 | state_user_dir = os.path.expanduser('~/.mozbuild') |
michael@0 | 158 | state_env_dir = os.environ.get('MOZBUILD_STATE_PATH', None) |
michael@0 | 159 | if state_env_dir: |
michael@0 | 160 | if not os.path.exists(state_env_dir): |
michael@0 | 161 | print('Creating global state directory from environment variable: %s' |
michael@0 | 162 | % state_env_dir) |
michael@0 | 163 | os.makedirs(state_env_dir, mode=0o770) |
michael@0 | 164 | print('Please re-run mach.') |
michael@0 | 165 | sys.exit(1) |
michael@0 | 166 | state_dir = state_env_dir |
michael@0 | 167 | else: |
michael@0 | 168 | if not os.path.exists(state_user_dir): |
michael@0 | 169 | print(STATE_DIR_FIRST_RUN.format(userdir=state_user_dir)) |
michael@0 | 170 | try: |
michael@0 | 171 | for i in range(20, -1, -1): |
michael@0 | 172 | time.sleep(1) |
michael@0 | 173 | sys.stdout.write('%d ' % i) |
michael@0 | 174 | sys.stdout.flush() |
michael@0 | 175 | except KeyboardInterrupt: |
michael@0 | 176 | sys.exit(1) |
michael@0 | 177 | |
michael@0 | 178 | print('\nCreating default state directory: %s' % state_user_dir) |
michael@0 | 179 | os.mkdir(state_user_dir) |
michael@0 | 180 | print('Please re-run mach.') |
michael@0 | 181 | sys.exit(1) |
michael@0 | 182 | state_dir = state_user_dir |
michael@0 | 183 | |
michael@0 | 184 | return state_dir |
michael@0 | 185 | if key == 'topdir': |
michael@0 | 186 | return topsrcdir |
michael@0 | 187 | raise AttributeError(key) |
michael@0 | 188 | |
michael@0 | 189 | mach = mach.main.Mach(os.getcwd()) |
michael@0 | 190 | mach.populate_context_handler = populate_context |
michael@0 | 191 | |
michael@0 | 192 | for category, meta in CATEGORIES.items(): |
michael@0 | 193 | mach.define_category(category, meta['short'], meta['long'], |
michael@0 | 194 | meta['priority']) |
michael@0 | 195 | |
michael@0 | 196 | for path in MACH_MODULES: |
michael@0 | 197 | mach.load_commands_from_file(os.path.join(mozilla_dir, path)) |
michael@0 | 198 | |
michael@0 | 199 | return mach |