OpenSecurity/bin/environment.py
changeset 14 c187aaceca32
parent 3 65432e6c6042
child 16 e16d64b5e008
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/OpenSecurity/bin/environment.py	Fri Dec 06 12:10:30 2013 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +#!/bin/env python
     1.5 +# -*- coding: utf-8 -*-
     1.6 +
     1.7 +# ------------------------------------------------------------
     1.8 +# environment.py
     1.9 +# 
    1.10 +# pick some current environment infos
    1.11 +#
    1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    1.13 +#
    1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
    1.15 +# AIT Austrian Institute of Technology GmbH
    1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
    1.17 +# http://www.ait.ac.at
    1.18 +#
    1.19 +# This program is free software; you can redistribute it and/or
    1.20 +# modify it under the terms of the GNU General Public License
    1.21 +# as published by the Free Software Foundation version 2.
    1.22 +# 
    1.23 +# This program is distributed in the hope that it will be useful,
    1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.26 +# GNU General Public License for more details.
    1.27 +# 
    1.28 +# You should have received a copy of the GNU General Public License
    1.29 +# along with this program; if not, write to the Free Software
    1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    1.31 +# Boston, MA  02110-1301, USA.
    1.32 +# ------------------------------------------------------------
    1.33 +
    1.34 +
    1.35 +# ------------------------------------------------------------
    1.36 +# imports
    1.37 +
    1.38 +import os
    1.39 +import os.path
    1.40 +import sys
    1.41 +
    1.42 +
    1.43 +# ------------------------------------------------------------
    1.44 +# code
    1.45 +
    1.46 +
    1.47 +class Environment(object):
    1.48 +    
    1.49 +    """Hold some nifty environment stuff in a dedicated class."""
    1.50 +    
    1.51 +    def __init__(self, application = None):
    1.52 +        
    1.53 +        # if we ain't got a path to start from, all is valid/lost
    1.54 +        if len(sys.path[0]) == 0:
    1.55 +            self._prefix_path = ''
    1.56 +            self._data_path = ''
    1.57 +            return
    1.58 +        
    1.59 +        # the prefix path
    1.60 +        #
    1.61 +        # - on Linux: this is ../../ to the current executable
    1.62 +        #   e.g. "/usr/bin/myprogram" --> "/usr"
    1.63 +        #
    1.64 +        # - on Windows (inkl. Cygwin): this is the installation folder
    1.65 +        #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
    1.66 +        #
    1.67 +        if sys.platform == 'linux2':
    1.68 +            self._prefix_path = os.path.split(sys.path[0])[0]
    1.69 +        elif sys.platform == 'win32' or sys.platform == 'cygwin':
    1.70 +            self._prefix_path = sys.path[0]
    1.71 +            
    1.72 +        # the data path where all data files are stored
    1.73 +        if sys.platform == 'linux2':
    1.74 +            if not application is None:
    1.75 +                self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
    1.76 +            else:
    1.77 +                self._data_path = os.path.join(self._prefix_path, 'share')
    1.78 +        elif sys.platform == 'win32' or sys.platform == 'cygwin':
    1.79 +            self._data_path = self._prefix_path
    1.80 +
    1.81 +            
    1.82 +    def data_path_get(self):
    1.83 +        """dat_path get"""
    1.84 +        return self._data_path
    1.85 +        
    1.86 +    data_path = property(data_path_get)
    1.87 +            
    1.88 +            
    1.89 +    def prefix_path_get(self):
    1.90 +        """prefix_path get"""
    1.91 +        return self._prefix_path
    1.92 +        
    1.93 +    prefix_path = property(prefix_path_get)
    1.94 +            
    1.95 +# test method			
    1.96 +def test():
    1.97 +
    1.98 +	"""Test: class Environment"""
    1.99 +	e = Environment('My Application')
   1.100 +	print('prefix_path: "{0}"'.format(e.prefix_path))
   1.101 +	print('  data_path: "{0}"'.format(e.data_path))
   1.102 +			
   1.103 +			
   1.104 +# test the module			
   1.105 +if __name__ == '__main__':
   1.106 +	test()