當前位置: 首頁>>代碼示例>>Python>>正文


Python tarfile.TarFile方法代碼示例

本文整理匯總了Python中tarfile.TarFile方法的典型用法代碼示例。如果您正苦於以下問題:Python tarfile.TarFile方法的具體用法?Python tarfile.TarFile怎麽用?Python tarfile.TarFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tarfile的用法示例。


在下文中一共展示了tarfile.TarFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: open_archive

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def open_archive(self, file_path, compression_type=None, mode='r'):
        """
        Test if archive can be assumed by filename

        @param file_path: Path to file
        @type file_path: str | unicode

        @return: True if stream
        @rtype: tarfile.TarFile
        """
        assert mode in self._modes, "Unsupported mode".format(mode)
        if compression_type is None:
            compression_type = self.get_compression_type(file_path)
        assert compression_type in self._modes[mode], "Unsupported compression '{}' for archive files.".format(
            compression_type)
        assert self.is_archive(file_path)

        if compression_type is None:
            compression_type = 'tar'

        mode = self._modes[mode][compression_type]
        return self._open[compression_type](file_path, mode=mode) 
開發者ID:CAMI-challenge,項目名稱:CAMISIM,代碼行數:24,代碼來源:archive.py

示例2: test_extract_tarred_chute

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_extract_tarred_chute():
    # Normal case: tar file with a paradrop.yaml file.
    with open("/tmp/paradrop.yaml", "w") as output:
        output.write("name: test")

    tar = tarfile.TarFile(name="/tmp/test_chute.tar", mode="w")
    tar.add("/tmp/paradrop.yaml", arcname="paradrop.yaml")
    tar.close()

    with open("/tmp/test_chute.tar", "r") as source:
        workdir, paradrop_yaml = chute_api.extract_tarred_chute(source)
        assert os.path.isdir(workdir)
        assert paradrop_yaml['name'] == "test"

    # Bad case: empty tar file, no paradrop.yaml.
    tar = tarfile.TarFile(name="/tmp/test_chute.tar", mode="w")
    tar.close()

    with open("/tmp/test_chute.tar", "r") as source:
        assert_raises(Exception, chute_api.extract_tarred_chute, source) 
開發者ID:ParadropLabs,項目名稱:Paradrop,代碼行數:22,代碼來源:test_chute_api.py

示例3: test_init_close_fobj

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        with open(empty, "wb") as fobj:
            fobj.write("")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            support.unlink(empty) 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_tarfile.py

示例4: extractall

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def extractall(file_path, destination, ext):
    """Extracts an archive file.

    This function extracts an archive file to a destination.

    Args:
        file_path (string): The path of a file to be extracted.
        destination (string): A directory path. The archive file
            will be extracted under this directory.
        ext (string): An extension suffix of the archive file.
            This function supports :obj:`'.zip'`, :obj:`'.tar'`,
            :obj:`'.gz'` and :obj:`'.tgz'`.

    """

    if ext == '.zip':
        with zipfile.ZipFile(file_path, 'r') as z:
            z.extractall(destination)
    elif ext == '.tar':
        with tarfile.TarFile(file_path, 'r') as t:
            t.extractall(destination)
    elif ext == '.gz' or ext == '.tgz':
        with tarfile.open(file_path, 'r:gz') as t:
            t.extractall(destination) 
開發者ID:pfnet-research,項目名稱:chainer-compiler,代碼行數:26,代碼來源:download.py

示例5: _tar_add_string_file

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def _tar_add_string_file(self, tarobj, fpath, content):
        """
        Given a tarfile object, add a file to it at ``fpath``, with content
        ``content``.

        Largely based on: http://stackoverflow.com/a/40392022

        :param tarobj: the tarfile to add to
        :type tarobj: tarfile.TarFile
        :param fpath: path to put the file at in the archive
        :type fpath: str
        :param content: file content
        :type content: str
        """
        logger.debug('Adding %d-length string to tarfile at %s',
                     len(content), fpath)
        data = content.encode('utf-8')
        f = BytesIO(data)
        info = tarfile.TarInfo(name=fpath)
        info.size = len(data)
        tarobj.addfile(tarinfo=info, fileobj=f) 
