Ejemplo n.º 1
0
def test_stderr_to_file_with_append(test, capfd):
    """Tests redirection of stderr to to a file with appending."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            temp_file.write(b"orig\n")
            temp_file.flush()

            process = sh.sh("-c", "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                _stderr=File(temp_file.name, append=True))
            process.execute()

            assert process.stdout() == "test1\nтест3\n"
            assert process.stderr() == ""

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            with open(temp_file.name, "rb") as stderr:
                assert psys.u(stderr.read()) == "orig\ntest2\nтест4\n"
    finally:
        logging.disable(logging.NOTSET)
Ejemplo n.º 2
0
def _arg_to_str(arg):
    """
    Returns a string representation (possibly quoted)
    of a command argument as it would be used in shell scripts.

    .. note::
        Very lazy formatting. Not suitable for automatic generation of shell scripts.
    """

    if type(arg) == bytes:
        for c in b""" '"\\\r\n\t""":
            if c in arg:
                arg = repr(arg)[1:] if PY3 else repr(arg)
                break
        else:
            arg = repr(arg)[1 + int(PY3):-1]

        return psys.u(arg)
    else:
        for c in """ '"\\\r\n\t""":
            if c in arg:
                for replacement in (
                    ("\\", r"\\"),
                    ("'", r"\'"),
                    ("\r", r"\r"),
                    ("\n", r"\n"),
                    ("\t", r"\t"),
                ):
                    arg = arg.replace(*replacement)

                arg = "'" + arg + "'"
                break

        return arg
Ejemplo n.º 3
0
    def create_instance_of(self, resource, resource_data):
        """Creates instance of specified resource."""

        response = self._session.post(
            "types/{type}/instances".format(type=resource),
            data=psys.u(json.dumps(resource_data)))
        return response["id"]
Ejemplo n.º 4
0
def _arg_to_str(arg):
    """
    Returns a string representation (possibly quoted)
    of a command argument as it would be used in shell scripts.

    .. note::
        Very lazy formatting. Not suitable for automatic generation of shell scripts.
    """

    if type(arg) == bytes:
        for c in b""" '"\\\r\n\t""":
            if c in arg:
                arg = repr(arg)[1:] if PY3 else repr(arg)
                break
        else:
            arg = repr(arg)[1 + int(PY3):-1]

        return psys.u(arg)
    else:
        for c in """ '"\\\r\n\t""":
            if c in arg:
                for replacement in (
                    ("\\", r"\\"),
                    ("'",  r"\'"),
                    ("\r", r"\r"),
                    ("\n", r"\n"),
                    ("\t", r"\t"),
                ):
                    arg = arg.replace(*replacement)

                arg = "'" + arg + "'"
                break

        return arg
Ejemplo n.º 5
0
    def perform_action_on_type(self, resource, action, action_data):
        """Performs action on specified resource type."""

        return self._session.post(
            "types/{type}/instances/action/{action}".format(type=resource,
                                                            action=action),
            data=psys.u(json.dumps(action_data)))
Ejemplo n.º 6
0
def test_stderr_to_file_with_append(test, capfd):
    """Tests redirection of stderr to to a file with appending."""

    logging.disable(logging.CRITICAL)

    try:
        with tempfile.NamedTemporaryFile() as temp_file:
            temp_file.write(b"orig\n")
            temp_file.flush()

            process = sh.sh(
                "-c",
                "echo test1; echo test2 >&2; echo тест3; echo тест4 >&2;",
                _stderr=File(temp_file.name, append=True))
            process.execute()

            assert process.stdout() == "test1\nтест3\n"
            assert process.stderr() == ""

            stdout, stderr = capfd.readouterr()
            assert stdout == ""
            assert stderr == ""

            with open(temp_file.name, "rb") as stderr:
                assert psys.u(stderr.read()) == "orig\ntest2\nтест4\n"
    finally:
        logging.disable(logging.NOTSET)
Ejemplo n.º 7
0
    def perform_action_on(self, resource, resource_id, action, action_data):
        """Performs action on single instance of specified resource type."""

        return self._session.post(
            "instances/{type}::{id}/action/{action}".format(type=resource,
                                                            id=resource_id,
                                                            action=action),
            data=psys.u(json.dumps(action_data)))
