-
Notifications
You must be signed in to change notification settings - Fork 0
/
pavement.py
209 lines (169 loc) · 4.96 KB
/
pavement.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
"""
Build file for the project.
"""
import os
import sys
import threading
from subprocess import call
from paver.easy import call_task, cmdopts, consume_args, pushd, task
from pkg_resources import load_entry_point
EXTRA_PYPI_INDEX = os.environ["PIP_INDEX_URL"]
BUILD_DIR = os.environ.get("CHEVAH_BUILD", "build-py3")
HAVE_CI = os.environ.get("CI", "false") == "true"
SOURCE_FILES = ["pavement.py", "src"]
def _get_option(options, name, default=None):
"""
Helper to extract the command line options from paver.
"""
option_keys = list(options.keys())
option_keys.remove("dry_run")
option_keys.remove("pavement_file")
bunch = options.get(option_keys[0])
value = bunch.get(name, None)
if value is None:
return default
if value is True:
return True
return value.lstrip("=")
@task
def default():
call_task("test")
@task
def deps():
"""
Install all dependencies.
"""
pip = load_entry_point("pip", "console_scripts", "pip")
pip_args = [
"install",
"-U",
"--extra-index-url",
EXTRA_PYPI_INDEX,
]
# Install wheel.
pip(args=pip_args + ["wheel"])
if not HAVE_CI:
pip_args.append("-e")
pip_args.append(".[dev]")
exit_code = pip(args=pip_args)
if exit_code:
raise Exception("Failed to install the deps.")
@task
@consume_args
def test(args):
"""
Run the test tests.
"""
_nose(args, cov=None)
def _nose(args, cov, base="chevah_keycert.tests"):
"""
Run nose tests in the same process.
"""
# Delay import after coverage is started.
import psutil
from chevah_compat.testing import ChevahTestCase
from chevah_compat.testing.nose_memory_usage import MemoryUsage
from chevah_compat.testing.nose_run_reporter import RunReporter
from chevah_compat.testing.nose_test_timer import TestTimer
from nose.core import main as nose_main
from nose.plugins.base import Plugin
class LoopPlugin(Plugin):
name = "loop"
new_arguments = [
"--with-randomly",
"--with-run-reporter",
"--with-timer",
"-v",
"-s",
]
have_tests = False
for argument in args:
if not argument.startswith("-"):
argument = "%s.%s" % (base, argument)
have_tests = True
new_arguments.append(argument)
if not have_tests:
# Run all base tests if no specific tests was requested.
new_arguments.append(base)
sys.argv = new_arguments
print(new_arguments)
plugins = [TestTimer(), RunReporter(), MemoryUsage(), LoopPlugin()]
with pushd(BUILD_DIR):
ChevahTestCase.initialize(drop_user="-")
ChevahTestCase.dropPrivileges()
try:
nose_main(addplugins=plugins)
finally:
process = psutil.Process(os.getpid())
print("Max RSS: {} MB".format(process.memory_info().rss / 1000000))
if cov:
cov.stop()
cov.save()
threads = threading.enumerate()
if len(threads) > 1:
print("There are still active threads: %s" % threads)
sys.stdout.flush()
sys.stderr.flush()
os._exit(1)
@task
@cmdopts(
[
("load=", "l", "Run key loading tests."),
("generate=", "g", "Run key generation tests."),
]
)
def test_interop(options):
"""
Run the SSH key interoperability tests.
"""
environ = os.environ.copy()
environ["CHEVAH_BUILD"] = BUILD_DIR
generate_option = _get_option(options, "generate")
key_type = _get_option(options, "load")
if generate_option:
test_command = "ssh_gen_keys_tests.sh {}".format(generate_option)
else:
test_command = "ssh_load_keys_tests.sh {}".format(key_type)
try:
os.mkdir(BUILD_DIR)
except OSError:
"""Already exists"""
exit_code = 1
with pushd(BUILD_DIR):
print("Testing: {}".format(test_command))
exit_code = call(
"../src/chevah_keycert/tests/{}".format(test_command),
shell=True,
env=environ,
)
sys.exit(exit_code)
@task
def lint():
"""
Run the static code analyzer.
"""
from black import patched_main
from isort.main import main as isort_main
from pyflakes.api import main as pyflakes_main
try:
pyflakes_main(args=SOURCE_FILES)
except SystemExit as error:
if error.code:
raise
exit_code = isort_main(argv=["--check"] + SOURCE_FILES)
if exit_code:
raise Exception("isort needs to update the code.")
sys.argv = ["black", "--check"] + SOURCE_FILES
exit_code = patched_main()
if exit_code:
raise Exception("Black needs to update the code.")
@task
def black():
"""
Run black on the whole source code.
"""
from black import patched_main
from isort.main import main as isort_main
isort_main(argv=SOURCE_FILES)
sys.argv = ["black"] + SOURCE_FILES
patched_main()