Skip to content

Commit b71fa69

Browse files
committed
Update docs: expected is not optional
(cherry picked from commit 31c6a01) (cherry picked from commit df8a438) (cherry picked from commit f4908a5) On subprocess kill write exit code to result to prevent changes (#120) * fix missed docstring (cherry picked from commit fb0ef72) Add timestamp of command start to execute_async for future processing (cherry picked from commit b3dc162) More info in result: start timestamp (#124) (cherry picked from commit 8b7a8e7) Always set timestamp on kill, provide spent time in str() (cherry picked from commit 06f1650)
1 parent 530d360 commit b71fa69

File tree

13 files changed

+264
-81
lines changed

13 files changed

+264
-81
lines changed

README.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Pros:
3939
* Open Source: https://github.com/python-useful-helpers/exec-helpers
4040
* PyPI packaged: https://pypi.python.org/pypi/exec-helpers
4141
* Self-documented code: docstrings with types in comments
42-
* Tested: see bages on top
42+
* Tested: see badges on top
4343
* Support multiple Python versions:
4444

4545
::
@@ -49,7 +49,7 @@ Pros:
4949
Python 3.7
5050
PyPy3 3.5+
5151

52-
.. note:: For Python 2.7 and PyPy please use versions 1.x.x. For python 3.5+ use versions 3.0+
52+
.. note:: Pythons: For Python 2.7 and PyPy use versions 1.x.x, python 3.5+ use versions 3.x.x, python 3.6+ use versions 4+
5353

5454
This package includes:
5555

@@ -100,7 +100,7 @@ Creation from scratch:
100100
)
101101
102102
Key is a main connection key (always tried first) and keys are alternate keys.
103-
Key filename is afilename or list of filenames with keys, which should be loaded.
103+
Key filename is a filename or list of filenames with keys, which should be loaded.
104104
Passphrase is an alternate password for keys, if it differs from main password.
105105
If main key now correct for username - alternate keys tried, if correct key found - it became main.
106106
If no working key - password is used and None is set as main key.
@@ -226,9 +226,11 @@ Possible to call commands in parallel on multiple hosts if it's not produce huge
226226
remotes, # type: typing.Iterable[SSHClient]
227227
command, # type: str
228228
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
229-
expected=None, # type: typing.Optional[typing.Iterable[int]]
229+
expected=(0,), # type: typing.Iterable[typing.Union[int, ExitCodes]]
230230
raise_on_err=True, # type: bool
231231
# Keyword only:
232+
stdin=None, # type: typing.Union[bytes, str, bytearray, None]
233+
log_mask_re=None, # type: typing.Optional[str]
232234
exception_class=ParallelCallProcessError # typing.Type[ParallelCallProcessError]
233235
)
234236
results # type: typing.Dict[typing.Tuple[str, int], exec_result.ExecResult]
@@ -248,6 +250,8 @@ For execute through SSH host can be used `execute_through_host` method:
248250
timeout=1 * 60 * 60, # type: type: typing.Union[int, float, None]
249251
verbose=False, # type: bool
250252
# Keyword only:
253+
stdin=None, # type: typing.Union[bytes, str, bytearray, None]
254+
log_mask_re=None, # type: typing.Optional[str]
251255
get_pty=False, # type: bool
252256
width=80, # type: int
253257
height=24 # type: int

appveyor.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@ environment:
55

66
matrix:
77
- PYTHON: "C:\\Python35"
8-
PYTHON_VERSION: "3.5.x" # currently 3.5.1
8+
PYTHON_VERSION: "3.5.x"
99
PYTHON_ARCH: "32"
1010

1111
- PYTHON: "C:\\Python35-x64"
12-
PYTHON_VERSION: "3.5.x" # currently 3.5.1
13-
PYTHON_ARCH: "64"
14-
15-
- PYTHON: "C:\\Python36"
16-
PYTHON_VERSION: "3.6.x" # currently 3.6.0
17-
PYTHON_ARCH: "32"
18-
19-
- PYTHON: "C:\\Python36-x64"
20-
PYTHON_VERSION: "3.6.x" # currently 3.6.0
12+
PYTHON_VERSION: "3.5.x"
2113
PYTHON_ARCH: "64"
2214

2315
install:

doc/source/ExecResult.rst

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ API: ExecResult
1010
1111
Command execution result.
1212

13-
.. py:method:: __init__(cmd, stdin=None, stdout=None, stderr=None, exit_code=ExitCodes.EX_INVALID)
13+
.. py:method:: __init__(cmd, stdin=None, stdout=None, stderr=None, exit_code=0xDEADBEEF, *, started=None)
1414
1515
:param cmd: command
1616
:type cmd: ``str``
@@ -22,6 +22,8 @@ API: ExecResult
2222
:type stderr: ``typing.Optional[typing.Iterable[bytes]]``
2323
:param exit_code: Exit code. If integer - try to convert to BASH enum.
2424
:type exit_code: typing.Union[int, ExitCodes]
25+
:param started: Timestamp of command start
26+
:type started: typing.Optional[datetime.datetime]
2527

