Blender V2.61 - r43446

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 
00029 
00030 def source_list(path, filename_check=None):
00031     from os.path import join
00032     for dirpath, dirnames, filenames in os.walk(path):
00033         # skip '.svn'
00034         if dirpath.startswith("."):
00035             continue
00036 
00037         for filename in filenames:
00038             filepath = join(dirpath, filename)
00039             if filename_check is None or filename_check(filepath):
00040                 yield filepath
00041 
00042 
00043 def load_addons():
00044     modules = addon_utils.modules({})
00045     modules.sort(key=lambda mod: mod.__name__)
00046     addons = bpy.context.user_preferences.addons
00047 
00048     # first disable all
00049     for mod_name in list(addons.keys()):
00050         addon_utils.disable(mod_name)
00051 
00052     assert(bool(addons) == False)
00053 
00054     for mod in modules:
00055         mod_name = mod.__name__
00056         addon_utils.enable(mod_name)
00057         assert(mod_name in addons)
00058 
00059 
00060 def load_modules():
00061     modules = []
00062     module_paths = []
00063 
00064     # paths blender stores scripts in.
00065     paths = bpy.utils.script_paths()
00066 
00067     print("Paths:")
00068     for script_path in paths:
00069         print("\t'%s'" % script_path)
00070 
00071     #
00072     # find all sys.path we added
00073     for script_path in paths:
00074         for mod_dir in sys.path:
00075             if mod_dir.startswith(script_path):
00076                 module_paths.append(mod_dir)
00077 
00078     #
00079     # collect modules from our paths.
00080     module_names = set()
00081     for mod_dir in module_paths:
00082         # print("mod_dir", mod_dir)
00083         for mod, mod_full in bpy.path.module_names(mod_dir):
00084             if mod in module_names:
00085                 raise Exception("Module found twice %r" % mod)
00086 
00087             modules.append(__import__(mod))
00088 
00089             module_names.add(mod)
00090     del module_names
00091 
00092     #
00093     # now submodules
00094     for m in modules:
00095         filepath = m.__file__
00096         if os.path.basename(filepath).startswith("__init__."):
00097             mod_dir = os.path.dirname(filepath)
00098             for submod, submod_full in bpy.path.module_names(mod_dir):
00099                 # fromlist is ignored, ugh.
00100                 mod_name_full = m.__name__ + "." + submod
00101                 __import__(mod_name_full)
00102                 mod_imp = sys.modules[mod_name_full]
00103 
00104                 # check we load what we ask for.
00105                 assert(os.path.samefile(mod_imp.__file__, submod_full))
00106 
00107                 modules.append(mod_imp)
00108 
00109     #
00110     # check which filepaths we didnt load
00111     source_files = []
00112     for mod_dir in module_paths:
00113         source_files.extend(source_list(mod_dir, filename_check=lambda f: f.endswith(".py")))
00114 
00115     source_files = list(set(source_files))
00116     source_files.sort()
00117 
00118     #
00119     # remove loaded files
00120     loaded_files = list({m.__file__ for m in modules})
00121     loaded_files.sort()
00122 
00123     for f in loaded_files:
00124         source_files.remove(f)
00125 
00126     #
00127     # test we tested all files except for presets and templates
00128     ignore_paths = [
00129         os.sep + "presets" + os.sep,
00130         os.sep + "templates" + os.sep,
00131     ]
00132 
00133     for f in source_files:
00134         ok = False
00135         for ignore in ignore_paths:
00136             if ignore in f:
00137                 ok = True
00138         if not ok:
00139             raise Exception("Source file %r not loaded in test" % f)
00140 
00141     print("loaded %d modules" % len(loaded_files))
00142 
00143 
00144 def main():
00145     load_addons()
00146     load_modules()
00147 
00148 if __name__ == "__main__":
00149     # So a python error exits(1)
00150     try:
00151         main()
00152     except:
00153         import traceback
00154         traceback.print_exc()
00155         sys.exit(1)