12 from sqlalchemy
.orm
import join
13 import cStringIO
as StringIO
18 from blobtracker
import BlobTracker
21 _msg_id_regex
= re
.compile(r
'<([^<>]+)>')
22 def _parse_msg_id(str):
23 m
= _msg_id_regex
.search(str)
27 parser
= email
.Parser
.Parser()
29 def _detect_reply_id(msg
):
30 if msg
['In-Reply-To']:
31 return _parse_msg_id(msg
['In-Reply-To'])
33 refs
= ' '.join(msg
.get_all('References'))
34 ref_ids
= [m
.group(1) for m
in _msg_id_regex
.finditer(refs
)]
37 def _get_text_payloads(msg
):
38 if not msg
.is_multipart():
39 yield msg
.get_payload()
41 for part
in email
.Iterators
.typed_subpart_iterator(msg
):
42 if part
.is_multipart():
43 yield part
.get_payload(0)
45 yield part
.get_payload()
47 _format_patch_regex
= re
.compile('.*^---$.*^diff --git', re
.MULTILINE|re
.DOTALL
)
48 _snip_patch_regex
= re
.compile('.*^-+ ?(?:8<|>8) ?-+\n(.*^diff --git.*)',
49 re
.MULTILINE|re
.DOTALL
)
50 def _guess_patch_contents(msg
):
51 for p
in _get_text_payloads(msg
):
52 if _format_patch_regex
.match(p
):
54 return msg
.as_string()
55 m
= _snip_patch_regex
.match(p
)
57 msg
.set_payload(m
.group(1))
58 return msg
.as_string()
62 def try_patch(m
, pp
, base_sha1
):
63 git('checkout', base_sha1
)
66 except patch
.PatchError
:
68 pipe
= git('show', ret_pipe
=True)
69 output
= git('patch-id', input_pipe
=pipe
)[0]
71 # this means the patch had no diff; e.g., a mode change
73 patch_id
, commit_id
= output
.split()
74 c
= db
.session
.query(db
.Commit
).filter(db
.Commit
.sha1
== commit_id
).first()
77 output
= git('log', '-1', '--pretty=format:%ct %at')[0]
78 adate
, cdate
= [int(s
) for s
in output
.split()]
79 c
= db
.Commit(commit_id
, cdate
, adate
, patch_id
, False)
80 blobtracker
.scan_commit_tree(c
)
81 p
= db
.Patch(c
, m
.id, pp
.notes
)
85 def find_blobs(upstream
, blobs
):
88 ret
= (db
.session
.query(db
.Blob
, db
.Commit
)
89 .filter(db
.Blob
.newest_commit_sha1
== db
.Commit
.sha1
)
90 .filter(db
.Blob
.sha1
.like(prefix
+'%'))
91 .filter(db
.Commit
.upstream
== upstream
)
92 .order_by(db
.Commit
.cdate
.desc()).first())
94 print 'blob %s not found?!' % prefix
96 commits
.append(ret
[1])
103 def try_patch_anywhere(msg
, m
):
104 print '*', m
.message_id
105 pdata
= _guess_patch_contents(msg
)
108 pp
= patch
.Patch(pdata
)
111 return # probably for another project
112 # perhaps we already know about this patch id
113 patch_id
= pp
.fast_patch_id()
115 cmt
= db
.query(db
.Commit
).filter(db
.Commit
.patch_id
==patch_id
).first()
117 p
= db
.Patch(cmt
, m
.id, pp
.notes
)
120 # first try on the commit given by the blobs
121 cmt
= find_blobs(True, pp
.blobs_pre
)
123 print 'trying canonical commit %s (upstream)' % cmt
.sha1
124 applied
= try_patch(m
, pp
, cmt
.sha1
)
127 # this is just hopeless: it doesn't apply to the commit it should!
130 print "no canonical commit found (upstream)"
132 cmt
= find_blobs(False, pp
.blobs_pre
)
134 print 'trying canonical commit %s (local)' % cmt
.sha1
135 applied
= try_patch(m
, pp
, cmt
.sha1
)
138 # this is just hopeless: it doesn't apply to the commit it should!
141 print "no canonical commit found (local)"
142 # if we have a parent, try on the parent
143 parent
= db
.session
.query(db
.Mail
).filter(db
.Mail
.message_id
==m
.in_reply_to
).first()
144 if parent
and parent
.has_patch
and parent
.patch_id
:
145 cmt
= (db
.session
.query(db
.Commit
)
146 .filter(db
.Commit
.patch_id
==parent
.patch_id
)
147 .order_by(db
.Commit
.cdate
.desc()).first())
148 print 'trying to apply on parent %s' % cmt
.sha1
149 applied
= try_patch(m
, pp
, cmt
.sha1
)
153 print "no parent commit found"
154 # try on origin/master
155 sha1
= git('rev-list', '--first-parent', '-1', '--before=%d' % m
.post_date
,
156 'origin/master')[0].strip()
158 sha1
= 'origin/master'
159 print 'trying on master at time of patch (%s)' % sha1
160 applied
= try_patch(m
, pp
, sha1
)
163 # same for origin/next
164 print 'trying on origin/next'
165 applied
= try_patch(m
, pp
, 'origin/next')
170 _whats_cooking_subject
= re
.compile(r
"^What's cooking in git\.git")
171 _whats_cooking_category
= re
.compile(r
"^\[(.*)\]$")
172 _whats_cooking_header
= re
.compile(r
"\* (../[a-zA-Z0-9-]+) \([^)]*\) \d+ commits?")
173 _whats_cooking_separator
= re
.compile(r
"^(-{5,}|-- )$")
175 def parse_whats_cooking(msg
, mail
):
176 if not (msg
["Subject"] and _whats_cooking_subject
.match(msg
["Subject"])):
179 branch
= 'pu' # initial part goes on 'pu'
181 def _rotate_notes(category
, branch
, notes
):
183 t
= db
.session
.query(db
.Topic
).filter(db
.Topic
.name
==branch
).first()
189 t
.cooking_notes
= '\n'.join(notes
)
192 notes
.append("[%s]" % category
)
194 text
= ''.join(_get_text_payloads(msg
))
195 for line
in text
.splitlines():
196 if _whats_cooking_separator
.match(line
):
198 notes
= _rotate_notes(category
, branch
, notes
)
201 m
= _whats_cooking_category
.match(line
)
203 category
= m
.group(1)
204 notes
= _rotate_notes(category
, branch
, notes
)
206 m
= _whats_cooking_header
.match(line
)
208 notes
= _rotate_notes(category
, branch
, notes
)
215 def process_mail(mail
):
216 msg
= parser
.parse(StringIO
.StringIO(mail
.data
))
217 parse_whats_cooking(msg
, mail
)
218 patch
= try_patch_anywhere(msg
, mail
)
220 mail
.patch_id
= patch
.commit
.patch_id
224 def _query_stale_mail():
225 return (db
.session
.query(db
.Mail
)
226 .filter(db
.Mail
.stale
== True)
227 .order_by(db
.Mail
.post_date
.asc(), db
.Mail
.subject
.asc()))
229 def walk_stale_mail():
231 blobtracker
= BlobTracker()
232 count
= _query_stale_mail().count()
233 for mail
in _query_stale_mail():
234 print "** %6d\n" % count
239 def get_mail_by_id(msg_id
):
240 # Note: use first() because we don't know it exists. The DB
241 # guarantees uniqueness anyway.
243 return dbcache
.mail_cache
[msg_id
]
248 _space_regex
= re
.compile(r
'\s+')
249 def sanitize_single_line(s
):
250 return _space_regex
.sub(' ', s
)[:255]
252 _gmane_id_regex
= re
.compile(r
'<http://permalink\.gmane\.org/gmane\.comp\.version-control\.git/(\d+)>')
253 def insert_mail_into_db(msg
):
254 if (msg
.get('Message-Id', None)
255 and get_mail_by_id(_parse_msg_id(msg
['Message-Id']))):
256 return # already exists
258 if msg
['Archived-At']:
259 m
= _gmane_id_regex
.match(msg
['Archived-At'])
261 gmane_id
= int(m
.group(1))
262 msgid
= msg
.get('Message-Id', None)
263 if not msgid
or not _parse_msg_id(msgid
):
265 msgid
= 'gmane-%d@mailnotes.trast.ch' % gmane_id
267 msgid
= 'fallback-%X@mailnotes.trast.ch' % random
.randrange(2**32)
269 msgid
= _parse_msg_id(msgid
)
270 mail
= dbcache
.mail_cache
.get(msgid
)
271 mail
.gmane_id
= gmane_id
272 mail
.message_id
= sanitize_single_line(msgid
)
274 mail
.author
= sanitize_single_line(msg
['From'])
277 tm
= email
.utils
.parsedate_tz(msg
['Date'])
279 tm
= email
.utils
.mktime_tz(time
.mktime(tm
))
284 mail
.subject
= sanitize_single_line(msg
['Subject'])
285 mail
.in_reply_to
= sanitize_single_line(_detect_reply_id(msg
))
286 mail
.data
= msg
.as_string()
287 mail
.stale
= mail
.has_patch
= bool(_guess_patch_contents(msg
))
288 if msg
['Subject'] and _whats_cooking_subject
.match(msg
['Subject']):
293 parent
= get_mail_by_id(mail
.in_reply_to
)
296 # Flag all so-far unapplied patches downwards of this one as
297 # 'stale' so they'll be tried again. XXX should use an sql UPDATE
299 for child
in (db
.session
.query(db
.Mail
)
300 .select_from(join(db
.Mail
, db
.Reference
,
301 db
.Mail
.id == db
.Reference
.mail_id
))
302 .filter(db
.Reference
.reference_id
== starter
.message_id
)
303 .filter(db
.Mail
.has_patch
== True)
304 .filter(db
.Mail
.patch_id
== None)
305 .filter(db
.Mail
.message_id
!= mail
.message_id
)
306 .filter(db
.Mail
.stale
== False)):
308 if msg
['References']:
309 for m
in _msg_id_regex
.finditer(' '.join(msg
.get_all('References'))):
310 db
.session
.add(db
.Reference(mail
.id,
311 sanitize_single_line(m
.group(1))))
313 def parse_mbox(fname
):
314 mbox
= mailbox
.mbox(fname
, parser
.parse
)
315 mbox_parsed
= list(mbox
)
316 count
= len(mbox_parsed
)
317 for msg
in mbox_parsed
:
318 insert_mail_into_db(msg
)
320 sys
.stderr
.write("%6d\r" % count
)
321 sys
.stderr
.write("\n")
322 dbcache
.mail_cache
.flush()
324 if __name__
== '__main__':
325 for mbox
in sys
.argv
[1:]: