Skip to content

Commit 4f36630

Browse files
committed
Mass backport from 3.5
* type hints * __all__ movement Signed-off-by: Aleksei Stepanov <penguinolog@gmail.com>
1 parent 6ac66ae commit 4f36630

14 files changed

+107
-159
lines changed

.travis.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ install:
1010
_python:
1111
- &python34
1212
name: "Python 3.4"
13-
python: 3.4
13+
python: "3.4"
1414
- &python35
1515
name: "Python 3.5"
16-
python: 3.5
16+
python: "3.5"
1717
- &python36
1818
name: "Python 3.6"
19-
python: 3.6
19+
python: "3.6"
2020
- &python37
2121
name: "Python 3.7"
22-
python: 3.7
22+
python: "3.7"
2323
dist: xenial
2424
sudo: true
2525
- &pypy3
2626
name: "PyPy3"
27-
python: pypy3.5
27+
python: "pypy3.5"
2828

2929
_helpers:
3030
- &install_cython pip install --upgrade Cython
@@ -68,7 +68,7 @@ jobs:
6868
install:
6969
- *upgrade_python_toolset
7070
- *install_deps
71-
- pip install --upgrade "pylint >= 2.1"
71+
- pip install --upgrade "pylint >= 2.2"
7272
script:
7373
- pylint exec_helpers
7474
- <<: *static_analysis
@@ -84,7 +84,7 @@ jobs:
8484
install:
8585
- *upgrade_python_toolset
8686
- *install_deps
87-
- pip install --upgrade "mypy >= 0.650"
87+
- pip install --upgrade "mypy >= 0.660"
8888
script:
8989
- mypy --strict exec_helpers
9090

@@ -135,7 +135,7 @@ jobs:
135135
# This prevents job from appearing in test plan unless commit is tagged:
136136
if: tag IS present
137137
# Run on pypy to build not cythonized wheel
138-
<<: *python34
138+
<<: *pypy3
139139
name: Build universal and cythonized bdist_wheel. Deploy bdist and sdist.
140140
services:
141141
- docker
@@ -144,7 +144,7 @@ jobs:
144144
script:
145145
- ./tools/run_docker.sh "exec_helpers"
146146
before_deploy:
147-
- pip install -r requirements.txt
147+
- pip install -r build_requirements.txt
148148
- *build_package
149149
deploy:
150150
- provider: pypi

appveyor.yml

Lines changed: 0 additions & 53 deletions
This file was deleted.

exec_helpers/__init__.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@
1414

1515
"""Execution helpers for simplified usage of subprocess and ssh."""
1616

17-
import typing
17+
__all__ = (
18+
"ExecHelperError",
19+
"ExecCalledProcessError",
20+
"CalledProcessError",
21+
"ParallelCallExceptions",
22+
"ParallelCallProcessError",
23+
"ExecHelperNoKillError",
24+
"ExecHelperTimeoutError",
25+
"ExecHelper",
26+
"SSHClient",
27+
"SshExecuteAsyncResult",
28+
"SSHAuth",
29+
"Subprocess",
30+
"SubprocessExecuteAsyncResult",
31+
"ExitCodes",
32+
"ExecResult",
33+
)
1834

1935
import pkg_resources
2036

@@ -37,26 +53,8 @@
3753
from ._ssh_client_base import SshExecuteAsyncResult
3854
from .subprocess_runner import Subprocess, SubprocessExecuteAsyncResult # nosec # Expected
3955

40-
__all__ = (
41-
"ExecHelperError",
42-
"ExecCalledProcessError",
43-
"CalledProcessError",
44-
"ParallelCallExceptions",
45-
"ParallelCallProcessError",
46-
"ExecHelperNoKillError",
47-
"ExecHelperTimeoutError",
48-
"ExecHelper",
49-
"SSHClient",
50-
"SshExecuteAsyncResult",
51-
"SSHAuth",
52-
"Subprocess",
53-
"SubprocessExecuteAsyncResult",
54-
"ExitCodes",
55-
"ExecResult",
56-
) # type: typing.Tuple[str, ...]
57-
5856
try: # pragma: no cover
59-
__version__ = pkg_resources.get_distribution(__name__).version
57+
__version__ = pkg_resources.get_distribution(__name__).version # type: str
6058
except pkg_resources.DistributionNotFound: # pragma: no cover
6159
# package is not installed, try to get from SCM
6260
try:

exec_helpers/_ssh_client_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ class _MemorizedSSH(abc.ABCMeta):
121121
__cache = {} # type: typing.Dict[typing.Tuple[str, int], SSHClientBase]
122122

