Fix ensure_dir_exists() function behaviour and exception handling
authorAntoni <a.adaszkiewi@samsung.com>
Mon, 18 Jul 2022 11:42:35 +0000 (13:42 +0200)
committerAntoni <a.adaszkiewi@samsung.com>
Thu, 21 Jul 2022 15:17:21 +0000 (17:17 +0200)
Change-Id: I98b5a184714d34784068e840a1bb2636458a752f

mk_delta/common/bin/CreatePatch.py

index 037f165..566b414 100755 (executable)
@@ -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)