Skip to content

Commit

Permalink
Improve command line help messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Dufresne committed Apr 2, 2012
1 parent 961f472 commit be1eef1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
26 changes: 14 additions & 12 deletions fakesmtpd
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import tempfile


class FakeSMTPServer(smtpd.SMTPServer):
def __init__(self, localaddr, path):
def __init__(self, localaddr, mail_dir):
smtpd.SMTPServer.__init__(self, localaddr, None)
self.recipients = {}
self.path = path
self.mail_dir = mail_dir

self.logger = logging.getLogger('fakesmtpd')
self.logger.info("SMTP server started")
self.logger.info("Mail path %s", path)
self.logger.info("Mail directory %s", self.mail_dir)


def process_message(self, peer, mailfrom, rcpttos, data):
Expand All @@ -28,7 +28,7 @@ class FakeSMTPServer(smtpd.SMTPServer):
count = self.recipients.get(recipient, 0) + 1
self.recipients[recipient] = count
filename = "{}.{:d}.mail".format(recipient, count)
path = os.path.join(self.path, filename)
path = os.path.join(self.mail_dir, filename)
with open(path, 'w') as f:
f.write(data)
f.write('\n')
Expand All @@ -37,30 +37,32 @@ class FakeSMTPServer(smtpd.SMTPServer):
def fakesmtpd_parser():
parser = argparse.ArgumentParser()
parser.add_argument('-H', '--host', default='localhost')
parser.add_argument('-p', '--port', default=25)
path = os.path.join(tempfile.gettempdir(),
'fakesmtpd{:d}'.format(os.getpid()))
parser.add_argument('--path', default=path)
parser.add_argument('--log')
parser.add_argument('-p', '--port', type=int, default=25)
mail_dir = os.path.join(tempfile.gettempdir(),
'fakesmtpd{:d}'.format(os.getpid()))
parser.add_argument('--mail-dir', default=mail_dir,
help="directory to save incoming mail")
parser.add_argument('--log-file',
help="send output to a log file instead of stdout")
return parser


def main():
parser = fakesmtpd_parser()
args = parser.parse_args()

logging.basicConfig(filename=args.log,
logging.basicConfig(filename=args.log_file,
format='%(asctime)s:%(levelname)s:%(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.DEBUG)

try:
os.mkdir(args.path)
os.mkdir(args.mail_dir)
except OSError as e:
if e.errno != errno.EEXIST:
raise

server = FakeSMTPServer((args.host, args.port), args.path)
server = FakeSMTPServer((args.host, args.port), args.mail_dir)
asyncore.loop()


Expand Down
2 changes: 1 addition & 1 deletion fakesmtpd.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ After=syslog.target network.target
Conflicts=postfix.service exim.service sendmail.service

[Service]
ExecStart=/usr/bin/fakesmtpd --log=/var/log/fakesmtpd.log
ExecStart=/usr/bin/fakesmtpd --log-file=/var/log/fakesmtpd.log

[Install]
WantedBy=multi-user.target

0 comments on commit be1eef1

Please sign in to comment.