From c82119c8362a4c29933062cd21138662af494016 Mon Sep 17 00:00:00 2001 From: Antoni Date: Mon, 18 Jul 2022 13:42:35 +0200 Subject: [PATCH] Fix ensure_dir_exists() function behaviour and exception handling Change-Id: I98b5a184714d34784068e840a1bb2636458a752f --- mk_delta/common/bin/CreatePatch.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/mk_delta/common/bin/CreatePatch.py b/mk_delta/common/bin/CreatePatch.py index 037f165..566b414 100755 --- a/mk_delta/common/bin/CreatePatch.py +++ b/mk_delta/common/bin/CreatePatch.py @@ -151,7 +151,11 @@ def main(): logging.info('*************** ENTERED PYTHON SCRIPT *****************') logging.info('Arguments Passed: [UpdateType - %s][Part Name - %s] [BaseOld - %s] [BaseNew - %s] \n [OUTPUTDir - %s] [BASE ATTR - %s] [TARGET ATTR - %s]' % (UPDATE_TYPE, PART_NAME, BASE_OLD, BASE_NEW, OUT_DIR, ATTR_OLD, ATTR_NEW)) - ensure_dir_exists(OUT_DIR) + try: + ensure_dir_exists(OUT_DIR) + except FileExistsError as exc: + logging.error('Argument passed as OUT_DIR - %s is already an existing file' % OUT_DIR) + raise exc if GenerateDiffAttr == "TRUE": if not (os.path.isfile(ATTR_OLD) and os.path.isfile(ATTR_NEW)): print >> sys.stderr, "Attributes missing -- ABORT" @@ -293,6 +297,8 @@ def zipdir(path, zip): def ensure_dir_exists(path): if not os.path.exists(path): os.makedirs(path) + elif os.path.isfile(path): + raise FileExistsError #shutil.rmtree(path) #os.makedirs(path) @@ -793,7 +799,11 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi for elt in files_new: dst_file = BASE_NEW + '/' + elt newfiles_dest_path = 'run/upgrade-sysroot/' - ensure_dir_exists(newfiles_dest_path) + try: + ensure_dir_exists(newfiles_dest_path) + except FileExistsError as exc: + logging.error('Directory %s used by this script is already an existing file' % newfiles_dest_path) + raise exc if os.path.islink(dst_file): patch = os.readlink(dst_file) logging.debug(' File New Links %s' % elt) @@ -834,7 +844,11 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi for elt in Dir_Added: newfiles_dest_path = 'run/upgrade-sysroot/' - ensure_dir_exists(newfiles_dest_path) + try: + ensure_dir_exists(newfiles_dest_path) + except FileExistsError as exc: + logging.error('Directory %s used by this script is already an existing file' % newfiles_dest_path) + raise exc destpath = newfiles_dest_path + elt if not os.path.exists(destpath): os.makedirs(destpath) -- 2.7.4