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