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 | |
Jonatan Antoni | 9a10fef | 2020-04-02 18:08:33 +0200 | [diff] [blame] | 93 | def _xsd(self, file, rev=False, history=False): |
| 94 | if rev: |
| 95 | return self._all_(file) |
| 96 | elif history: |
| 97 | return self._regex_(file, ".*[0-9]+\. [A-Z][a-z]+ [12][0-9]+: (v)?(\d+.\d+(.\d+)?).*", 2) |
| 98 | else: |
| 99 | xsd = lxml.etree.parse(str(file)).getroot() |
| 100 | return SemanticVersion(xsd.get("version", None)) |
| 101 | |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 102 | def overview_txt(self, file, skip = 0): |
| 103 | return self._revhistory_(file, skip) |
| 104 | |
| 105 | def introduction_txt(self, file, component = None): |
Jonatan Antoni | 6017b22 | 2018-05-17 16:05:45 +0200 | [diff] [blame] | 106 | table = self._cmtable_(file) |
Jonatan Antoni | fd687e5 | 2018-07-25 10:50:56 +0200 | [diff] [blame] | 107 | if table is None: |
| 108 | return None |
| 109 | |
| 110 | if component: |
Jonatan Antoni | 6017b22 | 2018-05-17 16:05:45 +0200 | [diff] [blame] | 111 | m = re.search(re.escape(component)+"\s+[Vv]?(\d+.\d+(.\d+)?)", table[1][1].text, re.MULTILINE) |
| 112 | if m: |
| 113 | return SemanticVersion(m.group(1)) |
Jonatan Antoni | fd687e5 | 2018-07-25 10:50:56 +0200 | [diff] [blame] | 114 | else: |
| 115 | return SemanticVersion(table[1][0].text) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 116 | |
| 117 | def dap_txt(self, file, skip = 0): |
| 118 | return self._revhistory_(file, skip) |
| 119 | |
| 120 | def general_txt(self, file, skip = 0): |
| 121 | return self._revhistory_(file, skip) |
| 122 | |
| 123 | def history_txt(self, file, skip = 0): |
| 124 | return self._revhistory_(file, skip) |
| 125 | |
| 126 | def _all_(self, file): |
| 127 | """Get the version or revision tag from an arbitrary file.""" |
| 128 | version = self._regex_(file, ".*@version\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 129 | if not version: |
| 130 | version = self._regex_(file, ".*\$Revision:\s+([vV])?([0-9]+[.][0-9]+([.][0-9]+)?).*", 2) |
| 131 | return version |
| 132 | |
| 133 | class CmsisPackLinter(PackLinter): |
| 134 | |
| 135 | def __init__(self, pdsc = "ARM.CMSIS.pdsc"): |
| 136 | super().__init__(pdsc) |
| 137 | self._versionParser = CmsisPackVersionParser(self._logger) |
| 138 | |
| 139 | def pack_version(self): |
| 140 | return self._pack.version() |
Jonatan Antoni | 9a10fef | 2020-04-02 18:08:33 +0200 | [diff] [blame] | 141 | |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 142 | def cmsis_corem_component(self): |
Jonatan Antoni | 0b73706 | 2020-03-27 17:23:43 +0100 | [diff] [blame] | 143 | rte = { 'components' : set(), 'Dcore' : "Cortex-M3", 'Dvendor' : "*", 'Dname' : "*", 'Dtz' : "*", 'Dsecure' : "*", 'Tcompiler' : "*", 'Toptions' : "*" } |
| 144 | cs = self._pack.component_by_name(rte, "CMSIS.CORE") |
| 145 | cvs = { SemanticVersion(c.version()) for c in cs } |
| 146 | if len(cvs) > 1: |
| 147 | self.warning("Not all CMSIS-Core(M) components have same version information: %s", str([ (c.name(), c.version()) for c in cs ])) |
| 148 | return cvs.pop() |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 149 | |
| 150 | def cmsis_corea_component(self): |
Jonatan Antoni | 0b73706 | 2020-03-27 17:23:43 +0100 | [diff] [blame] | 151 | rte = { 'components' : set(), 'Dcore' : "Cortex-A9", 'Dvendor' : "*", 'Dname' : "*", 'Dtz' : "*", 'Dsecure' : "*", 'Tcompiler' : "*", 'Toptions' : "*" } |
| 152 | cs = self._pack.component_by_name(rte, "CMSIS.CORE") |
| 153 | cvs = { SemanticVersion(c.version()) for c in cs } |
| 154 | if len(cvs) > 1: |
| 155 | self.warning("Not all CMSIS-Core(A) components have same version information: %s", str([ (c.name(), c.version()) for c in cs ])) |
| 156 | return cvs.pop() |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 157 | |
| 158 | def cmsis_rtos2_api(self): |
Jonatan Antoni | 0b73706 | 2020-03-27 17:23:43 +0100 | [diff] [blame] | 159 | cs = self._pack.components_by_name("CMSIS.RTOS2") |
| 160 | cvs = { SemanticVersion(c.version()) for c in cs } |
| 161 | if len(cvs) > 1: |
| 162 | self.warning("Not all CMSIS-RTOS2 APIs have same version information: %s", str([ (c.name(), c.version()) for c in cs ])) |
| 163 | return cvs.pop() |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 164 | |
| 165 | def cmsis_rtx5_component(self): |
| 166 | cs = self._pack.components_by_name("CMSIS.RTOS2.Keil RTX5*") |
| 167 | cvs = { (SemanticVersion(c.version()), SemanticVersion(c.apiversion())) for c in cs } |
| 168 | if len(cvs) == 1: |
| 169 | return cvs.pop() |
| 170 | elif len(cvs) > 1: |
| 171 | self.warning("Not all RTX5 components have same version information: %s", str([ (c.name(), c.version(), c.apiversion()) for c in cs ])) |
| 172 | return None, None |
| 173 | |
| 174 | def check_general(self): |
| 175 | """CMSIS version""" |
| 176 | v = self.pack_version() |
| 177 | self.verify_version("README.md", v) |
| 178 | self.verify_version("CMSIS/DoxyGen/General/general.dxy", v) |
Jonatan Antoni | fd687e5 | 2018-07-25 10:50:56 +0200 | [diff] [blame] | 179 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 180 | |
Jonatan Antoni | 9a10fef | 2020-04-02 18:08:33 +0200 | [diff] [blame] | 181 | def check_build(self): |
| 182 | """CMSIS-Build version""" |
| 183 | v = self._versionParser.get_version("CMSIS/DoxyGen/Build/Build.dxy") |
| 184 | self.verify_version("CMSIS/DoxyGen/Build/src/General.txt", v) |
| 185 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Build") |
| 186 | self.verify_version(self._pack.location(), v, component="CMSIS-Build") |
| 187 | |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 188 | def check_corem(self): |
| 189 | """CMSIS-Core(M) version""" |
| 190 | v = self.cmsis_corem_component() |
| 191 | self.verify_version("CMSIS/DoxyGen/Core/core.dxy", v) |
| 192 | self.verify_version("CMSIS/DoxyGen/Core/src/Overview.txt", v) |
| 193 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Core (Cortex-M)") |
| 194 | self.verify_version(self._pack.location(), v, component="CMSIS-Core(M)") |
| 195 | |
| 196 | def check_corea(self): |
| 197 | """CMSIS-Core(A) version""" |
| 198 | v = self.cmsis_corea_component() |
| 199 | self.verify_version("CMSIS/DoxyGen/Core_A/core_A.dxy", v) |
| 200 | self.verify_version("CMSIS/DoxyGen/Core_A/src/Overview.txt", v) |
| 201 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Core (Cortex-A)") |
| 202 | self.verify_version(self._pack.location(), v, component="CMSIS-Core(A)") |
| 203 | |
| 204 | def check_dap(self): |
| 205 | """CMSIS-DAP version""" |
| 206 | v = self._versionParser.get_version("CMSIS/DoxyGen/DAP/dap.dxy") |
| 207 | self.verify_version("CMSIS/DoxyGen/DAP/src/dap.txt", v) |
| 208 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-DAP") |
| 209 | self.verify_version(self._pack.location(), v, component="CMSIS-DAP") |
| 210 | |
| 211 | def check_driver(self): |
| 212 | """CMSIS-Driver version""" |
| 213 | v = self._versionParser.get_version("CMSIS/DoxyGen/Driver/Driver.dxy") |
| 214 | self.verify_version("CMSIS/DoxyGen/Driver/src/General.txt", v) |
| 215 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Driver") |
| 216 | self.verify_version(self._pack.location(), v, component="CMSIS-Driver") |
| 217 | |
| 218 | def check_dsp(self): |
| 219 | """CMSIS-DSP version""" |
| 220 | v = self._versionParser.get_version("CMSIS/DoxyGen/DSP/dsp.dxy") |
| 221 | self.verify_version("CMSIS/DoxyGen/DSP/src/history.txt", v) |
| 222 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-DSP") |
| 223 | self.verify_version(self._pack.location(), v, component="CMSIS-DSP") |
| 224 | |
| 225 | def check_nn(self): |
| 226 | """CMSIS-NN version""" |
| 227 | v = self._versionParser.get_version("CMSIS/DoxyGen/NN/nn.dxy") |
| 228 | self.verify_version("CMSIS/DoxyGen/NN/src/history.txt", v) |
| 229 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-NN") |
| 230 | self.verify_version(self._pack.location(), v, component="CMSIS-NN") |
| 231 | |
| 232 | def check_pack(self): |
| 233 | """CMSIS-Pack version""" |
Jonatan Antoni | 9a10fef | 2020-04-02 18:08:33 +0200 | [diff] [blame] | 234 | v = self._versionParser.get_version("CMSIS/Utilities/PACK.xsd") |
| 235 | self.verify_version("CMSIS/Utilities/PACK.xsd:Revision", v, rev=True) |
| 236 | self.verify_version("CMSIS/Utilities/PACK.xsd:History", v, history=True) |
| 237 | self.verify_version("CMSIS/DoxyGen/Pack/Pack.dxy", v) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 238 | self.verify_version("CMSIS/DoxyGen/Pack/src/General.txt", v) |
| 239 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v, component="CMSIS-Pack") |
| 240 | self.verify_version(self._pack.location(), v, component="CMSIS-Pack") |
| 241 | |
| 242 | def check_rtos2(self): |
| 243 | """CMSIS-RTOS2 version""" |
| 244 | api = self.cmsis_rtos2_api() |
| 245 | v, a = self.cmsis_rtx5_component() |
| 246 | self.verify_version("CMSIS/DoxyGen/RTOS2/rtos.dxy", api) |
| 247 | self.verify_version("CMSIS/DoxyGen/RTOS2/src/history.txt", api, skip=0) |
| 248 | self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", api, component="CMSIS-RTOS") |
| 249 | # self.verify_version(self._pack.location(), v, component="CMSIS-RTOS2") |
| 250 | if a and not api.match(a): |
| 251 | self.warning("RTX5 API version (%s) does not match RTOS2 API version (%s)!", a, api) |
| 252 | self.verify_version("CMSIS/DoxyGen/RTOS2/src/history.txt", v, skip=1) |
| 253 | |
| 254 | def check_files(self): |
| 255 | """Files referenced by pack description""" |
| 256 | # Check schema of pack description |
| 257 | self.verify_schema(self._pack.location(), "CMSIS/Utilities/PACK.xsd") |
| 258 | |
| 259 | # Check schema of SVD files |
| 260 | svdfiles = { d.svdfile() for d in self._pack.devices() if d.svdfile() } |
| 261 | for svd in svdfiles: |
| 262 | if os.path.exists(svd): |
| 263 | self.verify_schema(svd, "CMSIS/Utilities/CMSIS-SVD.xsd") |
| 264 | else: |
| 265 | self.warning("SVD File does not exist: %s!", svd) |
| 266 | |
| 267 | # Check component file version |
| 268 | for c in self._pack.components(): |
| 269 | cv = c.version() |
| 270 | for f in c.files(): |
| 271 | hv = f.version() |
| 272 | if c is Api: |
| 273 | if f.isHeader(): |
| 274 | if not hv: |
| 275 | self.verify_version(f.location(), cv) |
| 276 | if hv: |
Jonatan Antoni | fd687e5 | 2018-07-25 10:50:56 +0200 | [diff] [blame] | 277 | self.verify_version(f.location(), SemanticVersion(hv)) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 278 | |
Jonatan Antoni | fd687e5 | 2018-07-25 10:50:56 +0200 | [diff] [blame] | 279 | def check_doc(self, pattern="./CMSIS/Documentation/**/*.html"): |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 280 | """Documentation""" |
| 281 | self.debug("Using pattern '%s'", pattern) |
| 282 | for html in iglob(pattern, recursive=True): |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 283 | parser = AdvancedHTMLParser() |
| 284 | parser.parseFile(html) |
| 285 | links = parser.getElementsByTagName("a") |
Jonatan Antoni | bcdc829 | 2020-04-17 16:15:36 +0200 | [diff] [blame] | 286 | if links: |
| 287 | self.info("%s: Checking links ...", html) |
| 288 | else: |
| 289 | self.debug("%s: No links found...", html) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 290 | for l in links: |
| 291 | href = l.getAttribute("href") |
| 292 | if href: |
| 293 | href = urlparse(href) |
| 294 | if href.scheme in ["http", "https", "ftp", "ftps" ]: |
| 295 | try: |
| 296 | self.info("%s: Checking link to %s...", html, href.geturl()) |
| 297 | r = requests.head(href.geturl(), headers={'user-agent' : "packlint/1.0"}, timeout=10) |
Jonatan Antoni | bcdc829 | 2020-04-17 16:15:36 +0200 | [diff] [blame] | 298 | if r.status_code >= 400: |
| 299 | self.debug(f'HEAD method failed with HTTP-{r.status_code}, falling back to GET method.') |
| 300 | r = requests.get(href.geturl(), headers={'user-agent': "packlint/1.0"}, timeout=10) |
Jonatan Antoni | aabb9d9 | 2018-05-16 12:21:24 +0200 | [diff] [blame] | 301 | r.raise_for_status() |
| 302 | except (requests.exceptions.ConnectionError, requests.exceptions.HTTPError) as e: |
| 303 | exc_info = None |
| 304 | if self.loglevel() == logging.DEBUG: |
| 305 | exc_info = e |
| 306 | self.warning("%s: Broken web-link to %s!", html, href.geturl(), exc_info=exc_info) |
| 307 | except requests.exceptions.Timeout as e: |
| 308 | exc_info = None |
| 309 | if self.loglevel() == logging.DEBUG: |
| 310 | exc_info = e |
| 311 | self.warning("%s: Timeout following web-link to %s.", html, href.geturl(), exc_info=exc_info) |
| 312 | elif href.scheme == "javascript": |
| 313 | pass |
| 314 | elif not os.path.isabs(href.path): |
| 315 | target = os.path.normpath(os.path.join(os.path.dirname(html), href.path)) |
| 316 | if not os.path.exists(target): |
| 317 | self.warning("%s: Broken relative-link to %s!", html, href.path) |
| 318 | else: |
| 319 | self.warning("%s: Broken relative-link to %s!", html, href.path) |