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)
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
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"]
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)))
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)
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)))
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()
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
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
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
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
def stdout(self): """Returns the process' captured stdout (if ``_stdout = PIPE``).""" return psys.u(self.raw_stdout())
def generate_klass(name, subclasses, dict): if six.PY2: name = psys.b(name) else: name = psys.u(name) return type(name, subclasses, dict)
def stderr(self): """Returns the process' captured stderr (if ``_stderr = PIPE``).""" return psys.u(self.__stderr)
def stdout(self): """Returns the process' captured stdout (if ``_stdout = PIPE``).""" return psys.u(self.__stdout)