Ejemplo n.º 8
0
    def __response(self, response, request_uuid):
        """Handle response object."""

        response_payload = response.text

        log.debug("ScaleIO response (%s): %s", request_uuid, response_payload)
        try:
            return json.loads(psys.u(response_payload))
        except (ValueError, TypeError):
            raise exceptions.ScaleIOMalformedError()
Ejemplo n.º 9
0
    def __transform_block(self, block):
        """Transforms the data block to the output format."""

        if self.__raw:
            return block
        else:
            try:
                return psys.u(block)
            except:
                self.__finalize()
                raise
Ejemplo n.º 10
0
    def __transform_block(self, block):
        """Transforms the data block to the output format."""

        if self.__raw:
            return block
        else:
            try:
                return psys.u(block)
            except:
                self.__finalize()
                raise
Ejemplo n.º 11
0
Archivo: test.py Proyecto: wd5/psh
    def process_childs():
        process = subprocess.Popen(
            [ "ps", "-A", "-o", "ppid=,pid=,command=" ],
            stdout = subprocess.PIPE)

        stdout = psys.u(process.communicate()[0])
        assert not process.returncode
        assert stdout

        childs = ""

        for line in stdout.split("\n"):
            match = re.search(r"^\s*{0}\s+(\d+)".format(os.getpid()), line)

            if match is not None and int(match.group(1)) != process.pid:
                if childs:
                    childs += "\n"

                childs += line

        return childs
Ejemplo n.º 12
0
Archivo: process.py Proyecto: wd5/psh
    def __to_str(self):
        """Returns the command string.

        .. note::
            Very lazy formatting.
        """

        command = ""

        for arg in self.__command:
            if command:
                command += " "

            if type(arg) == bytes:
                for c in b""" '"\\\r\n\t""":
                    if c in arg:
                        arg = repr(arg)[1:] if PY3 else repr(arg)
                        break
                else:
                    arg = repr(arg)[1 + int(PY3):-1]

                command += psys.u(arg)
            else:
                for c in """ '"\\\r\n\t""":
                    if c in arg:
                        for replacement in (
                            ( "\\", r"\\" ),
                            ( "'",  r"\'" ),
                            ( "\r", r"\r" ),
                            ( "\n", r"\n" ),
                            ( "\t", r"\t" ),
                        ):
                            arg = arg.replace(*replacement)

                        arg = "'" + arg + "'"
                        break

                command += arg

        return command
Ejemplo n.º 13
0
    def __unicode__(self):
        """Returns the command string.

        .. note::
            Very lazy formatting.
        """

        command = ""

        for arg in self.__command:
            if command:
                command += " "

            if type(arg) == str:
                for c in b""" '"\\\r\n\t""":
                    if c in arg:
                        arg = repr(arg)
                        break
                else:
                    arg = repr(arg)[1:-1]

                command += psys.u(arg)
            else:
                for c in b""" '"\\\r\n\t""":
                    if c in arg:
                        for replacement in (
                            ( b"\\", br"\\" ),
                            ( b"'",  br"\'" ),
                            ( b"\r", br"\r" ),
                            ( b"\n", br"\n" ),
                            ( b"\t", br"\t" ),
                        ):
                            arg = arg.replace(*replacement)

                        arg = "'" + arg + "'"
                        break

                command += arg

        return command
Ejemplo n.º 14
0
    def stdout(self):
        """Returns the process' captured stdout (if ``_stdout = PIPE``)."""

        return psys.u(self.raw_stdout())
Ejemplo n.º 15
0
 def generate_klass(name, subclasses, dict):
     if six.PY2:
         name = psys.b(name)
     else:
         name = psys.u(name)
     return type(name, subclasses, dict)
Ejemplo n.º 16
0
    def stderr(self):
        """Returns the process' captured stderr (if ``_stderr = PIPE``)."""

        return psys.u(self.__stderr)
Ejemplo n.º 17
0
 def generate_klass(name, subclasses, dict):
     if six.PY2:
         name = psys.b(name)
     else:
         name = psys.u(name)
     return type(name, subclasses, dict)
Ejemplo n.º 18
0
    def stderr(self):
        """Returns the process' captured stderr (if ``_stderr = PIPE``)."""

        return psys.u(self.__stderr)
Ejemplo n.º 19
0
    def stdout(self):
        """Returns the process' captured stdout (if ``_stdout = PIPE``)."""

        return psys.u(self.__stdout)