Beispiel #1
0
    def test_convert_dict_to_override_args(self):
        overrides = {
            'one': {
                'two': 1,
                'three': {
                    'alpha': 'a'
                },
                'four': 'lorem ipsum',
            },
            'two': {}
        }

        expected_overrides = [
            'rc.one.two=1',
            'rc.one.three.alpha=a',
            'rc.one.four="lorem ipsum"',
        ]
        actual_overrides = convert_dict_to_override_args(overrides)

        eq_(set(actual_overrides), set(expected_overrides))
Beispiel #2
0
    def test_convert_dict_to_override_args(self):
        overrides = {
            'one': {
                'two': 1,
                'three': {
                    'alpha': 'a'
                },
                'four': 'lorem ipsum',
            },
            'two': {
            }
        }

        expected_overrides = [
            'rc.one.two=1',
            'rc.one.three.alpha=a',
            'rc.one.four="lorem ipsum"',
        ]
        actual_overrides = convert_dict_to_override_args(overrides)

        eq_(set(actual_overrides), set(expected_overrides))
    def _execute(self, *args, **kwargs):
        """ Execute a given taskwarrior command with arguments

        Returns a 2-tuple of stdout and stderr (respectively).

        """
        if "config_overrides" in kwargs:
            config_overrides = utils.convert_dict_to_override_args(
                kwargs.pop("config_overrides"))
        else:
            config_overrides = self.get_configuration_override_args()

        logger = self._get_logger(args)

        command = ([
            "task",
            f"rc:{self.config_filename}",
        ] + config_overrides + [str(arg) for arg in args])

        # subprocess is expecting bytestrings only, so nuke unicode if present
        for i in range(len(command)):
            if isinstance(command[i], str):
                command[i] = taskw.utils.clean_ctrl_chars(
                    command[i].encode("utf-8"))

        started = datetime.datetime.now()

        proc = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        _stdout, _stderr = proc.communicate()
        stdout = _stdout.decode("utf-8", "replace")
        stderr = _stderr.decode("utf-8", "replace")

        total_seconds = (datetime.datetime.now() - started).total_seconds()

        self.send_client_message(
            "metadata",
            "taskwarrior.execute",
            {
                "command":
                " ".join(pipes.quote(c.decode("utf-8")) for c in command),
                "arguments":
                " ".join(pipes.quote(str(arg)) for arg in args),
                "stderr":
                stderr,
                "stdout":
                stdout,
                "returncode":
                proc.returncode,
                "duration":
                total_seconds,
            },
        )

        base_command = args[0]
        if proc.returncode != 0 and base_command not in self.NONZERO_ALERT_EXEMPT:
            logger.warning(
                "Non-zero return code returned from taskwarrior: "
                f"{proc.returncode}; {stderr}",
                extra={
                    "stack": True,
                    "data": {
                        "code": proc.returncode,
                        "command": command,
                        "stdout": stdout,
                        "stderr": stderr,
                    },
                },
            )
            raise TaskwarriorError(stderr, stdout, proc.returncode)

        logger.debug("%s: %s", command, stdout)
        return stdout, stderr
    def _execute(self, *args, **kwargs):
        """ Execute a given taskwarrior command with arguments

        Returns a 2-tuple of stdout and stderr (respectively).

        """
        if 'config_overrides' in kwargs:
            config_overrides = utils.convert_dict_to_override_args(
                kwargs.pop('config_overrides'))
        else:
            config_overrides = self.get_configuration_override_args()

        logger = self._get_logger(args)

        command = ([
            'task',
            'rc:%s' % self.config_filename,
        ] + config_overrides + [six.text_type(arg) for arg in args])

        # subprocess is expecting bytestrings only, so nuke unicode if present
        for i in range(len(command)):
            if isinstance(command[i], six.text_type):
                command[i] = command[i].encode('utf-8')

        started = datetime.datetime.now()

        proc = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        stdout, stderr = proc.communicate()

        total_seconds = (datetime.datetime.now() - started).total_seconds()

        self.send_client_message(
            'metadata', 'taskwarrior.execute', {
                'command':
                ' '.join(pipes.quote(c.decode('utf-8')) for c in command),
                'arguments':
                ' '.join(pipes.quote(six.text_type(arg)) for arg in args),
                'stderr':
                stderr,
                'stdout':
                stdout,
                'returncode':
                proc.returncode,
                'duration':
                total_seconds,
            })

        if proc.returncode != 0:
            logger.error(
                'Non-zero return code returned from taskwarrior: %s; %s' % (
                    proc.returncode,
                    stderr,
                ),
                extra={
                    'stack': True,
                    'data': {
                        'code': proc.returncode,
                        'command': command,
                        'stdout': stdout,
                        'stderr': stderr,
                    }
                })
            raise TaskwarriorError(stderr, stdout, proc.returncode)

        logger.debug("%s: %s", command, stdout)
        return stdout, stderr
    def _execute(self, *args, **kwargs):
        """ Execute a given taskwarrior command with arguments

        Returns a 2-tuple of stdout and stderr (respectively).

        """
        if 'config_overrides' in kwargs:
            config_overrides = utils.convert_dict_to_override_args(
                kwargs.pop('config_overrides')
            )
        else:
            config_overrides = self.get_configuration_override_args()

        logger = self._get_logger(args)

        command = (
            [
                'task',
                'rc:%s' % self.config_filename,
            ] +
            config_overrides +
            [six.text_type(arg) for arg in args]
        )

        # subprocess is expecting bytestrings only, so nuke unicode if present
        for i in range(len(command)):
            if isinstance(command[i], six.text_type):
                command[i] = command[i].encode('utf-8')

        started = datetime.datetime.now()

        proc = subprocess.Popen(
            command,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
        )

        stdout, stderr = proc.communicate()

        total_seconds = (datetime.datetime.now() - started).total_seconds()

        self.send_client_message(
            'metadata',
            'taskwarrior.execute',
            {
                'command': ' '.join(
                    pipes.quote(c.decode('utf-8')) for c in command
                ),
                'arguments': ' '.join(
                    pipes.quote(six.text_type(arg)) for arg in args
                ),
                'stderr': stderr,
                'stdout': stdout,
                'returncode': proc.returncode,
                'duration': total_seconds,
            }
        )

        base_command = args[0]
        if (
            proc.returncode != 0 and
            base_command not in self.NONZERO_ALERT_EXEMPT
        ):
            logger.warning(
                'Non-zero return code returned from taskwarrior: %s; %s' % (
                    proc.returncode,
                    stderr,
                ),
                extra={
                    'stack': True,
                    'data': {
                        'code': proc.returncode,
                        'command': command,
                        'stdout': stdout,
                        'stderr': stderr,
                    }
                }
            )
            raise TaskwarriorError(stderr, stdout, proc.returncode)

        logger.debug("%s: %s", command, stdout)
        return stdout, stderr