本文整理汇总了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)
示例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)
示例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()
示例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
示例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()
示例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()
示例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()
示例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()
示例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()
示例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
示例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)
示例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()
示例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")
示例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)
示例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)