Mercurial > p > roundup > code
comparison roundup/scripts/roundup_mailgw.py @ 6041:c7a9f9c1801d
Flake8 whitespace fixes, remove obsolete version check.
Removed obsolete 2.4 version check. 2.7 or newer is required.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Tue, 07 Jan 2020 21:32:34 -0500 |
| parents | 55f09ca366c4 |
| children | 3359dc1dabb0 |
comparison
equal
deleted
inserted
replaced
| 6040:d5c51d1ef09c | 6041:c7a9f9c1801d |
|---|---|
| 40 import sys, os, re, getopt, socket, netrc | 40 import sys, os, re, getopt, socket, netrc |
| 41 | 41 |
| 42 from roundup import mailgw | 42 from roundup import mailgw |
| 43 from roundup.i18n import _ | 43 from roundup.i18n import _ |
| 44 | 44 |
| 45 | |
| 45 def usage(args, message=None): | 46 def usage(args, message=None): |
| 46 if message is not None: | 47 if message is not None: |
| 47 print(message) | 48 print(message) |
| 48 print(_( | 49 print(_( |
| 49 """Usage: %(program)s [-v] [-c class] [[-C class] -S field=value]* [instance home] [mail source [specification]] | 50 """Usage: %(program)s [-v] [-c class] [[-C class] -S field=value]* [instance home] [mail source [specification]] |
| 50 | 51 |
| 51 Options: | 52 Options: |
| 52 -v: print version and exit | 53 -v: print version and exit |
| 53 -c: default class of item to create (else the tracker's MAIL_DEFAULT_CLASS) | 54 -c: default class of item to create (else the tracker's MAIL_DEFAULT_CLASS) |
| 54 -C / -S: see below | 55 -C / -S: see below |
| 122 IMAPS_CRAM: | 123 IMAPS_CRAM: |
| 123 Connect to an IMAP server over ssl using CRAM-MD5 authentication. | 124 Connect to an IMAP server over ssl using CRAM-MD5 authentication. |
| 124 This supports the same notation as IMAP. | 125 This supports the same notation as IMAP. |
| 125 imaps_cram username:password@server [mailbox] | 126 imaps_cram username:password@server [mailbox] |
| 126 | 127 |
| 127 """)%{'program': args[0]}) | 128 """) % {'program': args[0]}) |
| 128 return 1 | 129 return 1 |
| 130 | |
| 129 | 131 |
| 130 def main(argv): | 132 def main(argv): |
| 131 '''Handle the arguments to the program and initialise environment. | 133 '''Handle the arguments to the program and initialise environment. |
| 132 ''' | 134 ''' |
| 133 # take the argv array and parse it leaving the non-option | 135 # take the argv array and parse it leaving the non-option |
| 134 # arguments in the args array. | 136 # arguments in the args array. |
| 135 try: | 137 try: |
| 136 optionsList, args = getopt.getopt(argv[1:], 'vc:C:S:', ['set=', | 138 optionsList, args = getopt.getopt(argv[1:], 'vc:C:S:', ['set=', |
| 137 'class=']) | 139 'class=']) |
| 138 except getopt.GetoptError: | 140 except getopt.GetoptError: |
| 139 # print help information and exit: | 141 # print help information and exit: |
| 140 usage(argv) | 142 usage(argv) |
| 141 sys.exit(2) | 143 sys.exit(2) |
| 142 | 144 |
| 143 for (opt, arg) in optionsList: | 145 for (opt, _arg) in optionsList: |
| 144 if opt == '-v': | 146 if opt == '-v': |
| 145 print('%s (python %s)'%(roundup_version, sys.version.split()[0])) | 147 print('%s (python %s)' % (roundup_version, sys.version.split()[0])) |
| 146 return | 148 return |
| 147 | 149 |
| 148 # figure the instance home | 150 # figure the instance home |
| 149 if len(args) > 0: | 151 if len(args) > 0: |
| 150 instance_home = args[0] | 152 instance_home = args[0] |
| 166 if len(args) == 1: | 168 if len(args) == 1: |
| 167 return handler.do_pipe() | 169 return handler.do_pipe() |
| 168 | 170 |
| 169 # otherwise, figure what sort of mail source to handle | 171 # otherwise, figure what sort of mail source to handle |
| 170 if len(args) < 3: | 172 if len(args) < 3: |
| 171 return usage(argv, _('Error: not enough source specification information')) | 173 return usage(argv, _( |
| 174 'Error: not enough source specification information')) | |
| 172 source, specification = args[1:3] | 175 source, specification = args[1:3] |
| 173 | 176 |
| 174 # time out net connections after a minute if we can | 177 # time out net connections after a minute if we can |
| 175 if source not in ('mailbox', 'imaps', 'imaps_cram'): | 178 if source not in ('mailbox', 'imaps', 'imaps_cram'): |
| 176 if hasattr(socket, 'setdefaulttimeout'): | 179 if hasattr(socket, 'setdefaulttimeout'): |
| 200 return usage(argv, _('Error: %s specification not valid') % source) | 203 return usage(argv, _('Error: %s specification not valid') % source) |
| 201 | 204 |
| 202 # now invoke the mailgw handler depending on the server handler requested | 205 # now invoke the mailgw handler depending on the server handler requested |
| 203 if source.startswith('pop'): | 206 if source.startswith('pop'): |
| 204 ssl = source.endswith('s') | 207 ssl = source.endswith('s') |
| 205 if ssl and sys.version_info<(2,4): | |
| 206 return usage(argv, _('Error: a later version of python is required')) | |
| 207 return handler.do_pop(server, username, password, ssl) | 208 return handler.do_pop(server, username, password, ssl) |
| 208 elif source == 'apop': | 209 elif source == 'apop': |
| 209 return handler.do_apop(server, username, password) | 210 return handler.do_apop(server, username, password) |
| 210 elif source.startswith('imap'): | 211 elif source.startswith('imap'): |
| 211 ssl = cram = 0 | 212 ssl = cram = 0 |
| 215 ssl = cram = 1 | 216 ssl = cram = 1 |
| 216 mailbox = '' | 217 mailbox = '' |
| 217 if len(args) > 3: | 218 if len(args) > 3: |
| 218 mailbox = args[3] | 219 mailbox = args[3] |
| 219 return handler.do_imap(server, username, password, mailbox, ssl, | 220 return handler.do_imap(server, username, password, mailbox, ssl, |
| 220 cram) | 221 cram) |
| 221 | 222 |
| 222 return usage(argv, _('Error: The source must be either "mailbox",' | 223 return usage(argv, _('Error: The source must be either "mailbox",' |
| 223 ' "pop", "pops", "apop", "imap", "imaps" or "imaps_cram')) | 224 ' "pop", "pops", "apop", "imap", "imaps" or' |
| 225 ' "imaps_cram')) | |
| 226 | |
| 224 | 227 |
| 225 def run(): | 228 def run(): |
| 226 sys.exit(main(sys.argv)) | 229 sys.exit(main(sys.argv)) |
| 230 | |
| 227 | 231 |
| 228 # call main | 232 # call main |
| 229 if __name__ == '__main__': | 233 if __name__ == '__main__': |
| 230 run() | 234 run() |
| 231 | 235 |