123123
@classmethod
124-
def __prepare__( # pylint: disable=unused-argument
124+
def __prepare__( # type: ignore # pylint: disable=unused-argument
125125
mcs: typing.Type["_MemorizedSSH"], name: str, bases: typing.Iterable[type], **kwargs: typing.Any
126126
) -> "collections.OrderedDict[str, typing.Any]":
127127
"""Metaclass magic for object storage.

exec_helpers/_subprocess_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def safe_stop(proc: psutil.Process, kill: bool = False) -> None:
4747
pass
4848

4949
parent = psutil.Process(pid)
50-
children = parent.children(recursive=True)
50+
children = parent.children(recursive=True) # type: typing.List[psutil.Process]
5151
for child in children: # type: psutil.Process
5252
safe_stop(child) # SIGTERM to allow cleanup
5353
_, alive = psutil.wait_procs(children, timeout=1)

exec_helpers/api.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,14 @@ def _mask_command(self, cmd: str, log_mask_re: typing.Optional[str] = None) -> s
109109
def mask(text: str, rules: str) -> str:
110110
"""Mask part of text using rules."""
111111
indexes = [0] # Start of the line
112+
masked = ""
112113

113114
# places to exclude
114115
for match in re.finditer(rules, text):
115116
for idx, _ in enumerate(match.groups()):
116117
indexes.extend(match.span(idx + 1))
117118
indexes.append(len(text)) # End
118119

119-
masked = ""
120-
121120
# Replace inserts
122121
for idx in range(0, len(indexes) - 2, 2):
123122
start = indexes[idx]
@@ -128,14 +127,14 @@ def mask(text: str, rules: str) -> str:
128127
masked += text[indexes[-2] : indexes[-1]] # final part
129128
return masked
130129

131-
cmd = cmd.rstrip()
130+
result = cmd.rstrip() # type: str
132131

133-
if self.log_mask_re:
134-
cmd = mask(cmd, self.log_mask_re)
135-
if log_mask_re:
136-
cmd = mask(cmd, log_mask_re)
132+
if self.log_mask_re is not None:
133+
result = mask(result, self.log_mask_re)
134+
if log_mask_re is not None:
135+
result = mask(result, log_mask_re)
137136

138-
return cmd
137+
return result
139138

140139
@abc.abstractmethod
141140
def execute_async(
@@ -248,7 +247,7 @@ def execute(
248247

249248
result = self._exec_command(
250249
command=command, async_result=async_result, timeout=timeout, verbose=verbose, **kwargs
251-
)
250+
) # type: exec_result.ExecResult
252251
message = "Command {result.cmd!r} exit code: {result.exit_code!s}".format(result=result)
253252
self.logger.log(level=logging.INFO if verbose else logging.DEBUG, msg=message) # type: ignore
254253
return result
@@ -318,18 +317,18 @@ def check_call(
318317
.. versionchanged:: 2.10.0 Expected is not optional, defaults os dependent
319318
"""
320319
expected_codes = proc_enums.exit_codes_to_enums(expected)
321-
ret = self.execute(command, verbose, timeout, **kwargs)
322-
if ret.exit_code not in expected_codes:
320+
result = self.execute(command, verbose, timeout, **kwargs) # type: exec_result.ExecResult
321+
if result.exit_code not in expected_codes:
323322
message = (
324323
"{append}Command {result.cmd!r} returned exit code "
325324
"{result.exit_code!s} while expected {expected!s}".format(
326-
append=error_info + "\n" if error_info else "", result=ret, expected=expected_codes
325+
append=error_info + "\n" if error_info else "", result=result, expected=expected_codes
327326
)
328327
)
329328
self.logger.error(msg=message)
330329
if raise_on_err:
331-
raise exception_class(result=ret, expected=expected_codes)
332-
return ret
330+
raise exception_class(result=result, expected=expected_codes)
331+
return result
333332

334333
def check_stderr(
335334
self,
@@ -370,7 +369,7 @@ def check_stderr(
370369
.. versionchanged:: 2.9.3 Exception class can be substituted
371370
.. versionchanged:: 2.10.0 Expected is not optional, defaults os dependent
372371
"""
373-
ret = self.check_call(
372+
result = self.check_call(
374373
command,
375374
verbose,
376375
timeout=timeout,
@@ -380,15 +379,16 @@ def check_stderr(
380379
exception_class=exception_class,
381380
**kwargs
382381
)
383-
if ret.stderr:
382+
append = error_info + "\n" if error_info else "" # type: str
383+
if result.stderr:
384384
message = (
385385
"{append}Command {result.cmd!r} output contains STDERR while not expected\n"
386-
"\texit code: {result.exit_code!s}".format(append=error_info + "\n" if error_info else "", result=ret)
386+
"\texit code: {result.exit_code!s}".format(append=append, result=result)
387387
)
388388
self.logger.error(msg=message)
389389
if raise_on_err:
390-
raise exception_class(result=ret, expected=expected)
391-
return ret
390+
raise exception_class(result=result, expected=expected)
391+
return result
392392

393393
@staticmethod
394394
def _string_bytes_bytearray_as_bytes(src: typing.Union[str, bytes, bytearray]) -> bytes:

exec_helpers/exceptions.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,6 @@
1414

1515
"""Package specific exceptions."""
1616

17-
import typing
18-
19-
from exec_helpers import proc_enums
20-
from exec_helpers import _log_templates
21-
22-
if typing.TYPE_CHECKING: # pragma: no cover
23-
from exec_helpers import exec_result # noqa: F401 # pylint: disable=cyclic-import
24-
2517
__all__ = (
2618
"ExecHelperError",
2719
"ExecHelperNoKillError",
@@ -32,6 +24,14 @@
3224
"ParallelCallExceptions",
3325
)
3426

27+
import typing
28+
29+
from exec_helpers import proc_enums
30+
from exec_helpers import _log_templates
31+
32+
if typing.TYPE_CHECKING: # pragma: no cover
33+
from exec_helpers import exec_result # noqa: F401 # pylint: disable=cyclic-import
34+
3535

3636
class ExecHelperError(Exception):
3737
"""Base class for all exceptions raised inside."""

0 commit comments

Comments
 (0)