def test_missing_app(self): """ Test that repeated app loading doesn't succeed in case there is an error. Refs #17667. """ # AppCache is a Borg, so we can instantiate one and change its # loaded to False to force the following code to actually try to # populate the cache. a = AppCache() a.loaded = False try: with override_settings(INSTALLED_APPS=('notexists',)): self.assertRaises(ImportError, get_model, 'notexists', 'nomodel', seed_cache=True) self.assertRaises(ImportError, get_model, 'notexists', 'nomodel', seed_cache=True) finally: a.loaded = True
def _reload_models_module(self, app_name): """ Reload Django models Based on http://stackoverflow.com/questions/890924/how-do-you-reload-a-django-model-module-using-the-interactive-interpreter-via-m """ curdir = os.getcwd() cache = AppCache() for app in cache.get_apps(): f = app.__file__ if f.startswith(curdir) and f.endswith('.pyc'): try: os.remove(f) except Exception: pass __import__(app.__name__) reload(app) cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False # Reset app's models in global scope # Using a dictionary here instead of cache.get_models(app_name) # The latter does not seem to work (look into that) reimported_app = importlib.import_module("{}.models".format(app_name)) model_names = self.model_globals[app_name] for model_name in model_names: self.shell_globals[model_name] = getattr(reimported_app, model_name) self._update_class_instances(reimported_app, model_name)
def _redo_app_cache(self): """ Used to repopulate AppCache after fiddling with INSTALLED_APPS. """ a = AppCache() a.loaded = False a._populate()
def clean_cache(): from django.db.models.loading import AppCache cache = AppCache() from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
def _redo_app_cache(self): """ Used to repopulate AppCache after fiddling with INSTALLED_APPS. """ a = AppCache() a.loaded = False a.handled = {} a.postponed = [] a.app_store = SortedDict() a.app_models = SortedDict() a.app_errors = {} a._populate()
def reload_django_appcache(): cache = AppCache() cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False for app in cache.get_apps(): __import__(app.__name__) reload(app)
def reload_models(): import os from django.db.models.loading import AppCache cache = AppCache() curdir = os.getcwd() for app in cache.get_apps(): f = app.__file__ if f.startswith(curdir) and f.endswith('.pyc'): os.remove(f) __import__(app.__name__) reload(app) from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
def redo_app_cache(self): from django.db.models.loading import AppCache a = AppCache() a.loaded = False a._populate()
from django.db.models.loading import AppCache cache = AppCache() for app in cache.get_apps(): __import__(app.__name__) reload(app) from django.utils.datastructures import SortedDict cache.app_store = SortedDict() cache.app_models = SortedDict() cache.app_errors = {} cache.handled = {} cache.loaded = False
def restart_cache(): import os from django.db.models.loading import AppCache cache = AppCache() cache.loaded = False
def reload_modules(): """ Reload modules that don't have any variables that can be reset. Note that python reloading is a tricky art and strange things have been known to happen if debugging and reloading a lot. A server cold reboot is often needed eventually. """ # We protect e.g. src/ from reload since reloading it in a running # server can create unexpected results (and besides, non-evennia devs # should never need to do that anyway). Updating src requires a server # reboot. Modules in except_dirs are considered ok to reload despite being # inside src/ protected_dirs = ('src.', 'django.', 'twisted.') # note that these MUST be tuples! except_dirs = ('src.commands.default.',) # " # flag 'dangerous' typeclasses (those which retain a memory # reference, notably Scripts with a timer component) for # non-reload, since these cannot be safely cleaned from memory # without causing havoc. A server reboot is required for updating # these (or killing all running, timed scripts). unsafe_modules = [] for scriptobj in ScriptDB.objects.get_all_scripts(): if (scriptobj.interval > -1) and scriptobj.typeclass_path: unsafe_modules.append(scriptobj.typeclass_path) unsafe_modules = list(set(unsafe_modules)) def safe_dir_to_reload(modpath): "Check so modpath is not a subdir of a protected dir, and not an ok exception" return not any(modpath.startswith(pdir) and not any(modpath.startswith(edir) for edir in except_dirs) for pdir in protected_dirs) def safe_mod_to_reload(modpath): "Check so modpath is not in an unsafe module" return not any(mpath.startswith(modpath) for mpath in unsafe_modules) #cemit_info(" Cleaning module caches ...") # clean as much of the caches as we can cache = AppCache() cache.app_store = SortedDict() #cache.app_models = SortedDict() # cannot clean this, it resets ContentTypes! cache.app_errors = {} cache.handled = {} cache.loaded = False # find modified modules modified = reimport.modified() safe_dir_modified = [mod for mod in modified if safe_dir_to_reload(mod)] unsafe_dir_modified = [mod for mod in modified if mod not in safe_dir_modified] safe_modified = [mod for mod in safe_dir_modified if safe_mod_to_reload(mod)] unsafe_mod_modified = [mod for mod in safe_dir_modified if mod not in safe_modified] string = "" if unsafe_dir_modified or unsafe_mod_modified: if unsafe_mod_modified: string += "\n {rModules containing Script classes with a timer component{n" string += "\n {rand which has already spawned instances cannot be reloaded safely.{n" string += "\n {rThese module(s) can only be reloaded by server reboot:{n\n %s\n" string = string % ", ".join(unsafe_dir_modified + unsafe_mod_modified) if string: cemit_info(string) if safe_modified: cemit_info(" Reloading safe module(s):{n\n %s" % "\n ".join(safe_modified)) reimport.reimport(*safe_modified) wait_time = 5 cemit_info(" Waiting %s secs to give modules time to re-cache ..." % wait_time) time.sleep(wait_time) cemit_info(" ... all safe modules reloaded.") else: cemit_info(" No modules reloaded.") # clean out cache dictionary of typeclasses, exits and channels typeclassmodels.reset() channelhandler.CHANNELHANDLER.update()