Skip to content

Commit

Permalink
Replace pydocstyle and pycodestyle with ruff - closes #655
Browse files Browse the repository at this point in the history
  • Loading branch information
fizyk committed Oct 17, 2023
1 parent 984818d commit 7f3c3bd
Show file tree
Hide file tree
Showing 27 changed files with 125 additions and 209 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ jobs:
mypy: true
rst: true
black: true
pydocstyle: true
pycodestyle: true
ruff: true

3 changes: 1 addition & 2 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ pytest = "==7.4.2"
pytest-cov = "==4.1.0"
coverage = "==7.3.2"
python-daemon = "==3.0.1"
pycodestyle = "==2.11.1"
pydocstyle = {version = "==6.3.0", extras = ["toml"]}
restructuredtext-lint = "==1.4.0"
mypy = "==1.6.0"
black = {version = "==23.9.1", markers="python_version >= '3.8'"}
pygments = "==2.16.1"
tbump = "==6.11.0"
ruff = "==0.1.0"
13 changes: 6 additions & 7 deletions mirakuru/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@
import logging

from mirakuru.base import Executor, SimpleExecutor
from mirakuru.output import OutputExecutor
from mirakuru.tcp import TCPExecutor
from mirakuru.http import HTTPExecutor
from mirakuru.pid import PidExecutor

from mirakuru.exceptions import (
ExecutorError,
TimeoutExpired,
AlreadyRunning,
ExecutorError,
ProcessExitedWithError,
TimeoutExpired,
)
from mirakuru.http import HTTPExecutor
from mirakuru.output import OutputExecutor
from mirakuru.pid import PidExecutor
from mirakuru.tcp import TCPExecutor

__version__ = "2.5.1"

Expand Down
73 changes: 29 additions & 44 deletions mirakuru/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,40 @@
"""Executor with the core functionality."""

import atexit
from contextlib import contextmanager
import errno
import logging
import os
import platform
import shlex
import signal
import subprocess
import time
import uuid
import errno
import platform
from contextlib import contextmanager
from types import TracebackType
from typing import (
Union,
IO,
Any,
Callable,
Dict,
Iterator,
List,
Tuple,
Optional,
Dict,
TypeVar,
Type,
Set,
Iterator,
Callable,
Tuple,
Type,
TypeVar,
Union,
)

from mirakuru.base_env import processes_with_env
from mirakuru.compat import SIGKILL
from mirakuru.exceptions import (
AlreadyRunning,
ProcessExitedWithError,
ProcessFinishedWithError,
TimeoutExpired,
)
from mirakuru.compat import SIGKILL

LOG = logging.getLogger(__name__)

Expand All @@ -75,8 +75,9 @@ def cleanup_subprocesses() -> None:
"""On python exit: find possibly running subprocesses and kill them."""
# atexit functions tends to loose global imports sometimes so reimport
# everything what is needed again here:
import os
import errno
import os

from mirakuru.base_env import processes_with_env
from mirakuru.compat import SIGKILL

