|
| 1 | +# Copyright The IETF Trust 2016, All Rights Reserved |
| 2 | + |
| 3 | +import os |
| 4 | +import re |
| 5 | +from xym import xym |
| 6 | +import shutil |
| 7 | +import tempfile |
| 8 | + |
| 9 | +from django.conf import settings |
| 10 | + |
| 11 | +import debug # pyflakes:ignore |
| 12 | + |
| 13 | +from ietf.utils.pipe import pipe |
| 14 | +from ietf.utils.log import log |
| 15 | + |
| 16 | +class DraftSubmissionChecker(): |
| 17 | + name = "" |
| 18 | + |
| 19 | + def check_file_txt(self, text): |
| 20 | + "Run checks on a text file" |
| 21 | + raise NotImplementedError |
| 22 | + |
| 23 | + def check_file_xml(self, xml): |
| 24 | + "Run checks on an xml file" |
| 25 | + raise NotImplementedError |
| 26 | + |
| 27 | + def check_fragment_txt(self, text): |
| 28 | + "Run checks on a fragment from a text file" |
| 29 | + raise NotImplementedError |
| 30 | + |
| 31 | + def check_fragment_xml(self, xml): |
| 32 | + "Run checks on a fragment from an xml file" |
| 33 | + raise NotImplementedError |
| 34 | + |
| 35 | + |
| 36 | +class DraftIdnitsChecker(object): |
| 37 | + """ |
| 38 | + Draft checker class for idnits. Idnits can only handle whole text files, |
| 39 | + so only check_file_txt() is defined; check_file_xml and check_fragment_* |
| 40 | + methods are undefined. |
| 41 | +
|
| 42 | + Furthermore, idnits doesn't provide an error code or line-by-line errors, |
| 43 | + so a bit of massage is needed in order to return the expected failure flag. |
| 44 | + """ |
| 45 | + name = "idnits check" |
| 46 | + |
| 47 | + def check_file_txt(self, path): |
| 48 | + """ |
| 49 | + Run an idnits check, and return a passed/failed indication, a message, |
| 50 | + and error and warning messages. |
| 51 | +
|
| 52 | + Error and warning list items are tuples: |
| 53 | + (line_number, line_text, message) |
| 54 | + """ |
| 55 | + filename = os.path.basename(path) |
| 56 | + result = {} |
| 57 | + items = [] |
| 58 | + errors = 0 |
| 59 | + warnings = 0 |
| 60 | + errstart = [' ** ', ' ~~ '] |
| 61 | + warnstart = [' == ', ' -- '] |
| 62 | + |
| 63 | + |
| 64 | + cmd = "%s --submitcheck --nitcount %s" % (settings.IDSUBMIT_IDNITS_BINARY, path) |
| 65 | + code, out, err = pipe(cmd) |
| 66 | + if code != 0 or out == "": |
| 67 | + message = "idnits error: %s:\n Error %s: %s" %( cmd, code, err) |
| 68 | + log(message) |
| 69 | + passed = False |
| 70 | + |
| 71 | + else: |
| 72 | + message = out |
| 73 | + if re.search("\s+Summary:\s+0\s+|No nits found", out): |
| 74 | + passed = True |
| 75 | + else: |
| 76 | + passed = False |
| 77 | + |
| 78 | + item = None |
| 79 | + for line in message.splitlines(): |
| 80 | + if line[:5] in (errstart + warnstart): |
| 81 | + item = line.rstrip() |
| 82 | + elif line.strip() == "" and item: |
| 83 | + tuple = (None, None, item) |
| 84 | + items.append(tuple) |
| 85 | + if item[:5] in errstart: |
| 86 | + errors += 1 |
| 87 | + elif item[:5] in warnstart: |
| 88 | + warnings += 1 |
| 89 | + else: |
| 90 | + raise RuntimeError("Unexpected state in idnits checker: item: %s, line: %s" % (item, line)) |
| 91 | + item = None |
| 92 | + elif item and line.strip() != "": |
| 93 | + item += " " + line.strip() |
| 94 | + else: |
| 95 | + pass |
| 96 | + result[filename] = { |
| 97 | + "passed": passed, |
| 98 | + "message": message, |
| 99 | + "errors": errors, |
| 100 | + "warnings":warnings, |
| 101 | + "items": items, |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + return passed, message, errors, warnings, result |
| 106 | + |
| 107 | +class DraftYangChecker(object): |
| 108 | + |
| 109 | + name = "yang validation" |
| 110 | + |
| 111 | + def check_file_txt(self, path): |
| 112 | + name = os.path.basename(path) |
| 113 | + workdir = tempfile.mkdtemp() |
| 114 | + results = {} |
| 115 | + |
| 116 | + extractor = xym.YangModuleExtractor(path, workdir, strict=True, debug_level = 0) |
| 117 | + with open(path) as file: |
| 118 | + try: |
| 119 | + # This places the yang models as files in workdir |
| 120 | + extractor.extract_yang_model(file.readlines()) |
| 121 | + model_list = extractor.get_extracted_models() |
| 122 | + except Exception as exc: |
| 123 | + passed = False |
| 124 | + message = exc |
| 125 | + errors = [ (name, None, None, exc) ] |
| 126 | + warnings = [] |
| 127 | + return passed, message, errors, warnings |
| 128 | + |
| 129 | + for model in model_list: |
| 130 | + path = os.path.join(workdir, model) |
| 131 | + with open(path) as file: |
| 132 | + text = file.readlines() |
| 133 | + cmd = settings.IDSUBMIT_PYANG_COMMAND % {"workdir": workdir, "model": path, } |
| 134 | + code, out, err = pipe(cmd) |
| 135 | + errors = 0 |
| 136 | + warnings = 0 |
| 137 | + items = [] |
| 138 | + if code > 0: |
| 139 | + error_lines = err.splitlines() |
| 140 | + for line in error_lines: |
| 141 | + fn, lnum, msg = line.split(':', 2) |
| 142 | + lnum = int(lnum) |
| 143 | + line = text[lnum-1].rstrip() |
| 144 | + items.append((lnum, line, msg)) |
| 145 | + if 'error: ' in msg: |
| 146 | + errors += 1 |
| 147 | + if 'warning: ' in msg: |
| 148 | + warnings += 1 |
| 149 | + results[model] = { |
| 150 | + "passed": code == 0, |
| 151 | + "message": out+"No validation errors\n" if code == 0 else err, |
| 152 | + "warnings": warnings, |
| 153 | + "errors": errors, |
| 154 | + "items": items, |
| 155 | + } |
| 156 | + |
| 157 | + shutil.rmtree(workdir) |
| 158 | + |
| 159 | + ## For now, never fail because of failed yang validation. |
| 160 | + if len(model_list): |
| 161 | + passed = True |
| 162 | + else: |
| 163 | + passed = None |
| 164 | + #passed = all( res["passed"] for res in results.values() ) |
| 165 | + message = "\n\n".join([ "\n".join([model+':', res["message"]]) for model, res in results.items() ]) |
| 166 | + errors = sum(res["errors"] for res in results.values() ) |
| 167 | + warnings = sum(res["warnings"] for res in results.values() ) |
| 168 | + items = [ e for res in results.values() for e in res["items"] ] |
| 169 | + |
| 170 | + return passed, message, errors, warnings, items |
| 171 | + |
0 commit comments