示例#1
0
    def temporary_fs(self, snapshot=None, **kwargs):
        with TemporaryDirectory() as tempdir:
            archive_file = os.path.join(tempdir, 'archive.zip')
            tempdir = os.path.join(tempdir, 'temp')
            if snapshot:
                for filename, payload in six.iteritems(snapshot):
                    content = payload[0]
                    file_path = os.path.join(tempdir, filename)
                    file_dir = os.path.split(file_path)[0]
                    makedirs(file_dir, exist_ok=True)
                    with open(file_path, 'wb') as f:
                        f.write(content)
            with zipfile.ZipFile(archive_file, 'w') as fobj:
                if snapshot:

                    def g(arcname, path):
                        for filename in os.listdir(path):
                            file_arcname = (arcname + '/' +
                                            filename if arcname else filename)
                            file_path = os.path.join(path, filename)
                            fobj.write(file_path, arcname=file_arcname)
                            if os.path.isdir(file_path):
                                g(file_arcname, file_path)

                    g('', tempdir)
            with ZipArchiveFS(archive_file, **kwargs) as fs:
                yield fs
示例#2
0
 def temporary_fs(self, snapshot=None, **kwargs):
     with TemporaryDirectory() as tempdir:
         if snapshot:
             for filename, payload in six.iteritems(snapshot):
                 content = payload[0]
                 file_path = os.path.join(tempdir, filename)
                 file_dir = os.path.split(file_path)[0]
                 makedirs(file_dir, exist_ok=True)
                 with open(file_path, 'wb') as f:
                     f.write(content)
         with LocalFS(tempdir, **kwargs) as fs:
             yield fs
示例#3
0
 def open(self, filename, mode):
     self.init()
     file_path = os.path.join(self.root_dir, filename)
     if mode == 'r':
         if not os.path.exists(file_path):
             raise DataFileNotExist(file_path)
         return self._active_files.add(open(file_path, 'rb'))
     elif mode == 'w':
         parent_dir = os.path.split(file_path)[0]
         makedirs(parent_dir, exist_ok=True)
         return self._active_files.add(open(file_path, 'wb'))
     else:
         raise InvalidOpenMode(mode)
示例#4
0
 def temporary_fs(self, snapshot=None, **kwargs):
     with TemporaryDirectory() as tempdir:
         fs = ExtendedLocalFS(tempdir, **kwargs)
         if snapshot:
             for filename, payload in six.iteritems(snapshot):
                 content = payload[0]
                 meta_dict = payload[1] if len(payload) > 1 else None
                 file_path = os.path.join(tempdir, filename)
                 file_dir = os.path.split(file_path)[0]
                 makedirs(file_dir, exist_ok=True)
                 with open(file_path, 'wb') as f:
                     f.write(content)
                 if meta_dict:
                     fs.put_meta(filename, meta_dict)
         with fs:
             yield fs
示例#5
0
 def temporary_fs(self, snapshot=None, **kwargs):
     with TemporaryDirectory() as tempdir:
         archive_file = os.path.join(tempdir, 'archive.tar.gz')
         tempdir = os.path.join(tempdir, 'temp')
         if snapshot:
             for filename, payload in six.iteritems(snapshot):
                 content = payload[0]
                 file_path = os.path.join(tempdir, filename)
                 file_dir = os.path.split(file_path)[0]
                 makedirs(file_dir, exist_ok=True)
                 with open(file_path, 'wb') as f:
                     f.write(content)
         with tarfile.open(archive_file, 'w:gz') as fobj:
             if snapshot:
                 for filename in os.listdir(tempdir):
                     temp_path = os.path.join(tempdir, filename)
                     fobj.add(temp_path, arcname=filename)
         with TarArchiveFS(archive_file, **kwargs) as fs:
             yield fs