in pkg/private/archive.py [0:0]
def add_tar(self,
tar,
rootuid=None,
rootgid=None,
numeric=False,
name_filter=None,
root=None):
"""Merge a tar content into the current tar, stripping timestamp.
Args:
tar: the name of tar to extract and put content into the current tar.
rootuid: user id that we will pretend is root (replaced by uid 0).
rootgid: group id that we will pretend is root (replaced by gid 0).
numeric: set to true to strip out name of owners (and just use the
numeric values).
name_filter: filter out file by names. If not none, this method will be
called for each file to add, given the name and should return true if
the file is to be added to the final tar and false otherwise.
root: place all non-absolute content under given root directory, if not
None.
Raises:
TarFileWriter.Error: if an error happens when uncompressing the tar file.
"""
if root and root[0] not in ['/', '.']:
# Root prefix should start with a '/', adds it if missing
root = '/' + root
intar = tarfile.open(name=tar, mode='r:*')
for tarinfo in intar:
if name_filter is None or name_filter(tarinfo.name):
if not self.preserve_mtime:
tarinfo.mtime = self.default_mtime
if rootuid is not None and tarinfo.uid == rootuid:
tarinfo.uid = 0
tarinfo.uname = 'root'
if rootgid is not None and tarinfo.gid == rootgid:
tarinfo.gid = 0
tarinfo.gname = 'root'
if numeric:
tarinfo.uname = ''
tarinfo.gname = ''
name = tarinfo.name
if (not name.startswith('/') and
not name.startswith(self.root_directory)):
name = self.root_directory + '/' + name
if root is not None:
if name.startswith('.'):
name = '.' + root + name.lstrip('.')
# Add root dir with same permissions if missing. Note that
# add_file deduplicates directories and is safe to call here.
self.add_file('.' + root,
tarfile.DIRTYPE,
uid=tarinfo.uid,
gid=tarinfo.gid,
uname=tarinfo.uname,
gname=tarinfo.gname,
mtime=tarinfo.mtime,
mode=0o755)
# Relocate internal hardlinks as well to avoid breaking them.
link = tarinfo.linkname
if link.startswith('.') and tarinfo.type == tarfile.LNKTYPE:
tarinfo.linkname = '.' + root + link.lstrip('.')
tarinfo.name = name
# Remove path pax header to ensure that the proposed name is going
# to be used. Without this, files with long names will not be
# properly written to its new path.
if 'path' in tarinfo.pax_headers:
del tarinfo.pax_headers['path']
if tarinfo.isfile():
# use extractfile(tarinfo) instead of tarinfo.name to preserve
# seek position in intar
self._addfile(tarinfo, intar.extractfile(tarinfo))
else:
self._addfile(tarinfo)
intar.close()