`

Python自动化 - Windows开发环境下提取修改过的文件并压缩上传到指定的FTP

 
阅读更多

日常工作中,经常有项目修改后,要更新,全部打包war包更新的话很费事,写个脚本,主要实现:

提取指定日期后修改的文件

过滤某些目录和文件

将提取的文件打包

FTP到指定的服务器

 

之前用VBS和VB写了一个,效率不理想。用Java写了一个效率不错,可惜想修改的话,就要动用eclipse,最后用python+DOS命令解决了快速修改和效率的问题

 

Eclipse项目目录版本:

import os
import shutil
import tarfile
from ftplib import FTP

app_name     = "lfpt_hb"

project_home = "D:\java\project_hf"
update_home  = "E:\PythonUpdate"
time_modify  = "4-26-2013"  #format m-d-y

path_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:file
exclude_all = "/EXCLUDE:update_client_exclude_all.txt"
exclude_lib = "/EXCLUDE:update_client_exclude_lib.txt"

# ftp settings
ftp_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-28

import os
import shutil
import tarfile
from 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-y

path_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:file
exclude_all = "/EXCLUDE:update_client_exclude_all.txt"
exclude_lib = "/EXCLUDE:update_client_exclude_lib.txt"

# ftp settings
ftp_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!")

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics