[PATCH rtems-tools 5 4/5] rtemstoolkit/mailer.py: Add --use-gitconfig option

Alex White alex.white at oarcorp.com
Wed Dec 15 21:32:55 UTC 2021


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

Updates #4553
---
 rtemstoolkit/mailer.py | 46 ++++++++++++++++++++++++++++++++++++++++--
 1 file 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']
@@ -125,6 +153,9 @@ class mail:
 
     def smtp_host(self):
         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():
@@ -135,6 +166,9 @@ class mail:
 
     def smtp_port(self):
         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():
@@ -142,10 +176,18 @@ class mail:
         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()
-- 
2.27.0



More information about the devel mailing list