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

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


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

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




评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫大叔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值