Windows SVN变化邮件通知(Python2.7实现)

1,新增文件post-commit.bat

内容:

rem REPOS-PATH (the path to this repository)
set REPOS=%1
rem REV (the number of the revision just committed)
set REV=%2 set HOOK_DIR=F:/Repositories/版本库名/hooks
set PYTHON_BIN=C:/Python27/python.exe
%PYTHON_BIN% %HOOK_DIR%/sendEmail.py %REPOS% %REV%

放在F:/Repositories/版本库名/hooks下

2,安装python2.7(已安装则跳过)

去官网下地2.7的版本http://www.python.org/getit/

3,新增sendEmail.py

#coding: utf-8
import sys
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header mail_host = 'smtp.qq.com' #发送邮件的smtp地址
mail_user = '****@qq.com' # 发送通知邮件的用户名
mail_pass = '****' # 用户的密码
me = 'SVN Service' + '<' + '****' + '@' + 'qq.com' + '>' #发送邮件人的地址标识
to_list = ['****@qq.com'] # 收件人 html_template = """
<html>
<h2 style="color:#FFFFFF; background: #008040;">基本信息</h2>
<div> <b>版本库:</b>
<a href="svn:%s">%s</a>
</div>
<div> <b>版本号:</b>%s
</div>
<div>
<b>提交者:</b>%s
</div>
<div>
<b>提交时间:</b>%s
</div>
<h2 style="color:#FFFFFF; background: #4682B4;">提交说明</h2> <font size="4" color="#BF6000"><xmp>%s</xmp></font>
<h2 style="color:#FFFFFF; background: #5353A8;">文件清单</h2>
<xmp>%s</xmp>
<hr>
<center>
☆ Powered by
<a href="http://garyelephant.me">Gary</a>
</center>
<center>
☆ Inspired by
<a href="http://crearo-sw.blogspot.com">CREARO-SW</a>
</center>
</html>
""" def get_repo_name(repo):
return os.path.basename(repo) def get_author(repo, rev):
"""svnlook author -r REV REPOS 获得提交者
"""
cmd = '%s author -r %s %s' % (svnlook_bin_path, rev, repo)
output = os.popen(cmd).read()
return output def get_date(repo, rev):
"""svnlook date -r REV REPOS 获得提交时间
"""
cmd = '%s date -r %s %s' % (svnlook_bin_path, rev, repo)
output = os.popen(cmd).read()
return output def get_log(repo, rev):
"""svnlook log -r REV REPOS 获得提交日志
"""
cmd = '%s log -r %s %s' % (svnlook_bin_path, rev, repo)
output = os.popen(cmd).read()
return output def get_file_list(repo, rev):
"""svnlook changed -r REV REPOS 获得发生变更的文件
"""
cmd = '%s changed -r %s %s' % (svnlook_bin_path, rev, repo)
output = os.popen(cmd).read()
return output def send_mail(msg, sender, to_list):
try:
s = smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user,mail_pass)
s.sendmail(sender, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False def write_mail(sender, to_list, sub, content):
msg = MIMEText(content, _subtype = 'html', _charset='utf-8')
msg['Subject'] = sub
msg['From'] = sender
msg['To'] = ';'.join(to_list)
msg["Accept-Language"]="zh-CN"
msg["Accept-Charset"]="ISO-8859-1,utf-8"
return msg global svnlook_bin_path def write_mail_content(repo, rev):
"""
repo: repository
rev: revision
"""
repo_name = get_repo_name(repo)
author = get_author(repo, rev)
date = get_date(repo, rev)
log = get_log(repo, rev)
file_list = get_file_list(repo, rev)
content = html_template % (repo, repo_name, rev, author, date, log.decode("GBK").encode('UTF-8'), file_list.decode("GBK").encode('UTF-8'))
return content
if __name__ == '__main__':
svnlook_bin_path = '"C:/Program Files/VisualSVN Server/bin/svnlook.exe"' subject = 'SVN Commit Notification'
content = write_mail_content(sys.argv[1], sys.argv[2])
msg = write_mail(me, to_list, subject, content)
send_mail(msg, me, to_list)

同样放在F:/Repositories/版本库名/hooks

可以测试提交一个文件。效果如下:

Windows SVN变化邮件通知(Python2.7实现)

制作好的文件下载:http://download.csdn.net/detail/yangwei19680827/6012623(需要更新文件中的版本库名称等)

鸣谢:http://blog.csdn.net/gaoyingju/article/details/8884276

上一篇:linux 进阶命令


下一篇:配置Python+selenium+firefox自动化测试