日常工作中,經常有項目修改後,要更新,全部打包war封裝更新的話很費事,寫個指令碼,主要實現:
提取指定日期後修改的檔案
過濾某些目錄和檔案
將提取的檔案打包
FTP到指定的伺服器
之前用VBS和VB寫了一個,效率不理想。用Java寫了一個效率不錯,可惜想修改的話,就要動用eclipse,最後用python+DOS命令解決了快速修改和效率的問題
Eclipse項目目錄版本:
import osimport shutilimport tarfilefrom ftplib import FTPapp_name = "lfpt_hb"project_home = "D:\java\project_hf"update_home = "E:\PythonUpdate"time_modify = "4-26-2013" #format m-d-ypath_src_web = r"%s\%s\WebContent" %(project_home, app_name)path_src_cls = r"%s\%s\build\classes" %(project_home, app_name)path_src_lib = r"%s\%s\WebContent\WEB-INF\lib" %(project_home, app_name)path_dst_web = r"%s\%s" %(update_home, app_name)path_dst_cls = r"%s\%s\WEB-INF\classes" %(update_home, app_name)path_dst_lib = r"%s\%s\WEB-INF\lib" %(update_home, app_name)file_zip = r"%s\%s_%s.tar.gz" %(update_home, app_name, time_modify)#set exclude file for xcopy /exclude:fileexclude_all = "/EXCLUDE:update_client_exclude_all.txt"exclude_lib = "/EXCLUDE:update_client_exclude_lib.txt"# ftp settingsftp_server = "192.168.3.231"ftp_port = "21"ftp_user = "update"ftp_password = "update"def copy_web_files(): cmd_xcopy = 'xcopy %s %s /I /Y /S /D:%s %s' %(path_src_web, path_dst_web, time_modify, exclude_all) print(cmd_xcopy) os.system(cmd_xcopy) print("copy_web_files end...")def copy_jar_files(): cmd_xcopy = "xcopy %s %s /I /Y /S %s" %(path_src_lib, path_dst_lib, exclude_lib) print(cmd_xcopy) os.system(cmd_xcopy)def copy_class_files(): if not os.path.exists(path_dst_cls): cmd_xcopy = "xcopy %s %s /I /Y /S /D:%s %s" %(path_src_cls, path_dst_cls, time_modify, exclude_all) print(cmd_xcopy) os.system(cmd_xcopy) print("copy_web_files end...")def tar_files(): print("taring files...") tar = tarfile.open(file_zip, "w:gz") tar.add(path_dst_web, "") tar.close()def ftp_stor_files(): cmd_stor = "STOR %s" %(os.path.split(file_zip)[1]) print(cmd_stor) ftp = FTP(ftp_server, ftp_user, ftp_password) ftp.getwelcome() ftp.storbinary(cmd_stor, open(file_zip, "rb"), 1024) ftp.close() #ftp.quit()def clear(): cmd_rmdir = "rmdir /S /Q %s" %(path_dst_web) cmd_del = "del /S /Q %s" %(file_zip) print cmd_rmdir print cmd_del os.system(cmd_rmdir) os.system(cmd_del) if __name__ == "__main__": copy_web_files() #copy_jar_files() copy_class_files() tar_files() ftp_stor_files() clear() print("done, python is great!")
Maven目錄結構的版本:
#! /usr/bin/env python # -*- coding: utf-8 -*- #@author jinqinghua@gmail.com #@version 2013-05-28import osimport shutilimport tarfilefrom ftplib import FTP#以下部分可能要修改app_name = "eoa-web"project_home = "D:\Java\workspace\project-svn\eoa-parent"update_home = "F:\PythonUpdate"time_modify = "5-27-2013" #format m-d-ypath_src_web = r"%s\%s\src\main\webapp" %(project_home, app_name)path_src_cls = r"%s\%s\target\classes" %(project_home, app_name)path_src_lib = r"%s\%s\target\eoa-web\WEB-INF\lib\WEB-INF\lib" %(project_home, app_name)path_dst_web = r"%s\%s" %(update_home, app_name)path_dst_cls = r"%s\%s\WEB-INF\classes" %(update_home, app_name)path_dst_lib = r"%s\%s\WEB-INF\lib" %(update_home, app_name)file_zip = r"%s\%s_%s.tar.gz" %(update_home, app_name, time_modify)#set exclude file for xcopy /exclude:fileexclude_all = "/EXCLUDE:update_client_exclude_all.txt"exclude_lib = "/EXCLUDE:update_client_exclude_lib.txt"# ftp settingsftp_server = "192.168.0.220"ftp_port = "21"ftp_user = "admin"ftp_password = "admin"def copy_web_files(): cmd_xcopy = 'xcopy %s %s /I /Y /S /D:%s %s' %(path_src_web, path_dst_web, time_modify, exclude_all) print(cmd_xcopy) os.system(cmd_xcopy) print("copy_web_files end...") def copy_jar_files(): cmd_xcopy = "xcopy %s %s /I /Y /S %s" %(path_src_lib, path_dst_lib, exclude_lib) print(cmd_xcopy) os.system(cmd_xcopy)def copy_class_files(): if not os.path.exists(path_dst_cls): cmd_xcopy = "xcopy %s %s /I /Y /S /D:%s %s" %(path_src_cls, path_dst_cls, time_modify, exclude_all) print(cmd_xcopy) os.system(cmd_xcopy) print("copy_web_files end...")def tar_files(): print("taring files...") tar = tarfile.open(file_zip, "w:gz") tar.add(path_dst_web, "") tar.close()def ftp_stor_files(): cmd_stor = "STOR %s" %(os.path.split(file_zip)[1]) print(cmd_stor) ftp = FTP(ftp_server, ftp_user, ftp_password) ftp.getwelcome() ftp.storbinary(cmd_stor, open(file_zip, "rb"), 1024) ftp.close() #ftp.quit()def clear(): cmd_rmdir = "rmdir /S /Q %s" %(path_dst_web) cmd_del = "del /S /Q %s" %(file_zip) print cmd_rmdir print cmd_del os.system(cmd_rmdir) os.system(cmd_del)#主運行程式,可能要修改if __name__ == "__main__": copy_web_files() #copy_jar_files() copy_class_files() tar_files() ftp_stor_files() #clear() print("done, python is great!")