Expand Down Expand Up @@ -107,8 +108,7 @@ def __init__( # pylint:disable=too-many-arguments
stdout: Union[None, int, IO[Any]] = subprocess.PIPE,
stderr: Union[None, int, IO[Any]] = None,
) -> None:
"""
Initialize executor.
"""Initialize executor.
:param (str, list) command: command to be run by the subprocess
:param str cwd: current working directory to be set for executor
Expand Down Expand Up @@ -172,8 +172,7 @@ def __init__( # pylint:disable=too-many-arguments
self._uuid = f"{os.getpid()}:{uuid.uuid4()}"

def __enter__(self: SimpleExecutorType) -> SimpleExecutorType:
"""
Enter context manager starting the subprocess.
"""Enter context manager starting the subprocess.
:returns: itself
:rtype: SimpleExecutor
Expand All @@ -190,8 +189,7 @@ def __exit__(
self.stop()

def running(self) -> bool:
"""
Check if executor is running.
"""Check if executor is running.
:returns: True if process is running, False otherwise
:rtype: bool
Expand All @@ -203,8 +201,7 @@ def running(self) -> bool:

@property
def _popen_kwargs(self) -> Dict[str, Any]:
"""
Get kwargs for the process instance.
"""Get kwargs for the process instance.
.. note::
We want to open ``stdin``, ``stdout`` and ``stderr`` as text
Expand Down Expand Up @@ -247,8 +244,7 @@ def _popen_kwargs(self) -> Dict[str, Any]:
return kwargs

def start(self: SimpleExecutorType) -> SimpleExecutorType:
"""
Start defined process.
"""Start defined process.
After process gets started, timeout countdown begins as well.
Expand All @@ -270,8 +266,7 @@ def _set_timeout(self) -> None:
self._endtime = time.time() + self._timeout

def _clear_process(self) -> None:
"""
Close stdin/stdout of subprocess.
"""Close stdin/stdout of subprocess.
It is required because of ResourceWarning in Python 3.
"""
Expand All @@ -282,8 +277,7 @@ def _clear_process(self) -> None:
self._endtime = None

def _kill_all_kids(self, sig: int) -> Set[int]:
"""
Kill all subprocesses (and its subprocesses) that executor started.
"""Kill all subprocesses (and its subprocesses) that executor started.
This function tries to kill all leftovers in process tree that current
executor may have left. It uses environment variable to recognise if
Expand Down Expand Up @@ -313,8 +307,7 @@ def stop(
stop_signal: Optional[int] = None,
expected_returncode: Optional[int] = None,
) -> SimpleExecutorType:
"""
Stop process running.
"""Stop process running.
Wait 10 seconds for the process to end, then just kill it.
Expand Down Expand Up @@ -378,8 +371,7 @@ def process_stopped() -> bool:

@contextmanager
def stopped(self: SimpleExecutorType) -> Iterator[SimpleExecutorType]:
"""
Stop process for given context and starts it afterwards.
"""Stop process for given context and starts it afterwards.
Allows for easier writing resistance integration tests whenever one of
the service fails.
Expand All @@ -394,8 +386,7 @@ def stopped(self: SimpleExecutorType) -> Iterator[SimpleExecutorType]:
def kill(
self: SimpleExecutorType, wait: bool = True, sig: Optional[int] = None
) -> SimpleExecutorType:
"""
Kill the process if running.
"""Kill the process if running.
:param bool wait: set to `True` to wait for the process to end,
or False, to simply proceed after sending signal.
Expand Down Expand Up @@ -430,8 +421,7 @@ def err_output(self) -> Optional[IO[Any]]:
def wait_for(
self: SimpleExecutorType, wait_for: Callable[[], bool]
) -> SimpleExecutorType:
"""
Wait for callback to return True.
"""Wait for callback to return True.
Simply returns if wait_for condition has been met,
raises TimeoutExpired otherwise and kills the process.
Expand All @@ -450,8 +440,7 @@ def wait_for(
raise TimeoutExpired(self, timeout=self._timeout)

def check_timeout(self) -> bool:
"""
Check if timeout has expired.
"""Check if timeout has expired.
Returns True if there is no timeout set or the timeout has not expired.
Kills the process and raises TimeoutExpired exception otherwise.
Expand Down Expand Up @@ -498,8 +487,7 @@ class Executor(SimpleExecutor):
"""Base class for executors with a pre- and after-start checks."""

def pre_start_check(self) -> bool:
"""
Check process before the start of executor.
"""Check process before the start of executor.
Should be overridden in order to return True when some other
executor (or process) has already started with the same configuration.
Expand All @@ -508,8 +496,7 @@ def pre_start_check(self) -> bool:
raise NotImplementedError

def start(self: ExecutorType) -> ExecutorType:
"""
Start executor with additional checks.
"""Start executor with additional checks.
Checks if previous executor isn't running then start process
(executor) and wait until it's started.
Expand All @@ -526,8 +513,7 @@ def start(self: ExecutorType) -> ExecutorType:
return self

def check_subprocess(self) -> bool:
"""
Make sure the process didn't exit with an error and run the checks.
"""Make sure the process didn't exit with an error and run the checks.
:rtype: bool
:return: the actual check status or False before starting the process
Expand All @@ -548,8 +534,7 @@ def check_subprocess(self) -> bool:
return self.after_start_check()

def after_start_check(self) -> bool:
"""
Check process after the start of executor.
"""Check process after the start of executor.
Should be overridden in order to return boolean value if executor
can be treated as started.
Expand Down
8 changes: 3 additions & 5 deletions mirakuru/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import logging
import re
import subprocess
from typing import Set, List
from typing import List, Set

try:
import psutil
Expand All @@ -37,8 +37,7 @@


def processes_with_env_psutil(env_name: str, env_value: str) -> Set[int]:
"""
Find PIDs of processes having environment variable matching given one.
"""Find PIDs of processes having environment variable matching given one.
Internally it uses `psutil` library.
Expand All @@ -65,8 +64,7 @@ def processes_with_env_psutil(env_name: str, env_value: str) -> Set[int]:


def processes_with_env_ps(env_name: str, env_value: str) -> Set[int]:
"""
Find PIDs of processes having environment variable matching given one.
"""Find PIDs of processes having environment variable matching given one.
It uses `$ ps xe -o pid,cmd` command so it works only on systems
having such command available (Linux, MacOS). If not available function
Expand Down
29 changes: 10 additions & 19 deletions mirakuru/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Mirakuru exceptions."""

from typing import Union, TYPE_CHECKING
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING: # pragma: no cover
from mirakuru.base import SimpleExecutor # pylint:disable=cyclic-import
Expand All @@ -10,8 +10,7 @@ class ExecutorError(Exception):
"""Base exception for executor failures."""

def __init__(self, executor: "SimpleExecutor") -> None:
"""
Exception initialization.
"""Exception initialization.
:param mirakuru.base.SimpleExecutor executor: for which exception
occurred
Expand All @@ -26,8 +25,7 @@ class TimeoutExpired(ExecutorError):
def __init__(
self, executor: "SimpleExecutor", timeout: Union[int, float]
) -> None:
"""
Exception initialization with an extra ``timeout`` argument.
"""Exception initialization with an extra ``timeout`` argument.
:param mirakuru.base.SimpleExecutor executor: for which exception
occurred
Expand All @@ -37,8 +35,7 @@ def __init__(
self.timeout = timeout

def __str__(self) -> str:
"""
Return Exception's string representation.
"""Return Exception's string representation.
:returns: string representation
:rtype: str
Expand All @@ -49,16 +46,14 @@ def __str__(self) -> str:


class AlreadyRunning(ExecutorError):
"""
Is raised when the executor seems to be already running.
"""Is raised when the executor seems to be already running.
When some other process (not necessary executor) seems to be started with
same configuration we can't bind to same port.
"""

def __str__(self) -> str:
"""
Return Exception's string representation.
"""Return Exception's string representation.
:returns: string representation
:rtype: str
Expand All @@ -78,17 +73,15 @@ def __str__(self) -> str:


class ProcessExitedWithError(ExecutorError):
"""
Raised when the process invoked by the executor returns a non-zero code.
"""Raised when the process invoked by the executor returns a non-zero code.
We allow the process to exit with zero because we support daemonizing
subprocesses. We assume that when double-forking, the parent process will
exit with 0 in case of successful daemonization.
"""

def __init__(self, executor: "SimpleExecutor", exit_code: int) -> None:
"""
Exception initialization with an extra ``exit_code`` argument.
"""Exception initialization with an extra ``exit_code`` argument.
:param mirakuru.base.SimpleExecutor executor: for which exception
occurred
Expand All @@ -98,8 +91,7 @@ def __init__(self, executor: "SimpleExecutor", exit_code: int) -> None:
self.exit_code = exit_code

def __str__(self) -> str:
"""
Return Exception's string representation.
"""Return Exception's string representation.
:returns: string representation
:rtype: str
Expand All @@ -111,8 +103,7 @@ def __str__(self) -> str:


class ProcessFinishedWithError(ProcessExitedWithError):
"""
Raised when the process invoked by the executor fails when stopping.
"""Raised when the process invoked by the executor fails when stopping.
When a process is stopped, it should shut down cleanly and return zero as
exit code. When is returns a non-zero exit code, this exception is raised.
Expand Down
Loading

0 comments on commit 7f3c3bd

Please sign in to comment.