def test_join_files(tmpdir, filedata, tmp_fs, monkeypatch): outfile = Path(tmpdir, 'testtar.tar.xz') test_outfs = tarfs.TarFS(str(outfile), compression='xz', write=True) file_io.join_files(tmp_fs, test_outfs) test_readout = tarfs.TarFS(str(outfile)) for key, value in filedata.items(): assert key in test_readout.listdir('/') with test_readout.open(key, 'r') as outfile: assert value in outfile.read() test_readout.close()
def test_unicode_paths(self): # https://github.com/PyFilesystem/pyfilesystem2/issues/135 with tarfs.TarFS(self._temp_path, write=True) as tar_fs: tar_fs.writetext("Файл", "some content") with tarfs.TarFS(self._temp_path) as tar_fs: paths = list(tar_fs.walk.files()) for path in paths: self.assertIsInstance(path, six.text_type) with tar_fs.openbin(path) as f: f.read()
def setUp(self): self.tempfile = self.tmpfs.open("test.tar", "wb+") with tarfile.open(mode="w", fileobj=self.tempfile) as tf: tf.addfile(tarfile.TarInfo("."), io.StringIO()) tf.addfile(tarfile.TarInfo("../foo.txt"), io.StringIO()) self.tempfile.seek(0) self.fs = tarfs.TarFS(self.tempfile)
def tarfile(tmpdir_factory, filedata, tmp_fs): tardir = tmpdir_factory.mktemp('tar_fixture') path = Path(tardir, 'stuff.tar') tar_fs = tarfs.TarFS(path, write=True) copy_fs(tmp_fs, tar_fs) tar_fs.close() return path
def test_read_non_existent_file(self): fs = tarfs.TarFS(open(self._temp_path, "rb")) # it has been very difficult to catch exception in __del__() del fs._tar try: fs.close() except AttributeError: self.fail("Could not close tar fs properly") except Exception: self.fail("Strange exception in closing fs")
def setUp(self): self.tempfile = self.tmpfs.open("test.tar", "wb+") with tarfile.open(mode="w", fileobj=self.tempfile) as tf: tf.addfile(tarfile.TarInfo("foo/bar/baz/spam.txt"), io.StringIO()) tf.addfile(tarfile.TarInfo("foo/eggs.bin"), io.StringIO()) tf.addfile(tarfile.TarInfo("foo/yolk/beans.txt"), io.StringIO()) info = tarfile.TarInfo("foo/yolk") info.type = tarfile.DIRTYPE tf.addfile(info, io.BytesIO()) self.tempfile.seek(0) self.fs = tarfs.TarFS(self.tempfile)
def _package(self, outfile): os.makedirs(os.path.dirname(outfile), exist_ok=True) if self.config['Store As']['Type'].lower() == 'tar': savefs = tarfs.TarFS( outfile, write=True, compression=self.config['Store As']['Compression'] ) elif self.config['Store As']['Type'].lower() == 'zip': savefs = zipfs.ZipFS(outfile) else: _config_error("Store AS: Type is invalid") file_io.join_files(self.tmp, savefs)
def test_write_out_backup(tmpdir_factory, filedata, tarfile, tmp_fs): testdir1 = tmpdir_factory.mktemp('write_out_backup_1') testdir2 = tmpdir_factory.mktemp('write_out_backup_2') testdir3 = tmpdir_factory.mktemp('write_out_backup_3') testdir4 = tmpdir_factory.mktemp('write_out_backup_4') testdir5 = tmpdir_factory.mktemp('write_out_backup_5') back_fs1 = fs.open_fs(str(testdir1)) back_fs2 = fs.open_fs(str(testdir2)) back_fs3 = fs.open_fs(str(testdir3)) back_fs4 = fs.open_fs(str(testdir4)) back_fs5 = fs.open_fs(str(testdir5)) file_io.write_out_backup([back_fs1, back_fs2], filepath=tarfile) file_io.write_out_backup(back_fs3, filesystem=tmp_fs, prefix='hi/') file_io.write_out_backup(back_fs4, filesystem=tmp_fs, prefix='hi') file_io.write_out_backup(back_fs5, filesystem=tmp_fs) assert 'stuff.tar' in back_fs1.listdir('/') assert 'stuff.tar' in back_fs2.listdir('/') back_fs1.close() back_fs2.close() tarout1 = tarfs.TarFS(str(testdir1) + '/stuff.tar') tarout2 = tarfs.TarFS(str(testdir2) + '/stuff.tar') for key, value in filedata.items(): with tarout1.open(key) as testfile: assert value in testfile with tarout2.open(key) as testfile: assert value in testfile assert key in back_fs3.listdir('/hi') assert key in back_fs4.listdir('/hi') assert key in back_fs5.listdir('/') with back_fs3.open('hi/' + key) as testfile: assert value in testfile with back_fs4.open('hi/' + key) as testfile: assert value in testfile with back_fs5.open(key) as testfile: assert value in testfile with pytest.raises(AttributeError): file_io.write_out_backup(back_fs1)
def make_fs(self): fh, _tar_file = tempfile.mkstemp() os.close(fh) fs = tarfs.TarFS(_tar_file, write=True, compression="gz") fs._tar_file = _tar_file return fs
def make_fs(self): _tar_file = six.BytesIO() fs = tarfs.TarFS(_tar_file, write=True) fs._tar_file = _tar_file return fs
def test_read_from_filename(self): try: tarfs.TarFS(self._temp_path) except: self.fail("Couldn't open tarfs from filename")
def test_read_from_fileobject(self): try: tarfs.TarFS(open(self._temp_path, "rb")) except: self.fail("Couldn't open tarfs from fileobject")
def load_archive(self): return tarfs.TarFS(self._temp_path)