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)
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# environment.py
om@13
     6
# 
om@13
     7
# pick some current environment infos
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import os
om@13
    36
import os.path
om@13
    37
import sys
om@13
    38
om@13
    39
om@13
    40
# ------------------------------------------------------------
om@13
    41
# code
om@13
    42
om@13
    43
om@13
    44
class Environment(object):
om@13
    45
    
om@13
    46
    """Hold some nifty environment stuff in a dedicated class."""
oliver@154
    47
oliver@154
    48
    _log_warning_shown = False
BarthaM@212
    49
    _prefix_path = ''
om@13
    50
    
om@13
    51
    def __init__(self, application = None):
om@13
    52
        
om@13
    53
        # if we ain't got a path to start from, all is valid/lost
om@13
    54
        if len(sys.path[0]) == 0:
om@13
    55
            self._prefix_path = ''
om@13
    56
            self._data_path = ''
om@13
    57
            return
om@13
    58
        
om@13
    59
        # the prefix path
om@13
    60
        #
om@13
    61
        # - on Linux: this is ../../ to the current executable
om@13
    62
        #   e.g. "/usr/bin/myprogram" --> "/usr"
om@13
    63
        #
om@13
    64
        # - on Windows (inkl. Cygwin): this is the installation folder
om@13
    65
        #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
om@13
    66
        #
om@13
    67
        if sys.platform == 'linux2':
om@13
    68
            self._prefix_path = os.path.split(sys.path[0])[0]
om@13
    69
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
BarthaM@212
    70
            for app_path in sys.path:
BarthaM@212
    71
                if 'OpenSecurity\\bin' in app_path:
BarthaM@212
    72
                    self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
BarthaM@212
    73
                    break
BarthaM@212
    74
        
BarthaM@212
    75
        if self._prefix_path == '':
BarthaM@212
    76
            raise OSError()
om@13
    77
            
om@13
    78
        # the data path where all data files are stored
om@13
    79
        if sys.platform == 'linux2':
om@13
    80
            if not application is None:
om@13
    81
                self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
om@13
    82
            else:
om@13
    83
                self._data_path = os.path.join(self._prefix_path, 'share')
om@13
    84
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
om@13
    85
            self._data_path = self._prefix_path
oliver@71
    86
        else:
oliver@71
    87
            raise OSError()
om@13
    88
om@13
    89
            
om@13
    90
    def data_path_get(self):
om@13
    91
        """dat_path get"""
om@13
    92
        return self._data_path
om@13
    93
        
om@13
    94
    data_path = property(data_path_get)
om@13
    95
            
om@13
    96
            
om@13
    97
    def prefix_path_get(self):
om@13
    98
        """prefix_path get"""
om@13
    99
        return self._prefix_path
om@13
   100
        
om@13
   101
    prefix_path = property(prefix_path_get)
oliver@71
   102
    
oliver@71
   103
    
oliver@71
   104
    def log_path_get(self):
oliver@71
   105
        
oliver@71
   106
        """the path where log files should be stored"""
oliver@71
   107
        
oliver@73
   108
        user_log_path = os.path.expanduser(os.path.join('~', '.log'))
oliver@71
   109
        
oliver@71
   110
        if sys.platform == 'linux2':
om@13
   111
            
oliver@71
   112
            if os.access('/var/log', os.W_OK):
oliver@71
   113
                return '/var/log'
oliver@71
   114
            
oliver@154
   115
            if not Environment._log_warning_shown:
oliver@154
   116
                print('no permissions to write log files in /var/log, switching to ~/.log')
oliver@154
   117
                Environment._log_warning_shown = True
oliver@71
   118
            
oliver@71
   119
            if not os.path.exists(user_log_path):
oliver@71
   120
                os.mkdir(user_log_path)
oliver@71
   121
            elif not os.path.isdir(user_log_path):
oliver@71
   122
                raise IOError(user_log_path + ': not a folder')
oliver@71
   123
            
oliver@71
   124
            return user_log_path
oliver@71
   125
        
oliver@71
   126
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@73
   127
oliver@86
   128
            # in OpenSecurity we expect the log path tp be
oliver@86
   129
            # somewhere like C:\Program Files\OpenSecurity\log
oliver@86
   130
            # having this script residing in 
oliver@86
   131
            # C:\Progam Files\OpenSecurity\bin
oliver@91
   132
            ideal_log_path = os.path.normpath(os.path.join(self.prefix_path, 'log'))
oliver@73
   133
oliver@86
   134
            # check ideal path first
oliver@86
   135
            if not os.path.exists(ideal_log_path):
oliver@86
   136
                os.mkdir(ideal_log_path)
oliver@86
   137
            elif not os.path.isdir(ideal_log_path):
oliver@86
   138
                raise IOError(ideal_log_path + ': not a folder')
oliver@86
   139
oliver@86
   140
            return ideal_log_path
oliver@71
   141
        
oliver@71
   142
        else:
oliver@71
   143
            raise OSError()
oliver@71
   144
            
oliver@71
   145
    log_path = property(log_path_get)
oliver@71
   146
            
oliver@71
   147
            
oliver@71
   148
# test method
om@13
   149
def test():
om@13
   150
mb@46
   151
    """Test: class Environment"""
BarthaM@212
   152
    e = Environment('OpenSecurity')
mb@46
   153
    print('prefix_path: "{0}"'.format(e.prefix_path))
mb@46
   154
    print('  data_path: "{0}"'.format(e.data_path))
oliver@71
   155
    print('   log_path: "{0}"'.format(e.log_path))
mb@46
   156
mb@46
   157
om@13
   158
# test the module			
om@13
   159
if __name__ == '__main__':
mb@46
   160
    test()
oliver@91
   161