diff --git a/.gitattributes b/.gitattributes index c0810bb161a45533e994579fd519e8f2c721d69e..6d5090e6d81fdffbeeff8e02121e0dabda771caa 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1623,6 +1623,7 @@ LCS/PyCommon/CMakeLists.txt -text LCS/PyCommon/__init__.py -text LCS/PyCommon/cache.py -text LCS/PyCommon/datetimeutils.py -text +LCS/PyCommon/defaultmailaddresses.py -text LCS/PyCommon/factory.py -text LCS/PyCommon/flask_utils.py -text LCS/PyCommon/math.py -text @@ -1633,6 +1634,9 @@ LCS/PyCommon/test/t_cache.py -text LCS/PyCommon/test/t_cache.sh -text LCS/PyCommon/test/t_dbcredentials.run eol=lf LCS/PyCommon/test/t_dbcredentials.sh eol=lf +LCS/PyCommon/test/t_defaultmailaddresses.py -text +LCS/PyCommon/test/t_defaultmailaddresses.run -text +LCS/PyCommon/test/t_defaultmailaddresses.sh -text LCS/PyCommon/test/t_methodtrigger.sh eol=lf LCS/PyCommon/test/t_test_utils.py -text LCS/PyCommon/test/t_test_utils.run -text diff --git a/CEP/Pipeline/framework/lofarpipe/support/loggingdecorators.py b/CEP/Pipeline/framework/lofarpipe/support/loggingdecorators.py index 1569da033fbb1440b517cd5ed7f25b009d920a8e..6fb010563a696fd2eb5d14b3df619a4e8d083511 100644 --- a/CEP/Pipeline/framework/lofarpipe/support/loggingdecorators.py +++ b/CEP/Pipeline/framework/lofarpipe/support/loggingdecorators.py @@ -7,6 +7,7 @@ from email.mime.text import MIMEText import time import os import xml.dom.minidom as _xml +from lofar.common.defaultmailaddresses import PipelineEmailConfig from lofarpipe.support.xmllogging import enter_active_stack, \ exit_active_stack, get_active_stack, get_child @@ -185,8 +186,13 @@ def mail_log_on_exception(target): type(message), message, active_stack_data) # mail all recipients + try: + pconfig = PipelineEmailConfig() + except Exception as e: + raise Exception("loggingdecorators.py: Could not find the pipeline email configuration file: %s" % (e) ) + for entry in mail_list: - _mail_msg_to("softwaresupport@astron.nl", entry, + _mail_msg_to(pconfig['error-sender'], entry, subject, msg) raise diff --git a/LCS/PyCommon/CMakeLists.txt b/LCS/PyCommon/CMakeLists.txt index 28338861f1e1d5682a6bc32879cfdf76c6bf4e19..5d6cdb28631646683d940fdf23bfa56ec986dedf 100644 --- a/LCS/PyCommon/CMakeLists.txt +++ b/LCS/PyCommon/CMakeLists.txt @@ -12,6 +12,7 @@ set(_py_files __init__.py cache.py dbcredentials.py + defaultmailaddresses.py factory.py math.py methodtrigger.py diff --git a/LCS/PyCommon/defaultmailaddresses.py b/LCS/PyCommon/defaultmailaddresses.py new file mode 100644 index 0000000000000000000000000000000000000000..d67442d11e07356ef0bacea1b616d37f9418572a --- /dev/null +++ b/LCS/PyCommon/defaultmailaddresses.py @@ -0,0 +1,80 @@ +# defaultmailaddresses.py: default mail addresses for the LOFAR software +# +# Copyright (C) 2017 +# ASTRON (Netherlands Institute for Radio Astronomy) +# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands +# +# This file is part of the LOFAR software suite. +# The LOFAR software suite 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. +# +# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>. +# +# $Id: $ +# +""" +This package contains the default mail addresses used in the LOFAR software +""" + +from ConfigParser import ConfigParser +import os +import pwd +from glob import glob + +# obtain the environment, and add USER and HOME if needed (since supervisord does not) +environ = os.environ +user_info = pwd.getpwuid(os.getuid()) +environ.setdefault("HOME", user_info.pw_dir) +environ.setdefault("USER", user_info.pw_name) + + + +def findfiles(pattern): + """ Returns a list of files matched by `pattern'. + The pattern can include environment variables using the + {VAR} notation. + """ + try: + return glob(pattern.format(**environ)) + except KeyError: + return [] + + +# Addresses used for the pipelines +class PipelineEmailConfig(): + """ + Pipeline email configuration class + """ + + def __init__(self, filepatterns = None): + if filepatterns is None: + filepatterns = ["{LOFARROOT}/etc/email/*.ini", + "{HOME}/.lofar/email/*.ini"]# TODO correct the pattern here + self.configfiles = sum([findfiles(p) for p in filepatterns],[]) + if not self.configfiles: + raise Exception("no config file found") + self.config = None + + def load_config(self): + self.config = ConfigParser() + self.config.read(self.configfiles) + + def get(self, key): + if not self.config: + self.load_config() + return self.config.get("Pipeline",key) + + def __getitem__(self, what): + return self.get(what) + + + + diff --git a/LCS/PyCommon/test/CMakeLists.txt b/LCS/PyCommon/test/CMakeLists.txt index 1f219cba046fd9c580029cace58ee1248dca5c86..48f4f4d47f53903b1d6b735e9f44f2c969b5ccb4 100644 --- a/LCS/PyCommon/test/CMakeLists.txt +++ b/LCS/PyCommon/test/CMakeLists.txt @@ -8,6 +8,7 @@ file(COPY lofar_add_test(t_cache) lofar_add_test(t_dbcredentials) +lofar_add_test(t_defaultmailaddresses) lofar_add_test(t_methodtrigger) lofar_add_test(t_util) lofar_add_test(t_test_utils) diff --git a/LCS/PyCommon/test/t_defaultmailaddresses.py b/LCS/PyCommon/test/t_defaultmailaddresses.py new file mode 100644 index 0000000000000000000000000000000000000000..9fc2dd1c957437c12b34841db54c0b54a2d77f5f --- /dev/null +++ b/LCS/PyCommon/test/t_defaultmailaddresses.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python + +import unittest +import tempfile +from lofar.common.defaultmailaddresses import PipelineEmailConfig + +def setUpModule(): + pass + +def tearDownModule(): + pass + + + +class TestPipelineEmailAddress(unittest.TestCase): + def test_get(self): + f = tempfile.NamedTemporaryFile() + f.write(""" +[Pipeline] +error-sender = softwaresupport@astron.nl + """) + f.flush() + + pec = PipelineEmailConfig(filepatterns=[f.name]) + self.assertEqual(pec["error-sender"], "softwaresupport@astron.nl") + + def test_failed_get(self): + f = tempfile.NamedTemporaryFile() + f.write(""" + [Pipeline] + error-sender = softwaresupport@astron.nl + """) + f.flush() + pec = PipelineEmailConfig(filepatterns=[f.name]) + with self.assertRaises(Exception): + + pec["error-senter"] + + def test_get_non_existing(self): + + with self.assertRaises(Exception): + dbc = PipelineEmailConfig(filepatterns=[]) + + + +def main(): + unittest.main() + +if __name__ == "__main__": + # run all tests + import sys + main() diff --git a/LCS/PyCommon/test/t_defaultmailaddresses.run b/LCS/PyCommon/test/t_defaultmailaddresses.run new file mode 100755 index 0000000000000000000000000000000000000000..cad0a43c04f38d64f888267601ba8e98068cb58b --- /dev/null +++ b/LCS/PyCommon/test/t_defaultmailaddresses.run @@ -0,0 +1,4 @@ +#!/bin/bash +source python-coverage.sh + +python_coverage_test defaultmailaddresses t_defaultmailaddresses.py diff --git a/LCS/PyCommon/test/t_defaultmailaddresses.sh b/LCS/PyCommon/test/t_defaultmailaddresses.sh new file mode 100755 index 0000000000000000000000000000000000000000..498398c515e12f66e00d316047f67d55d386f361 --- /dev/null +++ b/LCS/PyCommon/test/t_defaultmailaddresses.sh @@ -0,0 +1,2 @@ +#!/bin/sh +./runctest.sh t_defaultmailaddresses