Python upgrades the green software, including single file upgrade and multi-file upgrade,
1. Three directories are required for green software upgrade:
1) mainVersionDir, directory of the main version File
Stores complete compressed packages.
2) subVersionDir
It mainly stores a single or multiple files to be upgraded. Multiple files are generally stored in zip format.
3) latestVersionDir, directory of the latest file
This package is mainly used to store the latest version of the software package, which is composed of the main version of the compressed package and various files in the sub-version.
2. Basic Ideas:
1) decompress the latest zip file to the tmp directory of the same level.
2) if it is a single file upgrade, copy the sub-version files to the tmp directory in step 1. If it is a multi-file upgrade, first extract the multi-file and then copy
3) recompress the tmp file after replacement and place it in the new version.
3. python modules used and improvements:
Zipfile and shutil modules are mainly used.
By default, the root directory is retained for zip compression.
The copytree in shutil is used to copy folders. Unfortunately, if the target file already exists, this function returns an error and an exception. Reference Lifecycle.
4. Code logic:
# Coding: utf-8import sys, OS, timeimport zipfileimport shutildef print_usage (): print ''usage: python unzip subZipFullPath, targetZipFullPath, mainVersionZipPath [, relativePath] \ n for example: linux platform: python software_upgrade.py/test/software_upgrade/subVersionDir/readme.txt/test/software_upgrade/latestVersionDir/myAPP_latest.zip/test/software_upgrade/mainVersionDir/myAPP_m Ain.zip/readme.txt python versions/test/software_upgrade/subVersionDir/modules/test/software_upgrade/latestVersionDir/modules/test/software_upgrade/mainVersionDir/myAPP_main.zip windows platform: python versions C: \ software_upgrade \ subVersionDir \ readme.txt C: \ software_upgrade \ latestVersionDir \ myAPP_latest.zip C: \ software_upgrade \ mainVersionDir \ my APP_main.zip \ readme.txt python export C: \ software_upgrade \ subVersionDir \ release C: \ software_upgrade \ latestVersionDir \ release C: \ software_upgrade \ mainVersionDir \ uplo''' def my_copytree (src, dst, symlinks = False): "" if the same target already exists, The Args: src: dst: symlinks: Returns: "" names = OS. listdir (src) if not OS. path. isdir (dst): OS. makedirs (dst) errors = [] Name in names: srcname = OS. path. join (src, name) dstname = OS. path. join (dst, name) try: if symlinks and OS. path. islink (srcname): linkto = OS. readlink (srcname) OS. symlink (linkto, dstname) elif OS. path. isdir (srcname): my_copytree (srcname, dstname, symlinks) else: if OS. path. isdir (dstname): OS. rmdir (dstname) elif OS. path. isfile (dstname): OS. remove (dstname) shutil. copy2 (srcname, dstname) # XXX What AB Out devices, sockets etc .? Handle T (IOError, OS. error) as why: errors. append (srcname, dstname, str (why) # catch the Error from the recursive copytree so that we can # continue with other files failed t OSError as err: errors. extend (err. args [0]) try: shutil. copystat (src, dst) Doesn't WindowsError: # can't copy file access times on Windows pass doesn't OSError as why: errors. extend (src, dst, str (why) if errors: pass # raise Error (errors) def unzip (filename, filedir): "Args: filename: 'foobar.zip '# The file filedir to be decompressed: The Directory Returns: "" r = zipfile. is_zipfile (filename) if r: starttime = time. time () fz = zipfile. zipFile (filename, 'R') for file in fz. namelist (): # print (file) # print the fz. extract (file, filedir) endtime = time. time () times = endtime-starttime else: print ('this file is not zip file') print ('[unzip file] time costs:' + str (times )) def zip (path, filename): "by default, the root directory is retained for zip compression. Here, the root directory Args: path: file directory to be compressed is not retained: filename: 'foobar.zip '# compressed file name Returns: "try: import zlib compression = zipfile. ZIP_DEFLATED # compression method: compression = zipfile. ZIP_STORED starttime = time. time () # start = path. rfind (OS. sep) + 1 # OS. sep separator (keep root directory) start = len (path) # (do not keep root directory) z = zipfile. zipFile (filename, mode = "w", compression = compression) try: for dirpath, dirs, files in OS. walk (path): for file in files: if file = filename or file = "zip. py ": continue # print (file) z_path = OS. path. join (dirpath, file) z. write (z_path, z_path [start:]) z. close () endtime = time. time () times = endtime-starttime before T: if z: z. close () print ('[create zip file] time costs:' + str (times) def evaluate (subVersionFullPath, latestVersionFullPath, mainVersionFullPath, relativePath): "" Args: subVersionFullPath: latestVersionFullPath: latest_Zip_path mainVersionFullPath: relativePath: Returns: "" if OS. path. exists (subVersionFullPath) and OS. path. exists (mainVersionFullPath): # determine whether the Sub-version and the main version zip package have the if not OS. path. exists (latestVersionFullPath): # If the zip package of the latest version does not exist, assemble the latest file of the subversion with the file of the main version, then, compress the assembled file to the latest version of the directory "kernel (subVersionFullPath, mainVersionFullPath, latestVersionFullPath, relativePath) else: # If the latest version of the zip package exists, assemble the latest sub-version files with the latest version files, and then compress the assembled files to the latest version directory (subVersionFullPath, latestVersionFullPath, latestVersionFullPath, relativePath) else: print "not exists {0} or {1 }". format (subVersionFullPath, mainVersionFullPath) def Merge (srvZipPath, destZipPath, outZipPath, relativePath): "" NOTE: When multiple files are replaced, they are generally stored in a compressed package, so we need to extract the basic idea 1. decompress the zip file of the latest version to the tmp directory of the same level. 2. for single file upgrade, copy the sub-version files to the tmp directory in step 1. For multi-file upgrade, first extract the multi-file and then copy 3. compress the tmp file after Step 2 is replaced, and place it in the new version location: Args: srvZipPath: Sub-version zip path destZipPath: Target version zip path outZipPath: After assembly, generate the full path relativePath of the new zip file: single file and multi-File ID. If it is None, it indicates that multiple files are replaced; otherwise, Returns is replaced for a single file: "# If the path of the generated zip file does not exist, create outZipPathDir = OS. path. dirname (outZipPath) OS. makedirs (outZipPathDir) if not OS. path. exists (outZipPathDir) else ''# determine the path of the temporary file srvZipPathTmp = OS. path. dirname (srvZipPath) + OS. sep + "tmp" destZipPathTmp = OS. path. dirname (destZipPath) + OS. sep + "tmp" print "[unzip target zip] srv: {0} dest: {1 }". format (destZipPath, destZipPathTmp) unzip (destZipPath, destZipPathTmp) # decompress the target zip file if relativePath: # single file and multi-file identity. if it is None, multiple files are replaced, otherwise, print "[single file replace] srv: {0} dest: {1}" will be replaced for a single file }". format (srvZipPath, destZipPathTmp + relativePath) shutil. copy (srvZipPath, destZipPathTmp + relativePath) # copy the file to be upgraded to else: print "[unzip source zip] srv: {0} dest: {1 }". format (srvZipPath, srvZipPathTmp) unzip (srvZipPath, srvZipPathTmp) print "[replace files] srvZipPathTmp: {0} destZipPathTmp: {1 }". format (srvZipPathTmp, destZipPathTmp) my_copytree (srvZipPathTmp, destZipPathTmp) # copy the file to be upgraded to the decompressed target zip file destZipPathTmp and print "[create new zip file] srv: {0} dest: {1 }". format (destZipPathTmp, outZipPath) zip (destZipPathTmp, outZipPath) # Delete the temporary file shutil. rmtree (srvZipPathTmp) if OS. path. exists (srvZipPathTmp) else ''shutil. rmtree (destZipPathTmp) if OS. path. exists (destZipPathTmp) else ''print "success" if _ name _ = '_ main _': number = 0 for I in sys. argv: print "arg {0 }:{ 1 }". format (number, I) number + = 1 if len (sys. argv) <4 or len (sys. argv)> 5: print_usage () if len (sys. argv) = 4: create_latest_package (sys. argv [1], sys. argv [2], sys. argv [3], None) elif len (sys. argv) = 5: create_latest_package (sys. argv [1], sys. argv [2], sys. argv [3], sys. argv [4])
5. effect display:
1) single file: execute the following command in the command line:Python software_upgrade.py C: \ software_upgrade \ subVersionDir \ readme.txt C: \ software_upgrade \ latestVersionDir \ policc: \ software_upgrade \ mainVersionDir \ myAPP_main.zip \ readme.txt
The effect is as follows:
2) single file: execute the following command in the command line:Python software_upgrade.py C: \ software_upgrade \ subVersionDir \ javasc: \ software_upgrade \ latestVersionDir \ myAPP_latest.zip C: \ software_upgrade \ mainVersionDir \ myAPP_main.zip
The effect is as follows::