OpenSecurity/bin/environment.py
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 29 Oct 2014 15:18:22 +0100
changeset 240 d7ef04254e9c
parent 212 59ebaa44c12c
permissions -rwxr-xr-x
lizenz fixed in all files
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
#
oliver@240
    11
# Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
om@13
    12
# 
om@13
    13
# 
oliver@240
    14
#     X-Net Services GmbH
oliver@240
    15
#     Elisabethstrasse 1
oliver@240
    16
#     4020 Linz
oliver@240
    17
#     AUSTRIA
oliver@240
    18
#     https://www.x-net.at
oliver@240
    19
# 
oliver@240
    20
#     AIT Austrian Institute of Technology
oliver@240
    21
#     Donau City Strasse 1
oliver@240
    22
#     1220 Wien
oliver@240
    23
#     AUSTRIA
oliver@240
    24
#     http://www.ait.ac.at
oliver@240
    25
# 
oliver@240
    26
# 
oliver@240
    27
# Licensed under the Apache License, Version 2.0 (the "License");
oliver@240
    28
# you may not use this file except in compliance with the License.
oliver@240
    29
# You may obtain a copy of the License at
oliver@240
    30
# 
oliver@240
    31
#    http://www.apache.org/licenses/LICENSE-2.0
oliver@240
    32
# 
oliver@240
    33
# Unless required by applicable law or agreed to in writing, software
oliver@240
    34
# distributed under the License is distributed on an "AS IS" BASIS,
oliver@240
    35
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
oliver@240
    36
# See the License for the specific language governing permissions and
oliver@240
    37
# limitations under the License.
om@13
    38
# ------------------------------------------------------------
om@13
    39
om@13
    40
om@13
    41
# ------------------------------------------------------------
om@13
    42
# imports
om@13
    43
om@13
    44
import os
om@13
    45
import os.path
om@13
    46
import sys
om@13
    47
om@13
    48
om@13
    49
# ------------------------------------------------------------
om@13
    50
# code
om@13
    51
om@13
    52
om@13
    53
class Environment(object):
om@13
    54
    
om@13
    55
    """Hold some nifty environment stuff in a dedicated class."""
oliver@154
    56
oliver@154
    57
    _log_warning_shown = False
BarthaM@212
    58
    _prefix_path = ''
om@13
    59
    
om@13
    60
    def __init__(self, application = None):
om@13
    61
        
om@13
    62
        # if we ain't got a path to start from, all is valid/lost
om@13
    63
        if len(sys.path[0]) == 0:
om@13
    64
            self._prefix_path = ''
om@13
    65
            self._data_path = ''
om@13
    66
            return
om@13
    67
        
om@13
    68
        # the prefix path
om@13
    69
        #
om@13
    70
        # - on Linux: this is ../../ to the current executable
om@13
    71
        #   e.g. "/usr/bin/myprogram" --> "/usr"
om@13
    72
        #
om@13
    73
        # - on Windows (inkl. Cygwin): this is the installation folder
om@13
    74
        #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
om@13
    75
        #
om@13
    76
        if sys.platform == 'linux2':
om@13
    77
            self._prefix_path = os.path.split(sys.path[0])[0]
om@13
    78
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
BarthaM@212
    79
            for app_path in sys.path:
BarthaM@212
    80
                if 'OpenSecurity\\bin' in app_path:
BarthaM@212
    81
                    self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
BarthaM@212
    82
                    break
BarthaM@212
    83
        
BarthaM@212
    84
        if self._prefix_path == '':
BarthaM@212
    85
            raise OSError()
om@13
    86
            
om@13
    87
        # the data path where all data files are stored
om@13
    88
        if sys.platform == 'linux2':
om@13
    89
            if not application is None:
om@13
    90
                self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
om@13
    91
            else:
om@13
    92
                self._data_path = os.path.join(self._prefix_path, 'share')
om@13
    93
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
om@13
    94
            self._data_path = self._prefix_path
oliver@71
    95
        else:
oliver@71
    96
            raise OSError()
om@13
    97
om@13
    98
            
om@13
    99
    def data_path_get(self):
om@13
   100
        """dat_path get"""
om@13
   101
        return self._data_path
om@13
   102
        
om@13
   103
    data_path = property(data_path_get)
om@13
   104
            
om@13
   105
            
om@13
   106
    def prefix_path_get(self):
om@13
   107
        """prefix_path get"""
om@13
   108
        return self._prefix_path
om@13
   109
        
om@13
   110
    prefix_path = property(prefix_path_get)
oliver@71
   111
    
oliver@71
   112
    
oliver@71
   113
    def log_path_get(self):
oliver@71
   114
        
oliver@71
   115
        """the path where log files should be stored"""
oliver@71
   116
        
oliver@73
   117
        user_log_path = os.path.expanduser(os.path.join('~', '.log'))
oliver@71
   118
        
oliver@71
   119
        if sys.platform == 'linux2':
om@13
   120
            
oliver@71
   121
            if os.access('/var/log', os.W_OK):
oliver@71
   122
                return '/var/log'
oliver@71
   123
            
oliver@154
   124
            if not Environment._log_warning_shown:
oliver@154
   125
                print('no permissions to write log files in /var/log, switching to ~/.log')
oliver@154
   126
                Environment._log_warning_shown = True
oliver@71
   127
            
oliver@71
   128
            if not os.path.exists(user_log_path):
oliver@71
   129
                os.mkdir(user_log_path)
oliver@71
   130
            elif not os.path.isdir(user_log_path):
oliver@71
   131
                raise IOError(user_log_path + ': not a folder')
oliver@71
   132
            
oliver@71
   133
            return user_log_path
oliver@71
   134
        
oliver@71
   135
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
oliver@73
   136
oliver@86
   137
            # in OpenSecurity we expect the log path tp be
oliver@86
   138
            # somewhere like C:\Program Files\OpenSecurity\log
oliver@86
   139
            # having this script residing in 
oliver@86
   140
            # C:\Progam Files\OpenSecurity\bin
oliver@91
   141
            ideal_log_path = os.path.normpath(os.path.join(self.prefix_path, 'log'))
oliver@73
   142
oliver@86
   143
            # check ideal path first
oliver@86
   144
            if not os.path.exists(ideal_log_path):
oliver@86
   145
                os.mkdir(ideal_log_path)
oliver@86
   146
            elif not os.path.isdir(ideal_log_path):
oliver@86
   147
                raise IOError(ideal_log_path + ': not a folder')
oliver@86
   148
oliver@86
   149
            return ideal_log_path
oliver@71
   150
        
oliver@71
   151
        else:
oliver@71
   152
            raise OSError()
oliver@71
   153
            
oliver@71
   154
    log_path = property(log_path_get)
oliver@71
   155
            
oliver@71
   156
            
oliver@71
   157
# test method
om@13
   158
def test():
om@13
   159
mb@46
   160
    """Test: class Environment"""
BarthaM@212
   161
    e = Environment('OpenSecurity')
mb@46
   162
    print('prefix_path: "{0}"'.format(e.prefix_path))
mb@46
   163
    print('  data_path: "{0}"'.format(e.data_path))
oliver@71
   164
    print('   log_path: "{0}"'.format(e.log_path))
mb@46
   165
mb@46
   166
om@13
   167
# test the module			
om@13
   168
if __name__ == '__main__':
mb@46
   169
    test()
oliver@91
   170