本文整理匯總了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")