标签:svn python
之前是用ruby折腾的,现在学习python,然后原来为一个项目做的,现在为多项目,
现在记录一下。
#!/usr/bin/python # -*- coding:utf-8 -*- import re import sys import os import filecmp import smtplib import tempfile import linecache import datetime from email.mime.text import MIMEText svn_ser = { "AAA":"http://bbb.163.net/svn/AAA", "BBB":"http://aaa.163.net/svn/BBB" } svn_rel_ser = ‘‘ svn_rel = ‘‘ svn_rel_dir = "/branches" svn_tmp_dir = tempfile.mkdtemp() tar_list_name = svn_tmp_dir + "/tar.list" maillist = "/home/" + os.getlogin().replace(‘.‘,‘‘) + "/.maillist.txt" svn_name, svn_comment = ‘‘, ‘‘ old_dir = os.getcwd() run_user = ["li.li","se.ni","163"] def check_user(): if os.getlogin() in run_user: if not os.path.exists(maillist): print "Mail List File Not Exist!" print "Please VIM /home/%s/.maillist.txt" % os.getlogin().replace(‘.‘,‘‘) sys.exit(1) else: print "Permission Denied!" sys.exit(1) def send_mail(tar_name, diff): ci_time = datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘) mail_host="smtp.163.com" mail_user="test@163.com" mail_s= "abcabc" sub = "SVN_Release" content = ‘‘‘ Module: RTL Comment: %s Version: %s TarName: %s TarSize: %s CiTime: %s ============= DiffSVN: ====================== %s ========================================== ‘‘‘ % (svn_comment, re.findall(‘[0-9]{3}‘, tar_name)[0], tar_name, os.path.getsize(tar_name), ci_time, diff ) f = open(maillist,‘r‘) for i in f: if not re.match(‘^#‘,i): me="SVN"+"<"+mail_user+">" msg = MIMEText(content,_subtype=‘plain‘,_charset=‘gb2312‘) msg[‘Subject‘] = sub msg[‘From‘] = me msg[‘To‘] = i try: server = smtplib.SMTP() server.connect(mail_host) server.login(mail_user,mail_s) server.sendmail(me, i , msg.as_string()) server.close() except Exception, e: print "Send to %s ERROR" % i f.close() def usage(): tmp = ‘‘‘ usage script project_name [comment] e.g. script AAA "Test by me" script aaa ‘‘‘ print tmp def co_check(co_ser): os.chdir(svn_tmp_dir) os.system("svn co %s/Code/RTL 1>/dev/null 2>&1" % co_ser) def create_tar_list(): file_path = [] tar_list = [] for filepath,pathname,filename in os.walk(svn_tmp_dir): #filepath fullpath #pathname fullpath child dir #filename child dir file if os.path.exists(filepath + "/" + filepath.split(‘/‘)[-1] + ".f"): file_path.append(filepath + "/" + filepath.split(‘/‘)[-1] + ‘.f‘) tmp_line = [] true_path = ‘‘ for i in file_path: f = open(i,‘r‘) for line in f: if re.search(r‘^\/\/‘,line.strip()): next elif re.search(r‘^\.|^\w+‘,line.strip()): if re.search(r‘\/\/‘,line): tmp_line = line.strip().split(‘//‘)[0].rstrip().split(‘/‘) else: tmp_line = line.strip().split(‘/‘) if ‘.‘ in tmp_line: del tmp_line[tmp_line.index(‘.‘)] if ‘..‘ in tmp_line: del tmp_line[tmp_line.index(‘..‘)] ture_path = os.path.dirname(i) + "/" + ‘/‘.join(tmp_line) if os.path.isfile(ture_path): tar_list.append("." + ture_path.split(svn_tmp_dir)[-1]) f.close() open( tar_list_name, ‘w‘).write(‘\n‘.join(tar_list)) def tar_file_name(): os.system(‘cd %s && mkdir rel tmp && cd rel && svn co %s 1>/dev/null 2>&1‘ % (svn_tmp_dir, svn_rel_ser)) #can not find tar? tmp_name = os.popen("cd %s && file $(svn log -v | grep ‘tar.bz2‘ | awk ‘{print \".\"$NF}‘) | grep -v ERROR" % (svn_tmp_dir + "/rel/" + svn_rel )).read() if tmp_name.strip() == ‘‘: if svn_name == "AAA": return [svn_name + "_Release.001.tar.bz2"] else: return [svn_name + "_001.tar.bz2"] else: tmp_name = tmp_name.split()[0].split(‘/‘)[-1].strip()[0:-1] if svn_name == ‘AAA‘: tmp_no = int(tmp_name.split(‘.‘)[-3]) + 1 new_name = svn_name + "_Release.%03d.tar.bz2" % tmp_no return [new_name,tmp_name.split(‘/‘)[-1]] else: tmp_no = int(tmp_name.split(‘_‘)[-1][0:3]) + 1 new_name = svn_name + "_%03d.tar.bz2" % tmp_no return [new_name,tmp_name.split(‘/‘)[-1]] def diff_svn(tar_name): diff_tmp = [] tar_path = "./rel" + os.popen("cd %s/rel && find . -name %s" % (svn_tmp_dir, tar_name)).read().strip()[1:] os.system("cd %s && tar xf %s -C %s" % (svn_tmp_dir, tar_path, svn_tmp_dir + "/tmp")) f = open(tar_list_name, ‘r‘) for i in f: i = i.strip() filename = svn_tmp_dir + "/tmp" + i[1:] if os.path.isfile(svn_tmp_dir + "/tmp" + i[1:]): #exist file ---> cmp if not filecmp.cmp(svn_tmp_dir + i[1:],svn_tmp_dir + "/tmp" + i[1:]): diff_tmp.append(os.path.dirname(i.strip())) else: diff_tmp.append(os.path.dirname(i.strip())) f.close() return list(set(diff_tmp)) #-------------------------- if len(sys.argv) <= 1: usage() sys.exit(1) else: try: svn_name, svn_comment = str.upper(sys.argv[1]), sys.argv[2] except: svn_name, svn_comment = str.upper(sys.argv[1]), "Auto Release" if not svn_ser.has_key(svn_name): usage() sys.exit(1) if svn_name == ‘AAA‘: svn_rel_ser, svn_rel = svn_ser[svn_name] + "-REL", svn_name + "-REL" else: svn_rel_ser, svn_rel = svn_ser[svn_name] + "_REL", svn_name + "_REL" svn_rel_dir = svn_tmp_dir + "/rel/" + svn_rel + "/branches" check_user() co_check( svn_ser[svn_name]) create_tar_list() tar_name = tar_file_name() if "001" in tar_name[0]: diff = ‘‘ else: diff = diff_svn(tar_name[1]) if len(diff) == 0: diff = ‘‘ else: diff = "\n".join(diff) # svn ci if raw_input("Are You Sure Release? [y|n] \n " ).lower().strip() == "y": os.system("cd %s && tar -jc -T tar.list -f %s" % (svn_tmp_dir, tar_name[0])) os.chdir(svn_rel_dir) os.system("mv ../../../%s ." % tar_name[0]) os.system("svn add %s" % tar_name[0]) os.system("svn ci %s -m ‘%s‘" % (tar_name[0], svn_comment)) # send mail send_mail(tar_name[0],diff) print "SVN Release OK" else: pass # delete temp file os.chdir(‘/tmp‘) os.system(‘rm -rf $(ls -1 /tmp | grep -E "tmp[a-zA-Z0-9]+")‘) sys.exit(0)
标签:svn python
原文地址:http://abian.blog.51cto.com/751059/1587570