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