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


Python tarfile.TarInfo方法代碼示例

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


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

示例1: test

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test(self):
        with tarfile.open("test.tar", "w") as f:
            import io
            bio = io.BytesIO()
            bio.write(b"secret text\n")
            tarinfo = tarfile.TarInfo("hello_world")
            tarinfo.size = bio.tell()
            import time
            tarinfo.mtime = time.time()
            bio.seek(0)
            f.addfile(tarinfo, bio)

        if not tools.cross_building(self.settings):
            if os.path.exists("hello_world"):
                raise ConanException("file extracted by tar archive should not exist yet")
            bin_path = os.path.join("bin", "test_package")
            self.run("{} {}".format(bin_path, "test.tar"), run_environment=True)
            if not os.path.exists("hello_world"):
                raise ConanException("file not extracted")
            extracted_text = tools.load("hello_world")
            if extracted_text != "secret text\n":
                raise ConanException("File not loaded correctly. Got \"{}\"".format(repr(extracted_text)))

            self.run("libtar -t test.tar", run_environment=True) 
開發者ID:conan-io,項目名稱:conan-center-index,代碼行數:26,代碼來源:conanfile.py

示例2: add_data

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def add_data(self, name, data):
		"""
		Add arbitrary data directly to the archive under the specified name.
		This allows data to be directly inserted into the archive without first
		writing it to a file or file like object.

		:param str name: The name of the destination file in the archive.
		:param data: The data to place into the archive.
		:type data: bytes, str
		"""
		if its.py_v2 and isinstance(data, unicode):
			data = data.encode(self.encoding)
		elif its.py_v3 and isinstance(data, str):
			data = data.encode(self.encoding)
		pseudo_file = io.BytesIO()
		pseudo_file.write(data)

		tarinfo = tarfile.TarInfo(name=name)
		tarinfo.mtime = self.mtime
		tarinfo.size = pseudo_file.tell()
		pseudo_file.seek(os.SEEK_SET)
		self._tar_h.addfile(tarinfo=tarinfo, fileobj=pseudo_file) 
開發者ID:rsmusllp,項目名稱:king-phisher,代碼行數:24,代碼來源:archive.py

示例3: make_trivial_sdist

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def make_trivial_sdist(dist_path, setup_py):
    """Create a simple sdist tarball at dist_path, containing just a
    setup.py, the contents of which are provided by the setup_py string.
    """

    setup_py_file = tarfile.TarInfo(name='setup.py')
    try:
        # Python 3 (StringIO gets converted to io module)
        MemFile = BytesIO
    except AttributeError:
        MemFile = StringIO
    setup_py_bytes = MemFile(setup_py.encode('utf-8'))
    setup_py_file.size = len(setup_py_bytes.getvalue())
    dist = tarfile.open(dist_path, 'w:gz')
    try:
        dist.addfile(setup_py_file, fileobj=setup_py_bytes)
    finally:
        dist.close() 
開發者ID:MayOneUS,項目名稱:pledgeservice,代碼行數:20,代碼來源:test_easy_install.py

示例4: clean_tarinfo

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def clean_tarinfo(cls, tar_info):
        """
        Clean metadata from a TarInfo object to make it more reproducible.

            - Set uid & gid to 0
            - Set uname and gname to ""
            - Normalise permissions to 644 or 755
            - Set mtime if not None
        """
        ti = copy(tar_info)
        ti.uid = 0
        ti.gid = 0
        ti.uname = ""
        ti.gname = ""
        ti.mode = normalize_file_permissions(ti.mode)

        return ti 
開發者ID:python-poetry,項目名稱:poetry,代碼行數:19,代碼來源:sdist.py

示例5: test_premature_end_of_archive

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_premature_end_of_archive(self):
        for size in (512, 600, 1024, 1200):
            with tarfile.open(tmpname, "w:") as tar:
                t = tarfile.TarInfo("foo")
                t.size = 1024
                tar.addfile(t, StringIO.StringIO("a" * 1024))

            with open(tmpname, "r+b") as fobj:
                fobj.truncate(size)

            with tarfile.open(tmpname) as tar:
                with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
                    for t in tar:
                        pass

            with tarfile.open(tmpname) as tar:
                t = tar.next()

                with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
                    tar.extract(t, TEMPDIR)

                with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"):
                    tar.extractfile(t).read() 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:25,代碼來源:test_tarfile.py

示例6: test_100_char_name

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_100_char_name(self):
        # The name field in a tar header stores strings of at most 100 chars.
        # If a string is shorter than 100 chars it has to be padded with '\0',
        # which implies that a string of exactly 100 chars is stored without
        # a trailing '\0'.
        name = "0123456789" * 10
        tar = tarfile.open(tmpname, self.mode)
        try:
            t = tarfile.TarInfo(name)
            tar.addfile(t)
        finally:
            tar.close()

        tar = tarfile.open(tmpname)
        try:
            self.assertTrue(tar.getnames()[0] == name,
                    "failed to store 100 char filename")
        finally:
            tar.close() 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:21,代碼來源:test_tarfile.py

