will be created as a separated loop image.
"""
- def __init__(self, creatoropts=None, pkgmgr=None, taring_to=None):
+ def __init__(self, creatoropts=None, pkgmgr=None, compress_to=None):
"""Initialize a LoopImageCreator instance.
This method takes the same arguments as ImageCreator.__init__()
BaseImageCreator.__init__(self, creatoropts, pkgmgr)
- if taring_to:
- if '@NAME@' in taring_to:
- taring_to = taring_to.replace('@NAME@', self.name)
+ if compress_to:
+ if '@NAME@' in compress_to:
+ compress_to = compress_to.replace('@NAME@', self.name)
- if not taring_to.endswith('.tar'):
- taring_to += ".tar"
+ compress_imgdir_method = os.path.splitext(compress_to)[1]
+ if compress_imgdir_method in (".zip", ".tar"):
+ self.compress_imgdir_method = compress_imgdir_method[1:]
+ else:
+ self.compress_imgdir_method = "tar"
+ compress_to += ".tar"
- self.taring_to = taring_to
+ self.compress_to = compress_to
self.__fslabel = None
self.fslabel = self.name
else:
self.__image_size = 0
- if taring_to:
- self._img_name = self.taring_to
+ if compress_to:
+ self._img_name = self.compress_to
else:
self._img_name = self.name + ".img"
item['loop'].cleanup()
def _stage_final_image(self):
- if self.taring_to:
- import tarfile
+ if self.compress_to:
self._resparse(0)
- tarfile_name = self.taring_to
- mountfp_xml = os.path.splitext(tarfile_name)[0] + ".xml"
+ cfile_name = self.compress_to
+ mountfp_xml = os.path.splitext(cfile_name)[0] + ".xml"
for item in self._instloops:
imgfile = os.path.join(self.__imgdir, item['name'])
'-O ^huge_file,extents,uninit_bg %s ' \
% imgfile)
- msger.info("Tar all loop images together to %s" % tarfile_name)
- tar = tarfile.open(os.path.join(self._outdir, tarfile_name), 'w')
- for item in os.listdir(self.__imgdir):
- fpath = os.path.join(self.__imgdir, item)
- tar.add(fpath, item)
-
- tar.close()
+ msger.info("Compress all loop images together to %s" % cfile_name)
+ dstfile = os.path.join(self._outdir, cfile_name)
+ if self.compress_imgdir_method == "tar":
+ misc.taring(dstfile, self.__imgdir)
+ elif self.compress_imgdir_method == "zip":
+ misc.ziping(dstfile, self.__imgdir)
+ else:
+ raise CreatorError("Unsupported compress type: %s" \
+ % self.compress_imgdir_method)
# save mount points mapping file to xml
save_mountpoints(os.path.join(self._outdir, mountfp_xml),
RPM_FMT = "%(name)s.%(arch)s %(ver_rel)s"
SRPM_RE = re.compile("(.*)-(\d+.*)-(\d+\.\d+).src.rpm")
+def taring(dstfile, targetdir):
+ import tarfile
+ wf = tarfile.open(dstfile, 'w')
+ for item in os.listdir(targetdir):
+ wf.add(os.path.join(targetdir, item), item)
+ wf.close()
+
+def ziping(dstfile, targetdir):
+ import zipfile
+ wf = zipfile.ZipFile(dstfile, 'w', compression=zipfile.ZIP_DEFLATED)
+ for item in os.listdir(targetdir):
+ fpath = os.path.join(targetdir, item)
+ if not os.path.isfile(fpath):
+ continue
+ wf.write(fpath, item, zipfile.ZIP_DEFLATED)
+ wf.close()
+
def human_size(size):
"""Return human readable string for Bytes size
"""
name = 'loop'
@classmethod
- @cmdln.option("--taring-to", dest="taring_to", type='string', default=None,
- help="Specify the filename for packaging all loop images "
- "into a single tarball")
+ @cmdln.option("--taring-to", dest="compress_to", type='string',
+ default=None, help="same with '--compress-to'")
+ @cmdln.option("--compress-to", dest="compress_to", type='string',
+ default=None, help="Specify compress filename for all image "
+ "output, compress type decided by file extension, '.zip' for "
+ "zip format, '.tar' for tar format, default is tar format")
def do_create(self, subcmd, opts, *args):
"""${cmd_name}: create loop image
if creatoropts['runtime']:
rt_util.runmic_in_runtime(creatoropts['runtime'], creatoropts, ksconf, None)
- creator = LoopImageCreator(creatoropts, pkgmgr, opts.taring_to)
+ creator = LoopImageCreator(creatoropts, pkgmgr, opts.compress_to)
if len(recording_pkgs) > 0:
creator._recording_pkgs = recording_pkgs
if creatoropts['release'] is None:
- if opts.taring_to:
- imagefile = "%s.tar" % os.path.join(creator.destdir, opts.taring_to)
+ if opts.compress_to:
+ imagefile = "%s" % os.path.join(creator.destdir, creator.compress_to)
else:
imagefile = "%s.img" % os.path.join(creator.destdir, creator.name)