2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # pick some current environment infos
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
32 # ------------------------------------------------------------
40 # ------------------------------------------------------------
44 class Environment(object):
46 """Hold some nifty environment stuff in a dedicated class."""
48 _log_warning_shown = False
51 def __init__(self, application = None):
53 # if we ain't got a path to start from, all is valid/lost
54 if len(sys.path[0]) == 0:
55 self._prefix_path = ''
61 # - on Linux: this is ../../ to the current executable
62 # e.g. "/usr/bin/myprogram" --> "/usr"
64 # - on Windows (inkl. Cygwin): this is the installation folder
65 # e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
67 if sys.platform == 'linux2':
68 self._prefix_path = os.path.split(sys.path[0])[0]
69 elif sys.platform == 'win32' or sys.platform == 'cygwin':
70 for app_path in sys.path:
71 if 'OpenSecurity\\bin' in app_path:
72 self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
75 if self._prefix_path == '':
78 # the data path where all data files are stored
79 if sys.platform == 'linux2':
80 if not application is None:
81 self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
83 self._data_path = os.path.join(self._prefix_path, 'share')
84 elif sys.platform == 'win32' or sys.platform == 'cygwin':
85 self._data_path = self._prefix_path
90 def data_path_get(self):
92 return self._data_path
94 data_path = property(data_path_get)
97 def prefix_path_get(self):
99 return self._prefix_path
101 prefix_path = property(prefix_path_get)
104 def log_path_get(self):
106 """the path where log files should be stored"""
108 user_log_path = os.path.expanduser(os.path.join('~', '.log'))
110 if sys.platform == 'linux2':
112 if os.access('/var/log', os.W_OK):
115 if not Environment._log_warning_shown:
116 print('no permissions to write log files in /var/log, switching to ~/.log')
117 Environment._log_warning_shown = True
119 if not os.path.exists(user_log_path):
120 os.mkdir(user_log_path)
121 elif not os.path.isdir(user_log_path):
122 raise IOError(user_log_path + ': not a folder')
126 elif sys.platform == 'win32' or sys.platform == 'cygwin':
128 # in OpenSecurity we expect the log path tp be
129 # somewhere like C:\Program Files\OpenSecurity\log
130 # having this script residing in
131 # C:\Progam Files\OpenSecurity\bin
132 ideal_log_path = os.path.normpath(os.path.join(self.prefix_path, 'log'))
134 # check ideal path first
135 if not os.path.exists(ideal_log_path):
136 os.mkdir(ideal_log_path)
137 elif not os.path.isdir(ideal_log_path):
138 raise IOError(ideal_log_path + ': not a folder')
140 return ideal_log_path
145 log_path = property(log_path_get)
151 """Test: class Environment"""
152 e = Environment('OpenSecurity')
153 print('prefix_path: "{0}"'.format(e.prefix_path))
154 print(' data_path: "{0}"'.format(e.data_path))
155 print(' log_path: "{0}"'.format(e.log_path))
159 if __name__ == '__main__':