source: cows/trunk/cows/pylons/config.py @ 6422

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/config.py@6422
Revision 6422, 4.2 KB checked in by domlowe, 10 years ago (diff)

Added getCSMLDatasetInfo method to csml connector - used to quickly get csml info without parsing entire file.

Line 
1# BSD Licence
2# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
3# All rights reserved.
4#
5# See the LICENSE file in the source distribution of this software for
6# the full license text.
7
8"""
9Miscellaneous utility functions/classes
10
11#load csml config options
12#starts symlink checker
13#send email (used by symlink checker
14
15
16"""
17
18import os, time
19import pylons
20from threading import Thread
21import logging
22log=logging.getLogger(__name__)
23
24from email.MIMEMultipart import MIMEMultipart
25from email.MIMEText import MIMEText
26from email.Utils import COMMASPACE, formatdate
27import smtplib
28from cows.service.imps.csmlbackend.config import config
29
30
31def configCSML():
32    """Initialise the configuration of the CSML backend from pylons.config.
33
34    """
35   
36    for k in pylons.config:
37        parts = k.split('.')
38        if parts[:2] == ['cows', 'csml']:
39            k2 = '.'.join(parts[2:])
40            config[k2] = pylons.config[k]
41   
42    #set up the CSML symlink checker to check all symlinks in the csmlstore
43    #Symlink checker is currently set to check hourly but can be modified in the pylons ini file by changing csml.cows.symlink_check_interval (in minutes). 
44    if config.has_key('symlink_check_interval'):
45        minutes=float(config['symlink_check_interval'])
46    else:
47        #default to hourly checks
48        minutes=60
49    paths = getCSMLPaths(config['csmlstore'])
50    startSymLinkChecker(paths, minutes)
51       
52def getCSMLPaths(csmldirectory):   
53    paths=[]
54    for dirpath, dirnames, filenames in os.walk(csmldirectory):
55        for filename in filenames:
56            file_id, ext = os.path.splitext(filename)
57            if ext in ('.xml', '.csml'):
58                paths.append(os.path.join(dirpath, filename))
59    return paths
60
61def startSymLinkChecker(paths, minutes):
62    checkerProcess = SymlinkChecker(paths, minutes)
63    checkerProcess.start()
64
65class SymlinkChecker(Thread):
66    ''' This creates a new thread which sleeps most of the time, but occasionally checks that the CSML symlinks are working.'''
67    def __init__ (self,paths, sleepminutes):
68        Thread.__init__(self)
69        #self.setDaemon(True)
70        self.paths=paths
71        self.sleepminutes=sleepminutes
72   
73    def run(self):
74        while 1:
75            for path in self.paths:
76                if os.path.islink(path):
77                    #it's a symlink, get the link path
78                    symlinkpath=os.readlink(path)
79                    #this may be relative so make sure it isn't
80                    fullsymlinkpath=os.path.join(os.path.abspath(os.path.dirname(path) ), symlinkpath)
81                    #now check it's not broken.
82                    if not os.path.exists(fullsymlinkpath):
83                        #if broken, send notification email (if configured) and log a warning message
84                        if config.has_key('send_symlink_warnings_to'):
85                            if config.has_key('send_symlink_warnings_from'):
86                                if config.has_key('symlink_warnings_smtp'):
87                                    to=config['send_symlink_warnings_to'].split(',')
88                                    fro=config['send_symlink_warnings_from']
89                                    smtp=config['symlink_warnings_smtp']
90                                    subject = 'Broken Symlink in visualisation software'
91                                    message= 'This message is from the COWS pylons application. Symlink %s is broken' % path
92                                    sendemail(fro, to, subject, message, smtp)
93                        log.warning('INVESTIGATE: Symlink %s is broken ' % path)
94                    else:
95                        log.info('Symlink OK: %s '%path)
96            time.sleep(self.sleepminutes*60)
97   
98def sendemail(fro, to, subject, message, smtp):
99    ''' send an email '''
100    SERVER = smtp
101    FROM = fro
102    TO = to # must be a list
103    SUBJECT = subject
104    TEXT = message
105   
106    #create the message
107    msg = MIMEMultipart()
108    msg['From'] = FROM
109    msg['To'] = COMMASPACE.join(TO)
110    msg['Date'] = formatdate(localtime=True)
111    msg['Subject'] = SUBJECT
112    msg.attach( MIMEText(TEXT) )
113
114    # Send the mail
115    server = smtplib.SMTP(SERVER)
116    server.sendmail(FROM, TO, msg.as_string())
117    server.quit()
118       
119       
120               
121
122
123   
Note: See TracBrowser for help on using the repository browser.