forked from Cray-HPE/craycli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
noxfile.py
240 lines (195 loc) · 7.66 KB
/
noxfile.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
""" Nox definitions for tests, docs, and linting
MIT License
(C) Copyright [2020] Hewlett Packard Enterprise Development LP
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import absolute_import
import os
import nox # pylint: disable=import-error
COVERAGE_FAIL = 90
ERROR_ON_GENERATE = True
MODULE_PATH_TEMPLATE = 'cray/modules/{}'
# The version available in python 3.4 doesn't support this.
# So we have to hack around it for the jenkins pipelines.
if vars(nox).get('options'):
nox.options.keywords = 'not generate'
external = {"external": True}
else:
ERROR_ON_GENERATE = False
external = {}
def convert_file(session, path, filename):
session.run('/bin/bash', 'utils/convert.sh', path, filename, **external)
@nox.session(python='3')
def generate(session):
"""Generate a new CLI module"""
BASE_CLI_FILE = """\"\"\"{}\"\"\"
# pylint: disable=invalid-name
from cray.generator import generate
cli = generate(__file__)
"""
if len(session.posargs) != 2:
msg = 'Usage: nox -s generate -- [module name] [stash link to swagger]'
global ERROR_ON_GENERATE
if ERROR_ON_GENERATE:
raise Exception(msg)
else:
print(msg)
print("Ignore this is a full.")
return
from string import Template
module_name = session.posargs[0]
swagger_file = session.posargs[1]
test_template = 'tests/files/template.txt'
module_path = MODULE_PATH_TEMPLATE.format(module_name)
test_file = 'tests/test_modules/test_{}.py'.format(module_name)
init_file = '{}/__init__.py'.format(module_path)
cli_file = '{}/cli.py'.format(module_path)
is_local_file = os.path.exists(swagger_file)
if not os.path.exists(module_path):
os.makedirs(module_path)
if not os.path.isfile(init_file):
with open(init_file, 'a'):
os.utime(init_file, None)
if not os.path.isfile(cli_file):
with open(cli_file, 'w') as f:
f.write(BASE_CLI_FILE.format(module_name))
if not os.path.isfile(test_file):
with open (test_template) as test_template:
data = {'name': module_name}
tmp = Template(test_template.read()).substitute(data)
with open(test_file, 'w') as f:
f.write(tmp)
if is_local_file:
from shutil import copy2
copy2(swagger_file, module_path)
convert_file_name = os.path.basename(swagger_file)
else:
raise Exception("Please use a local file. .remote files with urls are deprecated.")
convert_file(session, module_path, convert_file_name)
@nox.session(python=None)
def swagger(session):
"""Run each swagger file through the converter in case anything changed.
This should be run before running unit tests"""
walk_path = 'cray/modules'
if session.posargs:
module = session.posargs[0]
module_path = MODULE_PATH_TEMPLATE.format(module)
if os.path.exists(module_path):
walk_path = module_path
else:
raise Exception("Module {} not found.".format(module))
else:
# If here we are doing all files so do test files too.
convert_file(session, './tests/files/', 'swagger.json')
for path, _, files in os.walk(walk_path):
swaggers = [f for f in files if f.startswith('swagger.')]
remotes = [f for f in files if f == '.remote']
swagger_file = None
remote_file = None
for f in files:
if f.startswith('swagger.'):
swagger_file = f
break
if f == '.remote':
remote_file = f
continue
if swagger_file:
convert_file(session, path, swagger_file)
continue
if remote_file:
convert_file(session, path, remote_file)
continue
@nox.session(python='3')
def lint_modules(session):
"""Validate .remote files and confirm other integration settings"""
for path, _, files in os.walk('./cray/modules'):
remotes = [f for f in files if f == '.remote']
swaggers = [f for f in files if f.startswith('swagger.')]
if remotes:
with open('{}/{}'.format(path, remotes[0])) as remote:
data = remote.read().rstrip()
if os.path.exists('{}/{}'.format(path, data)):
# remote used for local file
continue
else:
import warnings
warnings.warn(("URLs in .remote files are being deprecated. "
"Please use a local file"), DeprecationWarning)
continue
if remotes and swaggers:
raise Exception("Both a .remote and swagger file are provided. Please choose one.")
@nox.session(python='3')
def tests(session):
"""Default unit test session.
This is meant to be run against any python version intended to be used.
"""
# Install all test dependencies, then install this package in-place.
path = 'tests'
session.install('-r', 'requirements-test.txt')
session.install('-e', '.')
if session.posargs:
path = session.posargs[0]
# Run py.test against the tests.
session.run(
'py.test',
'--quiet',
'--cov=cray',
'--cov=tests',
'--cov-append',
'--cov-config=.coveragerc',
'--cov-report=',
'--cov-fail-under={}'.format(COVERAGE_FAIL),
path,
*session.posargs
)
@nox.session(python='3')
def lint(session):
"""Run linters.
Returns a failure if the linters find linting errors or sufficiently
serious code quality issues.
"""
run_cmd_code = ['pylint', 'cray']
if 'prod' not in session.posargs:
run_cmd_code.append('--disable=import-error')
run_cmd_code.append('--enable=fixme')
run_cmd_tests = ['pylint', 'tests']
if 'prod' not in session.posargs:
run_cmd_tests.append('--disable=import-error')
run_cmd_tests.append('--disable=fixme')
session.install('-r', 'requirements-lint.txt')
session.install('.')
session.run(*run_cmd_code)
session.run(*run_cmd_tests)
@nox.session(python='3')
def docs(session):
"""Run sphinx.
"""
session.install('-r', 'requirements-docs.txt')
session.install('.')
session.chdir('docs')
session.run('make', 'clean', **external)
session.run('make', 'html', **external)
@nox.session(python='3')
def cover(session):
"""Run the final coverage report.
This outputs the coverage report aggregating coverage from the unit
test runs, and then erases coverage data.
"""
session.install('coverage', 'pytest-cov')
session.run('coverage', 'report', '--show-missing',
'--fail-under={}'.format(COVERAGE_FAIL))
session.run('coverage', 'erase')