Exemplo n.º 1
0
    def clean_copy(self):
        ''' returns 'clean' taskresult object '''

        # FIXME: clean task_fields, _task and _host copies
        result = TaskResult(self._host, self._task, {}, self._task_fields)

        # statuses are already reflected on the event type
        if result._task and result._task.action in ['debug']:
            # debug is verbose by default to display vars, no need to add invocation
            ignore = _IGNORE + ('invocation', )
        else:
            ignore = _IGNORE

        subset = {}
        # preserve subset for later
        for sub in _SUB_PRESERVE:
            if sub in self._result:
                subset[sub] = {}
                for key in _SUB_PRESERVE[sub]:
                    if key in self._result[sub]:
                        subset[sub][key] = self._result[sub][key]

        if isinstance(self._task.no_log,
                      bool) and self._task.no_log or self._result.get(
                          '_assible_no_log', False):
            x = {
                "censored":
                "the output has been hidden due to the fact that 'no_log: true' was specified for this result"
            }

            # preserve full
            for preserve in _PRESERVE:
                if preserve in self._result:
                    x[preserve] = self._result[preserve]

            result._result = x
        elif self._result:
            result._result = module_response_deepcopy(self._result)

            # actualy remove
            for remove_key in ignore:
                if remove_key in result._result:
                    del result._result[remove_key]

            # remove almost ALL internal keys, keep ones relevant to callback
            strip_internal_keys(result._result, exceptions=CLEAN_EXCEPTIONS)

        # keep subset
        result._result.update(subset)

        return result
Exemplo n.º 2
0
    def _dump_results(self,
                      result,
                      indent=None,
                      sort_keys=True,
                      keep_invocation=False):

        if not indent and (result.get('_assible_verbose_always')
                           or self._display.verbosity > 2):
            indent = 4

        # All result keys stating with _assible_ are internal, so remove them from the result before we output anything.
        abridged_result = strip_internal_keys(module_response_deepcopy(result))

        # remove invocation unless specifically wanting it
        if not keep_invocation and self._display.verbosity < 3 and 'invocation' in result:
            del abridged_result['invocation']

        # remove diff information from screen output
        if self._display.verbosity < 3 and 'diff' in result:
            del abridged_result['diff']

        # remove exception from screen output
        if 'exception' in abridged_result:
            del abridged_result['exception']

        try:
            jsonified_results = json.dumps(abridged_result,
                                           cls=AssibleJSONEncoder,
                                           indent=indent,
                                           ensure_ascii=False,
                                           sort_keys=sort_keys)
        except TypeError:
            # Python3 bug: throws an exception when keys are non-homogenous types:
            # https://bugs.python.org/issue25457
            # sort into an OrderedDict and then json.dumps() that instead
            if not OrderedDict:
                raise
            jsonified_results = json.dumps(OrderedDict(
                sorted(abridged_result.items(), key=to_text)),
                                           cls=AssibleJSONEncoder,
                                           indent=indent,
                                           ensure_ascii=False,
                                           sort_keys=False)
        return jsonified_results
def test_module_response_deepcopy_tuple_of_immutables():
    x = ((1, 2), 3)
    y = module_response_deepcopy(x)
    assert x is y
def test_module_response_deepcopy_dict():
    x = {"foo": [1, 2], "bar": 3}
    y = module_response_deepcopy(x)
    assert y == x
    assert x is not y
    assert x["foo"] is not y["foo"]
def test_module_response_deepcopy_tuple():
    x = ([1, 2], 3)
    y = module_response_deepcopy(x)
    assert y == x
    assert x is not y
    assert x[0] is not y[0]
def test_module_response_deepcopy_empty_tuple():
    x = ()
    y = module_response_deepcopy(x)
    assert x is y
def test_module_response_deepcopy_list():
    x = [[1, 2], 3]
    y = module_response_deepcopy(x)
    assert y == x
    assert x is not y
    assert x[0] is not y[0]
def test_module_response_deepcopy_atomic():
    tests = [None, 42, 2**100, 3.14, True, False, 1j,
             "hello", u"hello\u1234"]
    for x in tests:
        assert module_response_deepcopy(x) is x
def test_module_response_deepcopy_basic():
    x = 42
    y = module_response_deepcopy(x)
    assert y == x