7a5539c060d7960293b4d0b658c2eb528d32f911
[patchq.git] / inject-mbox.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Inject one or more mbox of email, or a single email into the system.
5 #
6 # ./inject-mbox.py mbox [mbox ...]
7 # ./inject-mbox.py < single_email
8 #
9 # Note that if you inject the same message or message threads twice,
10 # then they are processed twice.  patchq does not deduplicate.
11 #
12 # This script will inject anything that looks similar enough to an
13 # email, even non-patches, cover letters, etc.  The ‘threader.py’
14 # script filters out non-patches.
15
16 import email
17 import mailbox
18 import pika
19 import sys
20 import config
21
22 connection = pika.BlockingConnection(pika.ConnectionParameters(
23     host = config.mq_server))
24 channel = connection.channel()
25
26 processed = 0
27
28 def inject(m):
29     global processed
30
31     print("Injecting %s" % m['Subject'])
32
33     channel.basic_publish(exchange = 'patchq_input',
34                           routing_key = '',
35                           body = m.as_string())
36     processed = processed+1
37
38 # Read from mboxes passed on the command line, or read a single
39 # email from stdin.
40 if len(sys.argv) > 1:
41     for arg in sys.argv[1:]:
42         mbox = mailbox.mbox(arg)
43         for m in mbox:
44             inject(m)
45 else:
46     m = email.message_from_file(sys.stdin)
47     inject(m)
48
49 print ("Processed %d email(s)." % processed)