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.
12 from __future__ import print_function
18 from chromite import cros
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.
25 for file_path in glob.glob(subdir_path + '/cros_*.py'):
26 if not file_path.endswith('_unittest.py'):
27 modules.append(file_path)
32 def _ImportCommands():
33 """Directly imports all cros_[!unittest] python modules.
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.
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]))
47 """Return a dictionary mapping command names to classes."""
48 # pylint: disable=W0212
49 return cros._commands.copy()