2021-05-19 18:07:42 +02:00
|
|
|
# coding: utf8
|
|
|
|
|
|
|
|
""" Scripts helpers """
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import logging
|
|
|
|
|
|
|
|
from mylib.email import EmailClient
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-05-19 19:19:57 +02:00
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
def init_logging(options, name, report=None):
|
2021-05-19 19:19:57 +02:00
|
|
|
""" Initialize logging from calling script options """
|
2021-05-19 18:07:42 +02:00
|
|
|
logformat = '%(asctime)s - ' + name + ' - %(levelname)s - %(message)s'
|
|
|
|
if options.debug:
|
|
|
|
loglevel = logging.DEBUG
|
|
|
|
elif options.verbose:
|
|
|
|
loglevel = logging.INFO
|
|
|
|
else:
|
|
|
|
loglevel = logging.WARNING
|
|
|
|
|
|
|
|
handlers = []
|
|
|
|
if options.logfile:
|
|
|
|
handlers.append(logging.FileHandler(options.logfile))
|
|
|
|
if not options.logfile or options.console:
|
|
|
|
handlers.append(logging.StreamHandler())
|
|
|
|
if report:
|
|
|
|
handlers.append(report.get_handler())
|
|
|
|
logging.basicConfig(level=loglevel, format=logformat, handlers=handlers)
|
|
|
|
|
|
|
|
|
2021-05-26 11:56:02 +02:00
|
|
|
def get_opts_parser(desc=None, just_try=False, just_one=False, progress=False):
|
2021-05-19 18:07:42 +02:00
|
|
|
""" Retrieve options parser """
|
2021-05-26 11:56:02 +02:00
|
|
|
parser = argparse.ArgumentParser(description=desc)
|
2021-05-19 18:07:42 +02:00
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbose',
|
|
|
|
action="store_true",
|
|
|
|
dest="verbose",
|
|
|
|
help="Enable verbose mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-d', '--debug',
|
|
|
|
action="store_true",
|
|
|
|
dest="debug",
|
|
|
|
help="Enable debug mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-l', '--log-file',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="logfile",
|
|
|
|
help="Log file path"
|
|
|
|
)
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
'-C', '--console',
|
|
|
|
action="store_true",
|
|
|
|
dest="console",
|
|
|
|
help="Always log on console (even if log file is configured)"
|
|
|
|
)
|
|
|
|
|
|
|
|
if just_try:
|
|
|
|
parser.add_argument(
|
|
|
|
'-j', '--just-try',
|
|
|
|
action="store_true",
|
|
|
|
dest="just_try",
|
|
|
|
help="Enable just-try mode"
|
|
|
|
)
|
|
|
|
|
2021-05-26 11:56:02 +02:00
|
|
|
if just_one:
|
|
|
|
parser.add_argument(
|
|
|
|
'-J', '--just-one',
|
|
|
|
action="store_true",
|
|
|
|
dest="just_one",
|
|
|
|
help="Enable just-one mode"
|
|
|
|
)
|
|
|
|
|
2021-05-19 18:07:42 +02:00
|
|
|
if progress:
|
|
|
|
parser.add_argument(
|
|
|
|
'-p', '--progress',
|
|
|
|
action="store_true",
|
|
|
|
dest="progress",
|
|
|
|
help="Enable progress bar"
|
|
|
|
)
|
|
|
|
|
|
|
|
return parser
|
|
|
|
|
|
|
|
|
|
|
|
def add_email_opts(parser):
|
|
|
|
""" Add email options """
|
|
|
|
email_opts = parser.add_argument_group('Email options')
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-H', '--smtp-host',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_host",
|
|
|
|
help="SMTP host"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-P', '--smtp-port',
|
|
|
|
action="store",
|
|
|
|
type=int,
|
|
|
|
dest="email_smtp_port",
|
|
|
|
help="SMTP port"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-S', '--smtp-ssl',
|
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_ssl",
|
|
|
|
help="Use SSL"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-T', '--smtp-tls',
|
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_tls",
|
|
|
|
help="Use TLS"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-u', '--smtp-user',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_user",
|
|
|
|
help="SMTP username"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-p', '--smtp-password',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_smtp_password",
|
|
|
|
help="SMTP password"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-D', '--smtp-debug',
|
|
|
|
action="store_true",
|
|
|
|
dest="email_smtp_debug",
|
|
|
|
help="Debug SMTP connection"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-e', '--email-encoding',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_encoding",
|
|
|
|
help="SMTP encoding"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-f', '--sender-name',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_sender_name",
|
|
|
|
help="Sender name"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-F', '--sender-email',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_sender_email",
|
|
|
|
help="Sender email"
|
|
|
|
)
|
|
|
|
|
|
|
|
email_opts.add_argument(
|
|
|
|
'-c', '--catch-all',
|
|
|
|
action="store",
|
|
|
|
type=str,
|
|
|
|
dest="email_catch_all",
|
|
|
|
help="Catch all sent email: specify catch recipient email address"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def init_email_client(options, **kwargs):
|
2021-05-19 19:19:57 +02:00
|
|
|
""" Initialize email client from calling script options """
|
2021-05-19 18:07:42 +02:00
|
|
|
log.info('Initialize Email client')
|
|
|
|
return EmailClient(
|
|
|
|
smtp_host=options.email_smtp_host,
|
|
|
|
smtp_port=options.email_smtp_port,
|
|
|
|
smtp_ssl=options.email_smtp_ssl,
|
|
|
|
smtp_tls=options.email_smtp_tls,
|
|
|
|
smtp_user=options.email_smtp_user,
|
|
|
|
smtp_password=options.email_smtp_password,
|
|
|
|
smtp_debug=options.email_smtp_debug,
|
|
|
|
sender_name=options.email_sender_name,
|
|
|
|
sender_email=options.email_sender_email,
|
|
|
|
catch_all_addr=options.email_catch_all,
|
|
|
|
just_try=options.just_try,
|
|
|
|
encoding=options.email_encoding,
|
|
|
|
**kwargs
|
|
|
|
)
|