15bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Copyright (C) 2016 The Android Open Source Project
25bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
35bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Licensed under the Apache License, Version 2.0 (the "License");
45bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# you may not use this file except in compliance with the License.
55bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# You may obtain a copy of the License at
65bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
75bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#      http://www.apache.org/licenses/LICENSE-2.0
85bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond#
95bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# Unless required by applicable law or agreed to in writing, software
105bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# distributed under the License is distributed on an "AS IS" BASIS,
115bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# See the License for the specific language governing permissions and
135bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond# limitations under the License.
145bbcc3861c44435f89481f80946ef5c9c49968f2Luke Drummond
15dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo'''This file contains utility functions used by both the test suite and the
16dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leosingle test executor.'''
17dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
18a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummondfrom __future__ import absolute_import
19a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond
20dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport os
21dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport importlib
22dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leoimport sys
23dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
24dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
25dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leodef load_py_module(path):
26dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    '''Load a python file from disk.
27dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
28dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    Args:
29dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        path: String path to python file.
30dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo
31dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    Returns:
32dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        python module if success, None otherwise.
33dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    '''
34dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    assert isinstance(path, str)
35dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    try:
36dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        if not os.path.exists(path):
37a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond            print('Path does not exist: ' + path)
38dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo            return None
39dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        path = os.path.abspath(path)
40dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        module_dir, module_file = os.path.split(path)
41dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        module_name, _ = os.path.splitext(module_file)
42dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # adjust sys.path, runtime counterpart of PYTHONPATH, to temporarily
43dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        # include the folder containing the user configuration module
44dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        sys.path.append(module_dir)
45dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        module_obj = importlib.import_module(module_name)
46dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        sys.path.pop(0)
47dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        return module_obj
48dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo    except ImportError as err:
49a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        print(str(err))
50a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        print("Looking in directory ")
51a3c6f62775506c95afd556e617f14d7a28839f01Luke Drummond        print(module_dir)
52dcecc0c8d22e894525e25a122ce25129b51338f2Dean De Leo        return None
53