forked from scrapd/scrapd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
92 lines (69 loc) · 2.27 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from pathlib import Path
from invoke import task
from nox.virtualenv import VirtualEnv
# Configuration values.
VENV = 'venv'
project_name = 'scrapd'
docker_org = 'scrapd'
docker_repo = f'{docker_org}/{project_name}'
@task
def build_docker(c):
"""Build a docker image."""
tag = c.run('git describe', hide=True)
docker_img = f'{docker_repo}:{tag.stdout.strip()}'
c.run(f'docker build -t {docker_img} .')
@task
def clean(c):
"""Remove unwanted files and artifacts in this project (!DESTRUCTIVE!)."""
clean_docker(c)
clean_repo(c)
@task
def clean_docker(c):
"""Remove all docker images built for this project (!DESTRUCTIVE!)."""
c.run(f'docker image rm -f $(docker image ls --filter reference={docker_repo} -q) || true')
@task
def clean_repo(c):
"""Remove unwanted files in project (!DESTRUCTIVE!)."""
c.run('git clean -ffdx')
c.run('git reset --hard')
@task
def flame_graph(c):
"""Create an interactive CPU flame graph."""
_, venv_bin, _ = get_venv(VENV)
pyspy = venv_bin / 'py-spy'
c.run(f'sudo {pyspy.resolve()} -d 20 --flame profile.svg -- {(venv_bin /project_name ).resolve()} -v --pages 5')
@task
def nox(c, s=''):
"""Wrapper for the nox tasks (`inv nox list` for details)."""
if not s:
c.run('nox --list')
else:
c.run(f'nox -s {s}')
@task
def profile(c):
"""Create an interactive CPU flame graph."""
_, venv_bin, _ = get_venv(VENV)
pyinstrument = venv_bin / 'pyinstrument'
c.run(f'{pyinstrument.resolve()} --renderer html {(venv_bin /project_name ).resolve()} -v --format count --pages 5',
pty=True)
@task
def publish(c):
"""Publish the documentation."""
c.run('./.circleci/publish.sh')
@task(default=True)
def setup(c):
"""Setup the developper environment."""
c.run('nox --envdir .')
def get_venv(venv):
"""
Return `Path` objects from the venv.
:param str venv: venv name
:return: the venv `Path`, the `bin` folder `Path` within the venv, and if specified, the `Path` object of the
activate script within the venv.
:rtype: a tuple of 3 `Path` objects.
"""
location = Path(venv)
venv = VirtualEnv(location.resolve())
venv_bin = Path(venv.bin)
activate = venv_bin / 'activate'
return venv, venv_bin, activate