Python 统计不同url svn代码变更数
2015-01-09 09:39
579 查看
#!/bin/bash/python # -*-coding:utf-8-*- #svn统计不同url代码行数变更脚本,过滤空行,不过滤注释。 import subprocess,os,sys,time,re,shutil from optparse import OptionParser #初始化temp文件: FOLDER = "/tmp/temp_cm_svnrtagdiff" #初始化设置私密配置文件: PRIVATE_FILE = "/home/wwl/conf/wwl_private.conf" #Exclude条件: EXCLUDE = r"\.(txt|dic|properties|xml|config|log|key|pem|crt|per|sql|prefs|ver|gif|png|jpg|war|jar|swf|)$" #清理temp文件夹 def clean(): if os.path.exists(FOLDER): shutil.rmtree(FOLDER) print "清理temp文件夹成功。" else: print "temp文件夹不存在。" #创建temp文件夹 def mkdir(): #os.mkdir(FOLDER) os.makedirs(FOLDER) print "创建temp文件夹成功。" #读取配置文件,私密信息 def get_conf_private(): if os.path.isfile(PRIVATE_FILE): with open(PRIVATE_FILE,'r') as private: for line in private: if line.startswith('SVN_USERNAME'): username = line.split('=')[1] elif line.startswith('SVN_PASSWORD'): passwd = line.split('=')[1] return (username,passwd) else: print "svn配置文件不存在,联系值班CM!!!" sys.exit(1) #检验svn的用户名、密码和url是否正确 def svn_check_url_u_p(uname,pword,url,temp_svninfo): cmd = "svn info --no-auth-cache --non-interactive --username='%s' --password='%s' %s >%s" %(uname,pword,url,temp_svninfo) p = subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE) (stderr_test,stdout_test) = p.communicate() #p.wait() 使用wait()容易造成死锁。 #stderr_test = p.stderr.read() if len(stderr_test) == 0: print "url正确,svn账号密码正确:",url elif 'authorization failed' in stderr_test: print "svn账号密码不正确,请联系值班CM!!!" sys.exit(1) elif 'Not a valid URL' in stderr_test: print "url错误,请检查配置:",url sys.exit(1) #比较两个url之间的差异: def svn_diff_url_o_n(old_url,new_url,uname,pword,temp_svndiff): cmd = "svn diff --no-auth-cache --non-interactive --old=%s --new=%s --username='%s' --password='%s' >%s" %(old_url,new_url,uname,pword,temp_svndiff) p = subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE) (stderr_test,stdout_test) = p.communicate() #p.wait() #stderr_test = p.stderr.read() if len(stderr_test) == 0: print "svn diff Sucess!!!" else: print "svn diff Error,请联系值班CM!!!" sys.exit(1) #判断过滤条件: def is_ignore_svn(file_name): ignore_file_pattern_svn = EXCLUDE match = re.search(ignore_file_pattern_svn,file_name) if match == None: return False else: return True #判断是否为二进制文件: def is_binary(uname,pword,newuf,olduf=None): cmd = "svn blame --no-auth-cache --non-interactive --username='%s' --password='%s' %s" %(uname,pword,newuf) p = subprocess.Popen(cmd,shell=True,stderr=subprocess.PIPE,stdout=subprocess.PIPE) (stderr_test,stdout_test) = p.communicate() #p.wait() #stderr_test = p.stderr.read() if stderr_test.startswith("Skipping binary file"): return True elif stderr_test.startswith("svn: warning: W160017"): return is_binary(uname,pword,olduf) else: return False def main(): #获取当前时间戳: print "####******Begin testing at: ",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())," ********####" now_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) clean() mkdir() temp_svninfo = os.path.join(FOLDER,"temp_svninfo."+now_time) temp_svndiff = os.path.join(FOLDER,"temp_svndiff."+now_time) #获取命令行参数并赋值给变量 parser = OptionParser() parser.add_option("-o",dest="old_url",default="",help="-o old_SVN_REPOSITORY_URL 旧的svn URL地址") parser.add_option("-n",dest="new_url",default="",help="-n new_SVN_REPOSITORY_URL 新的svn URL地址") parser.add_option("-u",dest="username",default=get_conf_private()[0].strip(),help="-u USER_NAME svn服务器用户名") parser.add_option("-p",dest="passwd",default=get_conf_private()[1].strip(),help="-p PASSWD svn服务器密码") parser.add_option("-f",dest="judge",default="N",help="-f yes|YES|Y|y 是否打印文件列表") (options,args)=parser.parse_args() old_url = options.old_url.strip() new_url = options.new_url.strip() #判断old_url和new_url参数是否赋值 if len(old_url) == 0: print "请输入old_SVN_REPOSITORY_URL:" sys.exit (1) if len(new_url) == 0: print "请输入new_SVN_REPOSITORY_URL:" sys.exit (1) #检查url是否正确,svn账号密码是否正确 svn_check_url_u_p(options.username,options.passwd,old_url,temp_svninfo) svn_check_url_u_p(options.username,options.passwd,new_url,temp_svninfo) #比较两个url之间的差异: svn_diff_url_o_n(old_url,new_url,options.username,options.passwd,temp_svndiff) #初始化参数: AddLineNum = 0 DelLineNum = 0 ModLineNum = 0 TotalLineNum = 0 ModFileNum = 0 ExcludeFileNum = 0 TotalFileNum = 0 #处理temp_svndiff文件 #判断diff文件是否为空: if len(open(temp_svndiff,'r').read()) == 0: print "没有变更!!!" else: dict = {} with open(temp_svndiff,'r') as svndiff: for line in svndiff: if line.startswith("Index:"): key = line.split(':')[-1].strip() if key not in dict: dict[key] = [0,0] if line.startswith('+') and len(line.strip()) > 1: dict[key][0] += 1 if line.startswith('+++'): dict[key][0] -= 1 if line.startswith('-') and len(line.strip()) > 1: dict[key][1] += 1 if line.startswith('---'): dict[key][1] -= 1 #判断是否显示本次所有的变更文件: if options.judge in ['Y','y','YES','yes']: print "本次变更的文件:" for line in dict.keys(): print line else: print "本次跳过的文件:" TotalFileNum = len(dict.keys()) for file in dict.keys(): olduf = os.path.join(old_url,file) newuf = os.path.join(new_url,file) if file == '.': TotalFileNum -= 1 elif is_ignore_svn(file): print "Skipping file : ",file ExcludeFileNum += 1 elif is_binary(options.username,options.passwd,newuf,olduf): print "Skipping binary file : ",file ExcludeFileNum += 1 else: AddLineNum += dict[file][0] DelLineNum += dict[file][1] TotalLineNum = AddLineNum + DelLineNum clean() print "===============代码行差异为:=================\n" print "新增的代码行 = ",AddLineNum," 行" print "删除的代码行 = ",DelLineNum," 行\n" print "代码行变更总计 = ",TotalLineNum," 行\n" print "变更文件总数 = ",TotalFileNum," 个\n" print "排除文件总数 = ",ExcludeFileNum," 个\n" print "=============代码行统计完成!=================" print "####******End of testing at: ",time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())," **********####" 196 if __name__ == "__main__": main()
wwl_private.conf文件用于存放默认的svn账号密码:(如果执行python的时候没有输入-u -p 脚本调用此私密配置)
#(username) SVN_USERNAME=wwl #(password) SVN_PASSWORD=wwl
如果找不到wwl_private.conf私密文件,可以修改代码,直接返回你知道的原文密码
使用环境:python2.7、svn1.6、Ubuntu13.04
使用命令:python xxx.py -o "old_url" -n "new_url" -f y -u "name" -p "password"
脚本解析:使用svn info 命令判断url是否正确,svn用户名、密码是否正确;
使用svn diff 对比2个svn-url输出结果到temp文件,然后解析temp文件;
使用svn blame 判断文件是否是二进制文件;
设置过滤器EXCLUDE,过滤指定文件不统计;
过滤空行增删,不过滤注释。
弊端:svn diff 命令对一个代码行的修改算成+1行和-1行,此脚本没有另行统计修改的行数。
改进方案:使用svn diff --diff-cmd /usr/bin/diff 命令调用linux的diff来对比。linux的diff使用(a - 增)(c - 改)(d - 删),比较方便分别计算增删改代码行,后期尝试修改。
PS:如果使用的是svn1.7版本:检验svn用户名、密码和url替换成下面代码
#检验svn的用户名、密码和url是否正确 def svn_check_url_u_p(uname,pword,url,temp_svninfo): cmd = "svn info --no-auth-cache --non-interactive --username='%s' --password='%s' %s >%s" %(uname,pword,url,temp_svninfo) p = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE) (stderr_test,stdout_test) = p.communicate() if len(stderr_test) == 0: print "url正确,svn账号密码正确:",url elif 'E170001' in stderr_test: print "svn账号密码不正确,请联系值班CM!!!" sys.exit(1) elif 'E000002' in stderr_test: print "url错误,请检查配置:",url sys.exit(1)
相关文章推荐
- Python-统计svn变更代码行数
- 统计文件夹下代码行,python脚本,linux可用
- alfresco repository 不同版本的代码统计和版本差异
- Python统计代码行数
- python实现代码行数统计示例分享
- python代码统计大文本文件行数
- 使用python写的代码行数统计程序
- 代码注释率统计的Python及Java实现
- 统计代码行数(python实现)
- 感知机 python 代码实现 ----- 统计学习方法
- Python实现代码统计(代码+注释+空行)
- python写的统计PYTHON文件的(代码行数,空行数,注释行数)小程序
- 代码行数统计(python实现)
- 统计项目的代码行数(python处女作)
- [python]写一个统计代码行数的codeCounter 推荐
- Python 代码覆盖率统计工具 coverage.py
- python 代码覆盖率统计
- python读取excel表格统计不同网段IP个数
- <Think Python>中统计文献单词的处理代码
- 使用svn命令统计两次上线之间的代码行数,以及不同作者的代码行数