Skip to content

Commit 04604fc

Browse files
committed
Expose log_mask_re from kwargs
Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com>
1 parent 52aa1b3 commit 04604fc

File tree

5 files changed

+100
-16
lines changed

5 files changed

+100
-16
lines changed

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ This methods are almost the same for `SSHClient` and `Subprocess`, except specif
133133
command, # type: str
134134
verbose=False, # type: bool
135135
timeout=1 * 60 * 60, # type: typing.Union[int, float, None]
136+
# Keyword only:
137+
log_mask_re=None, # type: typing.Optional[str]
136138
**kwargs
137139
)
138140
@@ -147,6 +149,7 @@ This methods are almost the same for `SSHClient` and `Subprocess`, except specif
147149
expected=(0,), # type: typing.Iterable[typing.Union[int, ExitCodes]]
148150
raise_on_err=True, # type: bool
149151
# Keyword only:
152+
log_mask_re=None, # type: typing.Optional[str]
150153
exception_class=CalledProcessError, # typing.Type[CalledProcessError]
151154
**kwargs
152155
)
@@ -160,6 +163,7 @@ This methods are almost the same for `SSHClient` and `Subprocess`, except specif
160163
error_info=None, # type: typing.Optional[str]
161164
raise_on_err=True, # type: bool
162165
# Keyword only:
166+
log_mask_re=None, # type: typing.Optional[str]
163167
expected=(0,), # typing.Iterable[typing.Union[int, ExitCodes]]
164168
exception_class=CalledProcessError, # typing.Type[CalledProcessError]
165169
)
@@ -170,6 +174,8 @@ This methods are almost the same for `SSHClient` and `Subprocess`, except specif
170174
command, # type: str
171175
verbose=False, # type: bool
172176
timeout=1 * 60 * 60, # type: typing.Union[int, float, None]
177+
# Keyword only:
178+
log_mask_re=None, # type: typing.Optional[str]
173179
**kwargs
174180
)
175181

doc/source/SSHClient.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ API: SSHClient and SSHAuth.
166166
.. versionchanged:: 2.1.0 Use typed NamedTuple as result
167167
.. versionchanged:: 3.2.0 Expose pty options as optional keyword-only arguments
168168

169-
.. py:method:: execute(command, verbose=False, timeout=1*60*60, **kwargs)
169+
.. py:method:: execute(command, verbose=False, timeout=1*60*60, *, log_mask_re=None, **kwargs)
170170
171171
Execute command and wait for return code.
172172

@@ -176,12 +176,15 @@ API: SSHClient and SSHAuth.
176176
:type verbose: ``bool``
177177
:param timeout: Timeout for command execution.
178178
:type timeout: ``typing.Union[int, float, None]``
179+
:param log_mask_re: regex lookup rule to mask command for logger.
180+
all MATCHED groups will be replaced by '<*masked*>'
181+
:type log_mask_re: ``typing.Optional[str]``
179182
:rtype: ExecResult
180183
:raises ExecHelperTimeoutError: Timeout exceeded
181184

182185
.. versionchanged:: 1.2.0 default timeout 1 hour
183186

184-
.. py:method:: __call__(command, verbose=False, timeout=1*60*60, **kwargs)
187+
.. py:method:: __call__(command, verbose=False, timeout=1*60*60, *, log_mask_re=None, **kwargs)
185188
186189
Execute command and wait for return code.
187190

@@ -191,12 +194,15 @@ API: SSHClient and SSHAuth.
191194
:type verbose: ``bool``
192195
:param timeout: Timeout for command execution.
193196
:type timeout: ``typing.Union[int, float, None]``
197+
:param log_mask_re: regex lookup rule to mask command for logger.
198+
all MATCHED groups will be replaced by '<*masked*>'
199+
:type log_mask_re: ``typing.Optional[str]``
194200
:rtype: ExecResult
195201
:raises ExecHelperTimeoutError: Timeout exceeded
196202

197203
.. versionadded:: 3.3.0
198204

199-
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
205+
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, log_mask_re=None, exception_class=CalledProcessError, **kwargs)
200206
201207
Execute command and check for return code.
202208

@@ -212,6 +218,9 @@ API: SSHClient and SSHAuth.
212218
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
213219
:param raise_on_err: Raise exception on unexpected return code
214220
:type raise_on_err: ``bool``
221+
:param log_mask_re: regex lookup rule to mask command for logger.
222+
all MATCHED groups will be replaced by '<*masked*>'
223+
:type log_mask_re: ``typing.Optional[str]``
215224
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
216225
:type exception_class: typing.Type[CalledProcessError]
217226
:rtype: ExecResult
@@ -222,7 +231,7 @@ API: SSHClient and SSHAuth.
222231
.. versionchanged:: 3.2.0 Exception class can be substituted
223232
.. versionchanged:: 3.4.0 Expected is not optional, defaults os dependent
224233

225-
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=(0,), exception_class=CalledProcessError, **kwargs)
234+
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, log_mask_re=None, expected=(0,), exception_class=CalledProcessError, **kwargs)
226235
227236
Execute command expecting return code 0 and empty STDERR.
228237

@@ -238,6 +247,9 @@ API: SSHClient and SSHAuth.
238247
:type raise_on_err: ``bool``
239248
:param expected: expected return codes (0 by default)
240249
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
250+
:param log_mask_re: regex lookup rule to mask command for logger.
251+
all MATCHED groups will be replaced by '<*masked*>'
252+
:type log_mask_re: ``typing.Optional[str]``
241253
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
242254
:type exception_class: typing.Type[CalledProcessError]
243255
:rtype: ExecResult

doc/source/Subprocess.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ API: Subprocess
8686
.. versionchanged:: 2.1.0 Use typed NamedTuple as result
8787
.. versionchanged:: 3.2.0 Expose cwd and env as optional keyword-only arguments
8888

89-
.. py:method:: execute(command, verbose=False, timeout=1*60*60, **kwargs)
89+
.. py:method:: execute(command, verbose=False, timeout=1*60*60, *, log_mask_re=None, **kwargs)
9090
9191
Execute command and wait for return code.
9292

@@ -96,6 +96,9 @@ API: Subprocess
9696
:type verbose: ``bool``
9797
:param timeout: Timeout for command execution.
9898
:type timeout: ``typing.Union[int, float, None]``
99+
:param log_mask_re: regex lookup rule to mask command for logger.
100+
all MATCHED groups will be replaced by '<*masked*>'
101+
:type log_mask_re: ``typing.Optional[str]``
99102
:rtype: ExecResult
100103
:raises ExecHelperTimeoutError: Timeout exceeded
101104

@@ -105,7 +108,7 @@ API: Subprocess
105108
.. versionchanged:: 1.2.0 default timeout 1 hour
106109
.. versionchanged:: 1.2.0 stdin data
107110

108-
.. py:method:: __call__(command, verbose=False, timeout=1*60*60, **kwargs)
111+
.. py:method:: __call__(command, verbose=False, timeout=1*60*60, *, log_mask_re=None, **kwargs)
109112
110113
Execute command and wait for return code.
111114

@@ -115,13 +118,16 @@ API: Subprocess
115118
:type verbose: ``bool``
116119
:param timeout: Timeout for command execution.
117120
:type timeout: ``typing.Union[int, float, None]``
121+
:param log_mask_re: regex lookup rule to mask command for logger.
122+
all MATCHED groups will be replaced by '<*masked*>'
123+
:type log_mask_re: ``typing.Optional[str]``
118124
:rtype: ExecResult
119125
:raises ExecHelperTimeoutError: Timeout exceeded
120126

121127
.. note:: stdin channel is closed after the input processing
122128
.. versionadded:: 3.3.0
123129

