ait/os/bin/opensecurityd/environment.py
branchom
changeset 2 c9bf2537109a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ait/os/bin/opensecurityd/environment.py	Tue Nov 12 11:31:34 2013 +0100
     1.3 @@ -0,0 +1,97 @@
     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 +            self.image_path = ''
    1.58 +            return
    1.59 +        
    1.60 +        # the prefix path
    1.61 +        #
    1.62 +        # - on Linux: this is ../../ to the current executable
    1.63 +        #   e.g. "/usr/bin/myprogram" --> "/usr"
    1.64 +        #
    1.65 +        # - on Windows: this is the installation folder
    1.66 +        #   e.g. "C:/Program Files/MyProgram/bin/myprogam" --> "C:/Program Files/MyProgram"
    1.67 +        #
    1.68 +        if sys.platform == 'linux2':
    1.69 +            self.prefix_path = os.path.split(sys.path[0])[0]
    1.70 +        elif sys.platform == 'win32':
    1.71 +            self.prefix_path = os.path.split(sys.path[0])[0]
    1.72 +            
    1.73 +        # the data path where all data files are stored
    1.74 +        if sys.platform == 'linux2':
    1.75 +            if not application is None:
    1.76 +                self.data_path = os.path.join(self.prefix_path, os.path.join('share', application))
    1.77 +            else:
    1.78 +                self.data_path = os.path.join(self.prefix_path, 'share')
    1.79 +        elif sys.platform == 'win32':
    1.80 +            self.data_path = self.prefix_path
    1.81 +
    1.82 +        # the image path 
    1.83 +        if sys.platform == 'linux2':
    1.84 +            self.image_path = os.path.join(self.data_path, 'gfx')
    1.85 +        elif sys.platform == 'win32':
    1.86 +            self.image_path = os.path.join(self.data_path, 'gfx')
    1.87 +
    1.88 +
    1.89 +# test the module
    1.90 +def test():
    1.91 +    """Module test call."""
    1.92 +    
    1.93 +    e = Environment("opensecurity")
    1.94 +    print("prefix_path: {0}".format(e.prefix_path))
    1.95 +    print("  data_path: {0}".format(e.data_path))
    1.96 +    print(" image_path: {0}".format(e.image_path))
    1.97 +
    1.98 +# standalone calls are module tests
    1.99 +if __name__ == '__main__':
   1.100 +    test()