Bug 1030717 - Don't try to create the mach state directory until it's actually needed. r=gps

This commit is contained in:
Mike Hommey 2014-07-03 07:15:31 +09:00
parent 20964fe02e
commit fad1758c54
4 changed files with 77 additions and 38 deletions

View File

@ -145,42 +145,47 @@ def bootstrap(topsrcdir, mozilla_dir=None):
# 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=0o770)
print('Please re-run mach.')
sys.exit(1)
state_dir = state_env_dir
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)
state_dir = state_user_dir
try:
import mach.main
except ImportError:
sys.path[0:0] = [os.path.join(mozilla_dir, path) for path in SEARCH_PATHS]
import mach.main
def populate_context(context):
context.state_dir = state_dir
context.topdir = topsrcdir
def populate_context(context, key=None):
if key is None:
return
if key == 'state_dir':
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=0o770)
print('Please re-run mach.')
sys.exit(1)
state_dir = state_env_dir
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)
state_dir = state_user_dir
return state_dir
if key == 'topdir':
return topsrcdir
raise AttributeError(key)
mach = mach.main.Mach(os.getcwd())
mach.populate_context_handler = populate_context

View File

@ -143,6 +143,28 @@ class ArgumentParser(argparse.ArgumentParser):
return text
class ContextWrapper(object):
def __init__(self, context, handler):
object.__setattr__(self, '_context', context)
object.__setattr__(self, '_handler', handler)
def __getattribute__(self, key):
try:
return getattr(object.__getattribute__(self, '_context'), key)
except AttributeError as e:
try:
ret = object.__getattribute__(self, '_handler')(self, key)
except AttributeError, TypeError:
# TypeError is in case the handler comes from old code not
# taking a key argument.
raise e
setattr(self, key, ret)
return ret
def __setattr__(self, key, value):
setattr(object.__getattribute__(self, '_context'), key, value)
@CommandProvider
class Mach(object):
"""Main mach driver type.
@ -154,10 +176,15 @@ class Mach(object):
behavior:
populate_context_handler -- If defined, it must be a callable. The
callable will be called with the mach.base.CommandContext instance
as its single argument right before command dispatch. This allows
modification of the context instance and thus passing of
arbitrary data to command handlers.
callable signature is the following:
populate_context_handler(context, key=None)
It acts as a fallback getter for the mach.base.CommandContext
instance.
This allows to augment the context instance with arbitrary data
for use in command handlers.
For backwards compatibility, it is also called before command
dispatch without a key, allowing the context handler to add
attributes to the context instance.
require_conditions -- If True, commands that do not have any condition
functions applied will be skipped. Defaults to False.
@ -343,6 +370,7 @@ To see more help for a specific command, run:
if self.populate_context_handler:
self.populate_context_handler(context)
context = ContextWrapper(context, self.populate_context_handler)
parser = self.get_argument_parser(context)

View File

@ -13,9 +13,14 @@ from mach.test.common import TestBase
from mozunit import main
def _populate_context(context):
context.foo = True
context.bar = False
def _populate_context(context, key=None):
if key is None:
return
if key == 'foo':
return True
if key == 'bar':
return False
raise AttributeError(key)
class TestConditions(TestBase):
"""Tests for conditionally filtering commands."""

View File

@ -922,6 +922,7 @@ class MachDebug(MachCommandBase):
@CommandArgument('--verbose', '-v', action='store_true',
help='Print verbose output.')
def environment(self, verbose=False):
state_dir = self._mach_context.state_dir
import platform
print('platform:\n\t%s' % platform.platform())
print('python version:\n\t%s' % sys.version)
@ -929,7 +930,7 @@ class MachDebug(MachCommandBase):
print('mach cwd:\n\t%s' % self._mach_context.cwd)
print('os cwd:\n\t%s' % os.getcwd())
print('mach directory:\n\t%s' % self._mach_context.topdir)
print('state directory:\n\t%s' % self._mach_context.state_dir)
print('state directory:\n\t%s' % state_dir)
try:
mb = MozbuildObject.from_environment(cwd=self._mach_context.cwd)