Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define package in setup.cfg instead of setup.py #452

Merged
merged 1 commit into from
May 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
[metadata]
name = mirakuru
version = 2.3.0
url = https://github.com/ClearcodeHQ/mirakuru
description = Process executor (not only) for tests.
long_description = file: README.rst, CHANGES.rst
long_description_content_type = text/x-rst
keywords = process, executor, tests, orchestration
license = LGPLv3+
maintainer = Grzegorz Śliwiński
maintainer_email = fizyk+pypi@fizyk.net.pl
classifiers =
Development Status :: 5 - Production/Stable
Intended Audience :: Developers
License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)
Natural Language :: English
Operating System :: OS Independent
Programming Language :: Python
Programming Language :: Python :: 3
Programming Language :: Python :: 3.7
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3 :: Only

[options]
zip_safe = False
include_package_data = True
python_requires = >= 3.7
packages = find:
package_dir =
=src
install_requires =
# psutil is used to find processes leaked during termination.
# It runs on many platforms but not Cygwin:
# <https://github.com/giampaolo/psutil/issues/82>.
psutil>=4.0.0; sys_platform != "cygwin"

[options.packages.find]
where = src

[options.extras_require]
tests =
pytest
pytest-cov
python-daemon

[pycodestyle]
max-line-length = 80
exclude = docs/*,build/*,venv/*
Expand Down
75 changes: 3 additions & 72 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2014 by Clearcode <http://clearcode.cc>
# Copyright (C) 2014-2021 by Clearcode <http://clearcode.cc>
# and associates (see AUTHORS).

# This file is part of mirakuru.
Expand All @@ -16,75 +16,6 @@
# You should have received a copy of the GNU Lesser General Public License
# along with mirakuru. If not, see <http://www.gnu.org/licenses/>.
"""Mirakuru installation module."""
from setuptools import setup

import os
from setuptools import setup, find_packages


here = os.path.dirname(__file__)


requirements = [
# psutil is used to find processes leaked during termination.
# It runs on many platforms but not Cygwin:
# <https://github.com/giampaolo/psutil/issues/82>.
'psutil>=4.0.0; sys_platform != "cygwin"',
]

tests_require = (
"pytest", # tests framework used
"pytest-cov", # coverage reports to verify tests quality
"python-daemon", # used in test for easy creation of daemons
)
extras_require = {
"docs": ["sphinx"],
"tests": tests_require,
}


def read(fname):
"""
Read filename.

:param str fname: name of a file to read
"""
return open(os.path.join(here, fname)).read()


setup(
name="mirakuru",
version="2.3.0",
description="Process executor for tests.",
long_description=(read("README.rst") + "\n\n" + read("CHANGES.rst")),
keywords="process executor tests summon_process",
url="https://github.com/ClearcodeHQ/mirakuru",
author="Clearcode - The A Room",
author_email="thearoom@clearcode.cc",
license="LGPL",
python_requires=">=3.6",
classifiers=[
"Development Status :: 5 - Production/Stable",
"Environment :: Web Environment",
"Intended Audience :: Developers",
"License :: OSI Approved :: "
"GNU Lesser General Public License v3 or later (LGPLv3+)",
"Natural Language :: English",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: Implementation :: PyPy",
"Topic :: Software Development :: Testing",
],
package_dir={"": "src"},
packages=find_packages("src"),
install_requires=requirements,
tests_require=tests_require,
test_suite="tests",
include_package_data=True,
zip_safe=False,
extras_require=extras_require,
)
setup()
15 changes: 5 additions & 10 deletions src/mirakuru/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
)
from mirakuru.compat import SIGKILL

log = logging.getLogger(__name__) # pylint: disable=invalid-name
LOG = logging.getLogger(__name__)

ENV_UUID = "mirakuru_uuid"
"""
Expand All @@ -73,16 +73,13 @@
@atexit.register
def cleanup_subprocesses() -> None:
"""On python exit: find possibly running subprocesses and kill them."""
# pylint: disable=redefined-outer-name, reimported, import-outside-toplevel
# atexit functions tends to loose global imports sometimes so reimport
# everything what is needed again here:
import os
import errno
from mirakuru.base_env import processes_with_env
from mirakuru.compat import SIGKILL

# pylint: enable=redefined-outer-name, reimported, import-outside-toplevel

pids = processes_with_env(ENV_UUID, str(os.getpid()))
for pid in pids:
try:
Expand Down Expand Up @@ -199,6 +196,7 @@ def running(self) -> bool:
:rtype: bool
"""
if self.process is None:
LOG.debug("There is no process running!")
return False
return self.process.poll() is None

Expand Down Expand Up @@ -260,10 +258,8 @@ def start(self: SimpleExecutorType) -> SimpleExecutorType:
command: Union[str, List[str], Tuple[str, ...]] = self.command
if not self._shell:
command = self.command_parts

# pylint:disable=consider-using-with
LOG.debug("Starting process: %s", command)
self.process = subprocess.Popen(command, **self._popen_kwargs)
# pylint:enable=consider-using-with

self._set_timeout()
return self
Expand Down Expand Up @@ -299,7 +295,7 @@ def _kill_all_kids(self, sig: int) -> Set[int]:
"""
pids = processes_with_env(ENV_UUID, self._uuid)
for pid in pids:
log.debug("Killing process %d ...", pid)
LOG.debug("Killing process %d ...", pid)
try:
os.kill(pid, sig)
except OSError as err:
Expand All @@ -308,7 +304,7 @@ def _kill_all_kids(self, sig: int) -> Set[int]:
pass
else:
raise
log.debug("Killed process %d.", pid)
LOG.debug("Killed process %d.", pid)
return pids

def stop(
Expand Down Expand Up @@ -370,7 +366,6 @@ def process_stopped() -> bool:
# Did the process shut down cleanly? A an exit code of `-sig` means
# that it has terminated due to signal `sig`, which is intended. So
# don't treat that as an error.
# pylint: disable=invalid-unary-operand-type
expected_exit_code = -sig
if exp_sig is not None:
expected_exit_code = -exp_sig
Expand Down
9 changes: 4 additions & 5 deletions src/mirakuru/base_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
psutil = None


log = logging.getLogger(__name__) # pylint: disable=invalid-name
LOG = logging.getLogger(__name__)


PS_XE_PID_MATCH = re.compile(r"^.*?(\d+).+$")
Expand Down Expand Up @@ -70,7 +70,7 @@ def processes_with_env_ps(env_name: str, env_value: str) -> Set[int]:

It uses `$ ps xe -o pid,cmd` command so it works only on systems
having such command available (Linux, MacOS). If not available function
will just log error.
will just LOG error.

:param str env_name: name of environment variable to be found
:param str env_value: environment variable value prefix
Expand All @@ -85,15 +85,15 @@ def processes_with_env_ps(env_name: str, env_value: str) -> Set[int]:
ps_xe = subprocess.check_output(cmd).splitlines()
except OSError as err:
if err.errno == errno.ENOENT:
log.error(
LOG.error(
"`$ ps xe -o pid,cmd` command was called but it is not "
"available on this operating system. Mirakuru will not "
"be able to list the process tree and find if there are "
"any leftovers of the Executor."
)
return pids
except subprocess.CalledProcessError:
log.error("`$ ps xe -o pid,cmd` command exited with non-zero code.")
LOG.error("`$ ps xe -o pid,cmd` command exited with non-zero code.")

env = f"{env_name}={env_value}"

Expand All @@ -109,7 +109,6 @@ def processes_with_env_ps(env_name: str, env_value: str) -> Set[int]:
return pids


# pylint: disable=invalid-name
if psutil:
processes_with_env = processes_with_env_psutil
else:
Expand Down
15 changes: 9 additions & 6 deletions tests/executors/test_tcp_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,29 @@
PORT = 7986

HTTP_SERVER = f"{HTTP_SERVER_CMD} {PORT}"
NC_COMMAND = 'bash -c "sleep 2 && nc -lk 3000"'


def test_start_and_wait(caplog: LogCaptureFixture):
"""Test if executor await for process to accept connections."""
caplog.set_level(logging.DEBUG)
command = 'bash -c "sleep 2 && nc -l 3000"'
executor = TCPExecutor(command, "localhost", port=3000, timeout=5)
caplog.set_level(logging.DEBUG, logger="mirakuru")
executor = TCPExecutor(NC_COMMAND, "localhost", port=3000, timeout=5)
executor.start()

assert executor.running() is True
executor.stop()


def test_repr_and_str():
"""Check the proper str and repr conversion."""
executor = TCPExecutor(NC_COMMAND, "localhost", port=3000, timeout=5)
# check proper __str__ and __repr__ rendering:
assert "TCPExecutor" in repr(executor)
assert command in str(executor)
assert NC_COMMAND in str(executor)


def test_it_raises_error_on_timeout():
"""Check if TimeoutExpired gets raised correctly."""
command = 'bash -c "sleep 10 && nc -l 3000"'
command = 'bash -c "sleep 10 && nc -lk 3000"'
executor = TCPExecutor(command, host="localhost", port=3000, timeout=2)

with pytest.raises(TimeoutExpired):
Expand Down