-
-
Notifications
You must be signed in to change notification settings - Fork 321
/
tasks.py
99 lines (79 loc) · 2.25 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
93
94
95
96
97
98
99
"""
tasks.py
--------
Project invoke tasks
Available commands
invoke --list
invoke fmt
invoke sort
invoke check
"""
import random
import invoke
TARGETS_DESCRIPTION = "Paths/directories to format. [default: . ]"
@invoke.task(help={"targets": TARGETS_DESCRIPTION})
def sort(ctx, targets="."):
"""Sort module imports."""
print("sorting imports ...")
args = ["isort", "--atomic", targets]
ctx.run(" ".join(args))
@invoke.task(pre=[sort], help={"targets": TARGETS_DESCRIPTION})
def fmt(ctx, targets="."):
"""Format python source code & sort imports."""
print("formatting ...")
args = ["black", targets]
ctx.run(" ".join(args))
@invoke.task
def check(ctx, fmt=False, sort=False, diff=False): # pylint: disable=redefined-outer-name
"""Check code format and import order."""
if not any([fmt, sort]):
fmt = True
sort = True
fmt_args = ["black", "--check", "."]
sort_args = ["isort", "--check", "."]
if diff:
fmt_args.append("--diff")
sort_args.append("--diff")
# FIXME: run each command and check return code
cmd_args = []
if fmt:
cmd_args.extend(fmt_args)
if sort:
if cmd_args:
cmd_args.append("&")
cmd_args.extend(sort_args)
ctx.run(" ".join(cmd_args))
@invoke.task
def lint(ctx):
"""Run linter."""
ctx.run(" ".join(["pylint", "app"]))
@invoke.task
def test(ctx):
"""Run pytest tests."""
ctx.run(" ".join(["pytest", "-v"]))
@invoke.task
def generate_reqs(ctx):
"""Generate requirements.txt"""
# NOTE: updated for pipenv 2020 release
# TODO: make backwards compatible
reqs = [
"pipenv lock -r > requirements.txt",
"pipenv lock -r --dev-only > requirements-dev.txt",
]
[ctx.run(req) for req in reqs]
@invoke.task
def docker(
ctx,
build=False,
run=False,
tag="covid-tracker-api:latest",
name=f"covid-api-{random.randint(0,999)}",
):
"""Build and run docker container."""
if not any([build, run]):
raise invoke.Exit(message="Specify either --build or --run", code=1)
if build:
docker_cmds = ["build", "."]
else:
docker_cmds = ["run", "--publish", "80", "--name", name]
ctx.run(" ".join(["docker", *docker_cmds, "-t", tag]))