当前位置: 首页>>代码示例>>Python>>正文


Python TarFile.extractfile方法代码示例

本文整理汇总了Python中tarfile.TarFile.extractfile方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.extractfile方法的具体用法?Python TarFile.extractfile怎么用?Python TarFile.extractfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tarfile.TarFile的用法示例。


在下文中一共展示了TarFile.extractfile方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: read_file_from_image

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
def read_file_from_image(img: tarfile.TarFile,
                         file_path: str,
                         autoclose=False) -> bytes:
    if autoclose:
        with closing(img.extractfile(file_path)) as fd:
            return fd.read()
    else:
        return img.extractfile(file_path).read()
开发者ID:yege0201,项目名称:dockerscan,代码行数:10,代码来源:docker_api.py

示例2: load_from_file

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
    def load_from_file(self, f):
        tar = TarFile(f, "r")

        # load info file
        f = tar.extractfile("info.py")
        self.agedesc, self.generation = eval(f.read(-1), {"__builtins__": None})
        f.close()

        # load agents
        for info in tar.getmembers():
            if (splitext(info.name)[1]==".agt" and info.isfile()):
                f = tar.extractfile(info)
                self.add(Agent(self.agedesc, file = f))
                f.close()

        tar.close()
开发者ID:grcff,项目名称:pyAGE,代码行数:18,代码来源:population.py

示例3: extract_docker_layer

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
def extract_docker_layer(img: tarfile.TarFile,
                         layer_id: str,
                         extract_path: str):
    with tarfile.open(fileobj=img.extractfile('%s/layer.tar' % layer_id),
                      errorlevel=0,
                      dereference=True) as layer:

        layer.extractall(path=extract_path)

        log.debug('processing whiteouts')
        for member in layer.getmembers():
            path = member.path
            if path.startswith('.wh.') or '/.wh.' in path:
                if path.startswith('.wh.'):
                    newpath = path[4:]
                else:
                    newpath = path.replace('/.wh.', '/')

                try:
                    log.debug('removing path %s', newpath)
                    os.unlink(path)
                    os.unlink(newpath)
                except OSError as err:
                    if err.errno != errno.ENOENT:
                        raise
开发者ID:yege0201,项目名称:dockerscan,代码行数:27,代码来源:docker_api.py

示例4: parse_backup_label

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
 def parse_backup_label(self, basebackup_path):
     tar = TarFile(basebackup_path)
     content = tar.extractfile("backup_label").read()  # pylint: disable=no-member
     for line in content.split(b"\n"):
         if line.startswith(b"START WAL LOCATION"):
             start_wal_segment = line.split(b" ")[5].strip(b")").decode("utf8")
     self.log.debug("Found: %r as starting wal segment", start_wal_segment)
     return start_wal_segment
开发者ID:Ormod,项目名称:pghoard,代码行数:10,代码来源:basebackup.py

示例5: TarFileWrapper

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
class TarFileWrapper(ArchiveFileWrapper):
    def __init__(self, fh, *args, **kwargs):
        self.archive = TarFile(fileobj=fh)
        super(TarFileWrapper, self).__init__(*args, **kwargs)

    def extract_file(self, *args, **kwarg):
        return self.archive.extractfile(*args, **kwarg)

    def names(self):
        return self.archive.getnames()
开发者ID:fcurella,项目名称:cookiejar,代码行数:12,代码来源:extractor.py

示例6: get_root_json_from_image

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
def get_root_json_from_image(img: tarfile.TarFile) -> Tuple[str, dict]:
    """
    Every docker image has a root .json file with the metadata information.
    this function locate this file, load it and return the value of it and
    their name

    >>> get_docker_image_layers(img)
    ('db079554b4d2f7c65c4df3adae88cb72d051c8c3b8613eb44e86f60c945b1ca7', dict(...))
    """
    for f in img.getmembers():
        if f.name.endswith("json") and "/" not in f.name:
            c = img.extractfile(f.name).read()
            if hasattr(c, "decode"):
                c = c.decode()

            return f.name.split(".")[0], json.loads(c)

    return None, None
开发者ID:yege0201,项目名称:dockerscan,代码行数:20,代码来源:docker_api.py

示例7: test_can_put_extracted_file_from_tar

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
    def test_can_put_extracted_file_from_tar(self):
        tempdir = self.make_tempdir()
        tarname = os.path.join(tempdir, "mytar.tar")
        filename = os.path.join(tempdir, "foo")

        # Set up a file to add the tarfile.
        with open(filename, "w") as f:
            f.write("bar")

        # Setup the tar file by adding the file to it.
        # Note there is no context handler for TarFile in python 2.6
        try:
            tar = TarFile(tarname, "w")
            tar.add(filename, "foo")
        finally:
            tar.close()

        # See if an extracted file can be uploaded to s3.
        try:
            tar = TarFile(tarname, "r")
            with closing(tar.extractfile("foo")) as f:
                self.assert_can_put_object(body=f)
        finally:
            tar.close()
