有一个git库,想隔一段时间去看看更新了什么内容,可以学习,后面怕麻烦,还可能忘记查看,就写了个脚本使用crontab来自动获取两周内的更新并发送邮件。初学,仅供参考。
- #!/usr/bin/python
- #coding=utf-8
- import os,re,time,datetime,commands
- import smtplib
- from email.Header import Header
- from email.mime.text import MIMEText
- from email.MIMEMultipart import MIMEMultipart
- tools_script_dir='/data/scrpit'
- def get_tools_log():
- global tools_script_dir
- GIT_STAT_CMD = "git log --stat"
- GIT_DETAILS_CMD = "git log -p"
- os.chdir(tools_script_dir)
- #get data
- d1 = datetime.datetime.now()
- dt=d1.strftime('%a %b %d')
- #print dt
- i = 15
- d2 = d1+datetime.timedelta(days=-i)
- two = d2.strftime('%a %b')
- da = int(d2.strftime('%d'))
- two_weeks_day = '%s %s'%(two,da)
- twy = d2.strftime('%Y')
- #get logs args
- gp = commands.getoutput('git pull')
- log = commands.getoutput('git log')
- commit_one = re.search(r'commit\s(.*)', log).group(1)
- commit_two = ''
- while 1:
- content = r'commit\s(.*)\n.*\n.*%s\s.*\s%s\s'% (two_weeks_day,twy)
- m = re.search(content,log)
- if m:
- commit_two = m.group(1)
- break
- else:
- i = i+1
- d2 = d1+datetime.timedelta(days=-i)
- two_weeks_day = d2.strftime('%a %b %d')
- print commit_one
- print commit_two
- #git detail
- cmd_stat = '%s %s...%s'%(GIT_STAT_CMD,commit_one,commit_two)
- cmd_detail = '%s %s...%s'%(GIT_DETAILS_CMD,commit_one,commit_two)
- result_stat = commands.getoutput(cmd_stat)
- result_detail = commands.getoutput(cmd_detail)
- #create result files
- stat_file = 'modify_stat.html'
- detail_file = 'modify_detail.html'
- html_result(stat_file,result_stat)
- html_result(detail_file,result_detail)
- #send email
- start = d2+datetime.timedelta(days=+1)
- start_day = start.strftime('%a %b %d')
- mail_subject = 'This is %s to %s script git log,please check it.'%(start_day,dt)
- content = 'Dear all,\n Here is script git log.\nIf this has some error,please tell me.'
- send_mail(mail_subject,content,stat_file,detail_file)
- #remove files
- rm_cmd = 'rm %s %s'%(stat_file,detail_file)
- commands.getoutput(rm_cmd)
-
- def html_result(htmlfile,htmlcontent):
- aa = re.sub(r'(commit.*)',r'\1',htmlcontent)
- ab = re.sub(r'(.*[+]+[-]+)',r'\1',aa)
- ac = re.sub(r'(.*)',r'\1',ab)
- ad = re.sub(r'([-]+.*)',r'\1',ac)
- ae = re.sub(r'([+]+.*)',r'\1',ad)
- con = re.sub(r'(.*)',r'\1
',ae)
- html = open(htmlfile, 'w')
- html.write("""
-
-
- %s
-
-
- """%htmlfile)
- html.write(con)
- html.write('
')
- html.close()
- def send_mail(subject,content,attfile1,attfile2):
- mail_host = 'mail.163.com'
- mail_user = '123@163.com'
- mail_pwd = '12121'
- mail_to = ['12313@123.com']
- #mail_cc = '123@163.com'
- msg = MIMEMultipart()
- #add mail content
- test_msg = MIMEText(content,'plain','utf-8')
- msg.attach(test_msg)
- #add attachment
- basename1 = os.path.basename(attfile1)
- att1 = MIMEText(open(attfile1, 'rb').read(), 'base64', 'utf-8')
- att1["Content-Type"] = 'application/octet-stream'
- att1["Content-Disposition"] = 'attachment; filename=%s' % basename1.encode('utf-8')
- msg.attach(att1)
- basename2 = os.path.basename(attfile2)
- att2 = MIMEText(open(attfile2, 'rb').read(), 'base64', 'utf-8')
- att2["Content-Type"] = 'application/octet-stream'
- att2["Content-Disposition"] = 'attachment; filename=%s' % basename2.encode('utf-8')
- msg.attach(att2)
- #general information
- msg['From'] = mail_user
- msg['To'] = ";".join(mail_to)
- #msg['Cc'] = mail_cc
- msg['Subject'] = Header(subject,'utf-8')
- try:
- s = smtplib.SMTP()
- s.connect(mail_host)
- #login
- s.login(mail_user,mail_pwd)
- #send mail
- print "sending mail....\nplase wait....."
- s.sendmail(mail_user,mail_to,msg.as_string())
- s.close()
- print "send mail success."
- except Exception ,e:
- print e
- if __name__ == '__main__':
- get_tools_log()
阅读(1678) | 评论(0) | 转发(0) |