124-
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, exception_class=CalledProcessError, **kwargs)
130+
.. py:method:: check_call(command, verbose=False, timeout=1*60*60, error_info=None, expected=(0,), raise_on_err=True, *, log_mask_re=None, exception_class=CalledProcessError, **kwargs)
125131
126132
Execute command and check for return code.
127133

@@ -137,6 +143,9 @@ API: Subprocess
137143
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
138144
:param raise_on_err: Raise exception on unexpected return code
139145
:type raise_on_err: ``bool``
146+
:param log_mask_re: regex lookup rule to mask command for logger.
147+
all MATCHED groups will be replaced by '<*masked*>'
148+
:type log_mask_re: ``typing.Optional[str]``
140149
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
141150
:type exception_class: typing.Type[CalledProcessError]
142151
:rtype: ExecResult
@@ -148,7 +157,7 @@ API: Subprocess
148157
.. versionchanged:: 3.2.0 Exception class can be substituted
149158
.. versionchanged:: 3.4.0 Expected is not optional, defaults os dependent
150159

151-
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, expected=(0,), exception_class=CalledProcessError, **kwargs)
160+
.. py:method:: check_stderr(command, verbose=False, timeout=1*60*60, error_info=None, raise_on_err=True, *, log_mask_re=None, expected=(0,), exception_class=CalledProcessError, **kwargs)
152161
153162
Execute command expecting return code 0 and empty STDERR.
154163

@@ -164,6 +173,9 @@ API: Subprocess
164173
:type raise_on_err: ``bool``
165174
:param expected: expected return codes (0 by default)
166175
:type expected: typing.Iterable[typing.Union[int, ExitCodes]]
176+
:param log_mask_re: regex lookup rule to mask command for logger.
177+
all MATCHED groups will be replaced by '<*masked*>'
178+
:type log_mask_re: ``typing.Optional[str]``
167179
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
168180
:type exception_class: typing.Type[CalledProcessError]
169181
:rtype: ExecResult

exec_helpers/api.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,8 @@ def execute(
308308
command: str,
309309
verbose: bool = False,
310310
timeout: typing.Union[int, float, None] = constants.DEFAULT_TIMEOUT,
311+
*,
312+
log_mask_re: typing.Optional[str] = None,
311313
**kwargs: typing.Any,
312314
) -> exec_result.ExecResult:
313315
"""Execute command and wait for return code.
@@ -318,6 +320,9 @@ def execute(
318320
:type verbose: bool
319321
:param timeout: Timeout for command execution.
320322
:type timeout: typing.Union[int, float, None]
323+
:param log_mask_re: regex lookup rule to mask command for logger.
324+
all MATCHED groups will be replaced by '<*masked*>'
325+
:type log_mask_re: typing.Optional[str]
321326
:param kwargs: additional parameters for call.
322327
:type kwargs: typing.Any
323328
:return: Execution result
@@ -327,10 +332,17 @@ def execute(
327332
.. versionchanged:: 1.2.0 default timeout 1 hour
328333
.. versionchanged:: 2.1.0 Allow parallel calls
329334
"""
330-
async_result: ExecuteAsyncResult = self.execute_async(command, verbose=verbose, **kwargs)
335+
async_result: ExecuteAsyncResult = self.execute_async(
336+
command, verbose=verbose, log_mask_re=log_mask_re, **kwargs
337+
)
331338

