00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 from objects.logic.common.logger import logger
00018 from objects.logic.common.exception import exception
00019 from objects.logic.utils.timeUtil import timeUtil
00020 import os
00021 from array import array
00022 from objects.logic.common.globalStrings import globalStrings
00023 import shutil
00024 import time
00025
00026
00027
00028
00029
00030 class geoIPUtils():
00031
00032
00033 def __init__(self, path):
00034 self.path = path
00035 logger.log(__name__ + ": Setting GEO IP Database Path: " + self.path)
00036
00037
00038
00039
00040 def getLastUpdateTime(self):
00041 infoFile = None
00042
00043 try:
00044 infoFile = open(globalStrings.geoIPInfoPath(),"r")
00045 infoString = infoFile.read(512);
00046 return infoString
00047
00048 except Exception, e:
00049 logger.log(__name__ + ": There was an error opening GEO IP info file: " + str(e))
00050 logger.log(__name__ + ": Could not determine when the last time the GEO IP Database was updated")
00051 return "";
00052
00053 finally:
00054 if infoFile != None:
00055 infoFile.close()
00056
00057
00058
00059
00060
00061 def isGEOIPDatabaseInstalled(self):
00062 return os.path.exists( globalStrings.geoIPLocPath() )
00063
00064
00065
00066
00067 def updateGEOIPDatabase(self, path):
00068 try:
00069 infoFile = None
00070
00071
00072 try:
00073 os.mkdir(globalStrings.geoIPResPath)
00074 os.mkdir(globalStrings.geoIPPath)
00075 except Exception, ex:
00076 logger.log(__name__ + "GEO IP Database folder already exists. This is not necessarily an error.")
00077
00078
00079 shutil.copyfile(path, globalStrings.tempIPLocPath())
00080
00081
00082 infoFile = open(globalStrings.tempIPInfoPath(),"w")
00083
00084 infoFile.write(timeUtil.formatDateTime(time.time()));
00085
00086 except Exception, ex:
00087 strErr = "There was an error updating database files: %s" % str(ex)
00088 logger.log(__name__ + ": " + strErr)
00089
00090 raise Exception, strErr
00091 finally:
00092 if infoFile != None:
00093 infoFile.close()
00094
00095
00096
00097
00098
00099 def installGeoIPIfNecessary():
00100 if os.path.isfile(globalStrings.tempIPInfoPath()):
00101 try:
00102 shutil.copyfile(globalStrings.tempIPInfoPath(), globalStrings.geoIPInfoPath())
00103 shutil.copyfile(globalStrings.tempIPLocPath(), globalStrings.geoIPLocPath())
00104
00105 os.remove(globalStrings.tempIPInfoPath())
00106 os.remove(globalStrings.tempIPLocPath())
00107 except Exception, ex:
00108 strErr = "There was an error updating database files: %s" % str(ex)
00109 logger.log(__name__ + ": " + strErr)
00110
00111 raise Exception, strErr
00112
00113
00114 try:
00115 shutil.copyfile(globalStrings.tempIPIspInfoPath(), globalStrings.geoIPIspPath())
00116 shutil.copyfile(globalStrings.tempIPOrgInfoPath(), globalStrings.geoIPOrgPath())
00117
00118 os.remove(globalStrings.tempIPIspInfoPath())
00119 os.remove(globalStrings.tempIPOrgInfoPath())
00120
00121 except Exception, ex:
00122
00123 strErr = "There was an error updating optional database files: %s" % str(ex)
00124 logger.log(__name__ + ": " + strErr)
00125
00126
00127 installGeoIPIfNecessary = staticmethod(installGeoIPIfNecessary)
00128
00129
00130
00131
00132