コード例 #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
ファイル: contextutil.py プロジェクト: traviscrawford/pants
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
ファイル: contextutil.py プロジェクト: foursquare/pants
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
ファイル: contextutil.py プロジェクト: ryokugyu/pants
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
ファイル: test_tarutil.py プロジェクト: CaitieM20/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)
コード例 #6
0
ファイル: test_tarutil.py プロジェクト: zvikihouzz/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)
コード例 #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
ファイル: test_tarutil.py プロジェクト: CaitieM20/pants
 def extract_tar(self, path, **kwargs):
   with TarFile.open(self.file_tar, mode='r', **kwargs) as tar:
     tar.extractall(path=self.basedir)
コード例 #10
0
ファイル: test_tarutil.py プロジェクト: zvikihouzz/pants
 def extract_tar(self, path, **kwargs):
     with TarFile.open(self.file_tar, mode='r', **kwargs) as tar:
         tar.extractall(path=self.basedir)