'Pull down the files, verify  and then stop the current process'

def _upgradeAgent(self, newAgentVersion):

    res = urllib2.urlopen((self.settings.upgrade_url % {'key': self.settings.mms_key}))
    resBson = None
    try:
        resBson = bson.decode_all(res.read())
    finally:
        if (res is not None):
            res.close()
            res = None
    if (len(resBson) != 1):
        return
    upgradeResponse = resBson[0]
    if (('status' not in upgradeResponse) or (upgradeResponse['status'] != 'ok') or ('files' not in upgradeResponse)):
        return
    for fileInfo in upgradeResponse['files']:
        if (fileInfo['fileAuthCode'] != hmac.new(self.settings.secret_key, fileInfo['file'], digestmod=hashlib.sha1).hexdigest()):
            self.logger.error('Invalid    file    auth    code    for    upgrade    -    cancelling')
            return
        if (fileInfo['fileNameAuthCode'] != hmac.new(self.settings.secret_key, fileInfo['fileName'], digestmod=hashlib.sha1).hexdigest()):
            self.logger.error('Invalid    file    name    auth    code    for    upgrade    -    cancelling')
            return
    for fileInfo in upgradeResponse['files']:
        fileContent = fileInfo['file']
        fileName = fileInfo['fileName']
        if ((fileName == 'settings.py') and (getattr(self.settings, 'globalAuthUsername', None) is not None) and (getattr(self.settings, 'globalAuthPassword', None) is not None)):
            fileContent = fileContent.replace('globalAuthPassword    =    None', ('globalAuthPassword=%r' % self.settings.globalAuthPassword))
            fileContent = fileContent.replace('globalAuthUsername    =    None', ('globalAuthUsername=%r' % self.settings.globalAuthUsername))
        fileSystemName = os.path.join(self.agentDir, fileName)
        newFile = open(fileSystemName, 'w')
        try:
            newFile.write(fileContent)
        finally:
            if (newFile is not None):
                newFile.close()
    try:
        self.processContainer.stopAgentProcess()
        self.settings.settingsAgentVersion = newAgentVersion
        self.logger.info((('Agent    upgraded    to    version:    ' + newAgentVersion) + '    -    there    is    up    to    a    five    minute    timeout    before    data    will    be    sent    again'))
    except Exception as e:
        self.logger.error(('Problem    restarting    agent    process:    ' + traceback.format_exc(e)))
