2 # -*- coding: utf-8 -*-
4 # Inject one or more mbox of email, or a single email into the system.
6 # ./inject-mbox.py mbox [mbox ...]
7 # ./inject-mbox.py < single_email
9 # Note that if you inject the same message or message threads twice,
10 # then they are processed twice. patchq does not deduplicate.
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.
22 connection = pika.BlockingConnection(pika.ConnectionParameters(
23 host = config.mq_server))
24 channel = connection.channel()
31 # Decode the subject line and store it back in the email as UTF-8.
32 # This saves a lot of effort later on, even though it's not
33 # strictly RFC822 compliant.
34 # https://stackoverflow.com/questions/7331351/python-email-header-decoding-utf-8/7331577#7331577
36 subj = email.header.decode_header(subj)
37 subj = ''.join([ unicode(t[0], t[1] or 'ASCII') for t in subj ])
40 print("Injecting %s" % m['Subject'])
42 channel.basic_publish(exchange = 'patchq_input',
45 processed = processed+1
47 # Read from mboxes passed on the command line, or read a single
50 for arg in sys.argv[1:]:
51 mbox = mailbox.mbox(arg)
55 m = email.message_from_file(sys.stdin)
58 print ("Processed %d email(s)." % processed)