本文整理汇总了Python中tarfile.REGTYPE属性的典型用法代码示例。如果您正苦于以下问题:Python tarfile.REGTYPE属性的具体用法?Python tarfile.REGTYPE怎么用?Python tarfile.REGTYPE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类tarfile
的用法示例。
在下文中一共展示了tarfile.REGTYPE属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _add_file
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def _add_file(self, tar, name, contents, mode=DEFAULT_FILE_MODE):
"""
Adds a single file in tarfile instance.
:param tar: tarfile instance
:param name: string representing filename or path
:param contents: string representing file contents
:param mode: string representing file mode, defaults to 644
:returns: None
"""
byte_contents = BytesIO(contents.encode('utf8'))
info = tarfile.TarInfo(name=name)
info.size = len(contents)
# mtime must be 0 or any checksum operation
# will return a different digest even when content is the same
info.mtime = 0
info.type = tarfile.REGTYPE
info.mode = int(mode, 8) # permissions converted to decimal notation
tar.addfile(tarinfo=info, fileobj=byte_contents)
示例2: __init__
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def __init__(self, name="", sparsemap=None):
super(TarSparseInfo, self).__init__(name)
if sparsemap is not None:
self.type = tarfile.REGTYPE
self.sparsemap = sparsemap
self.sparsemap_buf = self.format_sparse_map()
# compact size
self.size = functools.reduce(lambda x, y: x+y[1], sparsemap,
0) + len(self.sparsemap_buf)
self.pax_headers['GNU.sparse.major'] = '1'
self.pax_headers['GNU.sparse.minor'] = '0'
self.pax_headers['GNU.sparse.name'] = name
self.pax_headers['GNU.sparse.realsize'] = str(self.realsize)
self.name = '{}/GNUSparseFile.{}/{}'.format(
os.path.dirname(name), os.getpid(), os.path.basename(name))
else:
self.sparsemap = []
self.sparsemap_buf = b''
示例3: _make_test_archive
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def _make_test_archive(filename_1, dirname_1, filename_2):
# the file contents to write
fobj = io.BytesIO(b"content")
# create a tar file with a file, a directory, and a file within that
# directory. Assign various .uid/.gid values to them
items = [(filename_1, 99, 98, tarfile.REGTYPE, fobj),
(dirname_1, 77, 76, tarfile.DIRTYPE, None),
(filename_2, 88, 87, tarfile.REGTYPE, fobj),
]
with tarfile.open(tmpname, 'w') as tarfl:
for name, uid, gid, typ, contents in items:
t = tarfile.TarInfo(name)
t.uid = uid
t.gid = gid
t.uname = 'root'
t.gname = 'root'
t.type = typ
tarfl.addfile(t, contents)
# return the full pathname to the tar file
return tmpname
示例4: WriteFromFD
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def WriteFromFD(self, src_fd, arcname=None, st=None):
"""Write an archive member from a file like object.
Args:
src_fd: A file like object, must support seek(), tell(), read().
arcname: The name in the archive this should take.
st: A stat object to be used for setting headers.
Raises:
ValueError: If st is omitted.
"""
if st is None:
raise ValueError("Stat object can't be None.")
info = self.tar_fd.tarinfo()
info.tarfile = self.tar_fd
info.type = tarfile.REGTYPE
info.name = SmartStr(arcname)
info.size = st.st_size
info.mode = st.st_mode
info.mtime = st.st_mtime or time.time()
self.tar_fd.addfile(info, src_fd)
示例5: create_user_dirs
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def create_user_dirs(self, tar_handle):
"""
Create root file system tree in tar archive.
"""
tar_members = [
['dirs', tarfile.DIRTYPE],
['files', tarfile.REGTYPE],
]
for user in self.rootfs_tree:
for members, tar_type in tar_members:
self.create_tar_members(
tar_handle,
self.rootfs_tree[user][members],
tar_type,
uid=self.rootfs_tree[user]['uid'],
gid=self.rootfs_tree[user]['gid']
)
示例6: test_add_twice
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def test_add_twice(self):
# The same name will be added as a REGTYPE every
# time regardless of st_nlink.
tarinfo = self.tar.gettarinfo(self.foo)
self.assertTrue(tarinfo.type == tarfile.REGTYPE,
"add file as regular failed")
示例7: test_dereference_hardlink
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def test_dereference_hardlink(self):
self.tar.dereference = True
tarinfo = self.tar.gettarinfo(self.bar)
self.assertTrue(tarinfo.type == tarfile.REGTYPE,
"dereferencing hardlink failed")
示例8: test_add_twice
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def test_add_twice(self):
# The same name will be added as a REGTYPE every
# time regardless of st_nlink.
tarinfo = self.tar.gettarinfo(self.foo)
self.assertEqual(tarinfo.type, tarfile.REGTYPE,
"add file as regular failed")
示例9: test_dereference_hardlink
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def test_dereference_hardlink(self):
self.tar.dereference = True
tarinfo = self.tar.gettarinfo(self.bar)
self.assertEqual(tarinfo.type, tarfile.REGTYPE,
"dereferencing hardlink failed")
示例10: valid_tar_gz
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def valid_tar_gz(contents):
assert isinstance(contents, bytes)
with closing(BytesIO()) as layer_data:
with closing(tarfile.open(fileobj=layer_data, mode="w|gz")) as tar_file:
tar_file_info = tarfile.TarInfo(name="somefile")
tar_file_info.type = tarfile.REGTYPE
tar_file_info.size = len(contents)
tar_file_info.mtime = 1
tar_file.addfile(tar_file_info, BytesIO(contents))
layer_bytes = layer_data.getvalue()
return layer_bytes
示例11: layer_bytes_for_contents
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def layer_bytes_for_contents(contents, mode="|gz", other_files=None, empty=False):
if empty:
return EMPTY_LAYER_BYTES
layer_data = BytesIO()
tar_file = tarfile.open(fileobj=layer_data, mode="w" + mode)
def add_file(name, contents):
tar_file_info = tarfile.TarInfo(name=name)
tar_file_info.type = tarfile.REGTYPE
tar_file_info.size = len(contents)
tar_file_info.mtime = 1
tar_file.addfile(tar_file_info, BytesIO(contents))
add_file("contents", contents)
if other_files is not None:
for file_name, file_contents in other_files.items():
add_file(file_name, file_contents)
tar_file.close()
layer_bytes = layer_data.getvalue()
layer_data.close()
return layer_bytes
示例12: tar_file_header
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def tar_file_header(self, name, file_size, mtime=None):
"""
Returns tar file header data for a file with the given name and size.
"""
info = tarfile.TarInfo(name=name)
info.type = tarfile.REGTYPE
info.size = file_size
if mtime is not None:
info.mtime = mtime
return info.tobuf()
示例13: _WriteHeader
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def _WriteHeader(self, name, size, mode):
info = tarfile.TarInfo(name)
info.size = size
info.mode = mode
info.type = tarfile.REGTYPE
info.uid = info.gid = 0
info.uname = info.gname = 'root'
self._Write(info.tobuf())
示例14: _convertFileType
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def _convertFileType(type):
return {
tarfile.REGTYPE: S_IFREG,
tarfile.LNKTYPE: S_IFLNK,
tarfile.SYMTYPE: S_IFLNK,
tarfile.CHRTYPE: S_IFCHR,
tarfile.BLKTYPE: S_IFBLK,
tarfile.DIRTYPE: S_IFDIR,
tarfile.FIFOTYPE: S_IFIFO,
}.get(type, S_IFREG)
示例15: test_readlines
# 需要导入模块: import tarfile [as 别名]
# 或者: from tarfile import REGTYPE [as 别名]
def test_readlines(self):
"""Test readlines() method of _FileObject.
"""
if self.sep != "|":
filename = "0-REGTYPE-TEXT"
self.tar.extract(filename, dirname())
f = open(os.path.join(dirname(), filename), "rU")
lines1 = f.readlines()
f.close()
lines2 = self.tar.extractfile(filename).readlines()
self.assert_(lines1 == lines2,
"_FileObject.readline() does not work correctly")