Skip to content

Commit

Permalink
Merge 531d1cc into 730f2a9
Browse files Browse the repository at this point in the history
  • Loading branch information
penguinolog committed Jul 19, 2018
2 parents 730f2a9 + 531d1cc commit 93f9309
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 28 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,16 @@ jobs:
include:
- stage: Static analisys
python: 3.6
services: skip
services: []
install:
- *upgrade_python_toolset
- pip install tox
script:
- tox -e pylint,bandit
- tox -e pylint,bandit,mypy
after_success: skip

- stage: Code style check
python: *mainstream_python
services: skip
install:
- *upgrade_python_toolset
- pip install tox
Expand All @@ -45,8 +44,7 @@ jobs:
if: tag IS present
# Run on pypy to build not cythonized wheel
python: *pypy
services:
- docker
services: []
install:
- *upgrade_python_toolset
script:
Expand Down
2 changes: 2 additions & 0 deletions exec_helpers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

from .exec_result import ExecResult
from .api import ExecHelper
from .ssh_auth import SSHAuth
from .ssh_client import SSHClient
from .subprocess_runner import Subprocess # nosec # Expected
Expand All @@ -39,6 +40,7 @@
'ParallelCallExceptions',
'ParallelCallProcessError',
'ExecHelperTimeoutError',
'ExecHelper',
'SSHClient',
'SSHAuth',
'Subprocess',
Expand Down
18 changes: 10 additions & 8 deletions exec_helpers/_ssh_client_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import threaded
import six

from exec_helpers import _api
from exec_helpers import api
from exec_helpers import constants
from exec_helpers import exec_result
from exec_helpers import exceptions
Expand Down Expand Up @@ -97,10 +97,10 @@ class _MemorizedSSH(type):

@classmethod
def __prepare__(
mcs,
mcs, # type: typing.Type[_MemorizedSSH]
name, # type: str
bases, # type: typing.Iterable[typing.Type]
**kwargs
**kwargs # type: typing.Dict
): # type: (...) -> collections.OrderedDict # pylint: disable=unused-argument
"""Metaclass magic for object storage.
Expand All @@ -109,7 +109,7 @@ def __prepare__(
return collections.OrderedDict() # pragma: no cover

def __call__(
cls,
cls, # type: _MemorizedSSH
host, # type: str
port=22, # type: int
username=None, # type: typing.Optional[str]
Expand Down Expand Up @@ -167,7 +167,7 @@ def __call__(
return ssh

@classmethod
def clear_cache(mcs): # type: () -> None
def clear_cache(mcs): # type: (typing.Type[_MemorizedSSH]) -> None
"""Clear cached connections for initialize new instance on next call.
getrefcount is used to check for usage.
Expand All @@ -185,14 +185,14 @@ def clear_cache(mcs): # type: () -> None
mcs.__cache = {}

@classmethod
def close_connections(mcs): # type: (...) -> None
def close_connections(mcs): # type: (typing.Type[_MemorizedSSH]) -> None
"""Close connections for selected or all cached records."""
for ssh in mcs.__cache.values():
if ssh.is_alive:
ssh.close()


class SSHClientBase(six.with_metaclass(_MemorizedSSH, _api.ExecHelper)):
class SSHClientBase(six.with_metaclass(_MemorizedSSH, api.ExecHelper)):
"""SSH Client helper."""