332339
result: exec_result.ExecResult = self._exec_command(
333-
command=command, async_result=async_result, timeout=timeout, verbose=verbose, **kwargs
340+
command=command,
341+
async_result=async_result,
342+
timeout=timeout,
343+
verbose=verbose,
344+
log_mask_re=log_mask_re,
345+
**kwargs,
334346
)
335347
message = f"Command {result.cmd!r} exit code: {result.exit_code!s}"
336348
self.logger.log(level=logging.INFO if verbose else logging.DEBUG, msg=message) # type: ignore
@@ -341,6 +353,8 @@ def __call__(
341353
command: str,
342354
verbose: bool = False,
343355
timeout: typing.Union[int, float, None] = constants.DEFAULT_TIMEOUT,
356+
*,
357+
log_mask_re: typing.Optional[str] = None,
344358
**kwargs: typing.Any,
345359
) -> exec_result.ExecResult:
346360
"""Execute command and wait for return code.
@@ -351,6 +365,9 @@ def __call__(
351365
:type verbose: bool
352366
:param timeout: Timeout for command execution.
353367
:type timeout: typing.Union[int, float, None]
368+
:param log_mask_re: regex lookup rule to mask command for logger.
369+
all MATCHED groups will be replaced by '<*masked*>'
370+
:type log_mask_re: typing.Optional[str]
354371
:param kwargs: additional parameters for call.
355372
:type kwargs: typing.Any
356373
:return: Execution result
@@ -359,7 +376,7 @@ def __call__(
359376
360377
.. versionadded:: 3.3.0
361378
"""
362-
return self.execute(command=command, verbose=verbose, timeout=timeout, **kwargs)
379+
return self.execute(command=command, verbose=verbose, timeout=timeout, log_mask_re=log_mask_re, **kwargs)
363380

364381
def check_call(
365382
self,
@@ -370,6 +387,7 @@ def check_call(
370387
expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]] = (proc_enums.EXPECTED,),
371388
raise_on_err: bool = True,
372389
*,
390+
log_mask_re: typing.Optional[str] = None,
373391
exception_class: "typing.Type[exceptions.CalledProcessError]" = exceptions.CalledProcessError,
374392
**kwargs: typing.Any,
375393
) -> exec_result.ExecResult:
@@ -387,6 +405,9 @@ def check_call(
387405
:type expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]
388406
:param raise_on_err: Raise exception on unexpected return code
389407
:type raise_on_err: bool
408+
:param log_mask_re: regex lookup rule to mask command for logger.
409+
all MATCHED groups will be replaced by '<*masked*>'
410+
:type log_mask_re: typing.Optional[str]
390411
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
391412
:type exception_class: typing.Type[exceptions.CalledProcessError]
392413
:param kwargs: additional parameters for call.
@@ -403,7 +424,7 @@ def check_call(
403424
expected_codes: typing.Tuple[typing.Union[int, proc_enums.ExitCodes], ...] = proc_enums.exit_codes_to_enums(
404425
expected
405426
)
406-
result: exec_result.ExecResult = self.execute(command, verbose, timeout, **kwargs)
427+
result: exec_result.ExecResult = self.execute(command, verbose, timeout, log_mask_re=log_mask_re, **kwargs)
407428
append: str = error_info + "\n" if error_info else ""
408429
if result.exit_code not in expected_codes:
409430
message = (
@@ -423,6 +444,7 @@ def check_stderr(
423444
error_info: typing.Optional[str] = None,
424445
raise_on_err: bool = True,
425446
*,
447+
log_mask_re: typing.Optional[str] = None,
426448
expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]] = (proc_enums.EXPECTED,),
427449
exception_class: "typing.Type[exceptions.CalledProcessError]" = exceptions.CalledProcessError,
428450
**kwargs: typing.Any,
@@ -439,6 +461,9 @@ def check_stderr(
439461
:type error_info: typing.Optional[str]
440462
:param raise_on_err: Raise exception on unexpected return code
441463
:type raise_on_err: bool
464+
:param log_mask_re: regex lookup rule to mask command for logger.
465+
all MATCHED groups will be replaced by '<*masked*>'
466+
:type log_mask_re: typing.Optional[str]
442467
:param expected: expected return codes (0 by default)
443468
:type expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]
444469
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
@@ -462,6 +487,7 @@ def check_stderr(
462487
raise_on_err=raise_on_err,
463488
expected=expected,
464489
exception_class=exception_class,
490+
log_mask_re=log_mask_re,
465491
**kwargs,
466492
)
467493
append: str = error_info + "\n" if error_info else ""

0 commit comments

Comments
 (0)