Bug 873798 - Mach will now create a directory for holding global state; r=ted
authorGregory Szorc <gps@mozilla.com>
Mon, 20 May 2013 18:07:48 -0700
changeset 139593 69a8f7b3a2e22666f53bd40a595d9ab767f2aec3
parent 139592 c750d5d003ddf4433c97ca48c849f248a358473e
child 139594 7571fcebd2a095922062206c5410a3e49a49e785
push id3911
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 20:17:26 +0000
treeherdermozilla-aurora@7e26ca8db92b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted
bugs873798
milestone24.0a1
Bug 873798 - Mach will now create a directory for holding global state; r=ted
build/mach_bootstrap.py
--- a/build/mach_bootstrap.py
+++ b/build/mach_bootstrap.py
@@ -2,16 +2,31 @@
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 from __future__ import print_function, unicode_literals
 
 import os
 import platform
 import sys
+import time
+
+
+STATE_DIR_FIRST_RUN = '''
+mach and the build system store shared state in a common directory on the
+filesystem. The following directory will be created:
+
+  {userdir}
+
+If you would like to use a different directory, hit CTRL+c and set the
+MOZBUILD_STATE_PATH environment variable to the directory you would like to
+use and re-run mach. For this change to take effect forever, you'll likely
+want to export this environment variable from your shell's init scripts.
+'''.lstrip()
+
 
 # TODO Bug 794506 Integrate with the in-tree virtualenv configuration.
 SEARCH_PATHS = [
     'python/mach',
     'python/mozboot',
     'python/mozbuild',
     'python/blessings',
     'python/psutil',
@@ -93,16 +108,49 @@ def bootstrap(topsrcdir, mozilla_dir=Non
     # Ensure we are running Python 2.7+. We put this check here so we generate a
     # user-friendly error message rather than a cryptic stack trace on module
     # import.
     if sys.version_info[0] != 2 or sys.version_info[1] < 7:
         print('Python 2.7 or above (but not Python 3) is required to run mach.')
         print('You are running Python', platform.python_version())
         sys.exit(1)
 
+    # Global build system and mach state is stored in a central directory. By
+    # default, this is ~/.mozbuild. However, it can be defined via an
+    # environment variable. We detect first run (by lack of this directory
+    # existing) and notify the user that it will be created. The logic for
+    # creation is much simpler for the "advanced" environment variable use
+    # case. For default behavior, we educate users and give them an opportunity
+    # to react. We always exit after creating the directory because users don't
+    # like surprises.
+    state_user_dir = os.path.expanduser('~/.mozbuild')
+    state_env_dir = os.environ.get('MOZBUILD_STATE_PATH', None)
+    if state_env_dir:
+        if not os.path.exists(state_env_dir):
+            print('Creating global state directory from environment variable: %s'
+                % state_env_dir)
+            os.makedirs(state_env_dir, mode=0777)
+            print('Please re-run mach.')
+            sys.exit(1)
+    else:
+        if not os.path.exists(state_user_dir):
+            print(STATE_DIR_FIRST_RUN.format(userdir=state_user_dir))
+            try:
+                for i in range(20, -1, -1):
+                    time.sleep(1)
+                    sys.stdout.write('%d ' % i)
+                    sys.stdout.flush()
+            except KeyboardInterrupt:
+                sys.exit(1)
+
+            print('\nCreating default state directory: %s' % state_user_dir)
+            os.mkdir(state_user_dir)
+            print('Please re-run mach.')
+            sys.exit(1)
+
     try:
         import mach.main
     except ImportError:
         sys.path[0:0] = [os.path.join(mozilla_dir, path) for path in SEARCH_PATHS]
         import mach.main
 
     mach = mach.main.Mach(topsrcdir)
     for category, meta in CATEGORIES.items():