def _reset_files(self): for dir in DirEntry.all().fetch(1000): dir.delete() reloads = [] for dirpath, dirs, files in os.walk('nomic'): for name in files: if name.startswith('.') or name.endswith('.pyc'): continue path = os.path.join(dirpath, name)[len('nomic'):] dir = DirEntry.from_path(path, create=True) data = open('nomic/'+path).read() mime_type, encoding = mimetypes.guess_type(path, False) if not mime_type: mime_type = 'application/octet-stream' if mime_type.startswith('text'): data = data.replace('\r\n', '\n') dir.new_version(data) if path.endswith('.py'): mod_name = 'nomic.' + path[1:-3].replace('/', '.') mod = sys.modules.get(mod_name) if mod is not None: reloads.append(mod) for mod in reloads: try: reload(mod) except: pass
def load_module(self, name): logging.debug('Trying to load %s', name) path = self._modname_to_path(name) ispkg = self.is_package(name) if ispkg: code_file = ispkg else: dir = DirEntry.from_path(path) if not dir or not dir.latest: raise ImportError, 'datastore:%s not found (%s)'%(path, name) code_file = dir.latest mod = sys.modules.get(name) do_reload = True if mod is not None and mod.__mtime__ >= code_file.last_modified: do_reload = False if do_reload: logging.debug('Loading module %s', name) mod = sys.modules.setdefault(name, imp.new_module(name)) mod.__file__ = 'datastore:%s' % code_file.parent().path mod.__loader__ = self mod.__mtime__ = datetime.datetime.now() if ispkg: mod.__path__ = [code_file.parent().path[:-12]] code = 'from __future__ import absolute_import\n' + code_file.data + '\n\n' mod.__dict__['__NomicDirEntry__'] = DirEntry compiled = compile(code, mod.__file__, 'exec') exec compiled in mod.__dict__ if '.' in name: parent_name, child_name = name.rsplit('.', 1) setattr(sys.modules[parent_name], child_name, mod) return sys.modules[name]
def get_source(self, environment, template): path = '/templates/'+template dir = DirEntry.from_path(path) if not dir: return None ver = dir.latest if not ver: return None return ver.data, 'datastore:'+path, lambda: False
def get_source(self, name): pkg = self.is_package(name) if pkg: return '\n'+pkg.data path = self._modname_to_path(name) dir = DirEntry.from_path(name) if not dir: return None ver = dir.latest if not ver: return None return '\n'+ver.data
def get(self, path=None): dir = DirEntry.from_path('/htdocs/'+path) if not dir: self.error(404) return ver = dir.latest if not ver: self.error(404) return mime_type, encoding = mimetypes.guess_type(dir.path, False) if not mime_type: mime_type = 'application/octet-stream' self.response.headers['Content-Type'] = mime_type self.response.out.write(ver.data)
def is_package(self, name): path = self._pkgname_to_path(name) dir = DirEntry.from_path(path) if not dir: return False return dir.latest