Skip to content

Commit 7b63ab4

Browse files
authored
Clean up invoke tasks (scrapd#173)
* Create a utility function to manage venvs. * Reorganize/rename in order to simplify reusing the tasks/sessions.
1 parent 6b765aa commit 7b63ab4

7 files changed

+34
-58
lines changed

.github/CONTRIBUTING.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ The profiling for the project is mostly done using `pyinstrument`_
114114

115115
You can use the nox task to run the profiler automatically::
116116

117-
nox -s profiling
117+
inv profile
118118

119119
Additionally, you can also generate a flame graph with `py-spy`_. It requires root permissions, therefore must be run
120120
with sudo and will prompt you for your password::

noxfile.py

-13
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,6 @@ def lint_format(session):
7272
run_yapf(session, diff=True)
7373

7474

75-
@nox.session()
76-
def profiling(session):
77-
"""Setup the developper environment."""
78-
# Install dependencies.
79-
session.install('--upgrade', 'pip', 'setuptools')
80-
session.install('-r', 'requirements-profilers.txt')
81-
session.install('-e', '.')
82-
83-
env_dir = Path(session.bin)
84-
scrapd = env_dir / 'scrapd'
85-
session.run("pyinstrument", "--renderer", "html", f'{scrapd.resolve()}', "-v", "--format", "count", "--pages", "5")
86-
87-
8875
@nox.session()
8976
def pydocstyle(session):
9077
"""Check the docstrings."""

requirements-dev.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ aioresponses==0.6.0
55
asynctest==0.12.4
66
bpython
77
flake8==3.7.7
8+
py-spy==0.1.11
89
pydocstyle==3.0.0
10+
pyinstrument==3.0.3
911
pylint==2.3.1
1012
pytest-asyncio==0.10.0
1113
pytest-bdd==3.1.0

requirements-profilers.txt

-2
This file was deleted.

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ summary = Extract data from APD news site
44
description-file =
55
README.rst
66
author = Rémy Greinhofer
7-
author-email = [email protected]
7+
author-email = [email protected]
88
home-page = https://www.scrapd.org
99
classifier =
1010
Development Status :: 5 - Production/Stable

tasks.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from nox.virtualenv import VirtualEnv
55

66
# Configuration values.
7+
VENV = 'venv'
78
project_name = 'scrapd'
89
docker_org = 'scrapd'
910
docker_repo = f'{docker_org}/{project_name}'
@@ -39,12 +40,10 @@ def clean_repo(c):
3940

4041
@task
4142
def flame_graph(c):
42-
c.run(f'nox --install-only -s profiling')
43-
location = Path('.nox/profiling')
44-
venv = VirtualEnv(location.resolve())
45-
venv_bin = Path(venv.bin)
46-
scrapd = venv_bin / 'scrapd'
47-
c.run(f'sudo py-spy -d 20 --flame profile.svg -- {scrapd.resolve()} -v --pages 5')
43+
"""Create an interactive CPU flame graph."""
44+
_, venv_bin, _ = get_venv(VENV)
45+
pyspy = venv_bin / 'py-spy'
46+
c.run(f'sudo {pyspy.resolve()} -d 20 --flame profile.svg -- {(venv_bin /project_name ).resolve()} -v --pages 5')
4847

4948

5049
@task
@@ -56,6 +55,15 @@ def nox(c, s=''):
5655
c.run(f'nox -s {s}')
5756

5857

58+
@task
59+
def profile(c):
60+
"""Create an interactive CPU flame graph."""
61+
_, venv_bin, _ = get_venv(VENV)
62+
pyinstrument = venv_bin / 'pyinstrument'
63+
c.run(f'{pyinstrument.resolve()} --renderer html {(venv_bin /project_name ).resolve()} -v --format count --pages 5',
64+
pty=True)
65+
66+
5967
@task
6068
def publish(c):
6169
"""Publish the documentation."""
@@ -66,3 +74,19 @@ def publish(c):
6674
def setup(c):
6775
"""Setup the developper environment."""
6876
c.run('nox --envdir .')
77+
78+
79+
def get_venv(venv):
80+
"""
81+
Return `Path` objects from the venv.
82+
83+
:param str venv: venv name
84+
:return: the venv `Path`, the `bin` folder `Path` within the venv, and if specified, the `Path` object of the
85+
activate script within the venv.
86+
:rtype: a tuple of 3 `Path` objects.
87+
"""
88+
location = Path(venv)
89+
venv = VirtualEnv(location.resolve())
90+
venv_bin = Path(venv.bin)
91+
activate = venv_bin / 'activate'
92+
return venv, venv_bin, activate

tox.ini

-35
This file was deleted.

0 commit comments

Comments
 (0)