自动化验证文件是否更新

最近项目发布比较频繁,每次发布或者增量更新JS文件后,第一件事就是确保服务器上的文件确实已经更新。

不然随后的测试都是在做无用功,再加上Secure CRT和SecureFX经常周期性的有Bug,导致文件看起来已经更新,实际上却没有。

最致命的一点就是浏览器,浏览器经常出现缓存清不掉的情况,尤其是IE 7十分严重,常常要借助于QQ管家类似的软件才能完全清楚缓存。

所以在浏览器中输入URL手工验证JS文件是否已经更新是很不方便的,即使没有上面所说的种种BUG,你需要做的操作也不少。

包括:清空缓存、输入URL、寻找文件变更点来比对文件是否已经被更新,这每一步都可以非常耗时和低效。

如果遇见浏览器缓存BUG,那么砸电脑的心都会有!

所以下面的代码就产生了,其作用就是比较SVN项目中的文件和网络上的文件内容是否相同。

# coding=utf-8
#
 we use this script to check whether the javascript file on the server has been updated
#
 compare with local svn directory

__author__="xugaofan"
__date__ ="$2011-12-31 00:44:00$"

import filecmp
import urllib
import os
import time

# save server file as temp file,and compare with svn file
def compare(server_file,temp_file,svn_file):
        urllib.urlretrieve(server_file,temp_file)
        if filecmp.cmp(svn_file, temp_file, 0)  == False:
            print '##WARNING##'
            print server_file,' has not been updated yet!'

# only need to check the files modified recently,avoid using too much bindwith 
def modifyindays(file_path,days):
    return (time.time() - os.stat(file_path).st_mtime)< days*3600*24
    
if __name__ == "__main__":
    # the server site name
    server_name = 'http://www.gpsoo.net/'
    # your local svn working copy
    local_svn_dir = 'd:\\svn'
    # a temp directory to download files
    local_temp_dir = 'd:\\temp'
    # the local svn folder to compare
    static_dirs = ['/js/','/user/js/']
    # static files such as html,css,img
    static_files = ['/user/monitor.html']
    # files to download,using format for web url such as '/user/js/monitor.js'
    download_list = []
    
    # check config directoies
    if not os.path.exists(local_svn_dir):
        print 'svn directory config got error'
    if not os.path.exists(local_temp_dir):
        os.mkdir(local_temp_dir)

    # searching files to download in svn
    for i in static_dirs:
        # check directory 
        dir = local_temp_dir + i.replace('/''\\')
        if os.path.exists(dir) == False:
            print 'create a temp directory:',dir
            os.makedirs(dir)
        # find target files according to svn
        svn_dirs = local_svn_dir + i.replace('/''\\')
        files = os.listdir(svn_dirs)
        for file in files:
            if modifyindays(svn_dirs + file,5):
                download_list.append(i+file)
    for item in static_files:
        download_list.append(item)
        
    #download files into local temp directory,and compare them right now
    for file_name in download_list:
compare(server_name + file_name,local_temp_dir + file_name,local_svn_dir + file_name) 

 

更新记录:

1)上个版本需要下载所有指定目录的文件以及指定的文件,其实很多目录下的很多脚本万年不变,比如引用到的jQuery,如果再次下载显然浪费时间。

所以新增了一个方法,如果该文件最近几天之内没有修改过,表示不需要下载和比较内容

2)实际发布工作中,我们还需要知道相关的域名HOSTS是否正确。所以加入了域名IP对照表 

posted @ 2011-12-31 01:47  高凡凡高  阅读(719)  评论(1编辑  收藏  举报