Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1417885
  • 博文数量: 264
  • 博客积分: 5810
  • 博客等级: 大校
  • 技术积分: 3528
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-13 17:15
文章分类

全部博文(264)

文章存档

2011年(264)

分类: LINUX

2011-06-09 15:00:22

一直想做SVN提交时发邮件通知,关于方法就是用svn的post-commit的hooks。看到有一个python写的脚本(svnmailer),不过配置文件非常复杂,没搞出来。于是决定自己用python写个简单的脚本,适合自己的才是好的。废话不说,请往下看。

1.编写有关邮件的配置文件,mail.cfg.xml








" />

    " />
    " />

2.编写发送邮件通知的python脚本,mail.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#导入smtplib和MIMEText
from email.mime.text import MIMEText
from xml.dom.minidom import parse, parseString
import time 
import os, sys, smtplib

class mailconfig:
      def __setattr__(self, attr, value):
          if attr == 'host':
              self.__dict__[attr] = value
          elif attr == 'smtpauth':
              self.__dict__[attr] = value
          elif attr == 'username':
              self.__dict__[attr] = value
          elif attr == 'password':
              self.__dict__[attr] = value
          elif attr == 'fromusername':
              self.__dict__[attr] = value
          elif attr == 'frommail':
              self.__dict__[attr] = value
          elif attr == 'tos':
              self.__dict__[attr] = value
          else:
              raise AttributeError, attr + ' not allowed'
        
      def __getattr__(self, attr):
          if attr == "host":
              return self.__dict__[attr]
          elif attr == "smtpauth":
              return self.__dict__[attr]
          elif attr == 'username':
              return self.__dict__[attr]
          elif attr == 'password':
              return self.__dict__[attr]
          elif attr == 'fromusername':
              return self.__dict__[attr]
          elif attr == 'frommail':
              return self.__dict__[attr]
          elif attr == 'tos':
              return self.__dict__[attr]
          else:
              raise AttributeError, attr

def loadmailconfig(xmlfile):
      dom1 = parse(xmlfile)
      config_element = dom1.getElementsByTagName("mailconfig")[0]
      mailcfg = mailconfig()
      mailcfg.host = config_element.getElementsByTagName("host")[0].attributes["value"].value
      mailcfg.smtpauth = config_element.getElementsByTagName("smtpauth")[0].attributes["value"].value
      if mailcfg.smtpauth == "true":
          mailcfg.username = config_element.getElementsByTagName("username")[0].attributes["value"].value
          mailcfg.password = config_element.getElementsByTagName("password")[0].attributes["value"].value
      mailcfg.fromusername = config_element.getElementsByTagName("fromusername")[0].attributes["value"].value
      mailcfg.frommail = config_element.getElementsByTagName("frommail")[0].attributes["value"].value

      tos = config_element.getElementsByTagName("tos")[0].getElementsByTagName("to");
      tomails = {}
      for to in tos:
          key = to.attributes["username"].value
          value = to.attributes["mail"].value
          tomails[key] = value
      mailcfg.tos = tomails
      return mailcfg

def send_mail(mailcfg, sub, content):
      '''
      mailcfg:邮件配置,包含了主机、登陆用户名密码、发送地址、接收地址
      sub:主题
      content:内容
      send_mail("
")
      '''
      me = mailcfg.fromusername + "<" + mailcfg.frommail + ">"
      to_list = []
    
      for key in mailcfg.tos.keys():
          to_list.append(key + "<" + mailcfg.tos[key] + ">")
                       
      msg = MIMEText(content)
      msg['Subject'] = sub
      msg['From'] = me
      msg['date']= time.ctime(time.time())
      msg['To'] = ";".join(to_list)
      try:
          s = smtplib.SMTP()
          s.connect(mailcfg.host)
          if mailcfg.smtpauth == "true":
              s.login(mailcfg.username, mailcfg.password)
            
          s.sendmail(mailcfg.frommail, to_list, msg.as_string())
          s.close()
          return True
      except Exception, e:
          #print str(e)
          return False

def get_svn_commitlog(svnhome, repos, reversion):
      cmd = '%s/svnlook info %s -r %s'% (svnhome, repos, reversion)
      return os.popen(cmd, 'r').readlines()

if __name__ == '__main__':
      mailcfg = loadmailconfig(sys.argv[1] + "
")
      lines = get_svn_commitlog(sys.argv[2], sys.argv[3], sys.argv[4])
      subject = "A new commit log from " + lines[0].replace('\n','')
      content = ''
      for line in lines:
          content = content + line
    
      send_mail(mailcfg, subject, content)

3.最后编写一个post-commit.bat脚本文件,或者重命名版本库hooks文件下的post-commit.tmpl,修改内容如下:

@echo off
set REPOS = %1
set REV = %2

F:\AMP\SVNRespository\test\hooks\mail.py F:\AMP\SVNRespository\test\hooks F:\AMP\Subversion\bin %1 %2
exit 0

说明:F:\AMP\SVNRespository\test\hooks是mail.cfg.xml所在的路径,F:\AMP\Subversion\bin是subversion的bin所在路径。

最后说明,配置和邮件内容都相对简单了点,其他有需要的可以根据实际需要修改,另外那个svnmailer功能肯定强大,有空一定要好好研究,争取配置成功。

阅读(11196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~