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