-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
144 lines (108 loc) · 3.65 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
"""Tasks for use with Invoke."""
import os
import sys
from invoke import task
from diffsync_mock.const import DEFAULT_RECORDS
try:
import toml
except ImportError:
sys.exit("Please make sure to `pip install toml` or enable the Poetry shell and run `poetry install`.")
PYPROJECT_CONFIG = toml.load("pyproject.toml")
TOOL_CONFIG = PYPROJECT_CONFIG["tool"]["poetry"]
# Can be set to a separate Python version to be used for launching or building image
PYTHON_VER = os.getenv("PYTHON_VER", "3.6")
PKG_NAME = "diffsync_mock"
CONTAINER_NAME = "diffsync-redis-1"
@task
def run(context, redis=False):
"""Run the main python script to execute mocks."""
cmd = "fil-profile run diffsync_mock/main.py"
if redis:
cmd += " --redis"
context.run(cmd, pty=True)
@task
def load_redis(context, records=DEFAULT_RECORDS):
"""Run build.py script to load redis with mock data.
Args:
records (int): Amount of mock records to add.
context (obj): Used to run specific commands
"""
start(context)
cmd = f"python {PKG_NAME}/build.py --records {records} --redis"
context.run(cmd, pty=True)
@task
def load_local(context, records=DEFAULT_RECORDS):
"""Run build.py script to generate a mock of local data.
Args:
records (int): Amount of mock records to add.
context (obj): Used to run specific commands
"""
cmd = f"python {PKG_NAME}/build.py --records {records} --local"
context.run(cmd, pty=True)
@task
def container_up(context, name):
"""This will enter the image to perform troubleshooting or dev work.
Args:
context (obj): Used to run specific commands
name (str): name of the container to test if up.
"""
result = context.run(f"docker ps --filter name={name}", hide="out")
return name in result.stdout
@task
def start(context):
"""This will enter the image to perform troubleshooting or dev work.
Args:
context (obj): Used to run specific commands
"""
is_up = container_up(context, CONTAINER_NAME)
if is_up:
print("Redis container already running.")
return
print("Starting redis container.")
context.run(f"docker run --name {CONTAINER_NAME} -p 7379:6379 -d redis", pty=True)
@task
def stop(context):
"""This will enter the image to perform troubleshooting or dev work.
Args:
context (obj): Used to run specific commands
"""
is_up = container_up(context, CONTAINER_NAME)
if not is_up:
print("Redis container not running.")
return
print("Stopping redis container.")
context.run(f"docker rm {CONTAINER_NAME}", pty=True)
@task()
def black(context):
"""Run black to check that Python files adherence to black standards."""
exec_cmd = "black --check --diff ."
context.run(exec_cmd, pty=True)
@task()
def flake8(context):
"""Run flake8 code analysis."""
exec_cmd = "flake8 ."
context.run(exec_cmd, pty=True)
@task()
def pylint(context):
"""Run pylint code analysis."""
exec_cmd = 'find . -name "*.py" | xargs pylint'
context.run(exec_cmd, pty=True)
@task()
def pydocstyle(context):
"""Run pydocstyle to validate docstring formatting adheres to NTC defined standards."""
exec_cmd = "pydocstyle ."
context.run(exec_cmd, pty=True)
@task()
def bandit(context):
"""Run bandit to validate basic static code security analysis."""
exec_cmd = "bandit --recursive ./ --configfile .bandit.yml"
context.run(exec_cmd, pty=True)
@task()
def tests(context):
"""Run all tests for this repository."""
black(context)
flake8(context)
pylint(context)
pydocstyle(context)
bandit(context)
print("All tests have passed!")