Change DELTA_FS type deltas to be created with relative pathnames and remove their...
[platform/core/system/upgrade-tools.git] / mk_delta / common / bin / CreatePatch.py
index 99399df..f8dacb1 100755 (executable)
@@ -6,7 +6,6 @@ import filecmp
 import shutil
 import subprocess
 import re
-import ntpath
 import zipfile
 import datetime
 import hashlib
@@ -76,7 +75,7 @@ COMMON_BIN_PATH = "../../common/bin/"
 DIFF_UTIL = "/usr/local/bin/ss_bsdiff"
 DIFFPATCH_UTIL = "/usr/local/bin/ss_bspatch"
 #ZIPUTIL = "p7zip "
-ZIPUTIL = "7z -mf=off a system.7z "
+ZIPUTIL = "7z -mf=off a "
 NEW_FILES_PATH = "run/upgrade-sysroot"
 NEW_FILES_ZIP_NAME = "system.7z"
 SYMLINK_TYPE = "SYM"
@@ -305,15 +304,10 @@ def ensure_dir_exists(path):
 
 
 def path_leaf(path):
-       head, tail = ntpath.split(path)  # This is for windows?? Recheck
+       head, tail = os.path.split(path)
        return tail
 
 
-def path_head(path):
-       head, tail = ntpath.split(path)
-       return head
-
-
 # Creating Diff between OLD and NEW attribute files v12
 def Diff_AttrFiles(ATTR_OLD, ATTR_NEW, ATTR_FILE):
        if GenerateDiffAttr == "FALSE":
@@ -335,7 +329,7 @@ def Update_Attr(RequestedPath, Type, File_Attributes, Sym_Attributes):
        # Full File Path should MATCH
        if GenerateDiffAttr == "FALSE":
                return
-       FilePath = '"/' + RequestedPath + '"'
+       FilePath = '"' + RequestedPath + '"'
        #print ('FilePath - %s'% (FilePath))
        with open(AttributeFile) as f:
                for line in f:
@@ -672,7 +666,6 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi
 
        for new_file in NewList:
                R_Flag = 'FALSE'
-               DirPathNew = path_head(new_file)
                FileNameNew = path_leaf(new_file)
                DiffSize = 0
                winning_patch_sz = os.path.getsize(BASE_NEW + '/' + new_file)
@@ -785,22 +778,23 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi
                        Del_Cnt = Del_Cnt + 1
                        logging.debug(' Dir Deleted- %s' % src_file)
 
+       try:
+               ensure_dir_exists(NEW_FILES_PATH)
+       except FileExistsError as exc:
+               logging.error('Directory %s used by this script is already an existing file' % NEW_FILES_PATH)
+               raise exc
+
        for elt in files_new:
-               dst_file = BASE_NEW + '/' + elt
-               newfiles_dest_path = 'run/upgrade-sysroot/'
-               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
+               dst_file = os.path.join(BASE_NEW, elt)
+               destpath = os.path.join(NEW_FILES_PATH, elt)
+
                if os.path.islink(dst_file):
                        patch = os.readlink(dst_file)
                        logging.debug(' File New Links %s' % elt)
                        Partition_Doc_SymLinks.write('SYM:NEW:%s:%s\n' % (elt, patch))
                        # What if this is only a new sym link and folder already exists??? Should recheck
-                       destpath = newfiles_dest_path + elt
-                       if not os.path.exists(path_head(destpath)):
-                               os.makedirs(path_head(destpath))
+                       if not os.path.exists(os.path.dirname(destpath)):
+                               os.makedirs(os.path.dirname(destpath))
                                logging.info('New SymLink - Adding missing Dir')
                        Update_Attr(elt, "SYM", File_Attributes, Sym_Attributes)
                        Sym_New_Cnt = Sym_New_Cnt + 1
@@ -808,20 +802,17 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi
                        patch = New_hardlinks[elt]
                        logging.debug('File new hardlink %s' % elt)
                        Partition_Doc_HardLinks.write('HARD:NEW:%s:%s\n' %(elt, patch))
-                       destpath = newfiles_dest_path + elt
-                       if not os.path.exists(path_head(destpath)):
-                               os.makedirs(path_head(destpath))
+                       if not os.path.exists(os.path.dirname(destpath)):
+                               os.makedirs(os.path.dirname(destpath))
                                logging.info('New hardlink - Adding missing Dir')
                        Hard_New_Cnt += 1
                elif os.path.isdir(dst_file):  # We create just empty directory here
-                       destpath = newfiles_dest_path + elt
                        if not os.path.exists(destpath):
                                os.makedirs(destpath)
                                logging.debug(' File New Dir %s' % destpath)
                                New_Cnt = New_Cnt + 1
                else:
                        New_Cnt = New_Cnt + 1
-                       destpath = newfiles_dest_path + elt
                        destdir = os.path.dirname(destpath)
                        logging.debug('New files - %s ==> %s' % (dst_file, destdir))
 
@@ -842,13 +833,7 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi
                        Update_Attr(elt, "FILE", File_Attributes, Sym_Attributes)
 
        for elt in Dir_Added:
-               newfiles_dest_path = 'run/upgrade-sysroot/'
-               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
+               destpath = os.path.join(NEW_FILES_PATH, elt)
                if not os.path.exists(destpath):
                        os.makedirs(destpath)
                        logging.debug(' DirList New Dir %s' % destpath)
@@ -858,9 +843,10 @@ def SS_Generate_Delta(PART_NAME, BASE_OLD, Old_files, Old_dirs, BASE_NEW, New_fi
        print 'Compressing New files'
        if (New_Cnt > 0 or Sym_New_Cnt > 0):
                WorkingDir = os.getcwd()
-               os.chdir(os.getcwd() + "/" + NEW_FILES_PATH)
+               os.chdir(os.path.join(os.getcwd(), NEW_FILES_PATH))
                logging.info('Curr Working Dir - %s' % os.getcwd())
-               os.system(ZIPUTIL + NEW_FILES_PATH + " >> " + LOGFILE)
+               log_path = os.path.join(WorkingDir, LOGFILE)
+               os.system(ZIPUTIL + NEW_FILES_ZIP_NAME + " . " + " >> " + log_path)
                shutil.move(NEW_FILES_ZIP_NAME, WorkingDir + "/" + OUT_DIR)
                # New file size?? cos, we extract system.7z from delta.tar and then proceed with decompression
                SS_UpdateSize(WorkingDir + "/" + OUT_DIR + "/" + NEW_FILES_ZIP_NAME, WorkingDir + "/" + OUT_DIR + "/" + NEW_FILES_ZIP_NAME)