| 1 | __copyright__ = """ |
| 2 | Copyright (C) 2005, Catalin Marinas <catalin.marinas@gmail.com> |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License version 2 as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 16 | """ |
| 17 | |
| 18 | import sys, os, re, time, smtplib, email.Utils |
| 19 | from optparse import OptionParser, make_option |
| 20 | from time import gmtime, strftime |
| 21 | |
| 22 | from stgit.commands.common import * |
| 23 | from stgit.utils import * |
| 24 | from stgit import stack, git |
| 25 | from stgit.config import config |
| 26 | |
| 27 | |
| 28 | help = 'send a patch or series of patches by e-mail' |
| 29 | usage = """%prog [options] [<patch> [<patch2...]] |
| 30 | |
| 31 | Send a patch or a range of patches (defaulting to the applied patches) |
| 32 | by e-mail using the 'smtpserver' configuration option. The From |
| 33 | address and the e-mail format are generated from the template file |
| 34 | passed as argument to '--template' (defaulting to .git/patchmail.tmpl |
| 35 | or /usr/share/stgit/templates/patchmail.tmpl). The To/Cc/Bcc addresses |
| 36 | can either be added to the template file or passed via the |
| 37 | corresponding command line options. |
| 38 | |
| 39 | A preamble e-mail can be sent using the '--first' option. All the |
| 40 | subsequent e-mails appear as replies to the first e-mail sent (either |
| 41 | the preamble or the first patch). E-mails can be seen as replies to a |
| 42 | different e-mail by using the '--refid' option. |
| 43 | |
| 44 | SMTP authentication is also possible with '--smtp-user' and |
| 45 | '--smtp-password' options, also available as configuration settings: |
| 46 | 'smtpuser' and 'smtppassword'. |
| 47 | |
| 48 | The template e-mail headers and body must be separated by |
| 49 | '%(endofheaders)s' variable, which is replaced by StGIT with |
| 50 | additional headers and a blank line. The patch e-mail template accepts |
| 51 | the following variables: |
| 52 | |
| 53 | %(patch)s - patch name |
| 54 | %(maintainer)s - 'authname <authemail>' as read from the config file |
| 55 | %(shortdescr)s - the first line of the patch description |
| 56 | %(longdescr)s - the rest of the patch description, after the first line |
| 57 | %(endofheaders)s - delimiter between e-mail headers and body |
| 58 | %(diff)s - unified diff of the patch |
| 59 | %(diffstat)s - diff statistics |
| 60 | %(date)s - current date/time |
| 61 | %(version)s - ' version' string passed on the command line (or empty) |
| 62 | %(patchnr)s - patch number |
| 63 | %(totalnr)s - total number of patches to be sent |
| 64 | %(number)s - empty if only one patch is sent or ' patchnr/totalnr' |
| 65 | %(authname)s - author's name |
| 66 | %(authemail)s - author's email |
| 67 | %(authdate)s - patch creation date |
| 68 | %(commname)s - committer's name |
| 69 | %(commemail)s - committer's e-mail |
| 70 | |
| 71 | For the preamble e-mail template, only the %(maintainer)s, %(date)s, |
| 72 | %(endofheaders)s, %(version)s, %(patchnr)s, %(totalnr)s and %(number)s |
| 73 | variables are supported.""" |
| 74 | |
| 75 | options = [make_option('-a', '--all', |
| 76 | help = 'e-mail all the applied patches', |
| 77 | action = 'store_true'), |
| 78 | make_option('-r', '--range', |
| 79 | metavar = '[PATCH1][:[PATCH2]]', |
| 80 | help = 'e-mail patches between PATCH1 and PATCH2'), |
| 81 | make_option('--to', |
| 82 | help = 'add TO to the To: list', |
| 83 | action = 'append'), |
| 84 | make_option('--cc', |
| 85 | help = 'add CC to the Cc: list', |
| 86 | action = 'append'), |
| 87 | make_option('--bcc', |
| 88 | help = 'add BCC to the Bcc: list', |
| 89 | action = 'append'), |
| 90 | make_option('-v', '--version', metavar = 'VERSION', |
| 91 | help = 'add VERSION to the [PATCH ...] prefix'), |
| 92 | make_option('-t', '--template', metavar = 'FILE', |
| 93 | help = 'use FILE as the message template'), |
| 94 | make_option('-f', '--first', metavar = 'FILE', |
| 95 | help = 'send FILE as the first message'), |
| 96 | make_option('-s', '--sleep', type = 'int', metavar = 'SECONDS', |
| 97 | help = 'sleep for SECONDS between e-mails sending'), |
| 98 | make_option('--refid', |
| 99 | help = 'use REFID as the reference id'), |
| 100 | make_option('-u', '--smtp-user', metavar = 'USER', |
| 101 | help = 'username for SMTP authentication'), |
| 102 | make_option('-p', '--smtp-password', metavar = 'PASSWORD', |
| 103 | help = 'username for SMTP authentication'), |
| 104 | make_option('-b', '--branch', |
| 105 | help = 'use BRANCH instead of the default one')] |
| 106 | |
| 107 | |
| 108 | def __get_maintainer(): |
| 109 | """Return the 'authname <authemail>' string as read from the |
| 110 | configuration file |
| 111 | """ |
| 112 | if config.has_option('stgit', 'authname') \ |
| 113 | and config.has_option('stgit', 'authemail'): |
| 114 | return '%s <%s>' % (config.get('stgit', 'authname'), |
| 115 | config.get('stgit', 'authemail')) |
| 116 | else: |
| 117 | return None |
| 118 | |
| 119 | def __parse_addresses(string): |
| 120 | """Return a two elements tuple: (from, [to]) |
| 121 | """ |
| 122 | def __addr_list(string): |
| 123 | m = re.search('[^@\s<,]+@[^>\s,]+', string); |
| 124 | if (m == None): |
| 125 | return [] |
| 126 | return [ m.group() ] + __addr_list(string[m.end():]) |
| 127 | |
| 128 | from_addr_list = [] |
| 129 | to_addr_list = [] |
| 130 | for line in string.split('\n'): |
| 131 | if re.match('from:\s+', line, re.I): |
| 132 | from_addr_list += __addr_list(line) |
| 133 | elif re.match('(to|cc|bcc):\s+', line, re.I): |
| 134 | to_addr_list += __addr_list(line) |
| 135 | |
| 136 | if len(from_addr_list) == 0: |
| 137 | raise CmdException, 'No "From" address' |
| 138 | if len(to_addr_list) == 0: |
| 139 | raise CmdException, 'No "To/Cc/Bcc" addresses' |
| 140 | |
| 141 | return (from_addr_list[0], to_addr_list) |
| 142 | |
| 143 | def __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
| 144 | smtpuser, smtppassword): |
| 145 | """Send the message using the given SMTP server |
| 146 | """ |
| 147 | try: |
| 148 | s = smtplib.SMTP(smtpserver) |
| 149 | except Exception, err: |
| 150 | raise CmdException, str(err) |
| 151 | |
| 152 | s.set_debuglevel(0) |
| 153 | try: |
| 154 | if smtpuser and smtppassword: |
| 155 | s.ehlo() |
| 156 | s.login(smtpuser, smtppassword) |
| 157 | |
| 158 | s.sendmail(from_addr, to_addr_list, msg) |
| 159 | # give recipients a chance of receiving patches in the correct order |
| 160 | time.sleep(sleep) |
| 161 | except Exception, err: |
| 162 | raise CmdException, str(err) |
| 163 | |
| 164 | s.quit() |
| 165 | |
| 166 | def __build_address_headers(options): |
| 167 | headers_end = '' |
| 168 | if options.to: |
| 169 | headers_end += 'To: ' |
| 170 | for to in options.to: |
| 171 | headers_end += '%s,' % to |
| 172 | headers_end = headers_end[:-1] + '\n' |
| 173 | if options.cc: |
| 174 | headers_end += 'Cc: ' |
| 175 | for cc in options.cc: |
| 176 | headers_end += '%s,' % cc |
| 177 | headers_end = headers_end[:-1] + '\n' |
| 178 | if options.bcc: |
| 179 | headers_end += 'Bcc: ' |
| 180 | for bcc in options.bcc: |
| 181 | headers_end += '%s,' % bcc |
| 182 | headers_end = headers_end[:-1] + '\n' |
| 183 | return headers_end |
| 184 | |
| 185 | def __build_first(tmpl, total_nr, msg_id, options): |
| 186 | """Build the first message (series description) to be sent via SMTP |
| 187 | """ |
| 188 | maintainer = __get_maintainer() |
| 189 | if not maintainer: |
| 190 | maintainer = '' |
| 191 | |
| 192 | headers_end = __build_address_headers(options) |
| 193 | headers_end += 'Message-Id: %s\n' % msg_id |
| 194 | |
| 195 | if options.version: |
| 196 | version_str = ' %s' % options.version |
| 197 | else: |
| 198 | version_str = '' |
| 199 | |
| 200 | total_nr_str = str(total_nr) |
| 201 | patch_nr_str = '0'.zfill(len(total_nr_str)) |
| 202 | if total_nr > 1: |
| 203 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
| 204 | else: |
| 205 | number_str = '' |
| 206 | |
| 207 | tmpl_dict = {'maintainer': maintainer, |
| 208 | 'endofheaders': headers_end, |
| 209 | 'date': email.Utils.formatdate(localtime = True), |
| 210 | 'version': version_str, |
| 211 | 'patchnr': patch_nr_str, |
| 212 | 'totalnr': total_nr_str, |
| 213 | 'number': number_str} |
| 214 | |
| 215 | try: |
| 216 | msg = tmpl % tmpl_dict |
| 217 | except KeyError, err: |
| 218 | raise CmdException, 'Unknown patch template variable: %s' \ |
| 219 | % err |
| 220 | except TypeError: |
| 221 | raise CmdException, 'Only "%(name)s" variables are ' \ |
| 222 | 'supported in the patch template' |
| 223 | |
| 224 | return msg |
| 225 | |
| 226 | def __build_message(tmpl, patch, patch_nr, total_nr, msg_id, ref_id, options): |
| 227 | """Build the message to be sent via SMTP |
| 228 | """ |
| 229 | p = crt_series.get_patch(patch) |
| 230 | |
| 231 | descr = p.get_description().strip() |
| 232 | descr_lines = descr.split('\n') |
| 233 | |
| 234 | short_descr = descr_lines[0].rstrip() |
| 235 | long_descr = reduce(lambda x, y: x + '\n' + y, |
| 236 | descr_lines[1:], '').lstrip() |
| 237 | |
| 238 | maintainer = __get_maintainer() |
| 239 | if not maintainer: |
| 240 | maintainer = '%s <%s>' % (p.get_commname(), p.get_commemail()) |
| 241 | |
| 242 | headers_end = __build_address_headers(options) |
| 243 | headers_end += 'Message-Id: %s\n' % msg_id |
| 244 | if ref_id: |
| 245 | headers_end += "In-Reply-To: %s\n" % ref_id |
| 246 | headers_end += "References: %s\n" % ref_id |
| 247 | |
| 248 | if options.version: |
| 249 | version_str = ' %s' % options.version |
| 250 | else: |
| 251 | version_str = '' |
| 252 | |
| 253 | total_nr_str = str(total_nr) |
| 254 | patch_nr_str = str(patch_nr).zfill(len(total_nr_str)) |
| 255 | if total_nr > 1: |
| 256 | number_str = ' %s/%s' % (patch_nr_str, total_nr_str) |
| 257 | else: |
| 258 | number_str = '' |
| 259 | |
| 260 | tmpl_dict = {'patch': patch, |
| 261 | 'maintainer': maintainer, |
| 262 | 'shortdescr': short_descr, |
| 263 | 'longdescr': long_descr, |
| 264 | 'endofheaders': headers_end, |
| 265 | 'diff': git.diff(rev1 = git_id('%s/bottom' % patch), |
| 266 | rev2 = git_id('%s/top' % patch)), |
| 267 | 'diffstat': git.diffstat(rev1 = git_id('%s/bottom'%patch), |
| 268 | rev2 = git_id('%s/top' % patch)), |
| 269 | 'date': email.Utils.formatdate(localtime = True), |
| 270 | 'version': version_str, |
| 271 | 'patchnr': patch_nr_str, |
| 272 | 'totalnr': total_nr_str, |
| 273 | 'number': number_str, |
| 274 | 'authname': p.get_authname(), |
| 275 | 'authemail': p.get_authemail(), |
| 276 | 'authdate': p.get_authdate(), |
| 277 | 'commname': p.get_commname(), |
| 278 | 'commemail': p.get_commemail()} |
| 279 | for key in tmpl_dict: |
| 280 | if not tmpl_dict[key]: |
| 281 | tmpl_dict[key] = '' |
| 282 | |
| 283 | try: |
| 284 | msg = tmpl % tmpl_dict |
| 285 | except KeyError, err: |
| 286 | raise CmdException, 'Unknown patch template variable: %s' \ |
| 287 | % err |
| 288 | except TypeError: |
| 289 | raise CmdException, 'Only "%(name)s" variables are ' \ |
| 290 | 'supported in the patch template' |
| 291 | |
| 292 | return msg |
| 293 | |
| 294 | def func(parser, options, args): |
| 295 | """Send the patches by e-mail using the patchmail.tmpl file as |
| 296 | a template |
| 297 | """ |
| 298 | if not config.has_option('stgit', 'smtpserver'): |
| 299 | raise CmdException, 'smtpserver not defined' |
| 300 | smtpserver = config.get('stgit', 'smtpserver') |
| 301 | |
| 302 | smtpuser = None |
| 303 | smtppassword = None |
| 304 | if config.has_option('stgit', 'smtpuser'): |
| 305 | smtpuser = config.get('stgit', 'smtpuser') |
| 306 | if config.has_option('stgit', 'smtppassword'): |
| 307 | smtppassword = config.get('stgit', 'smtppassword') |
| 308 | |
| 309 | applied = crt_series.get_applied() |
| 310 | |
| 311 | if len(args) >= 1: |
| 312 | for patch in args: |
| 313 | if not patch in applied: |
| 314 | raise CmdException, 'Patch "%s" not applied' % patch |
| 315 | patches = args |
| 316 | elif options.all: |
| 317 | patches = applied |
| 318 | elif options.range: |
| 319 | boundaries = options.range.split(':') |
| 320 | if len(boundaries) == 1: |
| 321 | start = boundaries[0] |
| 322 | stop = boundaries[0] |
| 323 | elif len(boundaries) == 2: |
| 324 | if boundaries[0] == '': |
| 325 | start = applied[0] |
| 326 | else: |
| 327 | start = boundaries[0] |
| 328 | if boundaries[1] == '': |
| 329 | stop = applied[-1] |
| 330 | else: |
| 331 | stop = boundaries[1] |
| 332 | else: |
| 333 | raise CmdException, 'incorrect parameters to "--range"' |
| 334 | |
| 335 | if start in applied: |
| 336 | start_idx = applied.index(start) |
| 337 | else: |
| 338 | raise CmdException, 'Patch "%s" not applied' % start |
| 339 | if stop in applied: |
| 340 | stop_idx = applied.index(stop) + 1 |
| 341 | else: |
| 342 | raise CmdException, 'Patch "%s" not applied' % stop |
| 343 | |
| 344 | if start_idx >= stop_idx: |
| 345 | raise CmdException, 'Incorrect patch range order' |
| 346 | |
| 347 | patches = applied[start_idx:stop_idx] |
| 348 | else: |
| 349 | raise CmdException, 'Incorrect options. Unknown patches to send' |
| 350 | |
| 351 | if options.smtp_password: |
| 352 | smtppassword = options.smtp_password |
| 353 | |
| 354 | if options.smtp_user: |
| 355 | smtpuser = options.smtp_user |
| 356 | |
| 357 | if (smtppassword and not smtpuser): |
| 358 | raise CmdException, 'SMTP password supplied, username needed' |
| 359 | if (smtpuser and not smtppassword): |
| 360 | raise CmdException, 'SMTP username supplied, password needed' |
| 361 | |
| 362 | total_nr = len(patches) |
| 363 | if total_nr == 0: |
| 364 | raise CmdException, 'No patches to send' |
| 365 | |
| 366 | ref_id = options.refid |
| 367 | |
| 368 | if options.sleep != None: |
| 369 | sleep = options.sleep |
| 370 | else: |
| 371 | sleep = 2 |
| 372 | |
| 373 | # send the first message (if any) |
| 374 | if options.first: |
| 375 | tmpl = file(options.first).read() |
| 376 | |
| 377 | msg_id = email.Utils.make_msgid('stgit') |
| 378 | msg = __build_first(tmpl, total_nr, msg_id, options) |
| 379 | from_addr, to_addr_list = __parse_addresses(msg) |
| 380 | |
| 381 | # subsequent e-mails are seen as replies to the first one |
| 382 | ref_id = msg_id |
| 383 | |
| 384 | print 'Sending file "%s"...' % options.first, |
| 385 | sys.stdout.flush() |
| 386 | |
| 387 | __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
| 388 | smtpuser, smtppassword) |
| 389 | |
| 390 | print 'done' |
| 391 | |
| 392 | # send the patches |
| 393 | if options.template: |
| 394 | tfile_list = [options.template] |
| 395 | else: |
| 396 | tfile_list = [] |
| 397 | |
| 398 | tfile_list += [os.path.join(git.base_dir, 'patchmail.tmpl'), |
| 399 | os.path.join(sys.prefix, |
| 400 | 'share/stgit/templates/patchmail.tmpl')] |
| 401 | tmpl = None |
| 402 | for tfile in tfile_list: |
| 403 | if os.path.isfile(tfile): |
| 404 | tmpl = file(tfile).read() |
| 405 | break |
| 406 | if not tmpl: |
| 407 | raise CmdException, 'No e-mail template file: %s or %s' \ |
| 408 | % (tfile_list[-1], tfile_list[-2]) |
| 409 | |
| 410 | for (p, patch_nr) in zip(patches, range(1, len(patches) + 1)): |
| 411 | msg_id = email.Utils.make_msgid('stgit') |
| 412 | msg = __build_message(tmpl, p, patch_nr, total_nr, msg_id, ref_id, |
| 413 | options) |
| 414 | from_addr, to_addr_list = __parse_addresses(msg) |
| 415 | |
| 416 | # subsequent e-mails are seen as replies to the first one |
| 417 | if not ref_id: |
| 418 | ref_id = msg_id |
| 419 | |
| 420 | print 'Sending patch "%s"...' % p, |
| 421 | sys.stdout.flush() |
| 422 | |
| 423 | __send_message(smtpserver, from_addr, to_addr_list, msg, sleep, |
| 424 | smtpuser, smtppassword) |
| 425 | |
| 426 | print 'done' |