Upstream version 8.36.161.0
[platform/framework/web/crosswalk.git] / src / third_party / chromite / cros / commands / __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 """This package contains all valid cros commands and their unittests.
6
7 All commands can be either imported directly or looked up using this module.
8 This modules contains a dictionary called commands mapping
9 command names -> command classes e.g. image->cros_image.ImageCommand.
10 """
11
12 import glob
13 import imp
14 import os
15
16 from chromite import cros
17
18
19 def _FindModules(subdir_path):
20   """Returns a list of all the relevant python modules in |sub_dir_path|"""
21   # We only load cros_[!unittest] modules.
22   modules = []
23   for file_path in glob.glob(subdir_path + '/cros_*.py'):
24     if not file_path.endswith('_unittest.py'):
25       modules.append(file_path)
26
27   return modules
28
29
30 def _ImportCommands():
31   """Directly imports all cros_[!unittest] python modules.
32
33   This method imports the cros_[!unittest] modules which may contain
34   commands. When these modules are loaded, declared commands (those that use
35   the cros.CommandDecorator) will automatically get added to cros._commands.
36   """
37   subdir_path = os.path.dirname(__file__)
38   for file_path in _FindModules(subdir_path):
39     file_name = os.path.basename(file_path)
40     mod_name = os.path.splitext(file_name)[0]
41     imp.load_module(mod_name, *imp.find_module(mod_name, [subdir_path]))
42
43
44 def ListCommands():
45   """Return a dictionary mapping command names to classes."""
46   # pylint: disable=W0212
47   return cros._commands.copy()
48
49
50 _ImportCommands()
51