From 71a2db44ccd8a3c04fdb3d0242a741b3e57acb99 Mon Sep 17 00:00:00 2001 From: mmojzis Date: Tue, 4 Dec 2018 16:51:34 +0100 Subject: [PATCH] [RFR][NOTEST]Unzip rework (#8233) * GCE upload uses archived file so extraction removed * Reworked try/except in download_image --- cfme/utils/template/base.py | 55 +++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/cfme/utils/template/base.py b/cfme/utils/template/base.py index f7af3e1b4e..530654264f 100644 --- a/cfme/utils/template/base.py +++ b/cfme/utils/template/base.py @@ -1,6 +1,5 @@ import os import re -import tarfile from contextlib import closing @@ -241,12 +240,13 @@ def deploy_template(self): @log_wrap("download image locally") def download_image(self): + ARCHIVE_TYPES = ['zip'] suffix = re.compile( r'^.*?[.](?Ptar\.gz|tar\.bz2|\w+)$').match(self.image_name).group('ext') # Check if file exists already: - if path.isfile(self.local_file_path): + if path.isfile(self.image_name): logger.info('Local image found, skipping download: %s', self.local_file_path) - if suffix not in ['zip', 'tar.gz']: + if suffix not in ARCHIVE_TYPES: return True else: # Download file to cli-tool-client @@ -260,27 +260,36 @@ def download_image(self): # For EC2 and SCVMM images is zip used and for GCE is tar.gz used. archive_path = self.image_name - try: - if suffix == 'zip': - archive = ZipFile(archive_path) - zipinfo = archive.infolist() - self._unzipped_file = zipinfo[0].filename - elif suffix == 'tar.gz': - archive = tarfile.open(archive_path, "r:gz") - self._unzipped_file = archive.firstmember.name - else: - return True - if path.isfile(self.image_name): - os.remove(self.image_name) - logger.info('Image archived - unzipping as : %s', self._unzipped_file) - archive.extractall() - archive.close() - # remove the archive - os.remove(archive_path) + if suffix not in ARCHIVE_TYPES: return True - except Exception: - logger.exception("{} archive unzip failed.".format(suffix)) - return False + else: + if suffix == 'zip': + try: + archive = ZipFile(archive_path) + zipinfo = archive.infolist() + self._unzipped_file = zipinfo[0].filename + except Exception: + logger.exception("Getting information of {} archive failed.".format( + self.image_name)) + return False + + if path.isfile(self.image_name): + try: + os.remove(self.image_name) + except Exception: + logger.exception("Deleting previously unpacked file {} failed.".format( + self.image_name)) + return False + logger.info("Image archived - unpacking as : {}".format(self._unzipped_file)) + try: + archive.extractall() + archive.close() + # remove the archive + os.remove(archive_path) + return True + except Exception: + logger.exception("{} archive unpacked failed.".format(suffix)) + return False @log_wrap('add template to glance') def glance_upload(self):