async def save_one(self, src, dest): """use the contents manager to write a single file/folder""" # pylint: disable=broad-except stat = src.stat() is_dir = src.is_dir() model = dict( name=src.name, type="directory" if is_dir else "file", path=dest, last_modified=tz.utcfromtimestamp(stat.st_mtime), created=tz.utcfromtimestamp(stat.st_ctime), content=None if is_dir else base64.b64encode(src.read_bytes()).decode("utf-8"), format=None if is_dir else "base64", mimetype=None, size=stat.st_size, ) allow_hidden = None if hasattr(self.contents_manager, "allow_hidden"): allow_hidden = self.contents_manager.allow_hidden self.contents_manager.allow_hidden = True try: await ensure_async(self.contents_manager.save(model, dest)) except Exception as err: self.log.error(f"Couldn't save {dest}: {err}") finally: if allow_hidden is not None: self.contents_manager.allow_hidden = allow_hidden
def _base_model(self, path): """Build the common base of a contents model""" os_path = self._get_os_path(path) info = os.lstat(os_path) four_o_four = "file or directory does not exist: %r" % path if is_hidden(os_path, self.root_dir) and not self.allow_hidden: self.log.info( "Refusing to serve hidden file or directory %r, via 404 Error", os_path) raise web.HTTPError(404, four_o_four) try: # size of file size = info.st_size except (ValueError, OSError): self.log.warning("Unable to get size.") size = None try: last_modified = tz.utcfromtimestamp(info.st_mtime) except (ValueError, OSError): # Files can rarely have an invalid timestamp # https://github.com/jupyter/notebook/issues/2539 # https://github.com/jupyter/notebook/issues/2757 # Use the Unix epoch as a fallback so we don't crash. self.log.warning("Invalid mtime %s for %s", info.st_mtime, os_path) last_modified = datetime(1970, 1, 1, 0, 0, tzinfo=tz.UTC) try: created = tz.utcfromtimestamp(info.st_ctime) except (ValueError, OSError): # See above self.log.warning("Invalid ctime %s for %s", info.st_ctime, os_path) created = datetime(1970, 1, 1, 0, 0, tzinfo=tz.UTC) # Create the base model. model = {} model["name"] = path.rsplit("/", 1)[-1] model["path"] = path model["last_modified"] = last_modified model["created"] = created model["content"] = None model["format"] = None model["mimetype"] = None model["size"] = size model["writable"] = self.is_writable(path) return model
def _base_model(self, path): """Build the common base of a contents model""" os_path = self._get_os_path(path) info = os.lstat(os_path) try: # size of file size = info.st_size except (ValueError, OSError): self.log.warning("Unable to get size.") size = None try: last_modified = tz.utcfromtimestamp(info.st_mtime) except (ValueError, OSError): # Files can rarely have an invalid timestamp # https://github.com/jupyter/notebook/issues/2539 # https://github.com/jupyter/notebook/issues/2757 # Use the Unix epoch as a fallback so we don't crash. self.log.warning("Invalid mtime %s for %s", info.st_mtime, os_path) last_modified = datetime(1970, 1, 1, 0, 0, tzinfo=tz.UTC) try: created = tz.utcfromtimestamp(info.st_ctime) except (ValueError, OSError): # See above self.log.warning("Invalid ctime %s for %s", info.st_ctime, os_path) created = datetime(1970, 1, 1, 0, 0, tzinfo=tz.UTC) # Create the base model. model = {} model["name"] = path.rsplit("/", 1)[-1] model["path"] = path model["last_modified"] = last_modified model["created"] = created model["content"] = None model["format"] = None model["mimetype"] = None model["size"] = size try: model["writable"] = os.access(os_path, os.W_OK) except OSError: self.log.error("Failed to check write permissions on %s", os_path) model["writable"] = False return model
async def checkpoint_model(self, checkpoint_id, os_path): """construct the info dict for a given checkpoint""" stats = await run_sync(os.stat, os_path) last_modified = tz.utcfromtimestamp(stats.st_mtime) info = dict( id=checkpoint_id, last_modified=last_modified, ) return info
async def get(self): # if started was missing, use unix epoch started = self.settings.get('started', utcfromtimestamp(0)) started = isoformat(started) kernels = await ensure_async(self.kernel_manager.list_kernels()) total_connections = sum(k['connections'] for k in kernels) last_activity = isoformat(self.application.last_activity()) model = { 'started': started, 'last_activity': last_activity, 'kernels': len(kernels), 'connections': total_connections, } self.finish(json.dumps(model, sort_keys=True))
def get(self): # if started was missing, use unix epoch started = self.settings.get("started", utcfromtimestamp(0)) started = isoformat(started) kernels = yield maybe_future(self.kernel_manager.list_kernels()) total_connections = sum(k["connections"] for k in kernels) last_activity = isoformat(self.application.last_activity()) model = { "started": started, "last_activity": last_activity, "kernels": len(kernels), "connections": total_connections, } self.finish(json.dumps(model, sort_keys=True))