本文整理汇总了Python中tarfile.TarFile.addfile方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.addfile方法的具体用法?Python TarFile.addfile怎么用?Python TarFile.addfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tarfile.TarFile
的用法示例。
在下文中一共展示了TarFile.addfile方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: move_certs
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def move_certs(self, paths):
self.log.info("Staging internal ssl certs for %s", self._log_name)
yield self.pull_image(self.move_certs_image)
# create the volume
volume_name = self.format_volume_name(self.certs_volume_name, self)
# create volume passes even if it already exists
self.log.info("Creating ssl volume %s for %s", volume_name, self._log_name)
yield self.docker('create_volume', volume_name)
# create a tar archive of the internal cert files
# docker.put_archive takes a tarfile and a running container
# and unpacks the archive into the container
nb_paths = {}
tar_buf = BytesIO()
archive = TarFile(fileobj=tar_buf, mode='w')
for key, hub_path in paths.items():
fname = os.path.basename(hub_path)
nb_paths[key] = '/certs/' + fname
with open(hub_path, 'rb') as f:
content = f.read()
tarinfo = TarInfo(name=fname)
tarinfo.size = len(content)
tarinfo.mtime = os.stat(hub_path).st_mtime
tarinfo.mode = 0o644
archive.addfile(tarinfo, BytesIO(content))
archive.close()
tar_buf.seek(0)
# run a container to stage the certs,
# mounting the volume at /certs/
host_config = self.client.create_host_config(
binds={
volume_name: {"bind": "/certs", "mode": "rw"},
},
)
container = yield self.docker('create_container',
self.move_certs_image,
volumes=["/certs"],
host_config=host_config,
)
container_id = container['Id']
self.log.debug(
"Container %s is creating ssl certs for %s",
container_id[:12], self._log_name,
)
# start the container
yield self.docker('start', container_id)
# stage the archive to the container
try:
yield self.docker(
'put_archive',
container=container_id,
path='/certs',
data=tar_buf,
)
finally:
yield self.docker('remove_container', container_id)
return nb_paths
示例2: generate_tar
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def generate_tar(entries):
tar_buf = BytesIO()
tar_file = TarFile(mode="w", fileobj=tar_buf)
for path, contents in entries.items():
tar_info = TarInfo(name=path)
tar_info.size = len(contents)
tar_file.addfile(tar_info, fileobj=BytesIO(contents))
return BytesIO(tar_buf.getvalue())
示例3: replace_or_append_file_to_layer
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def replace_or_append_file_to_layer(file_to_replace: str,
content_or_path: bytes,
img: tarfile.TarFile):
# Is content or path?
if not os.path.exists(content_or_path):
# Is a content
t = tarfile.TarInfo(file_to_replace)
t.size = len(content_or_path)
img.addfile(t, io.BytesIO(content_or_path))
else:
# Is a path
img.add(content_or_path, file_to_replace)
示例4: run
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def run(self, args, argv):
# Create a temporary tarball with our whole build context and
# dockerfile for the update
tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
tmp_tar = TarFile(fileobj=tmp, mode='w')
# Add the executable to the tarball, using the current
# configured binfmt_misc path. If we don't get a path then we
# only need the support libraries copied
ff, enabled = _check_binfmt_misc(args.executable)
if not enabled:
print("binfmt_misc not enabled, update disabled")
return 1
if ff:
tmp_tar.add(args.executable, arcname=ff)
# Add any associated libraries
libs = _get_so_libs(args.executable)
if libs:
for l in libs:
tmp_tar.add(os.path.realpath(l), arcname=l)
# Create a Docker buildfile
df = StringIO()
df.write("FROM %s\n" % args.tag)
df.write("ADD . /\n")
df.seek(0)
df_tar = TarInfo(name="Dockerfile")
df_tar.size = len(df.buf)
tmp_tar.addfile(df_tar, fileobj=df)
tmp_tar.close()
# reset the file pointers
tmp.flush()
tmp.seek(0)
# Run the build with our tarball context
dkr = Docker()
dkr.update_image(args.tag, tmp, quiet=args.quiet)
return 0
示例5: save_to_file
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def save_to_file(self, f):
tar = TarFile(f, "w")
# save info file
f = StringIO(repr((self.agedesc, self.generation)))
info = tar.gettarinfo(None, "info.py", f)
tar.addfile(info, f)
f.close()
# save agents
for i in range(len(self.agents)):
f = StringIO()
self.agents[i].save_to_file(f)
info = tar.gettarinfo(None, str(i)+".agt", f)
tar.addfile(info, f)
f.close()
tar.close()
示例6: run
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def run(self, args, argv):
# Create a temporary tarball with our whole build context and
# dockerfile for the update
tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
tmp_tar = TarFile(fileobj=tmp, mode='w')
# Add the executable to the tarball
bn = os.path.basename(args.executable)
ff = "/usr/bin/%s" % bn
tmp_tar.add(args.executable, arcname=ff)
# Add any associated libraries
libs = _get_so_libs(args.executable)
if libs:
for l in libs:
tmp_tar.add(os.path.realpath(l), arcname=l)
# Create a Docker buildfile
df = StringIO()
df.write("FROM %s\n" % args.tag)
df.write("ADD . /\n")
df.seek(0)
df_tar = TarInfo(name="Dockerfile")
df_tar.size = len(df.buf)
tmp_tar.addfile(df_tar, fileobj=df)
tmp_tar.close()
# reset the file pointers
tmp.flush()
tmp.seek(0)
# Run the build with our tarball context
dkr = Docker()
dkr.update_image(args.tag, tmp, quiet=args.quiet)
return 0
示例7: plot_predictions
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import addfile [as 别名]
def plot_predictions(self):
epoch, batch, data = self.get_next_batch(train=False) # get a test batch
num_classes = self.test_data_provider.get_num_classes()
NUM_ROWS = 2
NUM_COLS = 4
NUM_IMGS = NUM_ROWS * NUM_COLS if not self.save_preds else data[0].shape[1]
NUM_TOP_CLASSES = min(num_classes, 5) # show this many top labels
NUM_OUTPUTS = self.model_state["layers"][self.softmax_name]["outputs"]
PRED_IDX = 1
label_names = [lab.split(",")[0] for lab in self.test_data_provider.batch_meta["label_names"]]
if self.only_errors:
preds = n.zeros((data[0].shape[1], NUM_OUTPUTS), dtype=n.single)
else:
preds = n.zeros((NUM_IMGS, NUM_OUTPUTS), dtype=n.single)
# rand_idx = nr.permutation(n.r_[n.arange(1), n.where(data[1] == 552)[1], n.where(data[1] == 795)[1], n.where(data[1] == 449)[1], n.where(data[1] == 274)[1]])[:NUM_IMGS]
rand_idx = nr.randint(0, data[0].shape[1], NUM_IMGS)
if NUM_IMGS < data[0].shape[1]:
data = [n.require(d[:, rand_idx], requirements="C") for d in data]
# data += [preds]
# Run the model
print [d.shape for d in data], preds.shape
self.libmodel.startFeatureWriter(data, [preds], [self.softmax_name])
IGPUModel.finish_batch(self)
print preds
data[0] = self.test_data_provider.get_plottable_data(data[0])
if self.save_preds:
if not gfile.Exists(self.save_preds):
gfile.MakeDirs(self.save_preds)
preds_thresh = preds > 0.5 # Binarize predictions
data[0] = data[0] * 255.0
data[0][data[0] < 0] = 0
data[0][data[0] > 255] = 255
data[0] = n.require(data[0], dtype=n.uint8)
dir_name = "%s_predictions_batch_%d" % (os.path.basename(self.save_file), batch)
tar_name = os.path.join(self.save_preds, "%s.tar" % dir_name)
tfo = gfile.GFile(tar_name, "w")
tf = TarFile(fileobj=tfo, mode="w")
for img_idx in xrange(NUM_IMGS):
img = data[0][img_idx, :, :, :]
imsave = Image.fromarray(img)
prefix = (
"CORRECT"
if data[1][0, img_idx] == preds_thresh[img_idx, PRED_IDX]
else "FALSE_POS"
if preds_thresh[img_idx, PRED_IDX] == 1
else "FALSE_NEG"
)
file_name = "%s_%.2f_%d_%05d_%d.png" % (
prefix,
preds[img_idx, PRED_IDX],
batch,
img_idx,
data[1][0, img_idx],
)
# gf = gfile.GFile(file_name, "w")
file_string = StringIO()
imsave.save(file_string, "PNG")
tarinf = TarInfo(os.path.join(dir_name, file_name))
tarinf.size = file_string.tell()
file_string.seek(0)
tf.addfile(tarinf, file_string)
tf.close()
tfo.close()
# gf.close()
print "Wrote %d prediction PNGs to %s" % (preds.shape[0], tar_name)
else:
fig = pl.figure(3, figsize=(12, 9))
fig.text(0.4, 0.95, "%s test samples" % ("Mistaken" if self.only_errors else "Random"))
if self.only_errors:
# what the net got wrong
if NUM_OUTPUTS > 1:
err_idx = [i for i, p in enumerate(preds.argmax(axis=1)) if p not in n.where(data[2][:, i] > 0)[0]]
else:
err_idx = n.where(data[1][0, :] != preds[:, 0].T)[0]
print err_idx
err_idx = r.sample(err_idx, min(len(err_idx), NUM_IMGS))
data[0], data[1], preds = data[0][:, err_idx], data[1][:, err_idx], preds[err_idx, :]
import matplotlib.gridspec as gridspec
import matplotlib.colors as colors
cconv = colors.ColorConverter()
gs = gridspec.GridSpec(NUM_ROWS * 2, NUM_COLS, width_ratios=[1] * NUM_COLS, height_ratios=[2, 1] * NUM_ROWS)
# print data[1]
for row in xrange(NUM_ROWS):
for col in xrange(NUM_COLS):
img_idx = row * NUM_COLS + col
if data[0].shape[0] <= img_idx:
break
pl.subplot(gs[(row * 2) * NUM_COLS + col])
# pl.subplot(NUM_ROWS*2, NUM_COLS, row * 2 * NUM_COLS + col + 1)
pl.xticks([])
pl.yticks([])
img = data[0][img_idx, :, :, :]
img = img.squeeze()
if len(img.shape) > 2: # more than 2 dimensions
if img.shape[2] is 2: # if two channels
# copy 2nd to 3rd channel for visualization
#.........这里部分代码省略.........