def test_print_help(self, stdout_mock): val = """Usage: blackhole [OPTIONS] (start|stop|status)\n\n -v, """\ """--version Print out program version\n"""\ """ -h, --help Show this help"""\ """ information\n\nBlackhole\n---------\n\n --conf=FILE"""\ """ Config file to parse and use."""\ """ Overrides command line args\n --group=GROUP"""\ """ Group to drop privs to during run"""\ """ time\n --host=IP IP address to"""\ """ bind go\n --log=FILE """\ """File to write logs to (not very verbose)\n """\ """--message_size_limit=BYTES Maximum size of a message"""\ """ in Bytes, returned in EHLO but\n"""\ """ not enforced\n"""\ """ --pid=FILE File to write process"""\ """ information to\n --port=PORT """\ """Port to listen for connections on\n --user=USER"""\ """ User to drop privs to during run"""\ """ time\n\nBlackhole SSL\n-------------\n\n --ssl=BOOL"""\ """ Enable/disable SSL\n"""\ """ --ssl_cert=PATH SSL Certificate\n"""\ """ --ssl_key=PATH SSL Private Key\n"""\ """ --ssl_port=PORT Port to listen for"""\ """ SSL connections on\n\nDebug\n-----\n\n --debug=BOOL"""\ """ Enable/disable debug logging mode."""\ """ Causes a lot of disk I/O\n\nDelay\n-----\n\n"""\ """ --delay=INT Delay SMTP connection"""\ """ for number of seconds passed\n\nMode\n----\n\n"""\ """ --mode=MODE Mode to run blackhole"""\ """ in (accept, bounce, random,\n"""\ """ unavailable,"""\ """ offline)\n\n """\ """accept - accept all email with code 250, 251, 252 or"""\ """ 253\n bounce -"""\ """ bounce all email with a random code,\n"""\ """ excluding 250, 251,"""\ """ 252, 253\n random"""\ """ - randomly accept or bounce all email with a random"""\ """ code\n unavailable"""\ """ - server always respondes with code 421\n"""\ """ - service is"""\ """ unavailable\n """\ """offline - server always responds with code 521 - """\ """server\n does not"""\ """ accept mail\n\nWorkers\n-------\n\n --workers=NUM"""\ """ Number of worker processes to"""\ """ spawn.(default: # of CPUs/Cores - 2 + 1 master)\n\n""" print_help() self.assertEquals(stdout_mock.getvalue(), val)
def set_action(): """ Figure out what action to perform based on arguments passed on the command line. start, stop or status """ action = None for arg in sys.argv[1:]: if not arg.startswith("--"): action = arg if action not in ('start', 'stop', 'status') or action is None: print_help() sys.exit(2) return action
def daemon(action): """ Trigger the daemon, run the action command and return the daemon object if required. 'action' is a string, either start, stop or status Returns an instance of deiman.Deiman """ d = Deiman(options.pid) if action == "stop": d.stop() sys.exit(0) elif action == "status": d.status() sys.exit(0) if len(sys.argv) == 1: print_help() sys.exit(2) if action == "start": d.start() return d else: sys.exit(0)
try: import ssl except ImportError: ssl = None import sys # Bypass tornado.options to print custom version # and help message from blackhole import __fullname__ from blackhole.opts import print_help for arg in sys.argv[1:]: if arg in ("--version", "-v"): print(__fullname__) sys.exit(0) if arg in ("--help", "-h"): print_help() sys.exit(0) # set default options from blackhole import opts from deiman import Deiman from tornado import (ioloop, process) from tornado.options import options options.parse_command_line() if options.conf and os.path.exists(options.conf): options.parse_config_file(options.conf) from blackhole.connection import (connection_ready, sockets) from blackhole.log import log from blackhole.ssl_utils import (BlackholeSSLException, verify_ssl_opts,