Python tarfile 模块,REGTYPE 实例源码

我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用tarfile.REGTYPE

项目:virt-bootstrap    作者:virt-manager    | 项目源码 | 文件源码
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']
                )
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
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")
项目:zippy    作者:securesystemslab    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertTrue(tarinfo.type == tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:oil    作者:oilshell    | 项目源码 | 文件源码
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")
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertTrue(tarinfo.type == tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
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")
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertTrue(tarinfo.type == tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
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")
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertEqual(tarinfo.type, tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
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")
项目:pefile.pypy    作者:cloudtracer    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertTrue(tarinfo.type == tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
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")
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertEqual(tarinfo.type, tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
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")
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertTrue(tarinfo.type == tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
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")
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_dereference_hardlink(self):
        self.tar.dereference = True
        tarinfo = self.tar.gettarinfo(self.bar)
        self.assertEqual(tarinfo.type, tarfile.REGTYPE,
                "dereferencing hardlink failed")
项目:bay    作者:eventbrite    | 项目源码 | 文件源码
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
项目:Marty    作者:NaPs    | 项目源码 | 文件源码
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)