def list_dirs(self, path): """List the directories for a given API style path.""" os_path = self._get_os_path('', path) self.log.debug("listing dir %s, nb_dir= %s", path, self.notebook_dir) if not os_path.endswith('/'): os_path += '/' if not key_exists(self.bucket, os_path): self.log.error("path does not exist " + os_path) raise web.HTTPError(404, u'directory does not exist: %r' % os_path) elif is_hidden(self.bucket, os_path): self.log.error("Refusing to serve hidden directory %s, via 404 Error" % os_path) raise web.HTTPError(404, u'directory does not exist: %s' % path) dir_names = list_keys(self.bucket, os_path, '/') dirs = [] for name in dir_names: dir_path = self._get_os_path(name, path) self.log.debug('checking folder %s name =%s path =%s' % (dir_path, name, path)) if self.should_list(dir_path) and not is_hidden(self.bucket, dir_path): model = self.get_dir_model(name, path) dirs.append(model) return sorted(dirs, key=sort_key)
def _dir_model(self, name, path='', content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(name, path) four_o_four = u'directory does not exist: %r' % os_path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir): self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path ) raise web.HTTPError(404, four_o_four) if name is None: if '/' in path: path, name = path.rsplit('/', 1) else: name = '' model = self._base_model(name, path) model['type'] = 'directory' dir_path = u'{}/{}'.format(path, name) if content: model['content'] = contents = [] for os_path in glob.glob(self._get_os_path('*', dir_path)): name = os.path.basename(os_path) if self.should_list(name) and not is_hidden(os_path, self.root_dir): contents.append(self.get_model(name=name, path=dir_path, content=False)) model['format'] = 'json' return model
def _dir_model(self, path, content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(path) four_o_four = u"directory does not exist: %r" % os_path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir): self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path) raise web.HTTPError(404, four_o_four) model = self._base_model(path) model["type"] = "directory" if content: model["content"] = contents = [] os_dir = self._get_os_path(path) for name in os.listdir(os_dir): os_path = os.path.join(os_dir, name) # skip over broken symlinks in listing if not os.path.exists(os_path): self.log.warn("%s doesn't exist", os_path) continue elif not os.path.isfile(os_path) and not os.path.isdir(os_path): self.log.debug("%s not a regular file", os_path) continue if self.should_list(name) and not is_hidden(os_path, self.root_dir): contents.append(self.get(path="%s/%s" % (path, name), content=False)) model["format"] = "json" return model
def test_is_hidden(): with TemporaryDirectory() as root: subdir1 = os.path.join(root, 'subdir') os.makedirs(subdir1) nt.assert_equal(is_hidden(subdir1, root), False) subdir2 = os.path.join(root, '.subdir2') os.makedirs(subdir2) nt.assert_equal(is_hidden(subdir2, root), True) subdir34 = os.path.join(root, 'subdir3', '.subdir4') os.makedirs(subdir34) nt.assert_equal(is_hidden(subdir34, root), True) nt.assert_equal(is_hidden(subdir34), True)
def _dir_model(self, path, content=True): """Build a model for a directory if content is requested, will include a listing of the directory if is root_dir will refresh notebook listing from shock """ os_path = self._get_os_path(path) four_o_four = u'directory does not exist: %r' % os_path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir): self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path ) raise web.HTTPError(404, four_o_four) model = self._base_model(path) model['type'] = 'directory' if content: model['content'] = contents = [] os_dir = self._get_os_path(path) # refresh notebooks if os_dir == self.root_dir: self._get_notebook_list() self._set_notebook_list() # get listing for name in os.listdir(os_dir): os_path = os.path.join(os_dir, name) # skip over broken symlinks in listing if not os.path.exists(os_path): self.log.warn("%s doesn't exist", os_path) continue elif not os.path.isfile(os_path) and not os.path.isdir(os_path): self.log.debug("%s not a regular file", os_path) continue if self.should_list(name) and not is_hidden(os_path, self.root_dir): contents.append(self.get( path='%s/%s' % (path, name), content=False) ) model['format'] = 'json' return model
def list_dirs(self, path): """List the directories for a given API style path.""" path = path.strip('/') os_path = self.get_os_path('', path) if not os.path.isdir(os_path) or is_hidden(os_path, self.notebook_dir): raise web.HTTPError(404, u'directory does not exist: %r' % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self.get_os_path(name, path) if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir): try: model = self.get_dir_model(name, path) except IOError: pass dirs.append(model) dirs = sorted(dirs, key=lambda item: item['name']) return dirs
def list_dirs(self, path): """List the directories for a given API style path.""" path = path.strip('/') os_path = self._get_os_path('', path) if not os.path.isdir(os_path) or is_hidden(os_path, self.notebook_dir): raise web.HTTPError(404, u'directory does not exist: %r' % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self._get_os_path(name, path) if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir): try: model = self.get_dir_model(name, path) except IOError: pass dirs.append(model) dirs = sorted(dirs, key=lambda item: item['name']) return dirs
def _save_directory(self, os_path, model, name='', path=''): """create a directory""" if is_hidden(os_path, self.root_dir): raise web.HTTPError(400, u'Cannot create hidden directory %r' % os_path) if not os.path.exists(os_path): os.mkdir(os_path) elif not os.path.isdir(os_path): raise web.HTTPError(400, u'Not a directory: %s' % (os_path)) else: self.log.debug("Directory %r already exists", os_path)
def _save_directory(self, os_path, model, path=""): """create a directory""" if is_hidden(os_path, self.root_dir): raise web.HTTPError(400, u"Cannot create hidden directory %r" % os_path) if not os.path.exists(os_path): with self.perm_to_403(): os.mkdir(os_path) elif not os.path.isdir(os_path): raise web.HTTPError(400, u"Not a directory: %s" % (os_path)) else: self.log.debug("Directory %r already exists", os_path)
def list_dirs(self, path): """List the directories for a given API style path.""" path = path.strip("/") os_path = self._get_os_path("", path) if not os.path.isdir(os_path): raise web.HTTPError(404, u"directory does not exist: %r" % os_path) elif is_hidden(os_path, self.notebook_dir): self.log.info("Refusing to serve hidden directory, via 404 Error") raise web.HTTPError(404, u"directory does not exist: %r" % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self._get_os_path(name, path) if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir) and self.should_list(name): try: model = self.get_dir_model(name, path) except IOError: pass dirs.append(model) dirs = sorted(dirs, key=sort_key) return dirs
def validate_absolute_path(self, root, absolute_path): """Validate and return the absolute path. Requires tornado 3.1 Adding to tornado's own handling, forbids the serving of hidden files. """ abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path) abs_root = os.path.abspath(root) if is_hidden(abs_path, abs_root): raise web.HTTPError(404) return abs_path
def _dir_model(self, path, content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(path) four_o_four = u'directory does not exist: %r' % path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir): self.log.info( "Refusing to serve hidden directory %r, via 404 Error", os_path) raise web.HTTPError(404, four_o_four) model = self._base_model(path) model['type'] = 'directory' if content: model['content'] = contents = [] os_dir = self._get_os_path(path) for name in os.listdir(os_dir): os_path = os.path.join(os_dir, name) # skip over broken symlinks in listing if not os.path.exists(os_path): self.log.warn("%s doesn't exist", os_path) continue elif not os.path.isfile(os_path) and not os.path.isdir( os_path): self.log.debug("%s not a regular file", os_path) continue if self.should_list(name) and not is_hidden( os_path, self.root_dir): contents.append( self.get(path='%s/%s' % (path, name), content=False)) model['format'] = 'json' return model
def list_dirs(self, path): """List the directories for a given API style path.""" path = path.strip('/') os_path = self._get_os_path('', path) if not os.path.isdir(os_path): raise web.HTTPError(404, u'directory does not exist: %r' % os_path) elif is_hidden(os_path, self.notebook_dir): self.log.info("Refusing to serve hidden directory, via 404 Error") raise web.HTTPError(404, u'directory does not exist: %r' % os_path) dir_names = os.listdir(os_path) dirs = [] for name in dir_names: os_path = self._get_os_path(name, path) if os.path.isdir(os_path) and not is_hidden(os_path, self.notebook_dir)\ and self.should_list(name): try: model = self.get_dir_model(name, path) except IOError: pass dirs.append(model) dirs = sorted(dirs, key=sort_key) return dirs
def validate_absolute_path(self, root, absolute_path): """Validate and return the absolute path. Requires tornado 3.1 Adding to tornado's own handling, forbids the serving of hidden files. """ abs_path = super(AuthenticatedFileHandler, self).validate_absolute_path(root, absolute_path) abs_root = os.path.abspath(root) if is_hidden(abs_path, abs_root): self.log.info("Refusing to serve hidden file, via 404 Error") raise web.HTTPError(404) return abs_path
def _dir_model(self, name, path="", content=True): """Build a model for a directory if content is requested, will include a listing of the directory """ os_path = self._get_os_path(name, path) four_o_four = u"directory does not exist: %r" % os_path if not os.path.isdir(os_path): raise web.HTTPError(404, four_o_four) elif is_hidden(os_path, self.root_dir): self.log.info("Refusing to serve hidden directory %r, via 404 Error", os_path) raise web.HTTPError(404, four_o_four) if name is None: if "/" in path: path, name = path.rsplit("/", 1) else: name = "" model = self._base_model(name, path) model["type"] = "directory" dir_path = u"{}/{}".format(path, name) if content: model["content"] = contents = [] for os_path in glob.glob(self._get_os_path("*", dir_path)): name = os.path.basename(os_path) # skip over broken symlinks in listing if not os.path.exists(os_path): self.log.warn("%s doesn't exist", os_path) continue if self.should_list(name) and not is_hidden(os_path, self.root_dir): contents.append(self.get_model(name=name, path=dir_path, content=False)) model["format"] = "json" return model
def is_hidden(self, path): """Does the API style path correspond to a hidden directory or file? Parameters ---------- path : string The path to check. This is an API path (`/` separated, relative to base notebook-dir). Returns ------- exists : bool Whether the path is hidden. """ os_path = self._get_os_path(path=path) return is_hidden(self.bucket, os_path)
def is_hidden(self, path): """Does the API style path correspond to a hidden directory or file? Parameters ---------- path : string The path to check. This is an API path (`/` separated, relative to root_dir). Returns ------- hidden : bool Whether the path exists and is hidden. """ path = path.strip('/') os_path = self._get_os_path(path=path) return is_hidden(os_path, self.root_dir)