OpenSecurity/bin/environment.py
author mb
Thu, 09 Jan 2014 10:44:42 +0100
changeset 46 f659d8fb57a8
parent 16 e16d64b5e008
child 71 0ca25608ed0f
permissions -rwxr-xr-x
latest changes from december 2013
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."""
om@13
    47
    
om@13
    48
    def __init__(self, application = None):
om@13
    49
        
om@13
    50
        # if we ain't got a path to start from, all is valid/lost
om@13
    51
        if len(sys.path[0]) == 0:
om@13
    52
            self._prefix_path = ''
om@13
    53
            self._data_path = ''
om@13
    54
            return
om@13
    55
        
om@13
    56
        # the prefix path
om@13
    57
        #
om@13
    58
        # - on Linux: this is ../../ to the current executable
om@13
    59
        #   e.g. "/usr/bin/myprogram" --> "/usr"
om@13
    60
        #
om@13
    61
        # - on Windows (inkl. Cygwin): this is the installation folder
om@13
    62
        #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
om@13
    63
        #
om@13
    64
        if sys.platform == 'linux2':
om@13
    65
            self._prefix_path = os.path.split(sys.path[0])[0]
om@13
    66
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
om@13
    67
            self._prefix_path = sys.path[0]
om@13
    68
            
om@13
    69
        # the data path where all data files are stored
om@13
    70
        if sys.platform == 'linux2':
om@13
    71
            if not application is None:
om@13
    72
                self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
om@13
    73
            else:
om@13
    74
                self._data_path = os.path.join(self._prefix_path, 'share')
om@13
    75
        elif sys.platform == 'win32' or sys.platform == 'cygwin':
om@13
    76
            self._data_path = self._prefix_path
om@13
    77
om@13
    78
            
om@13
    79
    def data_path_get(self):
om@13
    80
        """dat_path get"""
om@13
    81
        return self._data_path
om@13
    82
        
om@13
    83
    data_path = property(data_path_get)
om@13
    84
            
om@13
    85
            
om@13
    86
    def prefix_path_get(self):
om@13
    87
        """prefix_path get"""
om@13
    88
        return self._prefix_path
om@13
    89
        
om@13
    90
    prefix_path = property(prefix_path_get)
om@13
    91
            
om@13
    92
# test method			
om@13
    93
def test():
om@13
    94
mb@46
    95
    """Test: class Environment"""
mb@46
    96
    e = Environment('My Application')
mb@46
    97
    print('prefix_path: "{0}"'.format(e.prefix_path))
mb@46
    98
    print('  data_path: "{0}"'.format(e.data_path))
mb@46
    99
mb@46
   100
om@13
   101
# test the module			
om@13
   102
if __name__ == '__main__':
mb@46
   103
    test()