Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | |
| 3 | import logging |
| 4 | import lxml |
| 5 | import os |
| 6 | import os.path |
| 7 | import re |
| 8 | import requests |
| 9 | |
| 10 | from AdvancedHTMLParser import AdvancedHTMLParser |
| 11 | from glob import iglob |
| 12 | from urllib.parse import urlparse |
| 13 | |
| 14 | from cmsis.PackLint import PackLinter, VersionParser |
| 15 | from cmsis.Pack import Pack, Api, SemanticVersion |
| 16 | |
| 17 | def create(): |
| 18 | return CmsisPackLinter() |
| 19 | |
| 20 | class CmsisPackVersionParser(VersionParser): |
| 21 | def __init__(self, logger = None): |
| 22 | super().__init__(logger) |
| 23 | self._packs = {} |
| 24 | |
| 25 | def _file_version_(self, file): |
| 26 | v = self._regex_(file, ".*@version\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 27 | if not v: |
| 28 | v = self._regex_(file, ".*\$Revision:\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 29 | return v |
| 30 | |
| 31 | def _revhistory_(self, file, skip = 0): |
| 32 | table = "" |
| 33 | dump = False |
| 34 | with open(file, 'r') as f: |
| 35 | for l in f: |
| 36 | if not dump and l.strip() == "<table class=\"cmtable\" summary=\"Revision History\">": |
| 37 | if skip > 0: |
| 38 | skip -= 1 |
| 39 | else: |
| 40 | dump = True |
| 41 | if dump: |
| 42 | table += l.replace("<br>", "\\n") |
| 43 | if l.strip() == "</table>": |
| 44 | break |
| 45 | if table: |
| 46 | table = lxml.etree.fromstring(table) |
| 47 | m = re.match("[Vv]?(\d+.\d+(.\d+)?)", table[1][0].text) |
| 48 | if m: |
| 49 | return SemanticVersion(m.group(1)) |
| 50 | else: |
| 51 | self._logger.info("Revision History table not found in "+file) |
| 52 | return None |
| 53 | |
| 54 | def readme_md(self, file): |
| 55 | """Get the latest release version from README.md""" |
| 56 | return self._regex_(file, ".*repository contains the CMSIS Version ([0-9]+[.][0-9]+([.][0-9]+)?).*") |
| 57 | |
| 58 | def _dxy(self, file): |
| 59 | """Get the PROJECT_NUMBER from a Doxygen configuration file.""" |
| 60 | return self._regex_(file, "PROJECT_NUMBER\s*=\s*\"(Version\s+)?(\d+.\d+(.\d+)?)\"", 2) |
| 61 | |
| 62 | def _pdsc(self, file, component = None): |
| 63 | pack = None |
| 64 | if not file in self._packs: |
| 65 | pack = Pack(file, None) |
| 66 | self._packs[file] = pack |
| 67 | else: |
| 68 | pack = self._packs[file] |
| 69 | if component: |
| 70 | history = pack.history() |
| 71 | for r in sorted(history.keys(), reverse=True): |
| 72 | m = re.search(re.escape(component)+"(:)?\s+[Vv]?(\d+.\d+(.\d+)?)", history[r], re.MULTILINE) |
| 73 | if m: |
| 74 | return SemanticVersion(m.group(2)) |
| 75 | else: |
| 76 | return pack.version() |
| 77 | |
| 78 | def _h(self, file): |
| 79 | return self._file_version_(file) |
| 80 | |
| 81 | def _c(self, file): |
| 82 | return self._file_version_(file) |
| 83 | |
| 84 | def _s(self, file): |
| 85 | return self._file_version_(file) |
| 86 | |
| 87 | def overview_txt(self, file, skip = 0): |
| 88 | return self._revhistory_(file, skip) |
| 89 | |
| 90 | def introduction_txt(self, file, component = None): |
| 91 | if not component: |
| 92 | return None |
| 93 | |
| 94 | table = "" |
| 95 | dump = False |
| 96 | with open(file, 'r') as f: |
| 97 | for l in f: |
| 98 | if not dump and l.strip() == "<table class=\"cmtable\" summary=\"Revision History\">": |
| 99 | dump = True |
| 100 | if dump: |
| 101 | table += l.replace("<br>", "\\n") |
| 102 | if l.strip() == "</table>": |
| 103 | dump = False |
| 104 | table = lxml.etree.fromstring(table) |
| 105 | m = re.search(re.escape(component)+"\s+[Vv]?(\d+.\d+(.\d+)?)", table[1][1].text, re.MULTILINE) |
| 106 | if m: |
| 107 | return SemanticVersion(m.group(1)) |
| 108 | |
| 109 | return None |
| 110 | |
| 111 | def dap_txt(self, file, skip = 0): |
| 112 | return self._revhistory_(file, skip) |
| 113 | |
| 114 | def general_txt(self, file, skip = 0): |
| 115 | return self._revhistory_(file, skip) |
| 116 | |
| 117 | def history_txt(self, file, skip = 0): |
| 118 | return self._revhistory_(file, skip) |
| 119 | |
| 120 | def _all_(self, file): |
| 121 | """Get the version or revision tag from an arbitrary file.""" |
| 122 | version = self._regex_(file, ".*@version\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 123 | if not version: |
| 124 | version = self._regex_(file, ".*\$Revision:\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 125 | return version |
| 126 | |
| 127 | class CmsisPackLinter(PackLinter): |
| 128 | |
| 129 | def __init__(self, pdsc = "ARM.CMSIS.pdsc"): |
| 130 | super().__init__(pdsc) |
| 131 | self._versionParser = CmsisPackVersionParser(self._logger) |
| 132 | |
| 133 | def pack_version(self): |
| 134 | return self._pack.version() |
| 135 | |
| 136 | def cmsis_corem_component(self): |
| 137 | rte = { 'components' : set(), 'Dcore' : "Cortex-M3", 'Dvendor' : "", 'Dname' : "", 'Dtz' : "", 'Tcompiler' : "", 'Toptions' : "" } |
| 138 | comp = sorted(self._pack.component_by_name(rte, "CMSIS.CORE"), reverse=True)[0] |
| 139 | return SemanticVersion(comp.version()) |
| 140 | |
| 141 | def cmsis_corea_component(self): |
| 142 | rte = { 'components' : set(), 'Dcore' : "Cortex-A9", 'Dvendor' : "", 'Dname' : "", 'Dtz' : "", 'Tcompiler' : "", 'Toptions' : "" } |
| 143 | comp = sorted(self._pack.component_by_name(rte, "CMSIS.CORE"), reverse=True)[0] |
| 144 | return SemanticVersion(comp.version()) |
| 145 | |
| 146 | def cmsis_rtos2_api(self): |
| 147 | rte = { 'components' : set(), 'Dcore' : "", 'Dvendor' : "", 'Dname' : "", 'Dtz' : "", 'Tcompiler' : "", 'Toptions' : "" } |
| 148 | comp = sorted(self._pack.component_by_name(rte, "CMSIS.RTOS2"), reverse=True)[0] |
| 149 | return SemanticVersion(comp.version()) |
| 150 | |
| 151 | def cmsis_rtx5_component(self): |
| 152 | cs = self._pack.components_by_name("CMSIS.RTOS2.Keil RTX5*") |
| 153 | cvs = { (SemanticVersion(c.version()), SemanticVersion(c.apiversion())) for c in cs } |
| 154 | if len(cvs) == 1: |
| 155 | return cvs.pop() |
| 156 | elif len(cvs) > 1: |
| 157 | self.warning("Not all RTX5 components have same version information: %s", str([ (c.name(), c.version(), c.apiversion()) for c in cs ])) |
| 158 | return None, None |
| 159 | |
| 160 | def check_general(self): |
| 161 | """CMSIS version""" |
| 162 | v = self.pack_version() |
| 163 | self.verify_version("README.md", v) |
| 164 | self.verify_version("CMSIS/DoxyGen/General/general.dxy", v) |
| 165 | |
| 166 | def check_corem(self): |
| 167 | """CMSIS-Core(M) version""" |
| 168 | v = self.cmsis_corem_component() |
| 169 | self.verify_version("CMSIS/DoxyGen/Core/core.dxy", v) |
| 170 | self.verify_version("CMSIS/DoxyGen/Core/src/Overview.txt", v) |
| 171 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Core (Cortex-M)") |
| 172 | self.verify_version(self._pack.location(), v, component="CMSIS-Core(M)") |
| 173 | |
| 174 | def check_corea(self): |
| 175 | """CMSIS-Core(A) version""" |
| 176 | v = self.cmsis_corea_component() |
| 177 | self.verify_version("CMSIS/DoxyGen/Core_A/core_A.dxy", v) |
| 178 | self.verify_version("CMSIS/DoxyGen/Core_A/src/Overview.txt", v) |
| 179 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Core (Cortex-A)") |
| 180 | self.verify_version(self._pack.location(), v, component="CMSIS-Core(A)") |
| 181 | |
| 182 | def check_dap(self): |
| 183 | """CMSIS-DAP version""" |
| 184 | v = self._versionParser.get_version("CMSIS/DoxyGen/DAP/dap.dxy") |
| 185 | self.verify_version("CMSIS/DoxyGen/DAP/src/dap.txt", v) |
| 186 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-DAP") |
| 187 | self.verify_version(self._pack.location(), v, component="CMSIS-DAP") |
| 188 | |
| 189 | def check_driver(self): |
| 190 | """CMSIS-Driver version""" |
| 191 | v = self._versionParser.get_version("CMSIS/DoxyGen/Driver/Driver.dxy") |
| 192 | self.verify_version("CMSIS/DoxyGen/Driver/src/General.txt", v) |
| 193 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Driver") |
| 194 | self.verify_version(self._pack.location(), v, component="CMSIS-Driver") |
| 195 | |
| 196 | def check_dsp(self): |
| 197 | """CMSIS-DSP version""" |
| 198 | v = self._versionParser.get_version("CMSIS/DoxyGen/DSP/dsp.dxy") |
| 199 | self.verify_version("CMSIS/DoxyGen/DSP/src/history.txt", v) |
| 200 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-DSP") |
| 201 | self.verify_version(self._pack.location(), v, component="CMSIS-DSP") |
| 202 | |
| 203 | def check_nn(self): |
| 204 | """CMSIS-NN version""" |
| 205 | v = self._versionParser.get_version("CMSIS/DoxyGen/NN/nn.dxy") |
| 206 | self.verify_version("CMSIS/DoxyGen/NN/src/history.txt", v) |
| 207 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-NN") |
| 208 | self.verify_version(self._pack.location(), v, component="CMSIS-NN") |
| 209 | |
| 210 | def check_pack(self): |
| 211 | """CMSIS-Pack version""" |
| 212 | v = self._versionParser.get_version("CMSIS/DoxyGen/Pack/Pack.dxy") |
| 213 | self.verify_version("CMSIS/DoxyGen/Pack/src/General.txt", v) |
| 214 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Pack") |
| 215 | self.verify_version(self._pack.location(), v, component="CMSIS-Pack") |
| 216 | |
| 217 | def check_rtos2(self): |
| 218 | """CMSIS-RTOS2 version""" |
| 219 | api = self.cmsis_rtos2_api() |
| 220 | v, a = self.cmsis_rtx5_component() |
| 221 | self.verify_version("CMSIS/DoxyGen/RTOS2/rtos.dxy", api) |
| 222 | self.verify_version("CMSIS/DoxyGen/RTOS2/src/history.txt", api, skip=0) |
| 223 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", api, component="CMSIS-RTOS") |
| 224 | # self.verify_version(self._pack.location(), v, component="CMSIS-RTOS2") |
| 225 | if a and not api.match(a): |
| 226 | self.warning("RTX5 API version (%s) does not match RTOS2 API version (%s)!", a, api) |
| 227 | self.verify_version("CMSIS/DoxyGen/RTOS2/src/history.txt", v, skip=1) |
| 228 | |
| 229 | def check_files(self): |
| 230 | """Files referenced by pack description""" |
| 231 | # Check schema of pack description |
| 232 | self.verify_schema(self._pack.location(), "CMSIS/Utilities/PACK.xsd") |
| 233 | |
| 234 | # Check schema of SVD files |
| 235 | svdfiles = { d.svdfile() for d in self._pack.devices() if d.svdfile() } |
| 236 | for svd in svdfiles: |
| 237 | if os.path.exists(svd): |
| 238 | self.verify_schema(svd, "CMSIS/Utilities/CMSIS-SVD.xsd") |
| 239 | else: |
| 240 | self.warning("SVD File does not exist: %s!", svd) |
| 241 | |
| 242 | # Check component file version |
| 243 | for c in self._pack.components(): |
| 244 | cv = c.version() |
| 245 | for f in c.files(): |
| 246 | hv = f.version() |
| 247 | if c is Api: |
| 248 | if f.isHeader(): |
| 249 | if not hv: |
| 250 | self.verify_version(f.location(), cv) |
| 251 | if hv: |
| 252 | self.verify_version(f.location(), hv) |
| 253 | |
| 254 | def check_doc(self, pattern="./gen_pack/CMSIS/Documentation/**/*.html"): |
| 255 | """Documentation""" |
| 256 | self.debug("Using pattern '%s'", pattern) |
| 257 | for html in iglob(pattern, recursive=True): |
| 258 | self.info("%s: Checking links ...", html) |
| 259 | parser = AdvancedHTMLParser() |
| 260 | parser.parseFile(html) |
| 261 | links = parser.getElementsByTagName("a") |
| 262 | for l in links: |
| 263 | href = l.getAttribute("href") |
| 264 | if href: |
| 265 | href = urlparse(href) |
| 266 | if href.scheme in ["http", "https", "ftp", "ftps" ]: |
| 267 | try: |
| 268 | self.info("%s: Checking link to %s...", html, href.geturl()) |
| 269 | r = requests.head(href.geturl(), headers={'user-agent' : "packlint/1.0"}, timeout=10) |
| 270 | r.raise_for_status() |
| 271 | except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: |
| 272 | exc_info = None |
| 273 | if self.loglevel() == logging.DEBUG: |
| 274 | exc_info = e |
| 275 | self.warning("%s: Broken web-link to %s!", html, href.geturl(), exc_info=exc_info) |
| 276 | except requests.exceptions.Timeout as e: |
| 277 | exc_info = None |
| 278 | if self.loglevel() == logging.DEBUG: |
| 279 | exc_info = e |
| 280 | self.warning("%s: Timeout following web-link to %s.", html, href.geturl(), exc_info=exc_info) |
| 281 | elif href.scheme == "javascript": |
| 282 | pass |
| 283 | elif not os.path.isabs(href.path): |
| 284 | target = os.path.normpath(os.path.join(os.path.dirname(html), href.path)) |
| 285 | if not os.path.exists(target): |
| 286 | self.warning("%s: Broken relative-link to %s!", html, href.path) |
| 287 | else: |
| 288 | self.warning("%s: Broken relative-link to %s!", html, href.path) |
| 289 | |
| 290 | def check_schema(self): |
| 291 | """XML Schema""" |
| 292 | pass |