参考:
https://blog.csdn.net/wiker_yong/article/details/10334967
# -*- coding: utf-8 -*-
import sys
import os
import smtplib
from email.mime.text import MIMEText
from email.header import Header
mail_host = 'smtp.163.com' #发送邮件的smtp地址
mail_user = '[email protected]' # 发送通知邮件的用户名
mail_pass = 'xxx' # 用户的密码,stmp的授权码,不同的邮件服务商不一样,可以不是邮件登陆密码
#me = 'SVN Service' + '' + '' + '@' + 'qq.com' + '' #发送邮件人的地址标识
me = '[email protected]'
to_list = ['[email protected]','[email protected]'] # 收件人 如果是多个收件人,加入到这里面来。
html_template = """
基本信息
版本库:
%s
版本号:%s
提交者:%s
提交时间:%s
提交说明
%s
文件清单
%s
☆ Powered by
louis
☆ 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)
print(cmd)
output = os.popen(cmd).read()
print(output)
return output
def get_date(repo, rev):
"""svnlook date -r REV REPOS 获得提交时间
"""
cmd = '%s date -r %s %s' % (svnlook_bin_path, rev, repo)
print(cmd)
output = os.popen(cmd).read()
print(output)
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 = MIMEText('如果我不设置msg to这里怎么显示?', 'plain', '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)
#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.decode("GBK").encode('UTF-8'), log.decode("GBK").encode('UTF-8'), file_list.decode("GBK").encode('UTF-8'))
print(content)
return content
if __name__ == '__main__':
#这个地方我把path的绝对路径修改成相对路径了。而且这个exe应该已经添加到了系统path了。
svnlook_bin_path = 'svnlook.exe'
subject = 'SVN Commit Notification'
#content = write_mail_content(sys.argv[1], sys.argv[2])
content = write_mail_content(sys.argv[1], sys.argv[2])
msg = write_mail(me, to_list, subject, content)
#msg = write_mail(me, to_list, subject, 'teset')
send_mail(msg, me, to_list)
svn这一侧的配置
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=D:/Repositories/DMR-320-Dc320-2BD/hooks
set PYTHON_BIN=C:/Python27/python.exe
%PYTHON_BIN% %HOOK_DIR%/sendEmail.py %REPOS% %REV%