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