コード例 #1
0
ファイル: py3.py プロジェクト: mikeri/py3status
    def command_output(self, command, shell=False):
        """
        Run a command and return its output as unicode.
        The command can either be supplied as a sequence or string.

        An Exception is raised if an error occurs
        """
        # convert the command to sequence if a string
        if isinstance(command, basestring):
            command = shlex.split(command)
        try:
            process = Popen(command,
                            stdout=PIPE,
                            stderr=PIPE,
                            close_fds=True,
                            universal_newlines=True,
                            shell=shell)
        except Exception as e:
            msg = 'Command `{cmd}` {error}'.format(cmd=command[0], error=e)
            raise exceptions.CommandError(msg, error_code=e.errno)

        output, error = process.communicate()
        if self._is_python_2:
            output = output.decode('utf-8')
            error = error.decode('utf-8')
        retcode = process.poll()
        if retcode:
            # under certain conditions a successfully run command may get a
            # return code of -15 even though correct output was returned see
            # #664.  This issue seems to be related to arch linux but the
            # reason is not entirely clear.
            if retcode == -15:
                msg = 'Command `{cmd}` returned SIGTERM (ignoring)'
                self.log(msg.format(cmd=command))
            else:
                msg = 'Command `{cmd}` returned non-zero exit status {error}'
                output_oneline = output.replace('\n', ' ')
                if output_oneline:
                    msg += ' ({output})'
                msg = msg.format(cmd=command[0],
                                 error=retcode,
                                 output=output_oneline)
                raise exceptions.CommandError(msg,
                                              error_code=retcode,
                                              error=error,
                                              output=output)
        if error:
            msg = "Command '{cmd}' had error {error}".format(cmd=command[0],
                                                             error=error)
            raise exceptions.CommandError(msg,
                                          error_code=retcode,
                                          error=error,
                                          output=output)
        return output
コード例 #2
0
ファイル: py3.py プロジェクト: mikeri/py3status
    def command_run(self, command):
        """
        Runs a command and returns the exit code.
        The command can either be supplied as a sequence or string.

        An Exception is raised if an error occurs
        """
        # convert the command to sequence if a string
        if isinstance(command, basestring):
            command = shlex.split(command)
        try:
            return Popen(command, stdout=PIPE, stderr=PIPE,
                         close_fds=True).wait()
        except Exception as e:
            msg = 'Command `{cmd}` {error}'.format(cmd=command[0],
                                                   error=e.errno)
            raise exceptions.CommandError(msg, error_code=e.errno)
コード例 #3
0
ファイル: py3.py プロジェクト: steinex/py3status
    def command_run(self, command):
        """
        Runs a command and returns the exit code.
        The command can either be supplied as a sequence or string.

        An Exception is raised if an error occurs
        """
        # convert the command to sequence if a string
        if isinstance(command, str):
            command = shlex.split(command)
        try:
            return Popen(command, stdout=PIPE, stderr=PIPE, close_fds=True).wait()
        except Exception as e:
            # make a pretty command for error loggings and...
            if isinstance(command, str):
                pretty_cmd = command
            else:
                pretty_cmd = " ".join(command)
            msg = f"Command `{pretty_cmd}` {e.errno}"
            raise exceptions.CommandError(msg, error_code=e.errno)
コード例 #4
0
    def command_output(self,
                       command,
                       shell=False,
                       capture_stderr=False,
                       localized=False):
        """
        Run a command and return its output as unicode.
        The command can either be supplied as a sequence or string.

        :param command: command to run can be a str or list
        :param shell: if `True` then command is run through the shell
        :param capture_stderr: if `True` then STDERR is piped to STDOUT
        :param localized: if `False` then command is forced to use its default (English) locale

        A CommandError is raised if an error occurs
        """
        # make a pretty command for error loggings and...
        if isinstance(command, basestring):
            pretty_cmd = command
        else:
            pretty_cmd = " ".join(command)
        # convert the non-shell command to sequence if it is a string
        if not shell and isinstance(command, basestring):
            command = shlex.split(command)

        stderr = STDOUT if capture_stderr else PIPE
        env = self._english_env if not localized else None

        try:
            process = Popen(
                command,
                stdout=PIPE,
                stderr=stderr,
                close_fds=True,
                universal_newlines=True,
                shell=shell,
                env=env,
            )
        except Exception as e:
            msg = "Command `{cmd}` {error}".format(cmd=pretty_cmd, error=e)
            raise exceptions.CommandError(msg, error_code=e.errno)

        output, error = process.communicate()
        if self._is_python_2 and isinstance(output, str):
            output = output.decode("utf-8")
            error = error.decode("utf-8")
        retcode = process.poll()
        if retcode:
            # under certain conditions a successfully run command may get a
            # return code of -15 even though correct output was returned see
            # #664.  This issue seems to be related to arch linux but the
            # reason is not entirely clear.
            if retcode == -15:
                msg = "Command `{cmd}` returned SIGTERM (ignoring)"
                self.log(msg.format(cmd=pretty_cmd))
            else:
                msg = "Command `{cmd}` returned non-zero exit status {error}"
                output_oneline = output.replace("\n", " ")
                if output_oneline:
                    msg += " ({output})"
                msg = msg.format(cmd=pretty_cmd,
                                 error=retcode,
                                 output=output_oneline)
                raise exceptions.CommandError(msg,
                                              error_code=retcode,
                                              error=error,
                                              output=output)
        return output