Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cros / __init__.py
1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
4
5 """Module that contains meta-logic related to Cros Commands.
6
7 This module contains two important definitions used by all commands.
8
9   CrosCommand: The parent class of all cros commands.
10   CommandDecorator: Decorator that must be used to ensure that the command shows
11     up in _commands and is discoverable by cros.
12 """
13
14
15 _commands = dict()
16
17
18 class InvalidCommandError(Exception):
19   """Error that occurs when command class fails sanity checks."""
20   pass
21
22
23 def CommandDecorator(command_name):
24   """Decorator that sanity checks and adds class to list of usable commands."""
25
26   def InnerCommandDecorator(original_class):
27     """"Inner Decorator that actually wraps the class."""
28     if not hasattr(original_class, '__doc__'):
29       raise InvalidCommandError('All handlers must have docstrings: %s' %
30                                 original_class)
31
32     if not issubclass(original_class, CrosCommand):
33       raise InvalidCommandError('All Commands must derive from CrosCommand: '
34                                 '%s' % original_class)
35
36     _commands[command_name] = original_class
37     original_class.command_name = command_name
38
39     return original_class
40
41   return InnerCommandDecorator
42
43
44 class CrosCommand(object):
45   """All CrosCommands must derive from this class.
46
47   This class provides the abstract interface for all Cros Commands. When
48   designing a new command, you must sub-class from this class and use the
49   CommandDecorator decorator. You must specify a class docstring as that will be
50   used as the usage for the sub-command.
51
52   In addition your command should implement AddParser which is passed in a
53   parser that you can add your own custom arguments. See argparse for more
54   information.
55   """
56   # Indicates whether command stats should be uploaded for this command.
57   # Override to enable command stats uploading.
58   upload_stats = False
59   # We set the default timeout to 1 second, to prevent overly long waits for
60   # commands to complete.  From manual tests, stat uploads usually take
61   # between 0.35s-0.45s in MTV.
62   upload_stats_timeout = 1
63
64   # Indicates whether command uses cache related commandline options.
65   use_caching_options = False
66
67   def __init__(self, options):
68     self.options = options
69
70   @classmethod
71   def AddParser(cls, parser):
72     """Add arguments for this command to the parser."""
73     parser.set_defaults(cros_class=cls)
74
75   def Run(self):
76     """The command to run."""
77     raise NotImplementedError()