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

Used some flake8 rules to clean up the code a bit more #73

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/pkg1/test_mod1.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_sleep2():


@pytest.mark.parametrize(
("range_max", "other"), [(10, "10"), (100, "100"), (1000, "1000"), (10000, "10000")]
("range_max", "other"), [(10, "10"), (100, "100"), (1000, "1000"), (10000, "10000")],
)
def test_heavy(range_max, other):
assert len(["a" * i for i in range(range_max)]) == range_max
6 changes: 3 additions & 3 deletions pytest_monitor/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def prepare(self):
RUN_DATE varchar(64), -- Date of test run
SCM_ID varchar(128), -- SCM change id
RUN_DESCRIPTION json
);"""
);""",
)
cursor.execute(
"""
Expand All @@ -112,7 +112,7 @@ def prepare(self):
MEM_USAGE float, -- Max resident memory used.
FOREIGN KEY (ENV_H) REFERENCES EXECUTION_CONTEXTS(ENV_H),
FOREIGN KEY (SESSION_H) REFERENCES TEST_SESSIONS(SESSION_H)
);"""
);""",
)
cursor.execute(
"""
Expand All @@ -129,6 +129,6 @@ def prepare(self):
SYSTEM_INFO varchar(256),
PYTHON_INFO varchar(512)
);
"""
""",
)
self.__cnx.commit()
18 changes: 9 additions & 9 deletions pytest_monitor/pytest_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def pytest_addoption(parser):
" This requires the parameters to be stringifiable.",
)
group.addoption(
"--no-monitor", action="store_true", dest="mtr_none", help="Disable all traces"
"--no-monitor", action="store_true", dest="mtr_none", help="Disable all traces",
)
group.addoption(
"--remote-server",
Expand Down Expand Up @@ -106,7 +106,7 @@ def pytest_addoption(parser):

def pytest_configure(config):
config.addinivalue_line(
"markers", "monitor_skip_test: mark test to be executed but not monitored."
"markers", "monitor_skip_test: mark test to be executed but not monitored.",
)
config.addinivalue_line(
"markers",
Expand Down Expand Up @@ -147,7 +147,7 @@ def pytest_runtest_setup(item):
mark_to_del.append(set_marker)
if set_marker in PYTEST_MONITOR_DEPRECATED_MARKERS:
warnings.warn(
f"Marker {set_marker} is deprecated. Consider upgrading your tests"
f"Marker {set_marker} is deprecated. Consider upgrading your tests",
)

for marker in mark_to_del:
Expand Down Expand Up @@ -221,7 +221,7 @@ def wrapped_function():

def prof():
m = memory_profiler.memory_usage(
(wrapped_function, ()), max_iterations=1, max_usage=True, retval=True
(wrapped_function, ()), max_iterations=1, max_usage=True, retval=True,
)
if isinstance(m[1], BaseException): # Do we have any outcome?
raise m[1]
Expand Down Expand Up @@ -254,15 +254,15 @@ def pytest_sessionstart(session):
and session.config.option.mtr_component_prefix
):
raise pytest.UsageError(
"Invalid usage: --force-component and --component-prefix are incompatible options!"
"Invalid usage: --force-component and --component-prefix are incompatible options!",
)
if (
session.config.option.mtr_no_db
and not session.config.option.mtr_remote
and not session.config.option.mtr_none
):
warnings.warn(
"pytest-monitor: No storage specified but monitoring is requested. Disabling monitoring."
"pytest-monitor: No storage specified but monitoring is requested. Disabling monitoring.",
)
session.config.option.mtr_none = True
component = (
Expand All @@ -282,12 +282,12 @@ def pytest_sessionstart(session):
None if session.config.option.mtr_none else session.config.option.mtr_remote
)
session.pytest_monitor = PyTestMonitorSession(
db=db, remote=remote, component=component, scope=session.config.option.mtr_scope
db=db, remote=remote, component=component, scope=session.config.option.mtr_scope,
)
global PYTEST_MONITORING_ENABLED
PYTEST_MONITORING_ENABLED = not session.config.option.mtr_none
session.pytest_monitor.compute_info(
session.config.option.mtr_description, session.config.option.mtr_tags
session.config.option.mtr_description, session.config.option.mtr_tags,
)
yield

Expand Down Expand Up @@ -330,7 +330,7 @@ def prf_tracer(request):
yield
ptimes_b = request.session.pytest_monitor.process.cpu_times()
if not request.node.monitor_skip_test and getattr(
request.node, "monitor_results", False
request.node, "monitor_results", False,
):
item_name = request.node.originalname or request.node.name
item_loc = getattr(request.node, PYTEST_MONITOR_ITEM_LOC_MEMBER)[0]
Expand Down
8 changes: 4 additions & 4 deletions pytest_monitor/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get_env_id(self, env):
db, remote = None, None
if self.__db:
row = self.__db.query(
"SELECT ENV_H FROM EXECUTION_CONTEXTS WHERE ENV_H= ?", (env.hash(),)
"SELECT ENV_H FROM EXECUTION_CONTEXTS WHERE ENV_H= ?", (env.hash(),),
)
db = row[0] if row else None
if self.__remote:
Expand Down Expand Up @@ -112,14 +112,14 @@ def set_environment_info(self, env):
if self.__db and db_id is None:
self.__db.insert_execution_context(env)
db_id = self.__db.query(
"select ENV_H from EXECUTION_CONTEXTS where ENV_H = ?", (env.hash(),)
"select ENV_H from EXECUTION_CONTEXTS where ENV_H = ?", (env.hash(),),
)[0]
if self.__remote and remote_id is None:
# We must postpone that to be run at the end of the pytest session.
r = requests.post(f"{self.__remote}/contexts/", json=env.to_dict())
if r.status_code != HTTPStatus.CREATED:
warnings.warn(
f"Cannot insert execution context in remote server (rc={r.status_code}! Deactivating..."
f"Cannot insert execution context in remote server (rc={r.status_code}! Deactivating...",
)
self.__remote = ""
else:
Expand All @@ -131,7 +131,7 @@ def dummy():
return True

memuse = memory_profiler.memory_usage(
(dummy,), max_iterations=1, max_usage=True
(dummy,), max_iterations=1, max_usage=True,
)
self.__mem_usage_base = memuse[0] if type(memuse) is list else memuse

Expand Down
33 changes: 16 additions & 17 deletions pytest_monitor/sys_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@
def collect_ci_info():
d = dict()
# Test for jenkins
if "BUILD_NUMBER" in os.environ:
if "BRANCH_NAME" in os.environ or "JOB_NAME" in os.environ:
br = (
os.environ["BRANCH_NAME"]
if "BRANCH_NAME" in os.environ
else os.environ["JOB_NAME"]
)
d = dict(
pipeline_branch=br,
pipeline_build_no=os.environ["BUILD_NUMBER"],
__ci__="jenkinsci",
)
if "BUILD_NUMBER" in os.environ and ("BRANCH_NAME" in os.environ or "JOB_NAME" in os.environ):
br = (
os.environ["BRANCH_NAME"]
if "BRANCH_NAME" in os.environ
else os.environ["JOB_NAME"]
)
d = dict(
pipeline_branch=br,
pipeline_build_no=os.environ["BUILD_NUMBER"],
__ci__="jenkinsci",
)
# Test for CircleCI
if "CIRCLE_JOB" in os.environ and "CIRCLE_BUILD_NUM" in os.environ:
d = dict(
Expand Down Expand Up @@ -59,7 +58,7 @@ def collect_ci_info():
def determine_scm_revision():
for scm, cmd in (("git", r"git rev-parse HEAD"), ("p4", r"p4 changes -m1 \#have")):
p = subprocess.Popen(
cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE
cmd, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE,
)
p_out, _ = p.communicate()
if p.returncode == 0:
Expand All @@ -73,7 +72,7 @@ def determine_scm_revision():
def _get_cpu_string():
if platform.system().lower() == "darwin":
old_path = os.environ["PATH"]
os.environ["PATH"] = old_path + ":" + "/usr/sbin"
os.environ["PATH"] = f"{old_path}:/usr/sbin"
ret = (
subprocess.check_output("sysctl -n machdep.cpu.brand_string", shell=True)
.decode()
Expand All @@ -100,7 +99,7 @@ def __init__(self):
self.__cpu_freq_base = psutil.cpu_freq().current
except (AttributeError, NotImplementedError, FileNotFoundError):
warnings.warn(
"Unable to fetch CPU frequency. Trying to read it from environment.."
"Unable to fetch CPU frequency. Trying to read it from environment..",
)
self._read_cpu_freq_from_env()
self.__proc_typ = platform.processor()
Expand All @@ -114,11 +113,11 @@ def __init__(self):
def _read_cpu_freq_from_env(self):
try:
self.__cpu_freq_base = float(
os.environ.get("PYTEST_MONITOR_CPU_FREQ", "0.")
os.environ.get("PYTEST_MONITOR_CPU_FREQ", "0."),
)
except (ValueError, TypeError):
warnings.warn(
"Wrong type/value while reading cpu frequency from environment. Forcing to 0.0."
"Wrong type/value while reading cpu frequency from environment. Forcing to 0.0.",
)
self.__cpu_freq_base = 0.0

Expand Down
75 changes: 75 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pathlib
import re

from setuptools import find_packages, setup


def read_version():
p = pathlib.Path(__file__)
p = p.parent / "pytest_monitor" / "__init__.py"
with p.open("r") as f:
for line in f:
if line.startswith("__version__"):
line = line.split("=")[1].strip()
match = re.match(r"^['\"](\d+\.\d+\.\d+\w*)['\"]", line)
if match:
return match.group(1)
raise ValueError("Unable to compute version")


def read(fname):
file_path = pathlib.Path(__file__).parent / fname
with file_path.open("r", encoding="utf-8") as f:
return f.read()


setup(
name="pytest-monitor",
version=read_version(),
author="Jean-Sébastien Dieu",
author_email="[email protected]",
maintainer="Jean-Sébastien Dieu",
maintainer_email="[email protected]",
license="MIT",
project_urls=dict(
Source="https://github.com/CFMTech/pytest-monitor",
Tracker="https://github.com/CFMTech/pytest-monitor/issues",
),
url="https://pytest-monitor.readthedocs.io/",
description="Pytest plugin for analyzing resource usage.",
long_description=read("README.rst"),
packages=find_packages(".", exclude=("tests", "example", "docs")),
python_requires=">=3.5",
install_requires=[
"pytest",
"requests",
"psutil>=5.1.0",
"memory_profiler>=0.58",
"wheel",
],
options={"bdist_wheel": {"universal": False}},
classifiers=[
"Development Status :: 5 - Production/Stable",
"Framework :: Pytest",
"Intended Audience :: Developers",
"Topic :: Software Development :: Testing",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.5",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Operating System :: OS Independent",
"License :: OSI Approved :: MIT License",
],
entry_points={
"pytest11": [
"monitor = pytest_monitor.pytest_monitor",
],
},
)
Loading