/
GESInit.py
executable file
·143 lines (122 loc) · 4.29 KB
/
GESInit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
################ Copyright 2005-2016 Team GoldenEye: Source #################
#
# This file is part of GoldenEye: Source's Python Library.
#
# GoldenEye: Source's Python Library 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, either version 3 of the License,
# or(at your option) any later version.
#
# GoldenEye: Source's Python Library 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 GoldenEye: Source's Python Library.
# If not, see <http://www.gnu.org/licenses/>.
#############################################################################
import sys, datetime
import GEUtil
from GEGlobal import PY_BASE_DIR
# -----------------
# Setup PYTHONPATH
#
def SetPaths():
# Clear the current path to make sure we don't have lingering bad directories
sys.path = []
# Append each directory that we want to look in for modules
sys.path.append( PY_BASE_DIR + "/lib" )
sys.path.append( PY_BASE_DIR + "/ges" )
sys.path.append( PY_BASE_DIR + "/debug" )
def LoadManager( name ):
import reimport
try:
if name in sys.modules:
reimport.reimport( name )
else:
__import__( name, globals(), locals() )
mgr = sys.modules[name]
return mgr.GetManager()
except KeyError as e:
GEUtil.Warning( "Failed to find manager named: %s!\n" % name )
raise e
except NameError:
GEUtil.Warning( "%s must define a 'GetManager' function!\n" % name )
def UnloadManager( name ):
try:
mgr = sys.modules[name]
mgr.PurgeManager()
import gc
gc.collect()
except KeyError:
GEUtil.Warning( "Failed to find manager named: %s!\n" % name )
except NameError:
GEUtil.Warning( "%s must define a 'PurgeManager' function!\n" % name )
# -----------------
# Error Logger
#
class ErrorLog( object ):
def __init__( self ):
log_file = PY_BASE_DIR + '/python.log'
log_header = ''
# Make sure our log doesn't get too large (512 KB limit)
try:
import os
if os.path.getsize( log_file ) > 512000:
os.remove( log_file )
log_header = '\n\n'
except:
pass
try:
# Open log to append new errors
self.log = open( log_file, 'a+' )
self.log.write( "%s------------------------------------------\n" % log_header )
self.log.write( "Log started on %s\n" % datetime.datetime.now() )
self.log.write( "------------------------------------------\n\n" )
self.log.flush()
except:
GEUtil.Warning( "Failed to open Python log file for write!\n" )
if self.log:
self.log.close()
self.log = None
def __del__( self ):
try:
# Close the log out
self.log.write( "\n------------------------------------------\n" )
self.log.write( "Log ended on %s\n" % datetime.datetime.now() )
self.log.write( "\n------------------------------------------\n\n" )
self.log.close()
except ( IOError, TypeError ):
pass
def write( self, lines ):
try:
# Print error to the log
self.log.writelines( lines )
self.log.flush()
except ( IOError, TypeError ):
pass
# -----------------
# I/O Redirection
#
class OutputCatcher( object ):
def __init__( self ):
self.data = ''
def write( self, stuff ):
GEUtil.Msg( stuff )
class OutputErrCatcher( object ):
def __init__( self ):
self.data = ''
self.log = ErrorLog()
def write( self, stuff ):
GEUtil.Warning( stuff )
self.log.write( stuff )
def init():
GEUtil.Msg( "PYTHON: Setting paths and output redirects!\n" )
# Call our path setter
SetPaths()
# Redirect the stdout and stderr to the game console
oc = OutputCatcher()
sys.stdout = oc
oec = OutputErrCatcher()
sys.stderr = oec