2628
.. py:attribute:: stdout_lock
2729
@@ -42,6 +44,14 @@ API: ExecResult
4244
``typing.Optional(datetime.datetime)``
4345
Timestamp
4446

47+
.. py:method:: set_timestamp()
48+
49+
Set timestamp if empty.
50+
51+
This will block future object changes.
52+
53+
.. versionadded:: 2.11.0
54+
4555
.. py:attribute:: cmd
4656
4757
``str``
@@ -98,6 +108,13 @@ API: ExecResult
98108

99109
:rtype: typing.Union[int, ExitCodes]
100110

111+
.. py:attribute:: started
112+
113+
``datetime.datetime``
114+
Timestamp of command start.
115+
116+
.. versionadded:: 2.11.0
117+
101118
.. py:attribute:: stdout_json
102119
103120
JSON from stdout.

doc/source/SSHClient.rst

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ API: SSHClient and SSHAuth.
182182

183183
.. versionadded:: 2.9.4
184184

185-
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=None, raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
185+
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
186186
187187
Execute command and check for return code.
188188

@@ -195,7 +195,7 @@ API: SSHClient and SSHAuth.
195195
:param error_info: Text for error details, if fail happens
196196
:type error_info: ``typing.Optional[str]``
197197
:param expected: expected return codes (0 by default)
198-
:type expected: ``typing.Optional[typing.Iterable[int]]``
198+
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
199199
:param raise_on_err: Raise exception on unexpected return code
200200
:type raise_on_err: ``bool``
201201
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
@@ -206,8 +206,9 @@ API: SSHClient and SSHAuth.
206206

207207
.. versionchanged:: 1.2.0 default timeout 1 hour
208208
.. versionchanged:: 3.2.0 Exception class can be substituted
209+
.. versionchanged:: 2.11.0 Expected is not optional, defaults os dependent
209210

210-
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=None, exception_class=CalledProcessError, **kwargs)
211+
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=(0,), exception_class=CalledProcessError, **kwargs)
211212
212213
Execute command expecting return code 0 and empty STDERR.
213214

@@ -222,7 +223,7 @@ API: SSHClient and SSHAuth.
222223
:param raise_on_err: Raise exception on unexpected return code
223224
:type raise_on_err: ``bool``
224225
:param expected: expected return codes (0 by default)
225-
:type expected: typing.Optional[typing.Iterable[typing.Union[int, ExitCodes]]]
226+
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
226227
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
227228
:type exception_class: typing.Type[CalledProcessError]
228229
:rtype: ExecResult
@@ -232,7 +233,7 @@ API: SSHClient and SSHAuth.
232233
.. versionchanged:: 1.2.0 default timeout 1 hour
233234
.. versionchanged:: 3.2.0 Exception class can be substituted
234235

235-
.. py:method:: execute_through_host(hostname, command, auth=None, target_port=22, verbose=False, timeout=1*60*60, *, get_pty=False, width=80, height=24, **kwargs)
236+
.. py:method:: execute_through_host(hostname, command, auth=None, target_port=22, verbose=False, timeout=1*60*60, *, stdin=None, log_mask_re="", get_pty=False, width=80, height=24, **kwargs)
236237
237238
Execute command on remote host through currently connected host.
238239

@@ -248,6 +249,11 @@ API: SSHClient and SSHAuth.
248249
:type verbose: ``bool``
249250
:param timeout: Timeout for command execution.
250251
:type timeout: ``typing.Union[int, float, None]``
252+
:param stdin: pass STDIN text to the process
253+
:type stdin: typing.Union[bytes, str, bytearray, None]
254+
:param log_mask_re: regex lookup rule to mask command for logger.
255+
all MATCHED groups will be replaced by '<*masked*>'
256+
:type log_mask_re: typing.Optional[str]
251257
:param get_pty: open PTY on target machine
252258
:type get_pty: ``bool``
253259
:param width: PTY width
@@ -260,8 +266,9 @@ API: SSHClient and SSHAuth.
260266
.. versionchanged:: 1.2.0 default timeout 1 hour
261267
.. versionchanged:: 3.2.0 Expose pty options as optional keyword-only arguments
262268
.. versionchanged:: 3.2.0 Exception class can be substituted
269+
.. versionchanged:: 2.11.0 Expose stdin and log_mask_re as optional keyword-only arguments
263270

