2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # pick some current environment infos
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
11 # Copyright 2013-2014 X-Net and AIT Austrian Institute of Technology
18 # https://www.x-net.at
20 # AIT Austrian Institute of Technology
21 # Donau City Strasse 1
24 # http://www.ait.ac.at
27 # Licensed under the Apache License, Version 2.0 (the "License");
28 # you may not use this file except in compliance with the License.
29 # You may obtain a copy of the License at
31 # http://www.apache.org/licenses/LICENSE-2.0
33 # Unless required by applicable law or agreed to in writing, software
34 # distributed under the License is distributed on an "AS IS" BASIS,
35 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 # See the License for the specific language governing permissions and
37 # limitations under the License.
38 # ------------------------------------------------------------
41 # ------------------------------------------------------------
49 # ------------------------------------------------------------
53 class Environment(object):
55 """Hold some nifty environment stuff in a dedicated class."""
57 _log_warning_shown = False
60 def __init__(self, application = None):
62 # if we ain't got a path to start from, all is valid/lost
63 if len(sys.path[0]) == 0:
64 self._prefix_path = ''
70 # - on Linux: this is ../../ to the current executable
71 # e.g. "/usr/bin/myprogram" --> "/usr"
73 # - on Windows (inkl. Cygwin): this is the installation folder
74 # e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
76 if sys.platform == 'linux2':
77 self._prefix_path = os.path.split(sys.path[0])[0]
78 elif sys.platform == 'win32' or sys.platform == 'cygwin':
79 for app_path in sys.path:
80 if 'OpenSecurity\\bin' in app_path:
81 self._prefix_path = os.path.normpath(os.path.join(app_path, '..'))
84 if self._prefix_path == '':
87 # the data path where all data files are stored
88 if sys.platform == 'linux2':
89 if not application is None:
90 self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
92 self._data_path = os.path.join(self._prefix_path, 'share')
93 elif sys.platform == 'win32' or sys.platform == 'cygwin':
94 self._data_path = self._prefix_path
99 def data_path_get(self):
101 return self._data_path
103 data_path = property(data_path_get)
106 def prefix_path_get(self):
107 """prefix_path get"""
108 return self._prefix_path
110 prefix_path = property(prefix_path_get)
113 def log_path_get(self):
115 """the path where log files should be stored"""
117 user_log_path = os.path.expanduser(os.path.join('~', '.log'))
119 if sys.platform == 'linux2':
121 if os.access('/var/log', os.W_OK):
124 if not Environment._log_warning_shown:
125 print('no permissions to write log files in /var/log, switching to ~/.log')
126 Environment._log_warning_shown = True
128 if not os.path.exists(user_log_path):
129 os.mkdir(user_log_path)
130 elif not os.path.isdir(user_log_path):
131 raise IOError(user_log_path + ': not a folder')
135 elif sys.platform == 'win32' or sys.platform == 'cygwin':
137 # in OpenSecurity we expect the log path tp be
138 # somewhere like C:\Program Files\OpenSecurity\log
139 # having this script residing in
140 # C:\Progam Files\OpenSecurity\bin
141 ideal_log_path = os.path.normpath(os.path.join(self.prefix_path, 'log'))
143 # check ideal path first
144 if not os.path.exists(ideal_log_path):
145 os.mkdir(ideal_log_path)
146 elif not os.path.isdir(ideal_log_path):
147 raise IOError(ideal_log_path + ': not a folder')
149 return ideal_log_path
154 log_path = property(log_path_get)
160 """Test: class Environment"""
161 e = Environment('OpenSecurity')
162 print('prefix_path: "{0}"'.format(e.prefix_path))
163 print(' data_path: "{0}"'.format(e.data_path))
164 print(' log_path: "{0}"'.format(e.log_path))
168 if __name__ == '__main__':