-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
executable file
·173 lines (143 loc) · 4.45 KB
/
manage.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#!/usr/bin/env python3
import sys
import os
def print_green(s):
GREEN = "\033[92m"
ENDC = "\033[0m"
print(f"{GREEN}{s}{ENDC}")
def run_command(cmd):
print_green(cmd)
os.system(cmd)
def get_command_output(cmd):
return os.popen(cmd).read()
def dev(extra_args):
"""
This command starts up a development environment.
The development environment is started through docker-compose
and is visible at http://localhost
"""
command = f"docker-compose -f docker-compose.yml -f docker-compose.dev.yml \
up --build --renew-anon-volumes"
run_command(command + extra_args)
def test(extra_args):
"""
This command runs the tests within docker
and then exits.
"""
command = """\
docker build -t strider-testing -f Dockerfile.test .
docker run --rm -it strider-testing\
"""
run_command(command + extra_args)
def benchmark(extra_args):
"""
This command runs benchmarks within docker
and displays them
"""
command = """\
docker rm strider-testing || true
docker build -t strider-testing \
-f Dockerfile.test .
docker run --name strider-testing strider-testing \
python -m benchmarks {extra_args}
"""
run_command(command + extra_args)
def coverage(extra_args):
"""
Run tests in docker, copy out a coverage report,
and display in browser
"""
command = f"""\
docker rm strider-testing || true
docker build -t strider-testing \
-f Dockerfile.test .
docker run --name strider-testing strider-testing \
pytest --cov strider/ --cov-report html {extra_args}
docker cp strider-testing:/app/htmlcov /tmp/strider-cov/
open /tmp/strider-cov/index.html
"""
run_command(command)
def profile(extra_args):
"""
Profile a test in docker, copy out a report,
and display using the snakeviz utility
"""
command = f"""\
docker rm strider-profile || true
docker build -t strider-profile \
-f Dockerfile.test .
docker run --name strider-profile strider-profile \
python -m cProfile -o strider.prof -m pytest {extra_args}
docker cp strider-profile:/app/strider.prof /tmp/
snakeviz /tmp/strider.prof
"""
run_command(command)
REQUIREMENTS_FILES = {
"requirements.txt": "requirements-lock.txt",
"requirements-test.txt": "requirements-test-lock.txt",
}
def lock(extra_args):
"""
Write lockfiles without upgrading dependencies
"""
for src, locked in REQUIREMENTS_FILES.items():
command = f"""\
docker run --rm -v $(pwd):/app python:3.12 \
/bin/bash -c "
# Install lockfile first so that we get the
# currently installed versions of dependencies
pip install -r /app/{locked} &&
pip install -r /app/{src} &&
# Write lockfile
pip freeze > /app/{locked}
"
"""
run_command(command)
def verify_locked(extra_args):
"""
Verify that the lockfile is up-to-date
"""
for src, locked in REQUIREMENTS_FILES.items():
dependencies = get_command_output(
f"""\
docker run --rm -v $(pwd):/app python:3.12 \
/bin/bash -c "
pip install -qqq -r /app/{locked} &&
pip install -qqq -r /app/{src} &&
pip freeze
"
"""
)
lock_dependencies = get_command_output(
f"""\
docker run -v $(pwd):/app python:3.12 \
/bin/bash -c "
pip install -qqq -r /app/{locked} &&
pip freeze
"
"""
)
if dependencies != lock_dependencies:
sys.exit(f"Lock file {locked} not up-to-date, please run ./manage.py lock")
def upgrade(extra_args):
"""
Upgrade all dependencies
"""
for src, locked in REQUIREMENTS_FILES.items():
command = f"""\
docker run --rm -v $(pwd):/app python:3.12 \
/bin/bash -c "
# Install dependencies, getting latest version
pip install -r /app/{src} &&
# Write lockfile
pip freeze > /app/{locked}
"
"""
run_command(command)
def main():
command = sys.argv[1]
command_func = globals()[command]
extra_args = " " + " ".join(sys.argv[2:])
command_func(extra_args)
if __name__ == "__main__":
main()