本文整理汇总了Python中tarfile.TarFile.getmembers方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.getmembers方法的具体用法?Python TarFile.getmembers怎么用?Python TarFile.getmembers使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tarfile.TarFile
的用法示例。
在下文中一共展示了TarFile.getmembers方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: download
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import getmembers [as 别名]
def download(self, src, dest, extract_here=False):
client = connect()
with SpooledTemporaryFile() as file:
file.write(client.copy(self.container_id, src).read())
file.seek(0)
tfile = TarFile(fileobj=file)
if extract_here:
base = len(os.path.basename(src)) + 1
for member in tfile.getmembers():
member.name = member.name[base:]
tfile.extractall(path=dest)
示例2: load_from_file
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import getmembers [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()
示例3: get_root_json_from_image
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import getmembers [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
示例4: run
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import getmembers [as 别名]
def run(self):
"""
Interesting magic to get a source dist and running trial on it.
NOTE: there is magic going on here! If you know a better way feel
free to update it.
"""
# Clean out dist/
if os.path.exists("dist"):
for root, dirs, files in os.walk("dist", topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
# Import setup making it as if we ran setup.py with the sdist arg
sys.argv.append("sdist")
import setup # @Reimport @UnresolvedImport @UnusedImport
try:
# attempt to extract the sdist data
from gzip import GzipFile
from tarfile import TarFile
# We open up the gzip as well as using the first item as the sdist
gz = GzipFile(os.path.join("dist", os.listdir("dist")[0]))
tf = TarFile(fileobj=gz)
# Make the output dir and generate the extract path
os.mkdir(os.path.join("dist", "sdist_test"))
ex_path = os.path.join("dist", "sdist_test", tf.getmembers()[0].name, "buildbot", "test")
# Extract the data and run tests
print "Extracting to %s" % ex_path
tf.extractall(os.path.join("dist", "sdist_test"))
print "Executing tests ..."
self._run(os.path.normpath(os.path.abspath(ex_path)))
except IndexError, ie:
# We get called twice and the IndexError is OK
pass
示例5: create_new_docker_image
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import getmembers [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))