Bug 957856 - Synchronize mach with upstream repository; r=ahal

The test changes and setup.py are NPOTB. base.py and main.py
added a new feature to declare global arguments.

--HG--
extra : rebase_source : 4b14487d9aff98247464e0b0c08b4fc5cfbd862a
This commit is contained in:
Gregory Szorc 2014-01-08 16:14:39 -08:00
parent 56a77d573a
commit 62c47d4190
7 changed files with 64 additions and 12 deletions

View File

@ -309,3 +309,20 @@ takes a single parameter called *group*. This is the name of the entry
point group to load and defaults to ``mach.providers``. e.g.::
mach.load_commands_from_entry_point("mach.external.providers")
Adding Global Arguments
=======================
Arguments to mach commands are usually command-specific. However,
mach ships with a handful of global arguments that apply to all
commands.
It is possible to extend the list of global arguments. In your
*mach driver*, simply call ``add_global_argument()`` on your
``mach.main.Mach`` instance. e.g.::
mach = mach.main.Mach(os.getcwd())
# Will allow --example to be specified on every mach command.
mach.add_global_argument('--example', action='store_true',
help='Demonstrate an example global argument.')

View File

@ -8,12 +8,15 @@ from __future__ import unicode_literals
class CommandContext(object):
"""Holds run-time state so it can easily be passed to command providers."""
def __init__(self, cwd=None, settings=None, log_manager=None,
commands=None):
commands=None, **kwargs):
self.cwd = cwd
self.settings = settings
self.log_manager = log_manager
self.commands = commands
for k,v in kwargs.items():
setattr(self, k, v)
class MachError(Exception):
"""Base class for all errors raised by mach itself."""

View File

@ -189,9 +189,17 @@ To see more help for a specific command, run:
self.logger = logging.getLogger(__name__)
self.settings = ConfigSettings()
self.log_manager.register_structured_logger(self.logger)
self.global_arguments = []
self.populate_context_handler = None
self.log_manager.register_structured_logger(self.logger)
def add_global_argument(self, *args, **kwargs):
"""Register a global argument with the argument parser.
Arguments are proxied to ArgumentParser.add_argument()
"""
self.global_arguments.append((args, kwargs))
def load_commands_from_directory(self, path):
"""Scan for mach commands from modules in a directory.
@ -560,6 +568,9 @@ To see more help for a specific command, run:
help='Do not prefix log lines with times. By default, mach will '
'prefix each output line with the time since command start.')
for args, kwargs in self.global_arguments:
global_group.add_argument(*args, **kwargs)
# We need to be last because CommandAction swallows all remaining
# arguments and argparse parses arguments in the order they were added.
parser.add_argument('command', action=CommandAction,

View File

@ -17,12 +17,12 @@ from mach.test.providers import throw2
@CommandProvider
class TestCommandProvider(object):
@Command('throw')
@Command('throw', category='testing')
@CommandArgument('--message', '-m', default='General Error')
def throw(self, message):
raise Exception(message)
@Command('throw_deep')
@Command('throw_deep', category='testing')
@CommandArgument('--message', '-m', default='General Error')
def throw_deep(self, message):
throw2.throw_deep(message)

View File

@ -29,7 +29,7 @@ class TestErrorOutput(TestBase):
self.assertEqual(result, 1)
self.assertIn(mach.main.COMMAND_ERROR, stdout)
self.assertIn(COMMAND_ERROR, stdout)
def test_invoked_error(self):
result, stdout, stderr = self._run_mach(['throw_deep', '--message',
@ -37,4 +37,4 @@ class TestErrorOutput(TestBase):
self.assertEqual(result, 1)
self.assertIn(mach.main.MODULE_ERROR, stdout)
self.assertIn(MODULE_ERROR, stdout)

View File

@ -8,7 +8,7 @@ import logging
import time
import unittest
from mach.logger import StructuredHumanFormatter
from mach.logging import StructuredHumanFormatter
class DummyLogger(logging.Logger):
@ -29,7 +29,7 @@ class TestStructuredHumanFormatter(unittest.TestCase):
def on_record(record):
result = formatter.format(record)
relevant = result[5:]
relevant = result[9:]
self.assertEqual(relevant, 'Test: s\xe9curit\xe9')

View File

@ -2,16 +2,37 @@
# 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 setuptools import setup
try:
from setuptools import setup
except:
from distutils.core import setup
VERSION = '0.1'
VERSION = '0.3'
README = open('README.rst').read()
setup(
name='mach',
description='CLI frontend to mozilla-central.',
description='Generic command line command dispatching framework.',
long_description=README,
license='MPL 2.0',
author='Gregory Szorc',
author_email='gregory.szorc@gmail.com',
url='https://developer.mozilla.org/en-US/docs/Developer_Guide/mach',
packages=['mach'],
version=VERSION,
tests_require=['mock']
classifiers=[
'Environment :: Console',
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'Natural Language :: English',
],
install_requires=[
'blessings',
'mozfile',
'mozprocess',
],
tests_require=['mock'],
)