Python自動化 – Windows開發環境下提取修改過的檔案並壓縮上傳到指定的FTP

來源:互聯網
上載者:User

日常工作中,經常有項目修改後,要更新,全部打包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!")
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.