diff --git a/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py b/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py index e76f29cca..754eb5564 100644 --- a/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py +++ b/examples/protocols/http_server/advanced_tests/http_server_advanced_test.py @@ -40,8 +40,9 @@ import Utility # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw # Import client module +# TODO: replace with import expath = os.path.dirname(os.path.realpath(__file__)) -client = Utility.load_source("client", expath + "/scripts/test.py") +client = Utility.load_source(expath + "/scripts/test.py") # Due to connectivity issues (between runner host and DUT) in the runner environment, diff --git a/examples/protocols/http_server/persistent_sockets/http_server_persistence_test.py b/examples/protocols/http_server/persistent_sockets/http_server_persistence_test.py index f10a98485..71cf0db96 100644 --- a/examples/protocols/http_server/persistent_sockets/http_server_persistence_test.py +++ b/examples/protocols/http_server/persistent_sockets/http_server_persistence_test.py @@ -42,8 +42,9 @@ import Utility # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw # Import client module +# TODO: replace with import expath = os.path.dirname(os.path.realpath(__file__)) -client = Utility.load_source("client", expath + "/scripts/adder.py") +client = Utility.load_source(expath + "/scripts/adder.py") @IDF.idf_example_test(env_tag="Example_WIFI") diff --git a/examples/protocols/http_server/simple/http_server_simple_test.py b/examples/protocols/http_server/simple/http_server_simple_test.py index 75f5f4f26..bb209e204 100644 --- a/examples/protocols/http_server/simple/http_server_simple_test.py +++ b/examples/protocols/http_server/simple/http_server_simple_test.py @@ -44,7 +44,7 @@ import Utility # Import client module expath = os.path.dirname(os.path.realpath(__file__)) -client = Utility.load_source("client", expath + "/scripts/client.py") +client = Utility.load_source(expath + "/scripts/client.py") @IDF.idf_example_test(env_tag="Example_WIFI") diff --git a/tools/tiny-test-fw/Utility/CaseConfig.py b/tools/tiny-test-fw/Utility/CaseConfig.py index d306f9054..cf5d42f19 100644 --- a/tools/tiny-test-fw/Utility/CaseConfig.py +++ b/tools/tiny-test-fw/Utility/CaseConfig.py @@ -172,9 +172,9 @@ class Parser(object): """ output = dict() for key in overwrite: - _path = overwrite[key]["path"] - _module = load_source(str(hash(_path)), overwrite[key]["path"]) - output[key] = _module.__getattribute__(overwrite[key]["class"]) + path = overwrite[key]["path"] + module = load_source(path) + output[key] = module.__getattribute__(overwrite[key]["class"]) return output @classmethod diff --git a/tools/tiny-test-fw/Utility/SearchCases.py b/tools/tiny-test-fw/Utility/SearchCases.py index a0930d307..3ce98a07e 100644 --- a/tools/tiny-test-fw/Utility/SearchCases.py +++ b/tools/tiny-test-fw/Utility/SearchCases.py @@ -30,7 +30,7 @@ class Search(object): print("Try to get cases from: " + file_name) test_functions = [] try: - mod = load_source(str(hash(file_name)), file_name) + mod = load_source(file_name) for func in [mod.__getattribute__(x) for x in dir(mod) if isinstance(mod.__getattribute__(x), types.FunctionType)]: try: diff --git a/tools/tiny-test-fw/Utility/__init__.py b/tools/tiny-test-fw/Utility/__init__.py index fbd2989bb..7fec71828 100644 --- a/tools/tiny-test-fw/Utility/__init__.py +++ b/tools/tiny-test-fw/Utility/__init__.py @@ -1,4 +1,5 @@ from __future__ import print_function +import os.path import sys @@ -45,16 +46,26 @@ __LOADED_MODULES = dict() # it will lead to strange errors like `isinstance(object, type_of_this_object)` return False -def load_source(name, path): +def load_source(path): + """ + Dynamic loading python file. Note that this function SHOULD NOT be used to replace ``import``. + It should only be used when the package path is only available in runtime. + + :param path: The path of python file + :return: Loaded object + """ + path = os.path.realpath(path) + # load name need to be unique, otherwise it will update the already loaded module + load_name = str(len(__LOADED_MODULES)) try: - return __LOADED_MODULES[name] + return __LOADED_MODULES[path] except KeyError: try: from importlib.machinery import SourceFileLoader - ret = SourceFileLoader(name, path).load_module() + ret = SourceFileLoader(load_name, path).load_module() except ImportError: # importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3) import imp - ret = imp.load_source(name, path) - __LOADED_MODULES[name] = ret + ret = imp.load_source(load_name, path) + __LOADED_MODULES[path] = ret return ret