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 = """

        

基本信息

版本库: %s
版本号:%s
提交者:%s
提交时间:%s

提交说明

%s

文件清单

%s
☆ Powered by Gary
☆ Inspired by CREARO-SW
""" 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实现)_第1张图片

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

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




你可能感兴趣的:(SVN,svn邮件通知)