rtemstoolkit/mailer.py: Add --use-gitconfig option

This adds the option to pull mail-related configuration values from the
user's git configuration.

Closes #4384
This commit is contained in:
Alex White
2021-04-13 16:28:35 -05:00
committed by Joel Sherrill
parent 3b1407fb20
commit 82b752a438

View File

@@ -39,11 +39,13 @@ import smtplib
import socket import socket
from rtemstoolkit import error from rtemstoolkit import error
from rtemstoolkit import execute
from rtemstoolkit import options from rtemstoolkit import options
from rtemstoolkit import path from rtemstoolkit import path
_options = { _options = {
'--mail' : 'Send email report or results.', '--mail' : 'Send email report or results.',
'--use-gitconfig': 'Use mail configuration from git config.',
'--mail-to' : 'Email address to send the email to.', '--mail-to' : 'Email address to send the email to.',
'--mail-from' : 'Email address the report is from.', '--mail-from' : 'Email address the report is from.',
'--smtp-host' : 'SMTP host to send via.', '--smtp-host' : 'SMTP host to send via.',
@@ -58,12 +60,22 @@ def append_options(opts):
def add_arguments(argsp): def add_arguments(argsp):
argsp.add_argument('--mail', help = _options['--mail'], action = 'store_true') argsp.add_argument('--mail', help = _options['--mail'], action = 'store_true')
argsp.add_argument('--use-gitconfig', help = _options['--use-gitconfig'], action = 'store_true')
for o in list(_options)[1:]: for o in list(_options)[1:]:
argsp.add_argument(o, help = _options[o], type = str) argsp.add_argument(o, help = _options[o], type = str)
class mail: class mail:
def __init__(self, opts): def __init__(self, opts):
self.opts = opts self.opts = opts
self.gitconfig_lines = None
if opts.find_arg('--use-gitconfig') is not None:
# Read the output of `git config --list` instead of reading the
# .gitconfig file directly because Python 2 ConfigParser does not
# accept tabs at the beginning of lines.
e = execute.capture_execution()
exit_code, proc, output = e.open('git config --list', shell=True)
if exit_code == 0:
self.gitconfig_lines = output.split(os.linesep)
def _args_are_macros(self): def _args_are_macros(self):
return isinstance(self.opts, options.command_line) return isinstance(self.opts, options.command_line)
@@ -83,6 +95,16 @@ class mail:
value = None value = None
return value return value
def _get_from_gitconfig(self, variable_name):
if self.gitconfig_lines is None:
return None
for line in self.gitconfig_lines:
if line.startswith(variable_name):
ls = line.split('=')
if len(ls) >= 2:
return ls[1]
def from_address(self): def from_address(self):
def _clean(l): def _clean(l):
@@ -97,6 +119,12 @@ class mail:
addr = self._get_arg('--mail-from') addr = self._get_arg('--mail-from')
if addr is not None: if addr is not None:
return addr return addr
addr = self._get_from_gitconfig('user.email')
if addr is not None:
name = self._get_from_gitconfig('user.name')
if name is not None:
addr = '%s <%s>' % (name, addr)
return addr
mailrc = None mailrc = None
if 'MAILRC' in os.environ: if 'MAILRC' in os.environ:
mailrc = os.environ['MAILRC'] mailrc = os.environ['MAILRC']
@@ -125,6 +153,9 @@ class mail:
def smtp_host(self): def smtp_host(self):
host = self._get_arg('--smtp-host') host = self._get_arg('--smtp-host')
if host is not None:
return host
host = self._get_from_gitconfig('sendemail.smtpserver')
if host is not None: if host is not None:
return host return host
if self._args_are_macros(): if self._args_are_macros():
@@ -135,6 +166,9 @@ class mail:
def smtp_port(self): def smtp_port(self):
port = self._get_arg('--smtp-port') port = self._get_arg('--smtp-port')
if port is not None:
return port
port = self._get_from_gitconfig('sendemail.smtpserverport')
if port is not None: if port is not None:
return port return port
if self._args_are_macros(): if self._args_are_macros():
@@ -142,10 +176,18 @@ class mail:
return port return port
def smtp_user(self): def smtp_user(self):
return self._get_arg('--smtp-user') user = self._get_arg('--smtp-user')
if user is not None:
return user
user = self._get_from_gitconfig('sendemail.smtpuser')
return user
def smtp_password(self): def smtp_password(self):
return self._get_arg('--smtp-password') password = self._get_arg('--smtp-password')
if password is not None:
return password
password = self._get_from_gitconfig('sendemail.smtppass')
return password
def send(self, to_addr, subject, body): def send(self, to_addr, subject, body):
from_addr = self.from_address() from_addr = self.from_address()