ait/os/bin/opensecurityd/environment.py
author om
Tue, 12 Nov 2013 11:31:34 +0100
branchom
changeset 2 c9bf2537109a
permissions -rwxr-xr-x
added C/C++ and Python sources
     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 (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import os.path
    37 import sys
    38 
    39 
    40 # ------------------------------------------------------------
    41 # code
    42 
    43 
    44 class Environment(object):
    45     
    46     """Hold some nifty environment stuff in a dedicated class."""
    47     
    48     def __init__(self, application = None):
    49         
    50         # if we ain't got a path to start from, all is valid/lost
    51         if len(sys.path[0]) == 0:
    52             self.prefix_path = ''
    53             self.data_path = ''
    54             self.image_path = ''
    55             return
    56         
    57         # the prefix path
    58         #
    59         # - on Linux: this is ../../ to the current executable
    60         #   e.g. "/usr/bin/myprogram" --> "/usr"
    61         #
    62         # - on Windows: this is the installation folder
    63         #   e.g. "C:/Program Files/MyProgram/bin/myprogam" --> "C:/Program Files/MyProgram"
    64         #
    65         if sys.platform == 'linux2':
    66             self.prefix_path = os.path.split(sys.path[0])[0]
    67         elif sys.platform == 'win32':
    68             self.prefix_path = os.path.split(sys.path[0])[0]
    69             
    70         # the data path where all data files are stored
    71         if sys.platform == 'linux2':
    72             if not application is None:
    73                 self.data_path = os.path.join(self.prefix_path, os.path.join('share', application))
    74             else:
    75                 self.data_path = os.path.join(self.prefix_path, 'share')
    76         elif sys.platform == 'win32':
    77             self.data_path = self.prefix_path
    78 
    79         # the image path 
    80         if sys.platform == 'linux2':
    81             self.image_path = os.path.join(self.data_path, 'gfx')
    82         elif sys.platform == 'win32':
    83             self.image_path = os.path.join(self.data_path, 'gfx')
    84 
    85 
    86 # test the module
    87 def test():
    88     """Module test call."""
    89     
    90     e = Environment("opensecurity")
    91     print("prefix_path: {0}".format(e.prefix_path))
    92     print("  data_path: {0}".format(e.data_path))
    93     print(" image_path: {0}".format(e.image_path))
    94 
    95 # standalone calls are module tests
    96 if __name__ == '__main__':
    97     test()