1 # Copyright (c) 2013 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 containing import helper used by autoupdate utility.""" 6 7 import imp 8 import os 9 10 import common 11 from autotest_lib.utils import build_externals, external_packages 12 13 14 def download_and_import(module_name, package_class): 15 """Tries to import module, if it fails, downloads and imports it. 16 17 @param module_name: Name of the module e.g. devserver. 18 @param package_class: autotest external_packages class to use. 19 """ 20 try: 21 return imp.load_module(module_name, *imp.find_module(module_name)) 22 except ImportError: 23 pass 24 25 tot = external_packages.find_top_of_autotest_tree() 26 install_dir = os.path.join(tot, build_externals.INSTALL_DIR) 27 build_externals.build_and_install_packages( 28 [package_class], install_dir) 29 30 return imp.load_module(module_name, *imp.find_module(module_name)) 31