'Waits for a Stack to reach a given status.'

def wait_for_stack_status(self, stack_identifier, status, failure_pattern='^.*_FAILED$'):

    start = int(time.time())
    fail_regexp = re.compile(failure_pattern)
    while True:
        try:
            body = self.show_stack(stack_identifier)['stack']
        except lib_exc.NotFound:
            if (status == 'DELETE_COMPLETE'):
                return
        stack_name = body['stack_name']
        stack_status = body['stack_status']
        if (stack_status == status):
            return body
        if fail_regexp.search(stack_status):
            raise exceptions.StackBuildErrorException(stack_identifier=stack_identifier, stack_status=stack_status, stack_status_reason=body['stack_status_reason'])
        if ((int(time.time()) - start) >= self.build_timeout):
            message = ('Stack    %s    failed    to    reach    %s    status    (current:    %s)    within    the    required    time    (%s    s).' % (stack_name, status, stack_status, self.build_timeout))
            raise lib_exc.TimeoutException(message)
        time.sleep(self.build_interval)
