forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckers.py
More file actions
296 lines (258 loc) · 11.8 KB
/
checkers.py
File metadata and controls
296 lines (258 loc) · 11.8 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# Copyright The IETF Trust 2016-2020, All Rights Reserved
# -*- coding: utf-8 -*-
import io
import os
import re
import shutil
import sys
import tempfile
from xym import xym
from django.conf import settings
import debug # pyflakes:ignore
from ietf.utils.log import log, assertion
from ietf.utils.models import VersionInfo
from ietf.utils.pipe import pipe
from ietf.utils.test_runner import set_coverage_checking
class DraftSubmissionChecker(object):
name = ""
def check_file_txt(self, text):
"Run checks on a text file"
raise NotImplementedError
def check_file_xml(self, xml):
"Run checks on an xml file"
raise NotImplementedError
def check_fragment_txt(self, text):
"Run checks on a fragment from a text file"
raise NotImplementedError
def check_fragment_xml(self, xml):
"Run checks on a fragment from an xml file"
raise NotImplementedError
class DraftIdnitsChecker(object):
"""
Draft checker class for idnits. Idnits can only handle whole text files,
so only check_file_txt() is defined; check_file_xml and check_fragment_*
methods are undefined.
Furthermore, idnits doesn't provide an error code or line-by-line errors,
so a bit of massage is needed in order to return the expected failure flag.
"""
name = "idnits check"
# start using this when we provide more in the way of warnings during
# submission checking:
# symbol = '<span class="bi bi-check-square"></span>'
# symbol = u'<span class="large">\ua17d</span>' # Yi syllable 'nit'
# symbol = u'<span class="large">\ub2e1</span>' # Hangul syllable 'nit'
symbol = ""
def __init__(self, options=["--submitcheck", "--nitcount", ]):
assert isinstance(options, list)
if not "--nitcount" in options:
options.append("--nitcount")
self.options = ' '.join(options)
def check_file_txt(self, path):
"""
Run an idnits check, and return a passed/failed indication, a message,
and error and warning messages.
Error and warning list items are tuples:
(line_number, line_text, message)
"""
items = []
errors = 0
warnings = 0
errstart = [' ** ', ' ~~ ']
warnstart = [' == ', ' -- ']
cmd = "%s %s %s" % (settings.IDSUBMIT_IDNITS_BINARY, self.options, path)
code, out, err = pipe(cmd)
out = out.decode('utf-8')
err = err.decode('utf-8')
if code != 0 or out == "":
message = "idnits error: %s:\n Error %s: %s" %( cmd, code, err)
log(message)
passed = False
else:
message = out
if re.search(r"\s+Summary:\s+0\s+|No nits found", out):
passed = True
else:
passed = False
item = ""
for line in message.splitlines():
if line[:5] in (errstart + warnstart):
item = line.rstrip()
elif line.strip() == "" and item:
tuple = (None, None, item)
items.append(tuple)
if item[:5] in errstart:
errors += 1
elif item[:5] in warnstart:
warnings += 1
else:
raise RuntimeError("Unexpected state in idnits checker: item: %s, line: %s" % (item, line))
item = ""
elif item and line.strip() != "":
item += " " + line.strip()
else:
pass
info = {'checker': self.name, 'items': [], 'code': {}}
return passed, message, errors, warnings, info
class DraftYangChecker(object):
name = "yang validation"
symbol = '<i class="bi bi-yin-yang"></i>'
def check_file_txt(self, path):
name = os.path.basename(path)
workdir = tempfile.mkdtemp()
model_name_re = r'^[A-Za-z_][A-Za-z0-9_.-]*(@\d\d\d\d-\d\d-\d\d)?\.yang$'
errors = 0
warnings = 0
message = ""
results = []
passed = True # Used by the submission tool. Yang checks always pass.
model_list = []
info = {'checker': self.name, 'items': [], 'code': {}}
extractor = xym.YangModuleExtractor(path, workdir, strict=True, strict_examples=False, debug_level=1)
if not os.path.exists(path):
return None, "%s: No such file or directory: '%s'"%(name.capitalize(), path), errors, warnings, info
with open(path) as file:
out = ""
err = ""
code = 0
try:
# This places the yang models as files in workdir
saved_stdout = sys.stdout
saved_stderr = sys.stderr
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
extractor.extract_yang_model(file.readlines())
model_list = extractor.get_extracted_models(False, True)
out = sys.stdout.getvalue()
err = sys.stderr.getvalue()
# signature change in xym:
except Exception as exc:
sys.stdout = saved_stdout
sys.stderr = saved_stderr
msg = "Exception when running xym on %s: %s" % (name, exc)
log(msg)
raise
return None, msg, 0, 0, info
finally:
sys.stdout = saved_stdout
sys.stderr = saved_stderr
if not model_list:
# Found no yang models, don't deliver any YangChecker result
return None, "", 0, 0, info
for m in model_list:
if not re.search(model_name_re, m):
code += 1
err += "Error: Bad extracted model name: '%s'\n" % m
if len(set(model_list)) != len(model_list):
code += 1
err += "Error: Multiple models with the same name:\n %s\n" % ("\n ".join(model_list))
model_list = list(set(model_list))
command = "xym"
cmd_version = VersionInfo.objects.get(command=command).version
message = "%s:\n%s\n\n" % (cmd_version, out.replace('\n\n','\n').strip() if code == 0 else err)
results.append({
"name": name,
"passed": passed,
"message": message,
"warnings": 0,
"errors": code,
"items": [],
})
for model in model_list:
path = os.path.join(workdir, model)
message = ""
passed = True
errors = 0
warnings = 0
items = []
modpath = ':'.join([
workdir,
settings.SUBMIT_YANG_RFC_MODEL_DIR,
settings.SUBMIT_YANG_DRAFT_MODEL_DIR,
settings.SUBMIT_YANG_IANA_MODEL_DIR,
settings.SUBMIT_YANG_CATALOG_MODEL_DIR,
])
if os.path.exists(path):
with io.open(path) as file:
text = file.readlines()
# pyang
cmd_template = settings.SUBMIT_PYANG_COMMAND
command = [ w for w in cmd_template.split() if not '=' in w ][0]
cmd_version = VersionInfo.objects.get(command=command).version
cmd = cmd_template.format(libs=modpath, model=path)
venv_path = os.environ.get('VIRTUAL_ENV') or os.path.join(os.getcwd(), 'env')
venv_bin = os.path.join(venv_path, 'bin')
if not venv_bin in os.environ.get('PATH', '').split(':'):
os.environ['PATH'] = os.environ.get('PATH', '') + ":" + venv_bin
code, out, err = pipe(cmd)
out = out.decode('utf-8')
err = err.decode('utf-8')
if code > 0 or len(err.strip()) > 0 :
error_lines = err.splitlines()
assertion('len(error_lines) > 0')
for line in error_lines:
if line.strip():
try:
fn, lnum, msg = line.split(':', 2)
lnum = int(lnum)
if fn == model and (lnum-1) in range(len(text)):
line = text[lnum-1].rstrip()
else:
line = None
items.append((lnum, line, msg))
if 'error: ' in msg:
errors += 1
if 'warning: ' in msg:
warnings += 1
except ValueError:
pass
#passed = passed and code == 0 # For the submission tool. Yang checks always pass
message += "%s: %s:\n%s\n" % (cmd_version, cmd_template, out+"No validation errors\n" if (code == 0 and len(err) == 0) else out+err)
# yanglint
set_coverage_checking(False) # we can't count the following as it may or may not be run, depending on setup
if settings.SUBMIT_YANGLINT_COMMAND and os.path.exists(settings.YANGLINT_BINARY):
cmd_template = settings.SUBMIT_YANGLINT_COMMAND
command = [ w for w in cmd_template.split() if not '=' in w ][0]
cmd_version = VersionInfo.objects.get(command=command).version
cmd = cmd_template.format(model=path, rfclib=settings.SUBMIT_YANG_RFC_MODEL_DIR, tmplib=workdir,
draftlib=settings.SUBMIT_YANG_DRAFT_MODEL_DIR, ianalib=settings.SUBMIT_YANG_IANA_MODEL_DIR,
cataloglib=settings.SUBMIT_YANG_CATALOG_MODEL_DIR, )
code, out, err = pipe(cmd)
out = out.decode('utf-8')
err = err.decode('utf-8')
if code > 0 or len(err.strip()) > 0:
err_lines = err.splitlines()
for line in err_lines:
if line.strip():
try:
if 'err : ' in line:
errors += 1
if 'warn: ' in line:
warnings += 1
except ValueError:
pass
#passed = passed and code == 0 # For the submission tool. Yang checks always pass
message += "%s: %s:\n%s\n" % (cmd_version, cmd_template, out+"No validation errors\n" if (code == 0 and len(err) == 0) else out+err)
set_coverage_checking(True)
else:
errors += 1
message += "No such file: %s\nPossible mismatch between extracted xym file name and returned module name?\n" % (path)
dest = os.path.join(settings.SUBMIT_YANG_DRAFT_MODEL_DIR, model)
shutil.move(path, dest)
# summary result
results.append({
"name": model,
"passed": passed,
"message": message,
"warnings": warnings,
"errors": errors,
"items": items,
})
shutil.rmtree(workdir)
passed = all( res["passed"] for res in results )
message = "\n".join([ "\n".join([res['name']+':', res["message"]]) for res in results ])
errors = sum(res["errors"] for res in results )
warnings = sum(res["warnings"] for res in results )
items = [ e for res in results for e in res["items"] ]
info['items'] = items
info['code']['yang'] = model_list
return passed, message, errors, warnings, info