Mercurial > p > roundup > code
comparison scripts/imapServer.py @ 7053:b5fffd2a64af
issue2551195: port scripts to argparse
| author | Ralf Schlatterbeck <rsc@runtux.com> |
|---|---|
| date | Tue, 22 Nov 2022 14:29:42 +0100 |
| parents | 4cf48ff01e04 |
| children | fed0f839c260 |
comparison
equal
deleted
inserted
replaced
| 7052:4b6a6b794dfa | 7053:b5fffd2a64af |
|---|---|
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python3 |
| 2 """\ | 2 """\ |
| 3 This script is a wrapper around the mailgw.py script that exists in roundup. | 3 This script is a wrapper around the mailgw.py script that exists in roundup. |
| 4 It runs as service instead of running as a one-time shot. | 4 It runs as service instead of running as a one-time shot. |
| 5 It also connects to a secure IMAP server. The main reasons for this script are: | 5 It also connects to a secure IMAP server. The main reasons for this script are: |
| 6 | 6 |
| 32 | 32 |
| 33 from __future__ import print_function | 33 from __future__ import print_function |
| 34 import getpass | 34 import getpass |
| 35 import logging | 35 import logging |
| 36 import imaplib | 36 import imaplib |
| 37 import optparse | 37 import argparse |
| 38 import os | 38 import os |
| 39 import re | 39 import re |
| 40 import time | 40 import time |
| 41 import sys | |
| 41 | 42 |
| 42 from roundup.anypy.my_input import my_input | 43 from roundup.anypy.my_input import my_input |
| 43 | 44 |
| 44 logging.basicConfig() | 45 logging.basicConfig() |
| 45 log = logging.getLogger('roundup.IMAPServer') | 46 log = logging.getLogger('roundup.IMAPServer') |
| 46 | 47 |
| 47 version = '0.1.2' | 48 version = '0.1.3' |
| 48 | 49 |
| 49 class RoundupMailbox: | 50 class RoundupMailbox: |
| 50 """This contains all the info about each mailbox. | 51 """This contains all the info about each mailbox. |
| 51 Username, Password, server, security, roundup database | 52 Username, Password, server, security, roundup database |
| 52 """ | 53 """ |
| 299 else: | 300 else: |
| 300 return None | 301 return None |
| 301 | 302 |
| 302 def main(): | 303 def main(): |
| 303 """This is what is called if run at the prompt""" | 304 """This is what is called if run at the prompt""" |
| 304 parser = optparse.OptionParser( | 305 parser = argparse.ArgumentParser( |
| 305 version=('%prog ' + version), | 306 formatter_class=argparse.RawDescriptionHelpFormatter, |
| 306 usage="""usage: %prog [options] (home server)... | 307 epilog=""" |
| 307 | 308 |
| 309 The server takes pairs of home/server. | |
| 308 So each entry has a home, and then the server configuration. Home is just | 310 So each entry has a home, and then the server configuration. Home is just |
| 309 a path to the roundup issue tracker. The server is something of the form: | 311 a path to the roundup issue tracker. The server is something of the form: |
| 310 | 312 |
| 311 imaps://user:password@server/mailbox | 313 imaps://user:password@server/mailbox |
| 312 | 314 |
| 313 If you don't supply the protocol, imaps is assumed. Without user or | 315 If you don't supply the protocol, imaps is assumed. Without user or |
| 314 password, you will be prompted for them. The server must be supplied. | 316 password, you will be prompted for them. The server must be supplied. |
| 315 Without mailbox the INBOX is used. | 317 Without mailbox the INBOX is used. |
| 316 | 318 |
| 317 Examples: | 319 Examples: |
| 318 %prog /home/roundup/trackers/test imaps://test@imap.example.com/test | 320 %(prog)s /home/roundup/trackers/test imaps://test@imap.example.com/test |
| 319 %prog /home/roundup/trackers/test imap.example.com \ | 321 %(prog)s /home/roundup/trackers/test imap.example.com \\ |
| 320 /home/roundup/trackers/test2 imap.example.com/test2 | 322 /home/roundup/trackers/test2 imap.example.com/test2 |
| 321 """ | 323 """ % dict(prog = sys.argv [0]) |
| 322 ) | 324 ) |
| 323 parser.add_option('-d', '--delay', dest='delay', type='float', | 325 parser.add_argument('args', nargs='*') |
| 326 parser.add_argument('-d', '--delay', dest='delay', type=float, | |
| 324 metavar='<sec>', default=5, | 327 metavar='<sec>', default=5, |
| 325 help="Set the delay between checks in minutes. (default 5)" | 328 help="Set the delay between checks in minutes. (default 5)" |
| 326 ) | 329 ) |
| 327 parser.add_option('-p', '--pid-file', dest='pidfile', | 330 parser.add_argument('-p', '--pid-file', dest='pidfile', |
| 328 metavar='<file>', default=None, | 331 metavar='<file>', default=None, |
| 329 help="The pid of the server process will be written to <file>" | 332 help="The pid of the server process will be written to <file>" |
| 330 ) | 333 ) |
| 331 parser.add_option('-n', '--no-daemon', dest='daemon', | 334 parser.add_argument('-n', '--no-daemon', dest='daemon', |
| 332 action='store_false', default=True, | 335 action='store_false', default=True, |
| 333 help="Do not fork into the background after running the first check." | 336 help="Do not fork into the background after running the first check." |
| 334 ) | 337 ) |
| 335 parser.add_option('-v', '--verbose', dest='verbose', | 338 parser.add_argument('--version', action="store_true", |
| 339 help="Print version and exit") | |
| 340 parser.add_argument('-v', '--verbose', dest='verbose', | |
| 336 action='store_const', const=logging.INFO, | 341 action='store_const', const=logging.INFO, |
| 337 help="Be more verbose in letting you know what is going on." | 342 help="Be more verbose in letting you know what is going on." |
| 338 " Enables informational messages." | 343 " Enables informational messages." |
| 339 ) | 344 ) |
| 340 parser.add_option('-V', '--very-verbose', dest='verbose', | 345 parser.add_argument('-V', '--very-verbose', dest='verbose', |
| 341 action='store_const', const=logging.DEBUG, | 346 action='store_const', const=logging.DEBUG, |
| 342 help="Be very verbose in letting you know what is going on." | 347 help="Be very verbose in letting you know what is going on." |
| 343 " Enables debugging messages." | 348 " Enables debugging messages." |
| 344 ) | 349 ) |
| 345 parser.add_option('-q', '--quiet', dest='verbose', | 350 parser.add_argument('-q', '--quiet', dest='verbose', |
| 346 action='store_const', const=logging.ERROR, | 351 action='store_const', const=logging.ERROR, |
| 347 help="Be less verbose. Ignores warnings, only prints errors." | 352 help="Be less verbose. Ignores warnings, only prints errors." |
| 348 ) | 353 ) |
| 349 parser.add_option('-Q', '--very-quiet', dest='verbose', | 354 parser.add_argument('-Q', '--very-quiet', dest='verbose', |
| 350 action='store_const', const=logging.CRITICAL, | 355 action='store_const', const=logging.CRITICAL, |
| 351 help="Be much less verbose. Ignores warnings and errors." | 356 help="Be much less verbose. Ignores warnings and errors." |
| 352 " Only print CRITICAL messages." | 357 " Only print CRITICAL messages." |
| 353 ) | 358 ) |
| 354 | 359 |
| 355 (opts, args) = parser.parse_args() | 360 args = parser.parse_args() |
| 356 if (len(args) == 0) or (len(args) % 2 == 1): | 361 if args.version: |
| 362 print('%s %s' % (sys.argv [0], version)) | |
| 363 sys.exit(0) | |
| 364 if not len(args.args) or len(args.args) % 2 == 1: | |
| 357 parser.error('Invalid number of arguments. ' | 365 parser.error('Invalid number of arguments. ' |
| 358 'Each site needs a home and a server.') | 366 'Each site needs a home and a server.') |
| 359 | 367 |
| 360 if opts.verbose == None: | 368 if args.verbose == None: |
| 361 opts.verbose = logging.WARNING | 369 args.verbose = logging.WARNING |
| 362 | 370 |
| 363 log.setLevel(opts.verbose) | 371 log.setLevel(args.verbose) |
| 364 myServer = IMAPServer(delay=opts.delay, pidfile=opts.pidfile, | 372 myServer = IMAPServer(delay=args.delay, pidfile=args.pidfile, |
| 365 daemon=opts.daemon) | 373 daemon=args.daemon) |
| 366 for i in range(0,len(args),2): | 374 for i in range(0,len(args.args),2): |
| 367 home = args[i] | 375 home = args.args[i] |
| 368 server = args[i+1] | 376 server = args.args[i+1] |
| 369 if not os.path.exists(home): | 377 if not os.path.exists(home): |
| 370 parser.error('Home: "%s" does not exist' % home) | 378 parser.error('Home: "%s" does not exist' % home) |
| 371 | 379 |
| 372 info = getItems(server) | 380 info = getItems(server) |
| 373 if not info: | 381 if not info: |
