Skip to content

Commit b52b7f0

Browse files
committed
profile| Runs pylint against external code, generating profile-heatmaps
* Use https gitub uri so we do not need creditials * Shallow clone so we don't pull more data than we need * Ensure we skip external profiling unless explicitly wanted
1 parent 67d0572 commit b52b7f0

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
""" Profiles basic -jX functionality """
2+
# Copyright (c) 2020 Frank Harrison <[email protected]>
3+
4+
# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
5+
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
6+
7+
# pylint: disable=protected-access,missing-function-docstring,no-self-use
8+
9+
import os
10+
import pprint
11+
import shutil
12+
import tempfile
13+
14+
import pytest
15+
16+
from pylint.lint import Run
17+
from pylint.testutils import TestReporter as Reporter
18+
19+
20+
class TempdirGuard:
21+
""" creates and deletes a tmp-dir compatible with python2 and 3 """
22+
23+
def __init__(self, prefix):
24+
self.path = tempfile.mkdtemp(prefix=prefix + "_")
25+
26+
def __enter__(self):
27+
return self
28+
29+
def __exit__(self, x_type, x_value, x_traceback):
30+
shutil.rmtree(self.path) # always clean up on exit
31+
32+
33+
def _get_py_files(scanpath):
34+
assert os.path.exists(scanpath), "Dir not found %s" % scanpath
35+
36+
filepaths = []
37+
for dirpath, dirnames, filenames in os.walk(scanpath):
38+
dirnames[:] = [dirname for dirname in dirnames if dirname != "__pycache__"]
39+
filepaths.extend(
40+
[
41+
os.path.join(dirpath, filename)
42+
for filename in filenames
43+
if filename.endswith(".py")
44+
]
45+
)
46+
return filepaths
47+
48+
49+
@pytest.mark.skipif(
50+
not os.environ.get("PYTEST_PROFILE_EXTERNAL", False),
51+
reason="PYTEST_PROFILE_EXTERNAL, not set, assuming not a profile run",
52+
)
53+
@pytest.mark.parametrize(
54+
"name,git_repo", [("numpy", "https://github.com/numpy/numpy.git")]
55+
)
56+
def test_run(name, git_repo):
57+
""" Runs pylint against external sources """
58+
with TempdirGuard(prefix=name) as checkoutdir:
59+
os.system(
60+
"git clone --depth=1 {git_repo} {checkoutdir}".format(
61+
git_repo=git_repo, checkoutdir=checkoutdir.path
62+
)
63+
)
64+
filepaths = _get_py_files(scanpath=checkoutdir.path)
65+
print("Have %d files" % len(filepaths))
66+
67+
runner = Run(filepaths, reporter=Reporter(), do_exit=False)
68+
69+
print(
70+
"Had %d files with %d messages"
71+
% (len(filepaths), len(runner.linter.reporter.messages))
72+
)
73+
pprint.pprint(runner.linter.reporter.messages)
74+
75+
# one day: assert runner.linter.msg_status == 0, (
76+
# one day: "Expected no errors to be thrown: %s"
77+
# one day: % pprint.pformat(runner.linter.reporter.messages)
78+
# one day: )

tox.ini

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,26 @@ commands =
131131
rm -f extensions.rst
132132
python ./exts/pylint_extensions.py
133133
sphinx-build -W -b html -d _build/doctrees . _build/html
134+
135+
[testenv:profile_against_external]
136+
deps =
137+
https://github.com/PyCQA/astroid/tarball/master#egg=astroid-master-2.0
138+
coverage<5.0
139+
gprof2dot
140+
isort
141+
mccabe
142+
pytest
143+
pytest-xdist
144+
pytest-profiling
145+
146+
setenv =
147+
PYTEST_PROFILE_EXTERNAL = 1
148+
149+
whitelist_externals =
150+
git
151+
mkdir
152+
153+
commands =
154+
python -Wi -m pytest --exitfirst {toxinidir}/tests/profile/test_profile_against_externals.py --profile-svg
155+
156+
changedir = {toxworkdir}

0 commit comments

Comments
 (0)