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.
5 """This package contains all valid cros commands and their unittests.
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.
16 from chromite import cros
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.
23 for file_path in glob.glob(subdir_path + '/cros_*.py'):
24 if not file_path.endswith('_unittest.py'):
25 modules.append(file_path)
30 def _ImportCommands():
31 """Directly imports all cros_[!unittest] python modules.
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.
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]))
45 """Return a dictionary mapping command names to classes."""
46 # pylint: disable=W0212
47 return cros._commands.copy()