我们的文档均存档于SVN配置库中,项目组要求在指定目录(包含中文名)发生变更时,需要通知到对应的责任人。使用了SVN hooks中的post-commit脚本完成了该功能。
环境:SVN库部署在CentOS服务器
开发过程:
1.将项目配置库的/hooks目录下post-commit.tmpl 拷贝为一份为post-commit,调整文件权限为777。
2.编辑post-commit,脚本内容如下:
>temp_test
temp_test=/home/svn/Repo/Tooltest/hooks/test.txt
export LANG="zh_CN.UTF-8" #设置编码能识别到中文字符
REPOS="$1"
REV="$2"
#mailer.py commit "$REPOS" "$REV" /path/to/mailer.conf
echo "$REPOS" "$REV" > $temp_test
SVNLOOK=/usr/bin/svnlook
SH=/usr/bin/sh
Change_dir1=`$SVNLOOK dirs-changed -r "$2" "$1"` #读取有变更的目录
Change_file=`$SVNLOOK changed -r "$2" "$1"` #读取有变更的文件
Change_author=`$SVNLOOK author -r "$2" "$1"` #读取文件的作者
echo $Change_dir1>>/home/svn/Repo/Tooltest/hooks/test.txt
echo $Change_author>>/home/svn/Repo/Tooltest/hooks/test.txt
str="7.1.2清单"
if [[ $Change_dir1 =~ $str ]];then
python /home/svn/Script/SendEmail_Dirchange.py "$Change_file" "$Change_author"
fi
备注:当有多个目录下文件一起提交时,多个目录都会读取在Change_dir1变量中,以空格区分。所以不需要循环读写匹配。
3.SendEmail_Dirchange.py 脚本如下:
def sendmail(msg_file,msg_author):
date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
TO = ['[email protected]','[email protected]']
Cc = ['[email protected]']
mail_msg ="""
<p>各位好:</p>
<p>清单目录有更新,请关注。</p>
<p>更新文件:%s (A:新增,M:修改,D:删除)</p>
<p>作者:%s</p>"""%(msg_file,msg_author)
message = MIMEText(mail_msg, 'html', 'utf-8')
message['From'] = FROM
message['To'] = ','.join(TO)
message['Cc'] = ','.join(Cc)
subject = '清单更新提示'
message['Subject'] = Header(subject, 'utf-8')
try:
server = smtplib.SMTP(HOST,"587")
server.starttls()
#server.connect(HOST, "30")
server.login(Login_name, Login_passwd)
server.sendmail(FROM, TO+Cc, message.as_string())
server.quit()
print("邮件发送成功")
except smtplib.SMTPException as e:
print("Error: 无法发送邮件")
print(e)
呈现效果如下: