def import_mail(mail): # some basic sanity checks if 'From' not in mail: return 0 if 'Subject' not in mail: return 0 if 'Message-Id' not in mail: return 0 hint = mail.get('X-Patchwork-Hint', '').lower() if hint == 'ignore': return 0 header_parser = HeaderParser(mail) submitter = Submitter.get_or_create(name=header_parser.from_name, email=header_parser.from_email) project = find_project(header_parser.project_name) if project is None: print 'No project for %s found' % header_parser.project_name dump_mail(mail, header_parser.message_id) return 0 try: content_parser = ContentParser(project, mail) except: print 'Email %s is not parsable' % (header_parser.message_id) dump_mail(mail, header_parser.message_id) return 0 patch = None if content_parser.pull_url or content_parser.patch: subject_parser = SubjectParser(mail.get('Subject'), [project.linkname]) name = subject_parser.name tags = find_or_create_tags(subject_parser.tags) patch = Patch(name=name, pull_url=content_parser.pull_url, content=content_parser.patch, date=mail_date(mail), headers=mail_headers(mail), tags=tags) match = gitsendemail_re.match(header_parser.message_id) if match: (uid, num, email) = match.groups() series = Series.get_or_create(uid) else: series = Series.get_or_create(header_parser.message_id) series.patches.append(patch) db.session.add(series) patch.submitter = submitter patch.msgid = header_parser.message_id patch.project = project ancestor = find_ancestor(project, mail, patch) if ancestor: patch.ancestors.append(ancestor) if patch is None: patch = find_patch_for_mail(project, mail) if patch is not None: patch.state = PatchState.comments comment = None if content_parser.comment: if patch is not None: comment = Comment(patch=patch, date=mail_date(mail), content=content_parser.comment, headers=mail_headers(mail)) if patch is not None: # we delay the saving until we know we have a patch. db.session.add(patch) if comment is not None: # looks like the original constructor for Comment takes the pk # when the Comment is created. reset it here. if patch: comment.patch = patch comment.submitter = submitter comment.msgid = header_parser.message_id db.session.add(comment) db.session.commit() return 0
def import_mail(mail, project_name=None): # some basic sanity checks if 'From' not in mail: return 0 if 'Subject' not in mail: return 0 if 'Message-Id' not in mail: return 0 message_id = mail.get('Message-Id').strip() if Patch.query.filter_by(msgid=message_id).count() or \ Comment.query.filter_by(msgid=message_id).count(): print('We have already imported a message with id "%s"' % message_id) return hint = mail.get('X-Patchwork-Hint', '').lower() if hint == 'ignore': return 0 header_parser = HeaderParser(mail) submitter = Submitter.get_or_create(name=header_parser.from_name, email=header_parser.from_email) if not project_name: project_name = header_parser.project_name project = find_project(project_name) if project is None: print 'No project for %s found' % project_name dump_mail(mail, header_parser.message_id) return 0 try: content_parser = ContentParser(project, mail) except: print 'Email %s is not parsable' % (header_parser.message_id) dump_mail(mail, header_parser.message_id) return 0 patch = None if content_parser.pull_url or content_parser.patch: subject_parser = SubjectParser(mail.get('Subject'), [project.linkname]) name = subject_parser.name tags = find_or_create_tags(subject_parser.tags) patch = Patch(name=name, pull_url=content_parser.pull_url, content=content_parser.patch, date=mail_date(mail), headers=mail_headers(mail), tags=tags) match = gitsendemail_re.match(header_parser.message_id) if match: (uid, num, email) = match.groups() series = Series.get_or_create(uid) else: series = Series.get_or_create(header_parser.message_id) series.patches.append(patch) db.session.add(series) patch.submitter = submitter patch.msgid = header_parser.message_id patch.project = project ancestor = find_ancestor(project, mail, patch) if ancestor: patch.ancestors.append(ancestor) if patch is None: patch = find_patch_for_mail(project, mail) if patch is not None: patch.state = PatchState.comments if patch is not None: db.session.add(patch) if content_parser.comment and patch is not None: comment = Comment(patch=patch, date=mail_date(mail), content=content_parser.comment, headers=mail_headers(mail)) comment.submitter = submitter comment.msgid = header_parser.message_id db.session.add(comment) db.session.commit() return 0