Create a new send() method on OutputBase to wrap up the construction
of messages, with a long-format and a short-format fallback.

* tools/hook-scripts/mailer/mailer.py:
  (OutputBase.send): new method to pull together the start/finish on
    SELF, and use of the WRITER. This incorporates the new
    MessageTooLarge exception to fall back to a shorter message
    generation function.
  (Commit.generate): move message generation into a new long_commit()
    local function, and use .send() to build/send a message. Switch to
    using FAILED rather than RET.
  (PropChange.generate): similar to above, with long_propchange()
  (Lock.generate): similar to above, with long_lock()
  (class MessageTooLarge): new exception for when a generated message
    becomes too large, and a fallback to a short message is needed.
    This is caught in OutputBase.send(), but not (yet) raised.


git-svn-id: https://svn.apache.org/repos/asf/subversion/trunk@1914899 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/tools/hook-scripts/mailer/mailer.py b/tools/hook-scripts/mailer/mailer.py
index 53a2732..bb9968a 100755
--- a/tools/hook-scripts/mailer/mailer.py
+++ b/tools/hook-scripts/mailer/mailer.py
@@ -190,6 +190,21 @@
     self.repos = repos
     self._CHUNKSIZE = 128 * 1024
 
+  def send(self, basic_subject, group, params, long_func, short_func):
+      writer = self.start(basic_subject, group, params)
+
+      try:
+          try:
+              long_func(writer)
+          except MessageTooLarge:
+              short_func(writer)
+
+          self.finish()
+      except MessageSendFailure:
+        return True  # failed
+
+      return False  # succeeded
+
   def start(self, basic_subject, group, params):
     """Override this method.
 
@@ -517,24 +532,20 @@
     ### rather than rebuilding it each time.
 
     iterpool = svn.core.svn_pool_create(scratch_pool)
-    ret = 0
+    failed = False
 
     for (group, param_tuple), (params, paths) in sorted(self.groups.items()):
       subject_line = self.make_subject(self.basic_subject, group, params)
-      try:
-        writer = output.start(subject_line, group, params)
 
+      def long_commit(writer):
         # generate the content for this group and set of params
         generate_content(writer, self.cfg, self.repos, self.changelist,
                          group, params, paths, iterpool)
-
-        output.finish()
-      except MessageSendFailure:
-        ret = 1
+      failed |= output.send(subject_line, group, params, long_commit, None)
       svn.core.svn_pool_clear(iterpool)
 
     svn.core.svn_pool_destroy(iterpool)
-    return ret
+    return int(failed)
 
 
 class PropChange(Messenger):
@@ -555,13 +566,13 @@
 
   def generate(self, output, scratch_pool):
     actions = { 'A': 'added', 'M': 'modified', 'D': 'deleted' }
-    ret = 0
+    failed = False
     ### maybe create an iterpool?
 
     for (group, param_tuple), params in self.groups.items():
       subject_line = self.make_subject(self.basic_subject, group, params)
-      try:
-        writer = output.start(subject_line, group, params)
+
+      def long_propchange(writer):
         writer.write('Author: %s\n'
                           'Revision: %s\n'
                           'Property Name: %s\n'
@@ -589,10 +600,9 @@
               'to' : tempfile2.name,
               })):
               writer.write(to_str(diffs.raw))
-        output.finish()
-      except MessageSendFailure:
-        ret = 1
-    return ret
+      failed |= output.send(subject_line, group, params, long_propchange, None)
+
+    return int(failed)
 
 
 def get_commondir(dirlist):
@@ -672,12 +682,12 @@
                                        pool)
 
   def generate(self, output, scratch_pool):
-    ret = 0
+    failed = False
+
     for (group, param_tuple), (params, paths) in sorted(self.groups.items()):
       subject_line = self.make_subject(self.basic_subject, group, params)
-      try:
-        writer = output.start(subject_line, group, params)
 
+      def long_lock(writer):
         writer.write('Author: %s\n'
                           '%s paths:\n' %
                           (self.author, self.do_lock and 'Locked' or 'Unlocked'))
@@ -689,10 +699,9 @@
         if self.do_lock:
           writer.write('Comment:\n%s\n' % (self.lock.comment or ''))
 
-        output.finish()
-      except MessageSendFailure:
-        ret = 1
-    return ret
+      failed |= output.send(subject_line, group, params, long_lock, None)
+
+    return int(failed)
 
 
 class DiffSelections:
@@ -1444,6 +1453,8 @@
   pass
 class MessageSendFailure(Exception):
   pass
+class MessageTooLarge(Exception):
+  pass
 
 
 if __name__ == '__main__':