OpenSecurity/bin/opensecurity_util.py
author BarthaM@N3SIM1218.D03.arc.local
Fri, 05 Sep 2014 12:28:30 +0100
changeset 221 853af9cfab6a
parent 212 59ebaa44c12c
child 240 d7ef04254e9c
permissions -rwxr-xr-x
Integrated import script (rewritten in python) into opensecurity/vmmanager.py
Improoved user feedback upon import and update as well as logging.
Reduced system shutdown times and ui response times
Improoved the decoupling between UI and OSec subsystem.
Various other fixes
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurityd
     6 #   
     7 # the opensecurityd as RESTful server
     8 #
     9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>       
    10 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import logging
    36 import os
    37 import urllib
    38 import urllib2
    39 
    40 # local
    41 from environment import Environment
    42 
    43 class DictDiffer(object):
    44     """
    45     Calculate the difference between two dictionaries as:
    46     (1) items added
    47     (2) items removed
    48     (3) keys same in both but changed values
    49     (4) keys same in both and unchanged values
    50     """
    51     def __init__(self, current_dict, past_dict):
    52         self.current_dict, self.past_dict = current_dict, past_dict
    53         self.set_current, self.set_past = set(current_dict.keys()), set(past_dict.keys())
    54         self.intersect = self.set_current.intersection(self.set_past)
    55     def added(self):
    56         return self.set_current - self.intersect 
    57     def removed(self):
    58         return self.set_past - self.intersect 
    59     def changed(self):
    60         return set(o for o in self.intersect if self.past_dict[o] != self.current_dict[o])
    61     def unchanged(self):
    62         return set(o for o in self.intersect if self.past_dict[o] == self.current_dict[o])
    63     
    64 class OpenSecurityException(Exception):
    65     def __init__(self, value):
    66         self.value = value
    67     def __str__(self):
    68         return repr(self.value)
    69     
    70 def setupLogger(name='OpenSecurity'):
    71     logger = logging.getLogger(name)
    72     logger.setLevel(logging.DEBUG)
    73     # create formatter and add it to the handlers
    74     formatter = logging.Formatter('%(asctime)-15s - %(name)s - %(levelname)s - %(message)s')
    75     # create file handler which logs even debug messages
    76     fh = logging.FileHandler(os.path.join(Environment('OpenSecurity').log_path, name + '.log'))
    77     fh.setLevel(logging.DEBUG)
    78     fh.setFormatter(formatter)
    79     logger.addHandler(fh)
    80     # create console handler with a higher log level
    81     ch = logging.StreamHandler()
    82     ch.setLevel(logging.DEBUG)
    83     ch.setFormatter(formatter)
    84     logger.addHandler(ch)
    85     return logger
    86 
    87 logger = setupLogger()
    88 import_logger = setupLogger('OpenSecurity_initial_import')
    89 
    90 def showTrayMessage(text, timeout):
    91     """show a message on the system tray
    92     
    93     On windows this is shown like a ballon message.
    94     
    95     @param  text        the text to show
    96     @param  timeout     timeout hint in millisecs for the message 
    97 
    98     """
    99     try:
   100         d = { 'text': str(text), 'timeout': int(timeout) }
   101         urllib2.urlopen('http://127.0.0.1:8090/message?' + urllib.urlencode(d))
   102     except:
   103         pass
   104 
   105 
   106 # test method
   107 def test():
   108 
   109     """Test: OpenSecurity logging"""
   110     logger.info('test logging')
   111     showTrayMessage('tray message test\nwith mutliple lines', 1000)
   112 
   113 
   114 # test the module           
   115 if __name__ == '__main__':
   116     test()