author Gregory Szorc <>
Fri, 11 Oct 2013 12:44:15 -0700
changeset 150541 c1c61b305ce0b58450ec0092457f98cd7119d4ee
parent 144413 2b71067682f1b4647fba8a7cdec8e96082664fa2
child 162082 d53d1c6cdf13179ea89df2a9abb7794e276fc13f
permissions -rw-r--r--
Bug 924977 - More proper current working directory handling in mach; r=ahal

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at

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,
        self.cwd = cwd
        self.settings = settings
        self.log_manager = log_manager
        self.commands = commands

class MachError(Exception):
    """Base class for all errors raised by mach itself."""

class NoCommandError(MachError):
    """No command was passed into mach."""

class UnknownCommandError(MachError):
    """Raised when we attempted to execute an unknown command."""

    def __init__(self, command, verb):

        self.command = command
        self.verb = verb

class UnrecognizedArgumentError(MachError):
    """Raised when an unknown argument is passed to mach."""

    def __init__(self, command, arguments):

        self.command = command
        self.arguments = arguments

class MethodHandler(object):
    """Describes a Python method that implements a mach command.

    Instances of these are produced by mach when it processes classes
    defining mach commands.
    __slots__ = (
        # The Python class providing the command. This is the class type not
        # an instance of the class. Mach will instantiate a new instance of
        # the class if the command is executed.

        # Whether the __init__ method of the class should receive a mach
        # context instance. This should only affect the mach driver and how
        # it instantiates classes.

        # The name of the method providing the command. In other words, this
        # is the str name of the attribute on the class type corresponding to
        # the name of the function.

        # The name of the command.

        # String category this command belongs to.

        # Description of the purpose of this command.

        # Whether to allow all arguments from the parser.

        # Functions used to 'skip' commands if they don't meet the conditions
        # in a given context.

        # Arguments added to this command's parser. This is a 2-tuple of
        # positional and named arguments, respectively.

    def __init__(self, cls, method, name, category=None, description=None,
        allow_all_arguments=False, conditions=None, arguments=None,

        self.cls = cls
        self.method = method = name
        self.category = category
        self.description = description
        self.allow_all_arguments = allow_all_arguments
        self.conditions = conditions or []
        self.arguments = arguments or []
        self.pass_context = pass_context