From f28c626574ee2de46f6b6de1d1c41b9e1d8307ef Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 2 May 2016 21:01:10 +1000 Subject: Fix bl_load_py_modules test - scripts that execute directly need to include their basedir in the sys.path - modules which are in a directory without an __init__.py weren't importing. --- tests/python/bl_load_py_modules.py | 46 +++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/python/bl_load_py_modules.py b/tests/python/bl_load_py_modules.py index 4256cba0933..c13679d16f0 100644 --- a/tests/python/bl_load_py_modules.py +++ b/tests/python/bl_load_py_modules.py @@ -38,6 +38,12 @@ BLACKLIST = { 'io_import_dxf', # Because of cydxfentity.so dependency } +# Some modules need to add to the `sys.path`. +MODULE_SYS_PATHS = { + # Runs in a Python subprocess, so its expected its basedir can be imported. + "io_blend_utils.blendfile_pack": ".", + } + if not bpy.app.build_options.freestyle: BLACKLIST.add("render_freestyle_svg") @@ -46,6 +52,34 @@ BLACKLIST_DIRS = ( ) + tuple(addon_utils.paths()[1:]) +def module_names_recursive(mod_dir, *, parent=None): + """ + a version of bpy.path.module_names that includes non-packages + """ + + is_package = os.path.exists(os.path.join(mod_dir, "__init__.py")) + + for n in os.listdir(mod_dir): + if not n.startswith((".", "_")): + submod_full = os.path.join(mod_dir, n) + if os.path.isdir(submod_full): + if not parent: + subparent = n + else: + subparent = parent + "." + n + yield from module_names_recursive(submod_full, parent=subparent) + elif n.endswith(".py") and is_package is False: + submod = n[:-3] + if parent: + submod = parent + "." + submod + yield submod, submod_full + + +def module_names_all(mod_dir): + yield from bpy.path.module_names(mod_dir) + yield from module_names_recursive(mod_dir) + + def addon_modules_sorted(): modules = addon_utils.modules({}) modules[:] = [mod for mod in modules if not mod.__file__.startswith(BLACKLIST_DIRS)] @@ -130,12 +164,22 @@ def load_modules(): filepath = m.__file__ if os.path.basename(filepath).startswith("__init__."): mod_dir = os.path.dirname(filepath) - for submod, submod_full in bpy.path.module_names(mod_dir): + for submod, submod_full in module_names_all(mod_dir): # fromlist is ignored, ugh. mod_name_full = m.__name__ + "." + submod + + sys_path_back = sys.path[:] + + sys.path.extend([ + os.path.normpath(os.path.join(mod_dir, f)) + for f in MODULE_SYS_PATHS.get(mod_name_full, ()) + ]) + __import__(mod_name_full) mod_imp = sys.modules[mod_name_full] + sys.path[:] = sys_path_back + # check we load what we ask for. assert(os.path.samefile(mod_imp.__file__, submod_full)) -- cgit v1.2.3