Skip to content

Conversation

espositocloud
Copy link
Contributor

@espositocloud espositocloud commented Apr 12, 2018

What do these changes do?

Add basic stdin support to Subprocess and Paramiko.

Are there changes in behavior for the user?

The user can provide some content for stdin that will be available to the process.

Related issue number

Fix #8

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes

@coveralls
Copy link
Collaborator

coveralls commented Apr 12, 2018

Pull Request Test Coverage Report for Build 83

  • 10 of 19 (52.63%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-1.0%) to 98.06%

Changes Missing Coverage Covered Lines Changed/Added Lines %
exec_helpers/exec_result.py 4 5 80.0%
exec_helpers/_ssh_client_base.py 5 9 55.56%
exec_helpers/subprocess_runner.py 1 5 20.0%
Totals Coverage Status
Change from base Build 77: -1.0%
Covered Lines: 910
Relevant Lines: 928

💛 - Coveralls

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 73

  • 2 of 3 (66.67%) changed or added relevant lines in 1 file are covered.
  • 5 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.5%) to 98.48%

Changes Missing Coverage Covered Lines Changed/Added Lines %
exec_helpers/exec_result.py 2 3 66.67%
Files with Coverage Reduction New Missed Lines %
exec_helpers/proc_enums.py 1 99.07%
exec_helpers/subprocess_runner.py 4 91.09%
Totals Coverage Status
Change from base Build 72: -0.5%
Covered Lines: 907
Relevant Lines: 921

💛 - Coveralls

@penguinolog
Copy link
Collaborator

I missed 2nd part for SSH (it should look like copy-paste from Subprocess now, just stdin input in execute_async)

Copy link
Collaborator

@penguinolog penguinolog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's Update ExecResult arguments order (the single object, where it's technically allowed) and reduce encode/decode operations

Command execution result.

.. py:method:: __init__(cmd, stdout=None, stderr=None, exit_code=ExitCodes.EX_INVALID)
.. py:method:: __init__(cmd, stdout=None, stderr=None, exit_code=ExitCodes.EX_INVALID, stdin=None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably for ExecResult we can use urder: stdin, stdout, stderr as human-expected

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed that's more natural. I added it at the end only for consistency with the rest (where stdin is the latest for backward compatibility).

Copy link
Collaborator

@penguinolog penguinolog Apr 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExecResult class is exported for typing purposes only. It was not expected for re-use outside, except reading

)
if stdin is not None:
if not isinstance(stdin, six.text_type):
stdin = six.u(stdin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better to decode for ExecResult inside ExecResult and encode for stdin only if required (less transformations)

def test_check_stdin(self, check_call, logger):
print_stdin = 'cat <&0'
stdin = 'input data'
if not isinstance(stdin, six.text_type):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have full control over tests - better to test both cases:
stdin = b'input data and stdin = u'input data'

:param cmd: command
:type cmd: ``str``
:param stdout: STDIN
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy-paste error: should be :param stdin: STDIN and on the next line :type stdin: ...

:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param stdin: pass STDIN text to the process
:type stdin: ``typing.Optional[str]``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.Optional[bytes] and need to check for bytearray support (both Subprocess and SSH)

.. versionchanged:: 1.2.0

open_stdout and open_stderr flags
open_stdout, open_stderr and stdin flags
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdin separately - it's not flag, but data


stdin = chan.makefile('wb')
_stdin = chan.makefile('wb')
if stdin is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sudo should be processed before


self.__cmd = cmd
if stdin is not None and not isinstance(stdin, six.text_type):
stdin = six.u(stdin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdin = stdin.decode('utf-8')

Copy link
Contributor Author

@espositocloud espositocloud Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not six.text_type identifies e.g. non-unicode strings

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> b'dede'.decode('utf-8')
'dede'

Decode non-unicode to unicode

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and for exec_result errors='backslashreplace' is actual parameter, because we can have true binary data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I see

paramiko.ChannelFile,
typing.Optional[paramiko.ChannelFile],
typing.Optional[paramiko.ChannelFile],
typing.Any,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not change this, typing.Any will be used in the base class later (2.0.0+)

:param command: Command for execution
:type command: str
:param get_pty: open PTY on remote machine
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this argument is not removed

verbose=False, # type: bool
log_mask_re=None, # type: typing.Optional[str]
**kwargs
): # type: (...) -> _type_execute_async
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not remove this typing

return chan, stdin, stderr, stdout
if stdin is not None:
if not isinstance(stdin, six.text_type):
stdin = six.u(stdin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use encode/decode

@espositocloud espositocloud changed the title Fix #8 Add basic stdin support to Subprocess Fix #8 Add basic stdin support to Subprocess and Paramiko Apr 23, 2018
:param cmd: command
:type cmd: ``str``
:param stdin: STDIN
:type stdin: ``str``
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typing.Optional[str]

.. versionchanged:: 1.2.0 open_stdout and open_stderr flags
.. versionchanged:: 1.2.0
open_stdout and open_stderr flags
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make several records like Subprocess.rst

from __future__ import unicode_literals

import collections
import errno
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linux only -> we need separation linux/posix/windows

penguinolog
penguinolog previously approved these changes May 1, 2018
@espositocloud espositocloud merged commit 5f107c0 into python-useful-helpers:master May 1, 2018
@espositocloud espositocloud deleted the fix-8 branch May 1, 2018 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing support to non-interactive STDIN (only 1 input string)
3 participants