開發者ID:jantman,項目名稱:biweeklybudget,代碼行數:23,代碼來源:docker_build.py

示例6: test_ignore_zeros

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_ignore_zeros(self):
        # Test TarFile's ignore_zeros option.
        if self.mode.endswith(":gz"):
            _open = gzip.GzipFile
        elif self.mode.endswith(":bz2"):
            _open = bz2.BZ2File
        else:
            _open = open

        for char in ('\0', 'a'):
            # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
            # are ignored correctly.
            fobj = _open(tmpname, "wb")
            fobj.write(char * 1024)
            fobj.write(tarfile.TarInfo("foo").tobuf())
            fobj.close()

            tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
            self.assertListEqual(tar.getnames(), ["foo"],
                    "ignore_zeros=True should have skipped the %r-blocks" % char)
            tar.close() 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:23,代碼來源:test_tarfile.py

示例7: test_init_close_fobj

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        open(empty, "wb").write("")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            os.remove(empty) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:19,代碼來源:test_tarfile.py

示例8: reader

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def reader(self, remote_snapshots=None):
        """
        Package up filesystem contents as a tarball.
        """
        result = BytesIO()
        tarball = TarFile(fileobj=result, mode="w")
        for child in self.path.children():
            tarball.add(child.path, arcname=child.basename(), recursive=True)
        tarball.close()

        # You can append anything to the end of a tar stream without corrupting
        # it.  Smuggle some data about the snapshots through here.  This lets
        # tests verify that an incremental stream is really being produced
        # without forcing us to implement actual incremental streams on top of
        # dumb directories.
        if remote_snapshots:
            result.write(
                u"\nincremental stream based on\n{}".format(
                    u"\n".join(snapshot.name for snapshot in remote_snapshots)
                ).encode("ascii")
            )
        result.seek(0, 0)
        yield result 
開發者ID:ClusterHQ,項目名稱:flocker,代碼行數:25,代碼來源:memory.py

示例9: test_ignore_zeros

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_ignore_zeros(self):
        # Test TarFile's ignore_zeros option.
        for char in (b'\0', b'a'):
            # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a')
            # are ignored correctly.
            with self.open(tmpname, "w") as fobj:
                fobj.write(char * 1024)
                fobj.write(tarfile.TarInfo("foo").tobuf())

            tar = tarfile.open(tmpname, mode="r", ignore_zeros=True)
            try:
                self.assertListEqual(tar.getnames(), ["foo"],
                    "ignore_zeros=True should have skipped the %r-blocks" %
                    char)
            finally:
                tar.close() 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:18,代碼來源:test_tarfile.py

示例10: test_init_close_fobj

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def test_init_close_fobj(self):
        # Issue #7341: Close the internal file object in the TarFile
        # constructor in case of an error. For the test we rely on
        # the fact that opening an empty file raises a ReadError.
        empty = os.path.join(TEMPDIR, "empty")
        with open(empty, "wb") as fobj:
            fobj.write(b"")

        try:
            tar = object.__new__(tarfile.TarFile)
            try:
                tar.__init__(empty)
            except tarfile.ReadError:
                self.assertTrue(tar.fileobj.closed)
            else:
                self.fail("ReadError not raised")
        finally:
            support.unlink(empty) 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:20,代碼來源:test_tarfile.py

示例11: load_chunk

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def load_chunk(tarfile, size=None):
  """Load a number of images from a single imagenet .tar file.

  This function also converts the image from grayscale to RGB if necessary.

  Args:
    tarfile (tarfile.TarFile): The archive from which the files get loaded.
    size (Optional[Tuple[int, int]]): Resize the image to this size if provided.

  Returns:
    numpy.ndarray: Contains the image data in format [batch, w, h, c]
  """
  result = []
  filenames = []
  for member in tarfile.getmembers():
    filename = member.path
    content = tarfile.extractfile(member)
    img = Image.open(content)
    rgbimg = Image.new("RGB", img.size)
    rgbimg.paste(img)
    if size != None:
      rgbimg = rgbimg.resize(size, Image.ANTIALIAS)
    result.append(np.array(rgbimg).reshape(1, rgbimg.size[0], rgbimg.size[1], 3))
    filenames.append(filename)
  return np.concatenate(result), filenames 