__slots__ = (
Expand Down Expand Up @@ -454,7 +454,9 @@ def close(self):

# noinspection PyMethodParameters
@close.class_method
def close(cls): # pylint: disable=no-self-argument
def close( # pylint: disable=no-self-argument
cls # type: typing.Type[SSHClientBase]
): # type: (...) -> None
"""Close all memorized SSH and SFTP sessions."""
# noinspection PyUnresolvedReferences
cls.__class__.close_connections()
Expand Down
16 changes: 9 additions & 7 deletions exec_helpers/_ssh_client_base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import typing

import paramiko # type: ignore

from exec_helpers import exec_result, ssh_auth, _api
from exec_helpers import exec_result, ssh_auth, api

CPYTHON: bool = ...


class _MemorizedSSH(type):
@classmethod
def __prepare__(mcs: typing.Type, name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ...
def __prepare__(mcs: typing.Type[_MemorizedSSH], name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ...

def __call__( # type: ignore
cls: typing.Type[SSHClientBase],
cls: _MemorizedSSH,
host: str,
port: int=...,
username: typing.Optional[str]=...,
Expand All @@ -22,13 +24,13 @@ class _MemorizedSSH(type):
) -> SSHClientBase: ...

@classmethod
def clear_cache(mcs: typing.Type[SSHClientBase]) -> None: ... # type: ignore
def clear_cache(mcs: typing.Type[_MemorizedSSH]) -> None: ...

@classmethod
def close_connections(mcs: typing.Type[SSHClientBase]) -> None: ... # type: ignore
def close_connections(mcs: typing.Type[_MemorizedSSH]) -> None: ...


class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH):
class SSHClientBase(api.ExecHelper, metaclass=_MemorizedSSH):
def __hash__(self) -> int: ...

def __init__(
Expand Down Expand Up @@ -65,7 +67,7 @@ class SSHClientBase(_api.ExecHelper, metaclass=_MemorizedSSH):
def _sftp(self) -> paramiko.sftp_client.SFTPClient: ...

@classmethod
def close(cls) -> None: ...
def close(cls: typing.Union[SSHClientBase, typing.Type[SSHClientBase]]) -> None: ...

@classmethod
def _clear_cache(cls) -> None: ...
Expand Down
2 changes: 2 additions & 0 deletions exec_helpers/_api.py → exec_helpers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"""ExecHelpers global API.
.. versionadded:: 1.2.0
.. versionchanged:: 1.3.5 make API public to use as interface
"""

from __future__ import absolute_import
Expand Down Expand Up @@ -54,6 +55,7 @@ def __init__(
:type log_mask_re: typing.Optional[str]
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
.. versionchanged:: 1.3.5 make API public to use as interface
"""
self.__lock = threading.RLock()
self.__logger = logger
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions exec_helpers/subprocess_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import six
import threaded

from exec_helpers import _api
from exec_helpers import api
from exec_helpers import exec_result
from exec_helpers import exceptions
from exec_helpers import _log_templates
Expand Down Expand Up @@ -122,7 +122,7 @@ def set_nonblocking_pipe(pipe): # type: (typing.Any) -> None
)


class Subprocess(six.with_metaclass(SingletonMeta, _api.ExecHelper)):
class Subprocess(six.with_metaclass(SingletonMeta, api.ExecHelper)):
"""Subprocess helper with timeouts and lock-free FIFO."""

def __init__(
Expand Down
8 changes: 4 additions & 4 deletions exec_helpers/subprocess_runner.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import subprocess
import threading
import typing

from exec_helpers import exec_result, _api
from exec_helpers import exec_result, api

logger: logging.Logger
devnull: typing.IO
Expand All @@ -16,16 +16,16 @@ class SingletonMeta(type):
_instances: typing.Dict[typing.Type, typing.Any] = ...
_lock: threading.RLock = ...

def __call__(cls: typing.Type, *args: typing.Tuple, **kwargs: typing.Dict) -> typing.Any: ...
def __call__(cls: SingletonMeta, *args: typing.Tuple, **kwargs: typing.Dict) -> typing.Any: ...

@classmethod
def __prepare__(mcs: typing.Type, name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ...
def __prepare__(mcs: typing.Type[SingletonMeta], name: str, bases: typing.Iterable[typing.Type], **kwargs: typing.Dict) -> collections.OrderedDict: ...


def set_nonblocking_pipe(pipe: typing.Any) -> None: ...


class Subprocess(_api.ExecHelper, metaclass=SingletonMeta):
class Subprocess(api.ExecHelper, metaclass=SingletonMeta):
def __init__(
self,
log_mask_re: typing.Optional[str]=...
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _extension(modpath):


requires_optimization = [
_extension('exec_helpers._api'),
_extension('exec_helpers.api'),
_extension('exec_helpers.constants'),
_extension('exec_helpers._log_templates'),
_extension('exec_helpers.exceptions'),
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[tox]
minversion = 2.0
envlist = pep8, pep257, py{27,34,35,36,37,py,py3}, pylint, bandit, py{34,35,36,37}-nocov, docs,
envlist = pep8, pep257, py{27,34,35,36,37,py,py3}, pylint, bandit, py{34,35,36,37}-nocov, mypy, docs
skipsdist = True
skip_missing_interpreters = True

Expand Down

0 comments on commit 93f9309

Please sign in to comment.