我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用tarfile.SYMTYPE。
def add_link(self, symlink, destination): """Add a symbolic link pointing to `destination`. Args: symlink: the name of the symbolic link to add. destination: where the symbolic link point to. """ symlink = os.path.normpath(symlink) self.tarfile.add_file(symlink, tarfile.SYMTYPE, link=destination)
def _make_tar(archive_dest_dir, contents, compression=None): mode = 'w' extension = '.tar' if compression == 'gz': mode = mode + ':gz' extension = extension + '.gz' elif compression == 'bz2': mode = mode + ':bz2' extension = extension + '.bz2' # the tarfile API only lets us put in files, so we need # files to put in a_directory = os.path.join(archive_dest_dir, "a_directory") os.mkdir(a_directory) a_file = os.path.join(archive_dest_dir, "a_file") with open(a_file, 'w') as f: f.write("hello") a_symlink = os.path.join(archive_dest_dir, "a_link") if _CONTENTS_SYMLINK in contents.values(): os.symlink("/somewhere", a_symlink) archivefile = os.path.join(archive_dest_dir, "foo" + extension) with tarfile.open(archivefile, mode) as tf: for (key, what) in contents.items(): t = tarfile.TarInfo(key) if what is _CONTENTS_DIR: t.type = tarfile.DIRTYPE elif what is _CONTENTS_FILE: pass elif what is _CONTENTS_SYMLINK: t.type = tarfile.SYMTYPE tf.addfile(t) os.remove(a_file) os.rmdir(a_directory) if os.path.exists(a_symlink): os.remove(a_symlink) return archivefile
def load_archive(self, container, name, fileobj, root="/", uid=None, gid=None): self.logger.info( "Loading archive \"%s\" on container \"%s\"", name, container) def layout_filter(obj): if uid is not None: obj.uid = uid if gid is not None: obj.gid = gid return obj args = ['cp', '-', "{}:{}".format(container, root)] proc = DockerProcess(self, args, stdin=PIPE) tar_in = tarfile.open(fileobj=fileobj, mode='r|*') tar_out = tarfile.open(fileobj=proc.stdin, mode='w|') for tarinfo in tar_in: tarinfo = layout_filter(tarinfo) if tarinfo.name in ['./lib', './usr/lib'] and tarinfo.isdir(): lib64_tarinfo = deepcopy(tarinfo) lib64_tarinfo.name = "{}64".format(lib64_tarinfo.name) tar_out.addfile(lib64_tarinfo) tarinfo.type = tarfile.SYMTYPE tarinfo.linkname = os.path.basename(lib64_tarinfo.name) if tarinfo.isreg(): tar_out.addfile(tarinfo, fileobj=tar_in.extractfile(tarinfo)) else: tar_out.addfile(tarinfo) tar_out.close() proc.stdin.close() if proc.wait() != 0: raise ExternalProcessError( "Error loading archive on container \"{}\"".format(container), proc)
def export_tar(tree, storage, output, compression=None): """ Export a tree in tar format. """ mode = 'w' if compression in ('gz', 'bz2', 'xz'): mode += ':' + compression with tarfile.open(output, mode) as tar: for fullname, item in walk_tree(storage, tree): payload = None info = tarfile.TarInfo() info.name = fullname.decode('utf-8', 'ignore') if item.type == 'blob': payload = storage.get_blob(item.ref).blob info.type = tarfile.REGTYPE info.size = item['size'] printer.verbose('Adding to {out}: <b>{fn}</b> ({size})', out=output, fn=fullname.decode('utf-8', errors='ignore'), size=humanize.naturalsize(item['size'], binary=True)) elif item.type == 'tree': info.type = tarfile.DIRTYPE printer.verbose('Adding to {out}: <b>{fn}</b> (directory)', out=output, fn=fullname.decode('utf-8', errors='ignore')) else: if item['filetype'] == 'link': info.type = tarfile.SYMTYPE info.linkname = item['link'] printer.verbose('Adding to {out}: <b>{fn}</b> (link to {link})', out=output, fn=fullname.decode('utf-8', errors='ignore'), link=item['link'].decode('utf-8', errors='replace')) elif item['filetype'] == 'fifo': info.type = tarfile.FIFOTYPE printer.verbose('Adding to {out}: <b>{fn}</b> (fifo)', out=output, fn=fullname.decode('utf-8', errors='ignore')) else: continue # Ignore unknown file types # Set optional attributes: info.mode = item.get('mode') info.uid = item.get('uid') info.gid = item.get('gid') info.mtime = item.get('mtime') # Add the item into the tar file: tar.addfile(info, payload)