利用python实现svn自动发送邮件功能

利用python实现svn自动发送邮件功能

  1. Hooks

Hookssvn库下存放脚本的目录,可以利用这些脚本在svn执行相应操作时实现发送邮件提醒、自动备份版本库等功能(本文以发送邮件提醒为例),部分脚本名称及含义如下:

Post-commit.tmpl            commit之后执行此脚本

Pre-commit.tmpl             commit之前执行此脚本

我们要实现用户commit之后的自动提醒功能,仅用到post-commit.tmpl,具体修改步骤如下:

mv post-commit.tmpl post-commit    去掉临时脚本后缀

vi post-commit                注释末尾两行并添加如下内容

REPOS="$1"

REV="$2"

#commit-email.pl "$REPOS" "$REV" [email protected]

#log-commit.py --repository "$REPOS" --revision "$REV"

/home/svn/1111_doc/hooks/mailer/mailer.py commit "$REPOS" "$REV" /home/svn/1111_doc/conf/mailer.conf

#这句意思是利用mailer.py这个python程序及mailer.conf这个邮件配置文件在commit后,将“库名”“版本”发送至mailer.conf里配置好的邮箱,注意:项目路径需要根据实际情况修改

#log-commit.py --repository "$REPOS" --revision "$REV"

if [ $? -ne 0 ]; then

        echo "failure to mail.py commit $REPOS $REV" >> /home/svn/1111_doc/svnmail.log

#如果发送失败将日志发送到svnmail.log,自己创建即可,注意更改为svn的用户权限

fi

  1. Svntools

上文脚本中涉及的mailer.pymailer.conf文件实际上是svn自带工具,具体目录如下:

/home/svn/tmp/subversion-1.5.6/tools/hook-scripts/mailer/     (仅供参考)

该目录下有2个文件,分别为mailer.config.examplemailer.py,将其分别放入对应目录下并授予svn用户权限:

cp –R ./mailer /home/svn/1111_doc/hooks

chown –R apache:apache mailer

cd mailer/

cp mailer.config.example /home/svn/1111_doc/conf/mailer.conf

修改mailer.conf配置文件,根据实际情况修改如下高亮部分:

# This option specifies the hostname for delivery via SMTP.

smtp_hostname = smtp.xxxx.com.cn

 

# Username and password for SMTP servers requiring authorisation.

smtp_username = [email protected]

smtp_password = xxxxxxxx

# the from address, you can use "from_addr =".

from_addr = [email protected]

 

# The default To: addresses for message.  One or more addresses,

# separated by whitespace (no commas).

# NOTE: If you want to use a different character for separating the

#       addresses put it in front of the addresses included in square

#       brackets '[ ]'.

to_addr = [email protected]

  1. python导入库文件

测试发送邮件时可能会报错:you need version 1.5.0 or better of the subversion python bindings,此时需要进入python,导入svn相关的文件即可

python

>>>import svn.fs

>>>import svn.delta

>>>import svn.repos

>>>import svn.core

进行测试时可以用如下命令,指定库及特定版本,测试邮件是否发送成功(每行之间都是一个空格):

/home/svn/1111_doc/hooks/mailer/mailer.py  commit /home/svn/1111_doc  10 /home/svn/1111_doc/conf/mailer.conf

之后检查是否收到邮件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章