开发者ID:neilramsay,项目名称:botocore,代码行数:26,代码来源:test_s3.py

示例8: TarFile

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
FILENAME = "projectcounts-2008.tar"
MAX_REQUESTS = 33056088 * 0.4

if __name__ == "__main__":
    tar = TarFile(FILENAME)
    inidate = datetime(year=2008, month=1, day=1)
    maxrequests = 0
    for filename in tar.getnames():
        pre, date, time = filename.split("-")
        year = int(date[0:4])
        month = int(date[4:6])
        day = int(date[6:8])
        hour = int(time[0:2])
        minute = int(time[2:4])
        second = int(time[4:6])
        date = datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
        td = date - inidate
        seconds = td.days * 24 * 60 * 60 + td.seconds

        f = tar.extractfile(filename)
        for line in f.readlines():
            if line.startswith("en -"):
                line = line.replace("\n", "").replace("\r", "")
                lineSplit = line.split(" ")
                requests = int(lineSplit[2])
                if requests > MAX_REQUESTS:
                    requests = MAX_REQUESTS
                print "%d %.2f" % (seconds, float(requests) / MAX_REQUESTS)
                if requests > maxrequests:
                    maxrequests = requests
开发者ID:goiri,项目名称:greendcsimulator,代码行数:32,代码来源:parser.py

示例9: create_new_docker_image

# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import extractfile [as 别名]
def create_new_docker_image(manifest: dict,
                            image_output_path: str,
                            img: tarfile.TarFile,
                            old_layer_digest: str,
                            new_layer_path: str,
                            new_layer_digest: str,
                            json_metadata_last_layer: dict = None,
                            json_metadata_root: dict = None):
    with tarfile.open(image_output_path, "w") as s:

        for f in img.getmembers():
            log.debug("    _> Processing file: {}".format(f.name))

            # Add new manifest
            if f.name == "manifest.json":
                # Dump Manifest to JSON
                new_manifest_json = json.dumps(manifest).encode()
                replace_or_append_file_to_layer("manifest.json",
                                                new_manifest_json,
                                                s)

            #
            # NEW LAYER INFO
            #
            elif old_layer_digest in f.name:
                # Skip for old layer.tar file
                if f.name == "{}/layer.tar".format(old_layer_digest) or \
                        "/" not in f.name:

                    log.debug(
                        "    _> Replacing layer {} by {}".format(
                            f.name,
                            new_layer_digest
                        ))

                    replace_or_append_file_to_layer("{}/layer.tar".format(
                        new_layer_digest),
                        new_layer_path,
                        s)
                else:
                    #
                    # Extra files: "json" and "VERSION"
                    #
                    c = read_file_from_image(img, f.name)

                    if "json" in f.name:
                        # Modify the JSON content to add the new
                        # hash
                        if json_metadata_last_layer:
                            c = json.dumps(json_metadata_last_layer).encode()
                        else:
                            c = c.decode().replace(old_layer_digest,
                                                   new_layer_digest).encode()

                    replace_or_append_file_to_layer("{}/{}".format(
                        new_layer_digest,
                        os.path.basename(f.name)), c, s)

            #
            # Root .json file with the global info
            #
            elif "repositories" in f.name:
                c = read_file_from_image(img, f, autoclose=False)
                j = json.loads(c.decode())

                image = list(j.keys())[0]
                tag = list(j[image].keys())[0]

                # Update the latest layer
                j[image][tag] = new_layer_digest

                new_c = json.dumps(j).encode()

                replace_or_append_file_to_layer(f.name, new_c, s)

            elif ".json" in f.name and "/" not in f.name:
                c = read_file_from_image(img, f, autoclose=False)

                # Modify the JSON content to add the new
                # hash
                if json_metadata_root:
                    j = json_metadata_root
                else:
                    j = json.loads(c.decode())

                j["rootfs"]["diff_ids"][-1] = \
                    "sha256:{}".format(new_layer_digest)

                new_c = json.dumps(j).encode()

                replace_or_append_file_to_layer(f.name, new_c, s)

            # Add the rest of files / dirs
            else:
                s.addfile(f, img.extractfile(f))
开发者ID:yege0201,项目名称:dockerscan,代码行数:97,代码来源:docker_api.py


注:本文中的tarfile.TarFile.extractfile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。