def __init__(self, filename):
mail = mailbox.mbox(filename, create=False)[0]
# Simply name it commit_hash, otherwise we would have to refactor
# tons of code.
self.commit_hash = mail['Message-ID']
self.mail_subject = mail['Subject']
# we need timezone aware datetimes due to the fact, that most of all
# emails contain timezone aware timestamps. There's an issue with
# timezone unaware timestamps: they can't be compared to timezone aware
# timestamps. To cope with that, we simple shift those mails to UTC
# (which is also true in most cases).
#
# E.g. python converts this timestamp to an timezone unaware one,
# while it is GMT:
# 'Fri, 23 Feb 2007 13:35:50 -0000 (GMT)'
try:
date = email.utils.parsedate_to_datetime(mail['Date'])
except:
# assume epoch
log.debug(' Message %s: unable to parse date %s' %
(self.commit_hash, mail['Date']))
date = datetime.datetime.utcfromtimestamp(0)
if date.tzinfo is None:
date = date.replace(tzinfo=datetime.timezone.utc)
payload = mail.get_payload()
# Check encoding and decode
cte = mail['Content-Transfer-Encoding']
if cte == 'QUOTED-PRINTABLE':
charset = mail.get_content_charset()
if charset not in CHARSETS:
charset = 'ascii'
payload = quopri.decodestring(payload)
payload = payload.decode(charset, errors='ignore')
# MAY RAISE AN ERROR, FORBID RETURN NULL
msg, diff = parse_payload(payload)
# reconstruct commit message
subject = self.mail_subject
match = PATCH_SUBJECT_REGEX.match(self.mail_subject)
if match:
subject = match.group(1)
msg = [subject, ''] + msg
author_name = mail['From']
author_email = ''
match = MAIL_FROM_REGEX.match(author_name)
if match:
author_name = match.group(1)
author_email = match.group(2)
super(PatchMail, self).__init__(msg, diff, author_name, author_email,
date, snip_header=True)
评论列表
文章目录