開發者ID:ray-project,項目名稱:ray-legacy,代碼行數:27,代碼來源:alexnet.py

示例12: _unpack_data

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def _unpack_data(self, tar: TarFile, data_archive: TarFile):
        with io.BytesIO(
            str.encode(
                "\n".join(
                    [
                        member.name.lstrip(".")
                        for member in data_archive
                        if member.name.lstrip(".")
                    ]
                )
                + "\n"
            )
        ) as fileobj:
            info = TarInfo("list")
            info.size = fileobj.getbuffer().nbytes
            self._unpack_info_file(tar, info, fileobj)

        names = tar.getnames()

        for member in (member for member in data_archive if member.name not in names):
            if member.islnk() or member.issym() or member.isdir():
                tar.addfile(member)
            else:
                with data_archive.extractfile(member) as fileobj:
                    tar.addfile(member, fileobj) 
開發者ID:ThomasKluiters,項目名稱:fetchy,代碼行數:27,代碼來源:debian.py

示例13: tar_error

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def tar_error(tmpdir):
    def fixture(filename):
        path = os.path.join(str(tmpdir), filename)

        def reset(path):
            os.mkdir('d')
            with tarfile.TarFile(path, 'w') as archive:
                for file in ('a', 'b', 'c', 'd/e'):
                    with open(file, 'w') as f:
                        f.write('*')

                    archive.add(file)

                    os.remove(file)

            with tarfile.TarFile(path, 'r') as archive:
                archive.extractall()

        os.chdir(str(tmpdir))
        reset(path)

        assert set(os.listdir('.')) == {filename, 'a', 'b', 'c', 'd'}
        assert set(os.listdir('./d')) == {'e'}

    return fixture 
開發者ID:nvbn,項目名稱:thefuck,代碼行數:27,代碼來源:test_dirty_untar.py

示例14: install_pkg

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def install_pkg(pkg_spec, install_path):
    data = get_pkg_metadata(pkg_spec)

    latest_ver = data["info"]["version"]
    packages = data["releases"][latest_ver]
    assert len(packages) == 1
    package_url = packages[0]["url"]
    print("Installing %s %s from %s" % (pkg_spec, latest_ver, package_url))
    f1 = url_open(package_url)
    s = read_lines(f1)
    try:
        str1 = zlib.decompress(s, gzdict_sz)
        with tempfile.TemporaryFile() as temp_file:
            temp_file.write(str1)
            temp_file.seek(0)
            with tarfile.TarFile(fileobj=temp_file) as tar_file:  # Expects a file object
                meta = install_tar(tar_file, install_path)
    finally:
        f1.close()
    return meta 
開發者ID:peterhinch,項目名稱:micropython-samples,代碼行數:22,代碼來源:micropip.py

示例15: _create_test_corpus

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarFile [as 別名]
def _create_test_corpus(self, data):
        """
        Create a small tar in a similar format to the
        Ubuntu corpus file in memory for testing.
        """
        file_path = os.path.join(self.trainer.data_directory, 'ubuntu_dialogs.tgz')
        tar = tarfile.TarFile(file_path, 'w')

        tsv1 = BytesIO(data[0])
        tsv2 = BytesIO(data[1])

        tarinfo = tarfile.TarInfo('dialogs/3/1.tsv')
        tarinfo.size = len(data[0])
        tar.addfile(tarinfo, fileobj=tsv1)

        tarinfo = tarfile.TarInfo('dialogs/3/2.tsv')
        tarinfo.size = len(data[1])
        tar.addfile(tarinfo, fileobj=tsv2)

        tsv1.close()
        tsv2.close()
        tar.close()

        return file_path 
開發者ID:gunthercox,項目名稱:ChatterBot,代碼行數:26,代碼來源:test_ubuntu_corpus_training.py


注:本文中的tarfile.TarFile方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。