예제 #1
0
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
예제 #2
0
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
예제 #3
0
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
예제 #4
0
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
예제 #5
0
  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)
예제 #6
0
    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)
예제 #7
0
파일: test_tarutil.py 프로젝트: wiwa/pants
    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)
예제 #8
0
파일: contextutil.py 프로젝트: wiwa/pants
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
예제 #9
0
 def extract_tar(self, path, **kwargs):
   with TarFile.open(self.file_tar, mode='r', **kwargs) as tar:
     tar.extractall(path=self.basedir)
예제 #10
0
 def extract_tar(self, path, **kwargs):
     with TarFile.open(self.file_tar, mode='r', **kwargs) as tar:
         tar.extractall(path=self.basedir)