示例7: _test

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def _test(self, name, link=None):
        # See GNUWriteTest.
        tarinfo = tarfile.TarInfo(name)
        if link:
            tarinfo.linkname = link
            tarinfo.type = tarfile.LNKTYPE

        tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT)
        try:
            tar.addfile(tarinfo)
        finally:
            tar.close()

        tar = tarfile.open(tmpname)
        try:
            if link:
                l = tar.getmembers()[0].linkname
                self.assertTrue(link == l, "PAX longlink creation failed")
            else:
                n = tar.getmembers()[0].name
                self.assertTrue(name == n, "PAX longname creation failed")
        finally:
            tar.close() 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:25,代碼來源:test_tarfile.py

示例8: test_pax_extended_header

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_pax_extended_header(self):
        # The fields from the pax header have priority over the
        # TarInfo.
        pax_headers = {u"path": u"foo", u"uid": u"123"}

        tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
        try:
            t = tarfile.TarInfo()
            t.name = u"\xe4\xf6\xfc"     # non-ASCII
            t.uid = 8**8        # too large
            t.pax_headers = pax_headers
            tar.addfile(t)
        finally:
            tar.close()

        tar = tarfile.open(tmpname, encoding="iso8859-1")
        try:
            t = tar.getmembers()[0]
            self.assertEqual(t.pax_headers, pax_headers)
            self.assertEqual(t.name, "foo")
            self.assertEqual(t.uid, 123)
        finally:
            tar.close() 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:25,代碼來源:test_tarfile.py

示例9: test_unicode_filename_error

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_unicode_filename_error(self):
        tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
        try:
            tarinfo = tarfile.TarInfo()

            tarinfo.name = "\xe4\xf6\xfc"
            if self.format == tarfile.PAX_FORMAT:
                self.assertRaises(UnicodeError, tar.addfile, tarinfo)
            else:
                tar.addfile(tarinfo)

            tarinfo.name = u"\xe4\xf6\xfc"
            self.assertRaises(UnicodeError, tar.addfile, tarinfo)

            tarinfo.name = "foo"
            tarinfo.uname = u"\xe4\xf6\xfc"
            self.assertRaises(UnicodeError, tar.addfile, tarinfo)
        finally:
            tar.close() 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:21,代碼來源:test_tarfile.py

示例10: _test_partial_input

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def _test_partial_input(self, mode):
        class MyStringIO(StringIO.StringIO):
            hit_eof = False
            def read(self, n):
                if self.hit_eof:
                    raise AssertionError("infinite loop detected in tarfile.open()")
                self.hit_eof = self.pos == self.len
                return StringIO.StringIO.read(self, n)
            def seek(self, *args):
                self.hit_eof = False
                return StringIO.StringIO.seek(self, *args)

        data = bz2.compress(tarfile.TarInfo("foo").tobuf())
        for x in range(len(data) + 1):
            try:
                tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode)
            except tarfile.ReadError:
                pass # we have no interest in ReadErrors 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:20,代碼來源:test_tarfile.py

示例11: _tar_add_string_file

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [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

示例12: test_ignore_zeros

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [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

示例13: _test

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def _test(self, name, link=None):
        tarinfo = tarfile.TarInfo(name)
        if link:
            tarinfo.linkname = link
            tarinfo.type = tarfile.LNKTYPE

        tar = tarfile.open(tmpname, "w")
        tar.format = tarfile.GNU_FORMAT
        tar.addfile(tarinfo)

        v1 = self._calc_size(name, link)
        v2 = tar.offset
        self.assertTrue(v1 == v2, "GNU longname/longlink creation failed")

        tar.close()

        tar = tarfile.open(tmpname)
        member = tar.next()
        self.assertIsNotNone(member,
                "unable to read longname member")
        self.assertEqual(tarinfo.name, member.name,
                "unable to read longname member")
        self.assertEqual(tarinfo.linkname, member.linkname,
                "unable to read longname member") 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:26,代碼來源:test_tarfile.py

示例14: test_pax_extended_header

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_pax_extended_header(self):
        # The fields from the pax header have priority over the
        # TarInfo.
        pax_headers = {u"path": u"foo", u"uid": u"123"}

        tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1")
        t = tarfile.TarInfo()
        t.name = u"���"     # non-ASCII
        t.uid = 8**8        # too large
        t.pax_headers = pax_headers
        tar.addfile(t)
        tar.close()

        tar = tarfile.open(tmpname, encoding="iso8859-1")
        t = tar.getmembers()[0]
        self.assertEqual(t.pax_headers, pax_headers)
        self.assertEqual(t.name, "foo")
        self.assertEqual(t.uid, 123) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:20,代碼來源:test_tarfile.py

示例15: test_unicode_filename_error

# 需要導入模塊: import tarfile [as 別名]
# 或者: from tarfile import TarInfo [as 別名]
def test_unicode_filename_error(self):
        tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict")
        tarinfo = tarfile.TarInfo()

        tarinfo.name = "���"
        if self.format == tarfile.PAX_FORMAT:
            self.assertRaises(UnicodeError, tar.addfile, tarinfo)
        else:
            tar.addfile(tarinfo)

        tarinfo.name = u"���"
        self.assertRaises(UnicodeError, tar.addfile, tarinfo)

        tarinfo.name = "foo"
        tarinfo.uname = u"���"
        self.assertRaises(UnicodeError, tar.addfile, tarinfo) 
開發者ID:dxwu,項目名稱:BinderFilter,代碼行數:18,代碼來源:test_tarfile.py


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