脚本用途
git commit hooks(钩子),每当用户运行一次git commit时,会自动将此次用户填写的日志发送到指定邮箱
使用方法
将下面的脚本复制保持,命名为commit-msg,然后丢到git项目的【.git/hooks】目录下即可, windows与macos环境下都已验证通过。
说明:命名一定要为commit-msg,git钩子都是只认名字的,特定的动作会触发系统自动去调用特定的文件,如果找不到指定的文件,则认为没有这个类型的钩子
脚本内容
MailTool类是一个封装好的python发送邮件类,实例化后,直接调用它的send()方法就能发送邮件。
#!/usr/bin/env python
# coding=utf-8
"""
Author: DENGQINGYONG
Time: 17/2/10 11:05
Desc: 发送邮件示例代码
"""
import sys
import smtplib
import subprocess
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from os.path import exists
class MailTool(object):
"""邮件处理工具:主要用途——发送邮件"""
def __init__(self, send_username, send_smtp=None, send_password=None,):
"""初始化实例,需要提供发送方信息:发送smtp服务器,用户名,密码等"""
self.send_smtp = send_smtp
self.send_username = send_username
self.send_password = send_password
def send(self, receiver, subject, content, mail_format="plain", mail_charset="utf-8", attachs=None):
"""发送邮件"""
# 转换正文类型为规范类型
if mail_format == "text":
mail_format = "plain"
# 处理邮件正文格式
if mail_format == "plain":
mail_format = "plain"
# print type(content)
# print content
pass
elif mail_format == "html":
content = "<html><body>%s</body></html>" % content
# print content
else:
print "邮件格式输入错误: %s, 应该为空或html" % mail_format
return 1
msg = MIMEText(content, mail_format, mail_charset)
msg["Subject"] = Header(subject, mail_charset)
msg["From"] = Header(self.send_username, mail_charset)
msg["To"] = Header(receiver, mail_charset)
# 处理附件
if attachs:
msg = self.attachment(msg, attachs)
smtp = smtplib.SMTP()
smtp.connect(self.send_smtp)
smtp.login(self.send_username, self.send_password)
smtp.sendmail(self.send_username, receiver, msg.as_string())
smtp.quit()
def attachment(self, msg, attachs):
"""处理附件,将附件添加到邮件中"""
msg_part = MIMEMultipart()
msg_part["From"] = msg["From"]
msg_part["To"] = msg["To"]
msg_part["Subject"] = msg["Subject"]
# print msg.get_payload(decode=True)
msg_part.attach(msg)
if isinstance(attachs, (list, tuple)):
for attach in attachs:
msg_part = self.add_attach(msg_part, attach)
else:
msg_part = self.add_attach(msg_part, attachs)
return msg_part
def add_attach(self, attobj, filename):
"""将单个文件添加到附件对象中"""
if exists(filename):
attachname = filename.split('/')[-1]
if len(filename) == 0:
attachname = filename.split('\\')[-1]
else:
raise IOError("文件不存: %s" % filename)
attach = MIMEText(open(filename, "rb").read(), "base64", "utf-8")
attach["Content-Type"] = "application/octet-stream"
attach["Content-Disposition"] = "attachment; filename='%s'" % attachname
attobj.attach(attach)
return attobj
if __name__=="__main__":
# 日志开关
echo = False
if echo:
print "开始调用shell进程:"
# 获得git commit 日志
user = subprocess.Popen("git config --global user.name", stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT)
email = subprocess.Popen("git config --global user.email", stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT)
# log = subprocess.Popen("git log -n 1", stdout=subprocess.PIPE, shell=True, stderr=subprocess.STDOUT)
logfile = sys.argv[1]
log = open(logfile)
logtxt = ""
for logline in log.readlines():
if not logline.strip().startswith("#"):
logtxt += logline
user = user.communicate()[0]
email = email.communicate()[0]
# logtxt = log.communicate()[0]
if echo:
print "结束shell进程调用:"
print "logfile: %s" % logfile
print "当前用户: %s: %s" % (user, email)
print "本次commit日志:=============\n", logtxt, "\n\n"
# 发送邮件
mail = MailTool("sender@qq.com", "smtp.qq.com", "password")
mail.send("yu12377@163.com", "gitlab commit日志——FROM:%s" % user, logtxt)