Blender  V2.59
bl_load_py_modules.py
Go to the documentation of this file.
00001 # ##### BEGIN GPL LICENSE BLOCK #####
00002 #
00003 #  This program is free software; you can redistribute it and/or
00004 #  modify it under the terms of the GNU General Public License
00005 #  as published by the Free Software Foundation; either version 2
00006 #  of the License, or (at your option) any later version.
00007 #
00008 #  This program is distributed in the hope that it will be useful,
00009 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 #  GNU General Public License for more details.
00012 #
00013 #  You should have received a copy of the GNU General Public License
00014 #  along with this program; if not, write to the Free Software Foundation,
00015 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00016 #
00017 # ##### END GPL LICENSE BLOCK #####
00018 
00019 # <pep8 compliant>
00020 
00021 # simple script to enable all addons, and disable
00022 
00023 import bpy
00024 import addon_utils
00025 
00026 import sys
00027 import os
00028 import imp
00029 
00030 
00031 def source_list(path, filename_check=None):
00032     from os.path import join
00033     for dirpath, dirnames, filenames in os.walk(path):
00034         # skip '.svn'
00035         if dirpath.startswith("."):
00036             continue
00037 
00038         for filename in filenames:
00039             filepath = join(dirpath, filename)
00040             if filename_check is None or filename_check(filepath):
00041                 yield filepath
00042 
00043 
00044 def load_addons():
00045     modules = addon_utils.modules({})
00046     modules.sort(key=lambda mod: mod.__name__)
00047     addons = bpy.context.user_preferences.addons
00048 
00049     # first disable all
00050     for mod_name in list(addons.keys()):
00051         addon_utils.disable(mod_name)
00052 
00053     assert(bool(addons) == False)
00054 
00055     for mod in modules:
00056         mod_name = mod.__name__
00057         addon_utils.enable(mod_name)
00058         assert(mod_name in addons)
00059 
00060 
00061 def load_modules():
00062     modules = []
00063     module_paths = []
00064 
00065     # paths blender stores scripts in.
00066     paths = bpy.utils.script_paths()
00067 
00068     print("Paths:")
00069     for script_path in paths:
00070         print("\t'%s'" % script_path)
00071 
00072     #
00073     # find all sys.path we added
00074     for script_path in paths:
00075         for mod_dir in sys.path:
00076             if mod_dir.startswith(script_path):
00077                 module_paths.append(mod_dir)
00078 
00079     #
00080     # collect modules from our paths.
00081     module_names = set()
00082     for mod_dir in module_paths:
00083         # print("mod_dir", mod_dir)
00084         for mod, mod_full in bpy.path.module_names(mod_dir):
00085             if mod in module_names:
00086                 raise Exception("Module found twice %r" % mod)
00087 
00088             modules.append(__import__(mod))
00089 
00090             module_names.add(mod)
00091     del module_names
00092 
00093     #
00094     # now submodules
00095     for m in modules:
00096         filepath = m.__file__
00097         if os.path.basename(filepath).startswith("__init__."):
00098             mod_dir = os.path.dirname(filepath)
00099             for submod, submod_full in bpy.path.module_names(mod_dir):
00100                 # fromlist is ignored, ugh.
00101                 mod_name_full = m.__name__ + "." + submod
00102                 __import__(mod_name_full)
00103                 mod_imp = sys.modules[mod_name_full]
00104 
00105                 # check we load what we ask for.
00106                 assert(os.path.samefile(mod_imp.__file__, submod_full))
00107 
00108                 modules.append(mod_imp)
00109 
00110     #
00111     # check which filepaths we didnt load
00112     source_files = []
00113     for mod_dir in module_paths:
00114         source_files.extend(source_list(mod_dir, filename_check=lambda f: f.endswith(".py")))
00115 
00116     source_files = list(set(source_files))
00117     source_files.sort()
00118 
00119     #
00120     # remove loaded files
00121     loaded_files = list({m.__file__ for m in modules})
00122     loaded_files.sort()
00123 
00124     for f in loaded_files:
00125         source_files.remove(f)
00126 
00127     #
00128     # test we tested all files except for presets and templates
00129     ignore_paths = [
00130         os.sep + "presets" + os.sep,
00131         os.sep + "templates" + os.sep,
00132     ]
00133 
00134     for f in source_files:
00135         ok = False
00136         for ignore in ignore_paths:
00137             if ignore in f:
00138                 ok = True
00139         if not ok:
00140             raise Exception("Source file %r not loaded in test" % f)
00141 
00142     print("loaded %d modules" % len(loaded_files))
00143 
00144 
00145 def main():
00146     load_addons()
00147     load_modules()
00148 
00149 if __name__ == "__main__":
00150     # So a python error exits(1)
00151     try:
00152         main()
00153     except:
00154         import traceback
00155         traceback.print_exc()
00156         sys.exit(1)