264-
.. py:classmethod:: execute_together(remotes, command, timeout=1*60*60, expected=None, raise_on_err=True, *, exception_class=ParallelCallProcessError, **kwargs)
271+
.. py:classmethod:: execute_together(remotes, command, timeout=1*60*60, expected=(0,), raise_on_err=True, *, stdin=None, log_mask_re="", exception_class=ParallelCallProcessError, **kwargs)
265272
266273
Execute command on multiple remotes in async mode.
267274

@@ -272,9 +279,14 @@ API: SSHClient and SSHAuth.
272279
:param timeout: Timeout for command execution.
273280
:type timeout: ``typing.Union[int, float, None]``
274281
:param expected: expected return codes (0 by default)
275-
:type expected: ``typing.Optional[typing.Iterable[]]``
282+
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
276283
:param raise_on_err: Raise exception on unexpected return code
277284
:type raise_on_err: ``bool``
285+
:param stdin: pass STDIN text to the process
286+
:type stdin: typing.Union[bytes, str, bytearray, None]
287+
:param log_mask_re: regex lookup rule to mask command for logger.
288+
all MATCHED groups will be replaced by '<*masked*>'
289+
:type log_mask_re: typing.Optional[str]
278290
:param exception_class: Exception to raise on error. Mandatory subclass of ParallelCallProcessError
279291
:type exception_class: typing.Type[ParallelCallProcessError]
280292
:return: dictionary {(hostname, port): result}
@@ -284,6 +296,8 @@ API: SSHClient and SSHAuth.
284296

285297
.. versionchanged:: 1.2.0 default timeout 1 hour
286298
.. versionchanged:: 3.2.0 Exception class can be substituted
299+
.. versionchanged:: 2.11.0 Expected is not optional, defaults os dependent
300+
.. versionchanged:: 2.11.0 Expose stdin and log_mask_re as optional keyword-only arguments
287301

288302
.. py:method:: open(path, mode='r')
289303
@@ -448,3 +462,9 @@ API: SSHClient and SSHAuth.
448462
.. py:attribute:: stdout
449463
450464
``typing.Optional[paramiko.ChannelFile]``
465+
466+
.. py:attribute:: started
467+
468+
``datetime.datetime``
469+
470+
.. versionadded:: 2.11.0

doc/source/Subprocess.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ API: Subprocess
106106
.. note:: stdin channel is closed after the input processing
107107
.. versionadded:: 2.9.4
108108

109-
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=None, raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
109+
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
110110
111111
Execute command and check for return code.
112112

@@ -119,7 +119,7 @@ API: Subprocess
119119
:param error_info: Text for error details, if fail happens
120120
:type error_info: ``typing.Optional[str]``
121121
:param expected: expected return codes (0 by default)
122-
:type expected: ``typing.Optional[typing.Iterable[int]]``
122+
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
123123
:param raise_on_err: Raise exception on unexpected return code
124124
:type raise_on_err: ``bool``
125125
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
@@ -131,8 +131,9 @@ API: Subprocess
131131
.. versionchanged:: 1.1.0 make method
132132
.. versionchanged:: 1.2.0 default timeout 1 hour
133133
.. versionchanged:: 2.9.3 Exception class can be substituted
134+
.. versionchanged:: 2.11.0 Expected is not optional, defaults os dependent
134135

135-
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=None, exception_class=CalledProcessError, **kwargs)
136+
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=(0,), exception_class=CalledProcessError, **kwargs)
136137
137138
Execute command expecting return code 0 and empty STDERR.
138139

@@ -147,7 +148,7 @@ API: Subprocess
147148
:param raise_on_err: Raise exception on unexpected return code
148149
:type raise_on_err: ``bool``
149150
:param expected: expected return codes (0 by default)
150-
:type expected: typing.Optional[typing.Iterable[typing.Union[int, ExitCodes]]]
151+
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
151152
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
152153
:type exception_class: typing.Type[CalledProcessError]
153154
:rtype: ExecResult
@@ -157,6 +158,7 @@ API: Subprocess
157158
.. versionchanged:: 1.1.0 make method
158159
.. versionchanged:: 1.2.0 default timeout 1 hour
159160
.. versionchanged:: 2.9.3 Exception class can be substituted
161+
.. versionchanged:: 2.11.0 Expected is not optional, defaults os dependent
160162

161163

162164
.. py:class:: SubprocessExecuteAsyncResult
@@ -178,3 +180,9 @@ API: Subprocess
178180
.. py:attribute:: stdout
179181
180182
``typing.Optional[typing.IO]``
183+
184+
.. py:attribute:: started
185+
186+
``datetime.datetime``
187+
188+
.. versionadded:: 2.11.0

0 commit comments

Comments
 (0)