<s> import sys , os , traceback <EOL> supportFilesPath = os . path . join ( <EOL> os . path . dirname ( os . path . dirname ( <EOL> os . path . dirname ( os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) ) ) ) , "<STR_LIT>" ) <EOL> sys . path . append ( supportFilesPath ) <EOL> import httplib , urllib , json <EOL> import arcpy <EOL> import OpsServerConfig <EOL> import subprocess <EOL> from Utilities import makePath <EOL> from Utilities import findFilePath <EOL> from Utilities import editFiles <EOL> serverName = OpsServerConfig . serverName <EOL> serverPort = OpsServerConfig . serverPort <EOL> dbsForOpsServer = OpsServerConfig . databasesToCreate <EOL> rootPostgreSQLPath = OpsServerConfig . getPostgreSQLRootPath ( ) <EOL> siteExistsCheck = True <EOL> dbExistsCheck = True <EOL> def PreConfigChecks ( agsSiteAdminUserName , password ) : <EOL> try : <EOL> success = True <EOL> msgPrefix = "<STR_LIT>" <EOL> print <EOL> print "<STR_LIT>" <EOL> print <EOL> if siteExistsCheck : <EOL> print "<STR_LIT>" + serverName + "<STR_LIT:'>" <EOL> token = getToken ( agsSiteAdminUserName , password , serverName , serverPort ) <EOL> print <EOL> if token is None : <EOL> print "<STR_LIT>" <EOL> success = True <EOL> else : <EOL> print "<STR_LIT>" <EOL> success = False <EOL> print <EOL> if dbExistsCheck : <EOL> print "<STR_LIT>" <EOL> print <EOL> dbList = getPostgreSQLDatabases ( password ) <EOL> dbListAsStr = str ( dbList ) . strip ( '<STR_LIT>' ) <EOL> dbListAsStr = dbListAsStr . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) . lower ( ) <EOL> print <EOL> for db in dbsForOpsServer : <EOL> if dbListAsStr . find ( db . lower ( ) ) > - <NUM_LIT:1> : <EOL> success = False <EOL> print "<STR_LIT>" + db + "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" + db + "<STR_LIT>" <EOL> except : <EOL> success = False <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> tbinfo = traceback . format_tb ( tb ) [ <NUM_LIT:0> ] <EOL> pymsg = "<STR_LIT>" + tbinfo + "<STR_LIT>" + str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> msgs = "<STR_LIT>" + arcpy . GetMessages ( ) + "<STR_LIT:\n>" <EOL> print <EOL> print "<STR_LIT>" <EOL> print pymsg + "<STR_LIT:\n>" <EOL> print msgs <EOL> finally : <EOL> return success <EOL> def getPostgreSQLDatabases ( password ) : <EOL> try : <EOL> dbList = [ ] <EOL> f = "<STR_LIT>" <EOL> confFileExists = False <EOL> confFileFolder = os . path . join ( os . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> confFilePath = os . path . join ( confFileFolder , "<STR_LIT>" ) <EOL> scriptFileFolder = os . path . dirname ( sys . argv [ <NUM_LIT:0> ] ) <EOL> scriptFilePath = os . path . join ( scriptFileFolder , "<STR_LIT>" ) <EOL> outFile = os . path . join ( scriptFileFolder , "<STR_LIT>" ) <EOL> if os . path . exists ( outFile ) : <EOL> os . remove ( outFile ) <EOL> confString = "<STR_LIT>" + password <EOL> if not os . path . exists ( confFileFolder ) : <EOL> os . makedirs ( confFileFolder ) <EOL> if os . path . exists ( confFilePath ) : <EOL> confFileExists = True <EOL> os . rename ( confFilePath , confFilePath + "<STR_LIT>" ) <EOL> conf_f = open ( confFilePath , "<STR_LIT:w>" ) <EOL> conf_f . write ( confString + "<STR_LIT:\n>" ) <EOL> conf_f . close ( ) <EOL> print "<STR_LIT>" + scriptFilePath + "<STR_LIT>" <EOL> psqlExePath = findFilePath ( rootPostgreSQLPath , "<STR_LIT>" ) <EOL> strToWrite = '<STR_LIT:">' + psqlExePath + '<STR_LIT>' + '<STR_LIT>' + scriptFileFolder + os . sep + '<STR_LIT>' + outFile <EOL> batFile_f = open ( scriptFilePath , "<STR_LIT:w>" ) <EOL> batFile_f . write ( strToWrite + "<STR_LIT:\n>" ) <EOL> batFile_f . close ( ) <EOL> print "<STR_LIT>" + scriptFilePath + "<STR_LIT:)>" <EOL> print "<STR_LIT>" <EOL> subprocess . call ( scriptFilePath ) <EOL> if os . path . exists ( outFile ) : <EOL> f = open ( outFile , "<STR_LIT:r>" ) <EOL> for line in f : <EOL> line = line . strip ( ) <EOL> if line == "<STR_LIT>" or line . find ( "<STR_LIT>" ) < > - <NUM_LIT:1> or line . find ( "<STR_LIT>" ) < > - <NUM_LIT:1> : <EOL> continue <EOL> if len ( line ) > <NUM_LIT:0> : <EOL> dbList . append ( line ) <EOL> except : <EOL> tb = sys . exc_info ( ) [ <NUM_LIT:2> ] <EOL> tbinfo = traceback . format_tb ( tb ) [ <NUM_LIT:0> ] <EOL> pymsg = "<STR_LIT>" + tbinfo + "<STR_LIT>" + str ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> print <EOL> print "<STR_LIT>" <EOL> print pymsg + "<STR_LIT:\n>" <EOL> finally : <EOL> if f : <EOL> f . close ( ) <EOL> if conf_f : <EOL> conf_f . close ( ) <EOL> os . remove ( confFilePath ) <EOL> if confFileExists : <EOL> os . rename ( confFilePath + "<STR_LIT>" , confFilePath ) <EOL> return dbList <EOL> def getToken ( username , password , serverName , serverPort ) : <EOL> tokenURL = "<STR_LIT>" <EOL> params = urllib . urlencode ( { '<STR_LIT:username>' : username , '<STR_LIT:password>' : password , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:f>' : '<STR_LIT>' } ) <EOL> headers = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> httpConn = httplib . HTTPConnection ( serverName , serverPort ) <EOL> httpConn . request ( "<STR_LIT:POST>" , tokenURL , params , headers ) <EOL> response = httpConn . getresponse ( ) <EOL> if ( response . status != <NUM_LIT:200> ) : <EOL> httpConn . close ( ) <EOL> print "<STR_LIT>" <EOL> return None <EOL> else : <EOL> data = response . read ( ) <EOL> httpConn . close ( ) <EOL> if not assertJsonSuccess ( data ) : <EOL> return None <EOL> token = json . loads ( data ) <EOL> return token [ '<STR_LIT>' ] <EOL> def assertJsonSuccess ( data ) : <EOL> obj = json . loads ( data ) <EOL> if '<STR_LIT:status>' in obj and obj [ '<STR_LIT:status>' ] == "<STR_LIT:error>" : <EOL> return False <EOL> else : <EOL> return True </s>
