示例#1
0
文件: file.py 项目: animesh/appyter
 def exists(self, path):
   try:
     assert path
     return os.path.isfile(FS.join(self._prefix, path)) or os.path.islink(FS.join(self._prefix, path))
   except Exception:
     logger.error(traceback.format_exc())
     raise Exception(f"An error occurred while trying to access {path}")
示例#2
0
def data_files(path):
    if path.endswith('/'):
        mimetype = request.accept_mimetypes.best_match([
            'text/html',
            'application/json',
            'application/vnd.jupyter',
        ], 'text/html')
        if mimetype == 'text/html':
            fs = Filesystem(current_app.config['CWD'])
            env = get_jinja2_env(config=current_app.config)
            return env.get_template('landing.j2').render(_nb=os.path.basename(
                current_app.config['IPYNB']), )
        else:
            data_fs = Filesystem(
                Filesystem.join(current_app.config['DATA_DIR'], 'output'))
            path += current_app.config['IPYNB']
            if data_fs.exists(path):
                return send_file(data_fs.open(path, 'rb'),
                                 attachment_filename=os.path.basename(path))
    else:
        data_fs = Filesystem(
            Filesystem.join(current_app.config['DATA_DIR'], 'output'))
        if data_fs.exists(path):
            return send_file(data_fs.open(path, 'rb'),
                             attachment_filename=os.path.basename(path))
    abort(404)
示例#3
0
文件: file.py 项目: animesh/appyter
 def link(self, src, dst):
   try:
     assert src and dst
     os.makedirs(os.path.dirname(FS.join(self._prefix, dst)), exist_ok=True)
     return os.link(FS.join(self._prefix, src), FS.join(self._prefix, dst))
   except Exception:
     logger.error(traceback.format_exc())
     raise Exception(f"An error occurred while trying to link {src} to {dst}")
示例#4
0
文件: file.py 项目: animesh/appyter
 def rm(self, path, recursive=False):
   try:
     assert path
     if recursive:
       return shutil.rmtree(FS.join(self._prefix, path))
     else:
       return os.remove(FS.join(self._prefix, path))
   except Exception:
     logger.error(traceback.format_exc())
     raise Exception(f"An error occurred while trying to access {path}")
示例#5
0
文件: file.py 项目: animesh/appyter
 def open(self, path, mode='r'):
   try:
     assert path
     if mode[0] in {'w', 'a'}:
       os.makedirs(os.path.dirname(FS.join(self._prefix, path)), exist_ok=True)
     return open(FS.join(self._prefix, path), mode=mode)
   except FileNotFoundError:
     raise Exception(f"No such file or directory: {path}")
   except Exception:
     logger.error(traceback.format_exc())
     raise Exception(f"An error occurred while trying to access {path}")
示例#6
0
文件: file.py 项目: animesh/appyter
 def chmod_ro(self, path):
   try:
     assert path
     return os.chmod(FS.join(self._prefix, path), 400)
   except Exception:
     logger.error(traceback.format_exc())
     raise Exception(f"An error occurred while trying to access {path}")
示例#7
0
def organize_file_content(data_fs, tmp_fs, tmp_path):
  with tmp_fs.open(tmp_path, 'rb') as fr:
    content_hash = sha1sum_io(fr)
  data_path = Filesystem.join('input', content_hash)
  if not data_fs.exists(data_path):
    Filesystem.mv(src_fs=tmp_fs, src_path=tmp_path, dst_fs=data_fs, dst_path=data_path)
    data_fs.chmod_ro(data_path)
  return content_hash
示例#8
0
def prepare_results(data):
    results_hash = sha1sum_dict(dict(ipynb=get_ipynb_hash(), data=data))
    data_fs = Filesystem(current_app.config['DATA_DIR'])
    results_path = Filesystem.join('output', results_hash)
    if not data_fs.exists(
            Filesystem.join(results_path, current_app.config['IPYNB'])):
        # prepare files to be linked and update field to use filename
        file_fields = {
            field['args']['name']
            for field in get_fields() if field['field'] == 'FileField'
        }
        links = []
        files = {}
        for file_field in file_fields:
            if fdata := data.get(file_field):
                content_hash, filename = fdata.split('/', maxsplit=1)
                content_hash = sanitize_sha1sum(content_hash)
                filename = secure_filepath(filename)
                links.append((Filesystem.join('input', content_hash),
                              Filesystem.join(results_path, filename)))
                files[filename] = filename
                data[file_field] = filename
        # construct notebook
        env = get_jinja2_env(config=current_app.config,
                             context=data,
                             session=results_hash)
        fs = Filesystem(current_app.config['CWD'])
        with fs.open(current_app.config['IPYNB'], 'r') as fr:
            nbtemplate = nb_from_ipynb_io(fr)
        # in case of constraint failures, we'll fail here
        nb = render_nb_from_nbtemplate(env, nbtemplate, files=files)
        # actually link all input files into output directory
        for src, dest in links:
            data_fs.link(src, dest)
        # write notebook
        nbfile = Filesystem.join(results_path,
                                 os.path.basename(current_app.config['IPYNB']))
        with data_fs.open(nbfile, 'w') as fw:
            nb_to_ipynb_io(nb, fw)
示例#9
0
文件: s3.py 项目: animesh/appyter
 def ls(self, path=''):
   ls_path = FS.join(self._prefix, path) if path else self._prefix
   return [
     f[len(ls_path)+1:]
     for f in self._fs.glob(ls_path + '/*') + self._fs.glob(ls_path + '/**/*')
   ]
示例#10
0
文件: file.py 项目: animesh/appyter
 def path(self, path=''):
   return FS.join(self._prefix, path)