* Better Patch for Configfile handling
[opeanno-debian-packaging.git] / openanno.py
blob282f5d6554411df5b20367d13ce5e62b7da0e298
1 #!/usr/bin/env python
3 # ###################################################
4 # Copyright (C) 2008 The OpenAnno Team
5 # team@openanno.org
6 # This file is part of OpenAnno.
8 # OpenAnno is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 # ###################################################
24 """This is the OpenAnno launcher, it looks for fife and tries to start the game. If you want to digg
25 into the game, continue to game/main.py. Read all docstrings and get familiar with the functions and
26 attributes. I will mark all tutorial instructions with 'TUTORIAL:'. Have fun :-)"""
28 import sys
29 import os
30 def findFIFE():
31 # check if fife is already in python-path
32 try:
33 import fife
34 return
35 except (ImportError):
36 pass
38 global fife_path
39 try:
40 import config
41 _paths = [config.fife_path]
42 except (ImportError, AttributeError):
43 _paths = []
44 _paths += [ a + '/' + b + '/' + c for a in ('.', '..', '../..') for b in ('.', 'fife', 'FIFE', 'Fife') for c in ('.', 'trunk') ]
46 fife_path = None
47 for p in _paths:
48 if p not in sys.path:
49 # check if we are in a fife dir...
50 for pe in [ os.path.abspath(p + '/' + a) for a in ('.', 'engine', 'engine/extensions', 'engine/swigwrappers/python') ]:
51 if not os.path.exists(pe):
52 break
53 else:
54 fife_path = p
55 print "Found FIFE in:", fife_path
57 #add python paths (<fife>/engine/extensions <fife>/engine/swigwrappers/python)
58 for pe in [ os.path.abspath(fife_path + '/' + a) for a in ('engine/extensions', 'engine/swigwrappers/python') ]:
59 if os.path.exists(pe):
60 sys.path.append(pe)
61 os.environ['PYTHONPATH'] = os.path.pathsep.join(os.environ.get('PYTHONPATH', '').split(os.path.pathsep) + [ os.path.abspath(fife_path + '/' + a) for a in ('engine/extensions', 'engine/swigwrappers/python') ])
63 #add windows paths (<fife>/.)
64 os.environ['PATH'] = os.path.pathsep.join(os.environ.get('PATH', '').split(os.path.pathsep) + [ os.path.abspath(fife_path + '/' + a) for a in ('.') ])
65 os.path.defpath += os.path.pathsep + os.path.pathsep.join([ os.path.abspath(fife_path + '/' + a) for a in ('.') ])
66 break
67 else:
68 print 'FIFE was not found.'
69 print "Please create a config.py file and add a line with: fife_path = '<path to fife>' eg. fife_path = '../../fife/trunk/'"
70 exit()
72 try:
73 if not os.environ.get('LD_LIBRARY_PATH', '').startswith(os.path.abspath(fife_path)):
74 try:
75 import fife
76 except ImportError, e:
77 os.environ['LD_LIBRARY_PATH'] = os.path.pathsep.join([ os.path.abspath(p + '/' + a) for a in ('ext/minizip', 'ext/install/lib') ] + (os.environ['LD_LIBRARY_PATH'].split(os.path.pathsep) if os.environ.has_key('LD_LIBRARY_PATH') else []))
78 print "Restarting with proper LD_LIBRARY_PATH..."
79 args = [sys.executable] + sys.argv
80 #we are already in openanno root, so just exec local executeable
81 args[1] = os.path.split(os.path.realpath(args[1]))[1]
82 # support for python -O flag (disables __debug__)
83 if not __debug__:
84 args.insert(1, '-O')
85 os.execvp(args[0], args)
86 else:
87 import fife
88 except ImportError, e:
89 print 'FIFE was not found or failed to load.'
90 print 'Reason: ' + e.message
91 print "Please create a config.py file and add a line with: path = '<path to fife>' eg. path = '../../fife/trunk/'"
92 exit()
95 def getFifePath():
96 global fife_path
97 try:
98 import config
99 _paths = [config.fife_path]
100 except (ImportError, AttributeError):
101 _paths = []
102 _paths += [ a + '/' + b + '/' + c for a in ('.', '..', '../..') for b in ('.', 'fife', 'FIFE', 'Fife') for c in ('.', 'trunk') ]
104 fife_path = None
105 for p in _paths:
106 if p not in sys.path:
107 # check if we are in a fife dir...
108 for pe in [ os.path.abspath(p + '/' + a) for a in ('.', 'engine', 'engine/extensions', 'engine/swigwrappers/python') ]:
109 if not os.path.exists(pe):
110 break
111 else:
112 fife_path = p
113 print "Found FIFE in:", fife_path
115 #add python paths (<fife>/engine/extensions <fife>/engine/swigwrappers/python)
116 for pe in [ os.path.abspath(fife_path + '/' + a) for a in ('engine/extensions', 'engine/swigwrappers/python') ]:
117 if os.path.exists(pe):
118 sys.path.append(pe)
119 os.environ['PYTHONPATH'] = os.path.pathsep.join(os.environ.get('PYTHONPATH', '').split(os.path.pathsep) + [ os.path.abspath(fife_path + '/' + a) for a in ('engine/extensions', 'engine/swigwrappers/python') ])
121 #add windows paths (<fife>/.)
122 os.environ['PATH'] = os.path.pathsep.join(os.environ.get('PATH', '').split(os.path.pathsep) + [ os.path.abspath(fife_path + '/' + a) for a in ('.') ])
123 os.path.defpath += os.path.pathsep + os.path.pathsep.join([ os.path.abspath(fife_path + '/' + a) for a in ('.') ])
124 break
125 else:
126 print 'FIFE was not found.'
127 print "Please create a config.py file and add a line with: fife_path = '<path to fife>' eg. fife_path = '../../fife/trunk/'"
128 exit()
129 return fife_path
131 if __name__ == '__main__':
132 #chdir to openanno root
133 os.chdir( os.path.split( os.path.realpath( sys.argv[0]) )[0] )
135 #find fife and setup search paths
136 findFIFE()
138 #for some external libraries distributed with openanno
139 sys.path.append('game/ext')
141 #start openanno
142 import game.main
143 game.main.start()