Abhilash Raj pushed to branch master at GNU Mailman / Mailman Core

Commits:
0654a189 by Jan Veen at 2018-08-05T04:56:07Z
Add support for non-regular log files

- - - - -
fa444d58 by Abhilash Raj at 2018-08-05T04:56:07Z
Merge branch 'log-to-stdout' into 'master'

Add support for non-regular log files

See merge request mailman/mailman!407
- - - - -


3 changed files:

- src/mailman/core/logging.py
- + src/mailman/core/tests/test_logging.py
- src/mailman/docs/NEWS.rst


Changes:

=====================================
src/mailman/core/logging.py
=====================================
@@ -19,6 +19,7 @@
 
 import os
 import sys
+import stat
 import codecs
 import logging
 
@@ -50,7 +51,17 @@ class ReopenableFileHandler(logging.Handler):
         self._stream = self._open()
 
     def _open(self):
-        return codecs.open(self.filename, 'a', 'utf-8')
+        open_mode = 'a'
+        try:
+            status = os.stat(self.filename)
+            if stat.S_ISREG(status.st_mode):
+                open_mode = 'a'
+            elif stat.S_ISFIFO(status.st_mode) or stat.S_ISCHR(status.st_mode):
+                open_mode = 'w'
+        except FileNotFoundError as e:
+            open_mode = 'a'    # Assume regular file
+
+        return codecs.open(self.filename, open_mode, 'utf-8')
 
     def flush(self):
         if self._stream:


=====================================
src/mailman/core/tests/test_logging.py
=====================================
@@ -0,0 +1,30 @@
+# Copyright (C) 2012-2018 by the Free Software Foundation, Inc.
+#
+# This file is part of GNU Mailman.
+#
+# GNU Mailman is free software: you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option)
+# any later version.
+#
+# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+# more details.
+#
+# You should have received a copy of the GNU General Public License along with
+# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
+
+"""Test logging behavior."""
+
+import unittest
+
+from mailman.core.logging import ReopenableFileHandler
+
+
+class TestRunner(unittest.TestCase):
+
+    def test_opening_character_device(self):
+        handler = ReopenableFileHandler('test', '/dev/stdout')
+
+        handler.close()


=====================================
src/mailman/docs/NEWS.rst
=====================================
@@ -17,6 +17,10 @@ Command line
 * The ``mailman import21`` command properly converts all acceptable_aliases
   to regexps.  (Closes #496)
 
+Bugs
+----
+* Open non-regular log files (e.g. fifos) in write mode instead of append mode.
+  (See !407)
 
 3.2.0 -- "La Villa Strangiato"
 ==============================



View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/55443b04ff185ca908b1dcd8261d8b2d0f67626e...fa444d5834d68a4f06ffd5a7a557561574a2288a

-- 
View it on GitLab: 
https://gitlab.com/mailman/mailman/compare/55443b04ff185ca908b1dcd8261d8b2d0f67626e...fa444d5834d68a4f06ffd5a7a557561574a2288a
You're receiving this email because of your account on gitlab.com.
_______________________________________________
Mailman-checkins mailing list
[email protected]
Unsubscribe: 
https://mail.python.org/mailman/options/mailman-checkins/archive%40jab.org

Reply via email to