Chinaunix首页 | 论坛 | 博客
  • 博客访问: 64755
  • 博文数量: 7
  • 博客积分: 249
  • 博客等级: 二等列兵
  • 技术积分: 95
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-22 18:10
文章分类

全部博文(7)

文章存档

2012年(7)

我的朋友

分类: LINUX

2012-08-23 10:38:40

有一个git库,想隔一段时间去看看更新了什么内容,可以学习,后面怕麻烦,还可能忘记查看,就写了个脚本使用crontab来自动获取两周内的更新并发送邮件。初学,仅供参考。

点击(此处)折叠或打开

  1. #!/usr/bin/python
  2. #coding=utf-8

  3. import os,re,time,datetime,commands
  4. import smtplib
  5. from email.Header import Header
  6. from email.mime.text import MIMEText
  7. from email.MIMEMultipart import MIMEMultipart

  8. tools_script_dir='/data/scrpit'

  9. def get_tools_log():
  10.     global tools_script_dir
  11.     GIT_STAT_CMD = "git log --stat"
  12.     GIT_DETAILS_CMD = "git log -p"

  13.     os.chdir(tools_script_dir)
  14.     #get data
  15.     d1 = datetime.datetime.now()
  16.     dt=d1.strftime('%a %b %d')
  17.     #print dt
  18.     i = 15
  19.     d2 = d1+datetime.timedelta(days=-i)
  20.     two = d2.strftime('%a %b')
  21.     da = int(d2.strftime('%d'))
  22.     two_weeks_day = '%s %s'%(two,da)
  23.     twy = d2.strftime('%Y')
  24.     #get logs args
  25.     gp = commands.getoutput('git pull')
  26.     log = commands.getoutput('git log')

  27.     commit_one = re.search(r'commit\s(.*)', log).group(1)
  28.     commit_two = ''

  29.     while 1:
  30.         content = r'commit\s(.*)\n.*\n.*%s\s.*\s%s\s'% (two_weeks_day,twy)
  31.         m = re.search(content,log)
  32.         if m:
  33.             commit_two = m.group(1)
  34.             break
  35.         else:
  36.             i = i+1
  37.             d2 = d1+datetime.timedelta(days=-i)
  38.             two_weeks_day = d2.strftime('%a %b %d')
  39.     print commit_one
  40.     print commit_two

  41.     #git detail
  42.     cmd_stat = '%s %s...%s'%(GIT_STAT_CMD,commit_one,commit_two)
  43.     cmd_detail = '%s %s...%s'%(GIT_DETAILS_CMD,commit_one,commit_two)
  44.     result_stat = commands.getoutput(cmd_stat)
  45.     result_detail = commands.getoutput(cmd_detail)
  46.     #create result files
  47.     stat_file = 'modify_stat.html'
  48.     detail_file = 'modify_detail.html'

  49.     html_result(stat_file,result_stat)
  50.     html_result(detail_file,result_detail)
  51.     #send email
  52.     start = d2+datetime.timedelta(days=+1)
  53.     start_day = start.strftime('%a %b %d')
  54.     mail_subject = 'This is %s to %s script git log,please check it.'%(start_day,dt)
  55.     content = 'Dear all,\n Here is script git log.\nIf this has some error,please tell me.'
  56.     send_mail(mail_subject,content,stat_file,detail_file)
  57.     #remove files
  58.     rm_cmd = 'rm %s %s'%(stat_file,detail_file)
  59.     commands.getoutput(rm_cmd)
  60.      
  61. def html_result(htmlfile,htmlcontent):
  62.     aa = re.sub(r'(commit.*)',r'\1',htmlcontent)
  63.     ab = re.sub(r'(.*[+]+[-]+)',r'\1',aa)
  64.     ac = re.sub(r'(.*)',r'\1',ab)
  65.     ad = re.sub(r'([-]+.*)',r'\1',ac)
  66.     ae = re.sub(r'([+]+.*)',r'\1',ad)
  67.     con = re.sub(r'(.*)',r'\1
    '
    ,ae)
  68.     html = open(htmlfile, 'w')
  69.     html.write("""
  70.     
  71.     
  72.         %s
  73.             
  74.             
  75.             """%htmlfile)
  76.     html.write(con)
  77.     html.write(' ')
  78.     html.close()

  79. def send_mail(subject,content,attfile1,attfile2):
  80.     mail_host = 'mail.163.com'
  81.     mail_user = '123@163.com'
  82.     mail_pwd = '12121'
  83.     mail_to = ['12313@123.com']
  84.     #mail_cc = '123@163.com'


  85.     msg = MIMEMultipart()
  86.     #add mail content
  87.     test_msg = MIMEText(content,'plain','utf-8')
  88.     msg.attach(test_msg)
  89.     #add attachment
  90.     basename1 = os.path.basename(attfile1)
  91.     att1 = MIMEText(open(attfile1, 'rb').read(), 'base64', 'utf-8')
  92.     att1["Content-Type"] = 'application/octet-stream'
  93.     att1["Content-Disposition"] = 'attachment; filename=%s' % basename1.encode('utf-8')
  94.     msg.attach(att1)
  95.     basename2 = os.path.basename(attfile2)
  96.     att2 = MIMEText(open(attfile2, 'rb').read(), 'base64', 'utf-8')
  97.     att2["Content-Type"] = 'application/octet-stream'
  98.     att2["Content-Disposition"] = 'attachment; filename=%s' % basename2.encode('utf-8')
  99.     msg.attach(att2)
  100.     #general information
  101.     msg['From'] = mail_user
  102.     msg['To'] = ";".join(mail_to)
  103.     #msg['Cc'] = mail_cc
  104.     msg['Subject'] = Header(subject,'utf-8')
  105.     try:
  106.         s = smtplib.SMTP()
  107.         s.connect(mail_host)
  108.         #login
  109.         s.login(mail_user,mail_pwd)
  110.         #send mail
  111.         print "sending mail....\nplase wait....."
  112.         s.sendmail(mail_user,mail_to,msg.as_string())
  113.         s.close()
  114.         print "send mail success."
  115.     except Exception ,e:
  116.         print e

  117. if __name__ == '__main__':
  118.     get_tools_log()



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