-
Notifications
You must be signed in to change notification settings - Fork 2
/
do.py
276 lines (229 loc) · 6.51 KB
/
do.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import fnmatch
import os
import re
import sys
import shutil
import subprocess
def setup():
run(
[
py() + " -m pip install --upgrade pip",
py() + " -m pip install --upgrade virtualenv",
py() + " -m virtualenv .env",
]
)
def init():
run(
[
py() + " -m pip install -r requirements.txt",
]
)
def get_version():
if os.path.exists("version.txt"):
with open("version.txt") as f:
out = f.read()
workflow_id = re.findall(r"workflow_id: (.+)", out)
if workflow_id:
workflow_id = workflow_id[0]
version_info = re.findall(r"version: (.+)", out)
if version_info:
version_info = version_info[0]
snappi_convergence = re.findall(
r"snappi_convergence: (.+)", out
)[0]
if version_info:
new_data = []
with open("requirements.txt") as f:
out = f.readlines()
for line in out:
match = re.search(
r"snappi\[convergence,ixnetwork\]==.*", line
)
if match:
snappi_convergence = (
"snappi_convergence==" + snappi_convergence + "\n"
)
new_data.append("snappi" + "\n")
new_data.append(snappi_convergence)
new_version = (
"snappi-ixnetwork==" + version_info + "\n"
)
new_data.append(new_version)
else:
new_data.append(line)
f.close()
with open("requirements.txt", "w+") as f:
f.writelines(new_data)
elif workflow_id:
print(workflow_id)
def clear_version_file():
with open("version.txt", "w+") as f:
f.close()
def lint():
paths = ["tests", "do.py"]
run(
[
py() + " -m black " + " ".join(paths),
py() + " -m flake8 " + " ".join(paths),
]
)
def test():
args = [
'--location="https://otg-novus100g.lbj.is.keysight.com:5000"',
(
'--ports="otg-novus100g.lbj.is.keysight.com;1;1'
" otg-novus100g.lbj.is.keysight.com;1;2"
" otg-novus100g.lbj.is.keysight.com;1;3"
' otg-novus100g.lbj.is.keysight.com;1;4"'
),
"--ext=ixnetwork",
"--speed=speed_100_gbps",
"tests",
'-m "not dut and not l1_manual"',
]
run(
[
py() + " -m pytest -svvv {}".format(" ".join(args)),
]
)
def uhd_test():
args = [
'--location="https://10.36.70.75"',
(
'--ports="localuhd/25 localuhd/26 localuhd/27 localuhd/28"'
),
"--ext=ixnetwork",
"--speed=speed_100_gbps",
"--uhd=true",
"tests",
'-m "not dut and not l1_manual and not non_uhd"'
]
run(
[
py() + " -m pytest -svvv {}".format(" ".join(args)),
]
)
def dist():
clean()
run(
[
py() + " setup.py sdist bdist_wheel --universal",
]
)
print(os.listdir("dist"))
def install():
wheel = "{}-{}-py2.py3-none-any.whl".format(*pkg())
run(
[
"{} -m pip install --upgrade --force-reinstall {}[testing]".format(
py(), os.path.join("dist", wheel)
),
]
)
def release():
run(
[
py() + " -m pip install --upgrade twine",
"{} -m twine upload -u {} -p {} dist/*".format(
py(),
os.environ["PYPI_USERNAME"],
os.environ["PYPI_PASSWORD"],
),
]
)
def clean():
"""
Removes filenames or dirnames matching provided patterns.
"""
pwd_patterns = [
".pytype",
"dist",
"build",
"*.egg-info",
]
recursive_patterns = [
".pytest_cache",
"__pycache__",
"*.pyc",
"*.log",
]
for pattern in pwd_patterns:
for path in pattern_find(".", pattern, recursive=False):
rm_path(path)
for pattern in recursive_patterns:
for path in pattern_find(".", pattern, recursive=True):
rm_path(path)
def version():
print(pkg()[-1])
def pkg():
"""
Returns name of python package in current directory and its version.
"""
try:
return pkg.pkg
except AttributeError:
with open("setup.py") as f:
out = f.read()
name = re.findall(r"pkg_name = \"(.+)\"", out)[0]
version = re.findall(r"version = \"(.+)\"", out)[0]
pkg.pkg = (name, version)
return pkg.pkg
def rm_path(path):
"""
Removes a path if it exists.
"""
if os.path.exists(path):
if os.path.isdir(path):
shutil.rmtree(path)
else:
os.remove(path)
def pattern_find(src, pattern, recursive=True):
"""
Recursively searches for a dirname or filename matching given pattern and
returns all the matches.
"""
matches = []
if not recursive:
for name in os.listdir(src):
if fnmatch.fnmatch(name, pattern):
matches.append(os.path.join(src, name))
return matches
for dirpath, dirnames, filenames in os.walk(src):
for names in [dirnames, filenames]:
for name in names:
if fnmatch.fnmatch(name, pattern):
matches.append(os.path.join(dirpath, name))
return matches
def py():
"""
Returns path to python executable to be used.
"""
try:
return py.path
except AttributeError:
py.path = os.path.join(".env", "bin", "python")
if not os.path.exists(py.path):
py.path = sys.executable
# since some paths may contain spaces
py.path = '"' + py.path + '"'
return py.path
def run(commands):
"""
Executes a list of commands in a native shell and raises exception upon
failure.
"""
try:
for cmd in commands:
print(cmd)
subprocess.check_call(
cmd.encode("utf-8", errors="ignore"), shell=True
)
except Exception:
sys.exit(1)
def main():
if len(sys.argv) >= 2:
globals()[sys.argv[1]](*sys.argv[2:])
else:
print("usage: python do.py [args]")
if __name__ == "__main__":
main()