blob: cdbb0ed9c66ba3cd38558e1d72096d73ba2651ac [file] [log] [blame]
Jonatan Antoniaabb9d92018-05-16 12:21:24 +02001# -*- coding: utf-8 -*-
2
3import logging
4import lxml
5import os
6import os.path
7import re
8import requests
9
10from AdvancedHTMLParser import AdvancedHTMLParser
11from glob import iglob
12from urllib.parse import urlparse
13
14from cmsis.PackLint import PackLinter, VersionParser
15from cmsis.Pack import Pack, Api, SemanticVersion
16
17def create():
18 return CmsisPackLinter()
19
20class 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 Antoni6017b222018-05-17 16:05:45 +020030
31 def _cmtable_(self, file, skip = 0):
Jonatan Antoniaabb9d92018-05-16 12:21:24 +020032 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 Antoni6017b222018-05-17 16:05:45 +020042 table += l.replace("<br>", "\\n").replace("\\<", "&lt;").replace("\\>", "&gt;")
Jonatan Antoniaabb9d92018-05-16 12:21:24 +020043 if l.strip() == "</table>":
44 break
45 if table:
46 table = lxml.etree.fromstring(table)
Jonatan Antoni6017b222018-05-17 16:05:45 +020047 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 Antoniaabb9d92018-05-16 12:21:24 +020053 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 Antoni9a10fef2020-04-02 18:08:33 +020093 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 Antoniaabb9d92018-05-16 12:21:24 +0200102 def overview_txt(self, file, skip = 0):
103 return self._revhistory_(file, skip)
104
105 def introduction_txt(self, file, component = None):
Jonatan Antoni6017b222018-05-17 16:05:45 +0200106 table = self._cmtable_(file)
Jonatan Antonifd687e52018-07-25 10:50:56 +0200107 if table is None:
108 return None
109
110 if component:
Jonatan Antoni6017b222018-05-17 16:05:45 +0200111 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 Antonifd687e52018-07-25 10:50:56 +0200114 else:
115 return SemanticVersion(table[1][0].text)
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200116
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
133class 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 Antoni9a10fef2020-04-02 18:08:33 +0200141
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200142 def cmsis_corem_component(self):
Jonatan Antoni0b737062020-03-27 17:23:43 +0100143 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 Antoniaabb9d92018-05-16 12:21:24 +0200149
150 def cmsis_corea_component(self):
Jonatan Antoni0b737062020-03-27 17:23:43 +0100151 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 Antoniaabb9d92018-05-16 12:21:24 +0200157
158 def cmsis_rtos2_api(self):
Jonatan Antoni0b737062020-03-27 17:23:43 +0100159 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 Antoniaabb9d92018-05-16 12:21:24 +0200164
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 Antonifd687e52018-07-25 10:50:56 +0200179 self.verify_version("CMSIS/DoxyGen/General/src/introduction.txt", v)
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200180
Jonatan Antoni9a10fef2020-04-02 18:08:33 +0200181 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 Antoniaabb9d92018-05-16 12:21:24 +0200188 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 Antoni9a10fef2020-04-02 18:08:33 +0200234 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 Antoniaabb9d92018-05-16 12:21:24 +0200238 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 Antonifd687e52018-07-25 10:50:56 +0200277 self.verify_version(f.location(), SemanticVersion(hv))
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200278
Jonatan Antonifd687e52018-07-25 10:50:56 +0200279 def check_doc(self, pattern="./CMSIS/Documentation/**/*.html"):
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200280 """Documentation"""
281 self.debug("Using pattern '%s'", pattern)
282 for html in iglob(pattern, recursive=True):
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200283 parser = AdvancedHTMLParser()
284 parser.parseFile(html)
285 links = parser.getElementsByTagName("a")
Jonatan Antonibcdc8292020-04-17 16:15:36 +0200286 if links:
287 self.info("%s: Checking links ...", html)
288 else:
289 self.debug("%s: No links found...", html)
Jonatan Antoniaabb9d92018-05-16 12:21:24 +0200290 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 Antonibcdc8292020-04-17 16:15:36 +0200298 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 Antoniaabb9d92018-05-16 12:21:24 +0200301 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)