OpenSecurity/bin/environment.py
author BarthaM@N3SIM1218.D03.arc.local
Thu, 17 Jul 2014 10:20:10 +0100
changeset 212 59ebaa44c12c
parent 154 651bf8fd169e
child 240 d7ef04254e9c
permissions -rwxr-xr-x
Modified update_template to cope with unattached .vmdk
Added start method to vmmanager
Modified vmmanager to not start automatically over getInstance() invocation
Modified cygwin to corectly get the root folder (OpenSecurity//bin)
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # environment.py
     6 # 
     7 # pick some current environment infos
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@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 os
    36 import os.path
    37 import sys
    38 
    39 
    40 # ------------------------------------------------------------
    41 # code
    42 
    43 
    44 class Environment(object):
    45     
    46     """Hold some nifty environment stuff in a dedicated class."""
    47 
    48     _log_warning_shown = False
    49     _prefix_path = ''
    50     
    51     def __init__(self, application = None):
    52         
    53         # if we ain't got a path to start from, all is valid/lost
    54         if len(sys.path[0]) == 0:
    55             self._prefix_path = ''
    56             self._data_path = ''
    57             return
    58         
    59         # the prefix path
    60         #
    61         # - on Linux: this is ../../ to the current executable
    62         #   e.g. "/usr/bin/myprogram" --> "/usr"
    63         #
    64         # - on Windows (inkl. Cygwin): this is the installation folder
    65         #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
    66         #
    67         if sys.platform == 'linux2':
    68             self._prefix_path = os.path.split(sys.path[0])[0]
    69         elif sys.platform == 'win32' or sys.platform == 'cygwin':
    70             for app_path in sys.path:
    71                 if 'OpenSecurity\\bin' in app_path:
    72                     self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
    73                     break
    74         
    75         if self._prefix_path == '':
    76             raise OSError()
    77             
    78         # the data path where all data files are stored
    79         if sys.platform == 'linux2':
    80             if not application is None:
    81                 self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
    82             else:
    83                 self._data_path = os.path.join(self._prefix_path, 'share')
    84         elif sys.platform == 'win32' or sys.platform == 'cygwin':
    85             self._data_path = self._prefix_path
    86         else:
    87             raise OSError()
    88 
    89             
    90     def data_path_get(self):
    91         """dat_path get"""
    92         return self._data_path
    93         
    94     data_path = property(data_path_get)
    95             
    96             
    97     def prefix_path_get(self):
    98         """prefix_path get"""
    99         return self._prefix_path
   100         
   101     prefix_path = property(prefix_path_get)
   102     
   103     
   104     def log_path_get(self):
   105         
   106         """the path where log files should be stored"""
   107         
   108         user_log_path = os.path.expanduser(os.path.join('~', '.log'))
   109         
   110         if sys.platform == 'linux2':
   111             
   112             if os.access('/var/log', os.W_OK):
   113                 return '/var/log'
   114             
   115             if not Environment._log_warning_shown:
   116                 print('no permissions to write log files in /var/log, switching to ~/.log')
   117                 Environment._log_warning_shown = True
   118             
   119             if not os.path.exists(user_log_path):
   120                 os.mkdir(user_log_path)
   121             elif not os.path.isdir(user_log_path):
   122                 raise IOError(user_log_path + ': not a folder')
   123             
   124             return user_log_path
   125         
   126         elif sys.platform == 'win32' or sys.platform == 'cygwin':
   127 
   128             # in OpenSecurity we expect the log path tp be
   129             # somewhere like C:\Program Files\OpenSecurity\log
   130             # having this script residing in 
   131             # C:\Progam Files\OpenSecurity\bin
   132             ideal_log_path = os.path.normpath(os.path.join(self.prefix_path, 'log'))
   133 
   134             # check ideal path first
   135             if not os.path.exists(ideal_log_path):
   136                 os.mkdir(ideal_log_path)
   137             elif not os.path.isdir(ideal_log_path):
   138                 raise IOError(ideal_log_path + ': not a folder')
   139 
   140             return ideal_log_path
   141         
   142         else:
   143             raise OSError()
   144             
   145     log_path = property(log_path_get)
   146             
   147             
   148 # test method
   149 def test():
   150 
   151     """Test: class Environment"""
   152     e = Environment('OpenSecurity')
   153     print('prefix_path: "{0}"'.format(e.prefix_path))
   154     print('  data_path: "{0}"'.format(e.data_path))
   155     print('   log_path: "{0}"'.format(e.log_path))
   156 
   157 
   158 # test the module			
   159 if __name__ == '__main__':
   160     test()
   161