summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex White <alex.white@oarcorp.com>2021-04-13 16:28:35 -0500
committerJoel Sherrill <joel@rtems.org>2021-04-14 09:32:11 -0500
commit82b752a43851a59d5029b470057464f1b83155d9 (patch)
tree994aaabca118c6268fda62be0d1d55117e2844fc
parentrtemstoolkit/mailer.py: Add SMTP login options (diff)
downloadrtems-tools-82b752a43851a59d5029b470057464f1b83155d9.tar.bz2
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
-rw-r--r--rtemstoolkit/mailer.py46
1 files changed, 44 insertions, 2 deletions
diff --git a/rtemstoolkit/mailer.py b/rtemstoolkit/mailer.py
index aa804ae..ae51d78 100644
--- a/rtemstoolkit/mailer.py
+++ b/rtemstoolkit/mailer.py
@@ -39,11 +39,13 @@ import smtplib
import socket
from rtemstoolkit import error
+from rtemstoolkit import execute
from rtemstoolkit import options
from rtemstoolkit import path
_options = {
'--mail' : 'Send email report or results.',
+ '--use-gitconfig': 'Use mail configuration from git config.',
'--mail-to' : 'Email address to send the email to.',
'--mail-from' : 'Email address the report is from.',
'--smtp-host' : 'SMTP host to send via.',
@@ -58,12 +60,22 @@ def append_options(opts):
def add_arguments(argsp):
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:]:
argsp.add_argument(o, help = _options[o], type = str)
class mail:
def __init__(self, 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):
return isinstance(self.opts, options.command_line)
@@ -83,6 +95,16 @@ class mail:
value = None
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 _clean(l):
@@ -97,6 +119,12 @@ class mail:
addr = self._get_arg('--mail-from')
if addr is not None:
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
if 'MAILRC' in os.environ:
mailrc = os.environ['MAILRC']
@@ -127,6 +155,9 @@ class mail:
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:
+ return host
if self._args_are_macros():
host = self.opts.defaults.get_value('%{_mail_smtp_host}')
if host is not None:
@@ -137,15 +168,26 @@ class mail:
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:
+ return port
if self._args_are_macros():
port = self.opts.defaults.get_value('%{_mail_smtp_port}')
return port
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):
- 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):
from_addr = self.from_address()