Mercurial > hg
annotate contrib/patchbomb @ 876:14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Adding patches as attachments makes it difficult or impossible for some
mail clients to quote them effectively.
author | Bryan O'Sullivan <bos@serpentine.com> |
---|---|
date | Tue, 09 Aug 2005 20:53:50 -0800 |
parents | d3f836bf6cc1 |
children | 25430c523677 |
rev | line source |
---|---|
875 | 1 #!/usr/bin/python |
2 # | |
3 # Interactive script for sending a collection of Mercurial changesets | |
4 # as a series of patch emails. | |
5 # | |
6 # The series is started off with a "[PATCH 0 of N]" introduction, | |
7 # which describes the series as a whole. | |
8 # | |
9 # Each patch email has a Subject line of "[PATCH M of N] ...", using | |
10 # the first line of the changeset description as the subject text. | |
11 # The message contains two or three body parts: | |
12 # | |
13 # The remainder of the changeset description. | |
14 # | |
15 # If the diffstat program is installed, the result of running | |
16 # diffstat on the patch. | |
17 # | |
18 # The patch itself, as generated by "hg export". | |
19 # | |
20 # Each message refers to all of its predecessors using the In-Reply-To | |
21 # and References headers, so they will show up as a sequence in | |
22 # threaded mail and news readers, and in mail archives. | |
23 # | |
24 # For each changeset, you will be prompted with a diffstat summary and | |
25 # the changeset summary, so you can be sure you are sending the right | |
26 # changes. | |
27 # | |
28 # It is best to run this script with the "-n" (test only) flag before | |
29 # firing it up "for real", in which case it will display each of the | |
30 # messages that it would send. | |
31 # | |
32 # To configure a default mail host, add a section like this to your | |
33 # hgrc file: | |
34 # | |
35 # [smtp] | |
36 # host = my_mail_host | |
37 # port = 1025 | |
38 | |
39 from email.MIMEMultipart import MIMEMultipart | |
40 from email.MIMEText import MIMEText | |
41 from mercurial import commands | |
42 from mercurial import fancyopts | |
43 from mercurial import hg | |
44 from mercurial import ui | |
45 import os | |
46 import popen2 | |
47 import readline | |
48 import smtplib | |
49 import socket | |
50 import sys | |
51 import tempfile | |
52 import time | |
53 | |
54 def diffstat(patch): | |
55 fd, name = tempfile.mkstemp() | |
56 try: | |
57 p = popen2.Popen3('diffstat -p1 -w79 2>/dev/null > ' + name) | |
58 try: | |
59 for line in patch: print >> p.tochild, line | |
60 p.tochild.close() | |
61 if p.wait(): return | |
62 fp = os.fdopen(fd, 'r') | |
63 stat = [] | |
64 for line in fp: stat.append(line.lstrip()) | |
65 last = stat.pop() | |
66 stat.insert(0, last) | |
67 stat = ''.join(stat) | |
68 if stat.startswith('0 files'): raise ValueError | |
69 return stat | |
70 except: raise | |
71 finally: | |
72 try: os.unlink(name) | |
73 except: pass | |
74 | |
75 def patchbomb(ui, repo, *revs, **opts): | |
76 def prompt(prompt, default = None, rest = ': ', empty_ok = False): | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
77 if default: prompt += ' [%s]' % default |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
78 prompt += rest |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
79 while True: |
875 | 80 r = raw_input(prompt) |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
81 if r: return r |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
82 if default is not None: return default |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
83 if empty_ok: return r |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
84 print >> sys.stderr, 'Please enter a valid value.' |
875 | 85 |
86 def confirm(s): | |
87 if not prompt(s, default = 'y', rest = '? ').lower().startswith('y'): | |
88 raise ValueError | |
89 | |
90 def cdiffstat(summary, patch): | |
91 s = diffstat(patch) | |
92 if s: | |
93 if summary: | |
94 ui.write(summary, '\n') | |
95 ui.write(s, '\n') | |
96 confirm('Does the diffstat above look okay') | |
97 return s | |
98 | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
99 def makepatch(patch, idx, total): |
875 | 100 desc = [] |
101 node = None | |
102 for line in patch: | |
103 if line.startswith('#'): | |
104 if line.startswith('# Node ID'): node = line.split()[-1] | |
105 continue | |
106 if line.startswith('diff -r'): break | |
107 desc.append(line) | |
108 if not node: raise ValueError | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
109 body = ('\n'.join(desc[1:]).strip() or |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
110 'Patch subject is complete summary.') |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
111 body += '\n\n\n' + cdiffstat('\n'.join(desc), patch) + '\n\n' |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
112 body += '\n'.join(patch) |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
113 msg = MIMEText(body) |
875 | 114 subj = '[PATCH %d of %d] %s' % (idx, total, desc[0].strip()) |
115 if subj.endswith('.'): subj = subj[:-1] | |
116 msg['Subject'] = subj | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
117 msg['X-Mercurial-Node'] = node |
875 | 118 return msg |
119 | |
120 start_time = int(time.time()) | |
121 | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
122 def genmsgid(id): |
875 | 123 return '<%s.%s@%s>' % (id[:20], start_time, socket.getfqdn()) |
124 | |
125 patches = [] | |
126 | |
127 class exportee: | |
128 def __init__(self, container): | |
129 self.lines = [] | |
130 self.container = container | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
131 self.name = 'email' |
875 | 132 |
133 def write(self, data): | |
134 self.lines.append(data) | |
135 | |
136 def close(self): | |
137 self.container.append(''.join(self.lines).split('\n')) | |
138 self.lines = [] | |
139 | |
140 commands.export(ui, repo, *args, **{'output': exportee(patches)}) | |
141 | |
142 jumbo = [] | |
143 msgs = [] | |
144 | |
145 ui.write('This patch series consists of %d patches.\n\n' % len(patches)) | |
146 | |
147 for p, i in zip(patches, range(len(patches))): | |
148 jumbo.extend(p) | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
149 msgs.append(makepatch(p, i + 1, len(patches))) |
875 | 150 |
151 ui.write('\nWrite the introductory message for the patch series.\n\n') | |
152 | |
153 sender = opts['sender'] or prompt('From', ui.username()) | |
154 | |
155 msg = MIMEMultipart() | |
156 msg['Subject'] = '[PATCH 0 of %d] %s' % ( | |
157 len(patches), | |
158 prompt('Subject:', rest = ' [PATCH 0 of %d] ' % len(patches))) | |
159 to = opts['to'] or [s.strip() for s in prompt('To').split(',')] | |
160 cc = opts['cc'] or [s.strip() for s in prompt('Cc', default = '').split(',')] | |
161 | |
162 ui.write('Finish with ^D or a dot on a line by itself.\n\n') | |
163 | |
164 body = [] | |
165 | |
166 while True: | |
167 try: l = raw_input() | |
168 except EOFError: break | |
169 if l == '.': break | |
170 body.append(l) | |
171 | |
172 msg.attach(MIMEText('\n'.join(body) + '\n')) | |
173 | |
174 ui.write('\n') | |
175 | |
176 d = cdiffstat('Final summary:\n', jumbo) | |
177 if d: msg.attach(MIMEText(d)) | |
178 | |
179 msgs.insert(0, msg) | |
180 | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
181 if not opts['test']: |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
182 s = smtplib.SMTP() |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
183 s.connect(host = ui.config('smtp', 'host', 'mail'), |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
184 port = int(ui.config('smtp', 'port', 25))) |
875 | 185 |
186 parent = None | |
187 tz = time.strftime('%z') | |
188 for m in msgs: | |
189 try: | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
190 m['Message-Id'] = genmsgid(m['X-Mercurial-Node']) |
875 | 191 except TypeError: |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
192 m['Message-Id'] = genmsgid('patchbomb') |
875 | 193 if parent: |
194 m['In-Reply-To'] = parent | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
195 else: |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
196 parent = m['Message-Id'] |
875 | 197 m['Date'] = time.strftime('%a, %m %b %Y %T ', time.localtime(start_time)) + tz |
198 start_time += 1 | |
199 m['From'] = sender | |
200 m['To'] = ', '.join(to) | |
201 if cc: m['Cc'] = ', '.join(cc) | |
202 ui.status('Sending ', m['Subject'], ' ...\n') | |
203 if opts['test']: | |
204 fp = os.popen(os.getenv('PAGER', 'more'), 'w') | |
205 fp.write(m.as_string(0)) | |
206 fp.write('\n') | |
207 fp.close() | |
208 else: | |
209 s.sendmail(sender, to + cc, m.as_string(0)) | |
876
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
210 if not opts['test']: |
14cfaaec2e8e
Get patchbomb script to not use MIME attachments.
Bryan O'Sullivan <bos@serpentine.com>
parents:
875
diff
changeset
|
211 s.close() |
875 | 212 |
213 if __name__ == '__main__': | |
214 optspec = [('c', 'cc', [], 'email addresses of copy recipients'), | |
215 ('n', 'test', None, 'print messages that would be sent'), | |
216 ('s', 'sender', '', 'email address of sender'), | |
217 ('t', 'to', [], 'email addresses of recipients')] | |
218 options = {} | |
219 try: | |
220 args = fancyopts.fancyopts(sys.argv[1:], commands.globalopts + optspec, | |
221 options) | |
222 except fancyopts.getopt.GetoptError, inst: | |
223 u = ui.ui() | |
224 u.warn('error: %s' % inst) | |
225 sys.exit(1) | |
226 | |
227 u = ui.ui(options["verbose"], options["debug"], options["quiet"], | |
228 not options["noninteractive"]) | |
229 repo = hg.repository(ui = u) | |
230 | |
231 patchbomb(u, repo, *args, **options) |