我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用tarfile.REGTYPE。
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'] )
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")
def test_dereference_hardlink(self): self.tar.dereference = True tarinfo = self.tar.gettarinfo(self.bar) self.assertTrue(tarinfo.type == tarfile.REGTYPE, "dereferencing hardlink failed")
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")
def test_dereference_hardlink(self): self.tar.dereference = True tarinfo = self.tar.gettarinfo(self.bar) self.assertEqual(tarinfo.type, tarfile.REGTYPE, "dereferencing hardlink failed")
def make_build_context(self): """ Makes a Docker build context from a local directory. Normalises all file ownership and times so that the docker hashes align better. """ # Start temporary tar file fileobj = tempfile.NamedTemporaryFile() tfile = tarfile.open(mode='w:gz', fileobj=fileobj) # Get list of files/dirs to add to the tar paths = exclude_paths(self.container.path, []) # For each file, add it to the tar with normalisation for path in paths: disk_location = os.path.join(self.container.path, path) # Directory addition if os.path.isdir(disk_location): info = tarfile.TarInfo(name=path) info.mtime = 0 info.mode = 0o775 info.type = tarfile.DIRTYPE info.uid = 0 info.gid = 0 info.uname = "root" info.gname = "root" tfile.addfile(info) # Normal file addition elif os.path.isfile(disk_location): stat = os.stat(disk_location) info = tarfile.TarInfo(name=path) info.mtime = 0 info.size = stat.st_size info.mode = 0o755 info.type = tarfile.REGTYPE info.uid = 0 info.gid = 0 info.uname = "root" info.gname = "root" # Rewrite docker FROM lines with a : in them and raise a warning # TODO: Deprecate this! if path.lstrip("/") == self.container.dockerfile_name: # Read in dockerfile line by line, replacing the FROM line dockerfile = io.BytesIO() with open(disk_location, "r") as fh: for line in fh: if line.upper().startswith("FROM ") and self.container.build_parent_in_prefix: line = line.replace(":", "-") dockerfile.write(line.encode("utf8")) dockerfile.seek(0) tfile.addfile(info, dockerfile) else: with open(disk_location, "rb") as fh: tfile.addfile(info, fh) # Error for anything else else: raise ValueError( "Cannot add non-file/dir %s to docker build context" % path ) # Return that tarfile tfile.close() fileobj.seek(0) return fileobj
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)