def test_blacklist(self): import reloader reloader.enable(['blacklisted']) self.write_module('blacklisted', "def func(): return True\n") self.write_module('testmodule', "import tests.blacklisted\n") import tests.blacklisted, tests.testmodule reloader.disable()
def test_reload(self): import reloader reloader.enable() self.write_module('testmodule', "def func(): return 'Some code.'\n") import tests.testmodule self.assertEqual('Some code.', tests.testmodule.func()) self.write_module('testmodule', "def func(): return 'New code.'\n") reloader.reload(tests.testmodule) self.assertEqual('New code.', tests.testmodule.func()) self.write_module('testmodule', "def func(): return 'More code.'\n") reloader.reload(tests.testmodule) self.assertEqual('More code.', tests.testmodule.func()) reloader.disable()
def import_apps(): """import or reimport modules and exposed static files""" reloader.enable() folder = os.environ['WEB3PY_APPLICATIONS_FOLDER'] app = bottle.default_app() app.routes.clear() # app.routes = app.routes[:] new_apps = [] for app_name in os.listdir(folder): path = os.path.join(folder, app_name) if os.path.isdir(path) and not path.endswith('__'): try: module = Reloader.MODULES.get(app_name) if not module: module = importlib.import_module(app_name) Reloader.MODULES[app_name] = module new_apps.append(path) else: reloader.reload(module) Reloader.ERRORS[app_name] = None except: print(traceback.format_exc()) Reloader.ERRORS[app_name] = traceback.format_exc() reloader.disable() # expose static files for path in new_apps: @bottle.route('/%s/static/<filename:path>' % path.split(os.path.sep)[-1]) def server_static(filename, path=path): return bottle.static_file(filename, root=os.path.join(path, 'static')) # register routes routes = [] def to_filename(module): filename = module.replace('.', os.path.sep) filename = os.path.join(filename, '__init__.py') if not module.count('.') else filename + '.py' return filename for route in app.routes: func = route.callback routes.append({'rule': route.rule, 'method': route.method, 'filename': to_filename(func.__module__), 'action': func.__name__}) Reloader.ROUTES = sorted(routes, key=lambda item: item['rule'])
def reloadallmodules(): from config import es_constants from database import connectdb from database import querydb from apps.productmanagement import datasets from apps.productmanagement import products import reloader reloader.enable() # print reloader.get_dependencies(es_constants) reloader.reload(es_constants) reloader.reload(connectdb) reloader.reload(querydb) reloader.reload(datasets) reloader.reload(products) reloader.disable() # reloader.reload(sys.modules['config']) # from config import es_constants as constantsreloaded # for setting in constantsreloaded.es2globals: # logger.info(setting + ': ' + str(es_constants.es2globals[setting]))
def scan(self,runner_folder): base_dir = os.path.abspath(os.path.dirname(__file__)) loaded_module_names = sys.modules.keys() module_to_reload = [] # pat to recognize a runner script has_runner_pat = re.compile('^@(after_|before_)?runner') for root,folders,files in os.walk(runner_folder): runner_folder_root = os.path.normpath(os.path.join(runner_folder,root)) relpath = os.path.relpath(runner_folder_root,base_dir) paths = runner_folder_root.split('/') if (not relpath.startswith('..')) and len(relpath.split('/'))<2: sub_foldername = None if relpath=='.': foldername = None lib_folder = base_dir else: foldername = paths[-1] lib_folder = os.path.join(base_dir,paths[-1]) else: foldername = paths[-2] sub_foldername = paths[-1] lib_folder = '/'+os.path.join(*paths[:-2]) lib_folder_inserted = False if not lib_folder in sys.path: lib_folder_inserted = True sys.path.insert(0,lib_folder) for file in files: if file.startswith('.') or file.startswith('_'): continue elif file[-3:]=='.py': # avoid to load twice if execution is from the runner subfolder for unit testing file_path = os.path.join(runner_folder_root,file) # only load file which wants to register runner is_runner_script = False fd = open(file_path,'r') for line in fd: if not has_runner_pat.search(line): continue is_runner_script = True break fd.close() if not is_runner_script: continue name = os.path.splitext(file)[0] if sub_foldername is None: module_name = '%s.%s' % (foldername,name) else: module_name = '%s.%s.%s' % (foldername,sub_foldername, name) if module_name in loaded_module_names: log.msg('reload runner %s' % (module_name)) module_to_reload.append(module_name) else: log.msg('load runner %s' % (module_name)) log.msg('lib ',lib_folder) __import__(module_name, globals(), locals(), [], 0) # remove added path if lib_folder_inserted: sys.path.remove(lib_folder) if len(module_to_reload): reloader.enable() for module_name in module_to_reload: reloader.reload(sys.modules[module_name]) reloader.disable()
def import_apps(): """import or reimport modules and exposed static files""" reloader.enable() folder = os.environ['WEB3PY_APPS_FOLDER'] app = bottle.default_app() app.routes.clear() new_apps = [] for app_name in os.listdir(folder): path = os.path.join(folder, app_name) init = os.path.join(path, '__init__.py') if os.path.isdir( path) and not path.endswith('__') and os.path.exists(init): module_name = 'apps.%s' % app_name try: module = Reloader.MODULES.get(app_name) if not module: print('[ ] loading %s ...' % app_name) module = importlib.machinery.SourceFileLoader( module_name, init).load_module() Reloader.MODULES[app_name] = module new_apps.append(path) print('\x1b[A[OK] loaded %s ' % app_name) else: print('[ ] reloading %s ...' % app_name) reloader.reload(module) print('\x1b[A[OK] loaded %s ' % app_name) Reloader.ERRORS[app_name] = None except: print('\x1b[A[FAILED] loading %s ' % app_name) print('\n'.join( ' ' + line for line in traceback.format_exc().split('\n'))) Reloader.ERRORS[app_name] = traceback.format_exc() reloader.disable() # expose static files for path in new_apps: @bottle.route('/%s/static/<filename:path>' % path.split(os.path.sep)[-1]) def server_static(filename, path=path): return bottle.static_file(filename, root=os.path.join(path, 'static')) # register routes routes = [] def to_filename(module): filename = os.path.join(*module.split('.')[1:]) filename = os.path.join(filename, '__init__.py') if not filename.count( os.sep) else filename + '.py' return filename for route in app.routes: func = route.callback routes.append({ 'rule': route.rule, 'method': route.method, 'filename': to_filename(func.__module__), 'action': func.__name__ }) Reloader.ROUTES = sorted(routes, key=lambda item: item['rule'])