diff -r 000000000000 -r c9bf2537109a ait/os/bin/opensecurityd/environment.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ait/os/bin/opensecurityd/environment.py Tue Nov 12 11:31:34 2013 +0100 @@ -0,0 +1,97 @@ +#!/bin/env python +# -*- coding: utf-8 -*- + +# ------------------------------------------------------------ +# environment.py +# +# pick some current environment infos +# +# Autor: Oliver Maurhart, +# +# Copyright (C) 2013 AIT Austrian Institute of Technology +# AIT Austrian Institute of Technology GmbH +# Donau-City-Strasse 1 | 1220 Vienna | Austria +# http://www.ait.ac.at +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation version 2. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301, USA. +# ------------------------------------------------------------ + + +# ------------------------------------------------------------ +# imports + +import os +import os.path +import sys + + +# ------------------------------------------------------------ +# code + + +class Environment(object): + + """Hold some nifty environment stuff in a dedicated class.""" + + def __init__(self, application = None): + + # if we ain't got a path to start from, all is valid/lost + if len(sys.path[0]) == 0: + self.prefix_path = '' + self.data_path = '' + self.image_path = '' + return + + # the prefix path + # + # - on Linux: this is ../../ to the current executable + # e.g. "/usr/bin/myprogram" --> "/usr" + # + # - on Windows: this is the installation folder + # e.g. "C:/Program Files/MyProgram/bin/myprogam" --> "C:/Program Files/MyProgram" + # + if sys.platform == 'linux2': + self.prefix_path = os.path.split(sys.path[0])[0] + elif sys.platform == 'win32': + self.prefix_path = os.path.split(sys.path[0])[0] + + # the data path where all data files are stored + if sys.platform == 'linux2': + if not application is None: + self.data_path = os.path.join(self.prefix_path, os.path.join('share', application)) + else: + self.data_path = os.path.join(self.prefix_path, 'share') + elif sys.platform == 'win32': + self.data_path = self.prefix_path + + # the image path + if sys.platform == 'linux2': + self.image_path = os.path.join(self.data_path, 'gfx') + elif sys.platform == 'win32': + self.image_path = os.path.join(self.data_path, 'gfx') + + +# test the module +def test(): + """Module test call.""" + + e = Environment("opensecurity") + print("prefix_path: {0}".format(e.prefix_path)) + print(" data_path: {0}".format(e.data_path)) + print(" image_path: {0}".format(e.image_path)) + +# standalone calls are module tests +if __name__ == '__main__': + test()