Upstream version 10.39.225.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 from __future__ import print_function
13
14 import glob
15 import imp
16 import os
17
18 from chromite import cros
19
20
21 def _FindModules(subdir_path):
22   """Returns a list of all the relevant python modules in |sub_dir_path|"""
23   # We only load cros_[!unittest] modules.
24   modules = []
25   for file_path in glob.glob(subdir_path + '/cros_*.py'):
26     if not file_path.endswith('_unittest.py'):
27       modules.append(file_path)
28
29   return modules
30
31
32 def _ImportCommands():
33   """Directly imports all cros_[!unittest] python modules.
34
35   This method imports the cros_[!unittest] modules which may contain
36   commands. When these modules are loaded, declared commands (those that use
37   the cros.CommandDecorator) will automatically get added to cros._commands.
38   """
39   subdir_path = os.path.dirname(__file__)
40   for file_path in _FindModules(subdir_path):
41     file_name = os.path.basename(file_path)
42     mod_name = os.path.splitext(file_name)[0]
43     imp.load_module(mod_name, *imp.find_module(mod_name, [subdir_path]))
44
45
46 def ListCommands():
47   """Return a dictionary mapping command names to classes."""
48   # pylint: disable=W0212
49   return cros._commands.copy()
50
51
52 _ImportCommands()
53