def open_tar(path_or_file, *args, **kwargs): """ A with-context for tar files. Passes through positional and kwargs to tarfile.open. If path_or_file is a file, caller must close it separately. """ (path, fileobj) = (path_or_file, None) if isinstance(path_or_file, str) else (None, path_or_file) with closing(TarFile.open(path, *args, fileobj=fileobj, **kwargs)) as tar: yield tar
def open_tar(path_or_file, *args, **kwargs): """ A with-context for tar files. Passes through positional and kwargs to tarfile.open. If path_or_file is a file, caller must close it separately. """ (path, fileobj) = ((path_or_file, None) if isinstance(path_or_file, string_types) else (None, path_or_file)) with closing(TarFile.open(path, *args, fileobj=fileobj, **kwargs)) as tar: yield tar
def open_tar(path_or_file, *args, **kwargs): """ A with-context for tar files. Passes through positional and kwargs to tarfile.open. If path_or_file is a file, caller must close it separately. """ (path, fileobj) = ((path_or_file, None) if isinstance(path_or_file, string_types) else (None, path_or_file)) # TODO(python3port): stop using six.string_types # This should only accept python3 `str`, not byte strings. with closing(TarFile.open(path, *args, fileobj=fileobj, **kwargs)) as tar: yield tar
def setUp(self): self.basedir = safe_mkdtemp() self.file_list = ['a', 'b', 'c'] self.file_tar = os.path.join(self.basedir, 'test.tar') with TarFile.open(self.file_tar, mode='w') as tar: for f in self.file_list: full_path = os.path.join(self.basedir, f) touch(full_path) tar.add(full_path, f) safe_delete(full_path)
def setUp(self): self.basedir = safe_mkdtemp() self.file_list = ["a", "b", "c"] self.file_tar = os.path.join(self.basedir, "test.tar") with TarFile.open(self.file_tar, mode="w") as tar: for f in self.file_list: full_path = os.path.join(self.basedir, f) touch(full_path) tar.add(full_path, f) safe_delete(full_path)
def open_tar(path_or_file: Union[str, Any], *args, **kwargs) -> Iterator[TarFile]: """A with-context for tar files. Passes through positional and kwargs to tarfile.open. If path_or_file is a file, caller must close it separately. """ (path, fileobj) = ((path_or_file, None) if isinstance(path_or_file, str) else (None, path_or_file)) kwargs["fileobj"] = fileobj with closing(TarFile.open(path, *args, **kwargs)) as tar: # We must cast the normal tarfile.TarFile to our custom pants.util.tarutil.TarFile. typed_tar = cast(TarFile, tar) yield typed_tar
def extract_tar(self, path, **kwargs): with TarFile.open(self.file_tar, mode='r', **kwargs) as tar: tar.extractall(path=self.basedir)