| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
import json, os, re, sys, yaml |
| 8 |
|
| 9 |
import jsonschema |
| 10 |
|
| 11 |
def empty_data(): |
| 12 |
return {'dirs': {}} |
| 13 |
|
| 14 |
def verify_data(data): |
| 15 |
|
| 16 |
if data == empty_data(): |
| 17 |
return True |
| 18 |
|
| 19 |
ret = True |
| 20 |
|
| 21 |
blocklineno = min(data['dirs'].values()) |
| 22 |
|
| 23 |
for (entry, lineno) in data['dirs'].items(): |
| 24 |
if not os.path.exists(entry): |
| 25 |
sys.stderr.write("WHENCE:%d: dir %s doesn't exist\n" % (lineno, entry)) |
| 26 |
ret = False |
| 27 |
elif not os.path.isdir(entry): |
| 28 |
sys.stderr.write("WHENCE:%d: %s is not a directory\n" % (lineno, entry)) |
| 29 |
ret = False |
| 30 |
elif entry.endswith("/"): |
| 31 |
sys.stderr.write("WHENCE:%d: stray ending '/' in %s\n" % (lineno, entry)) |
| 32 |
ret = False |
| 33 |
|
| 34 |
if 'licence' in data: |
| 35 |
lic, lineno = data['licence'] |
| 36 |
if not os.path.exists(lic): |
| 37 |
sys.stderr.write("WHENCE:%d: licence %s doesn't exist\n" % (lineno, lic)) |
| 38 |
ret = False |
| 39 |
elif not os.path.isfile(lic): |
| 40 |
sys.stderr.write("WHENCE:%d: %s is not a file\n" % (lineno, lic)) |
| 41 |
ret = False |
| 42 |
else: |
| 43 |
sys.stderr.write("WHENCE:%d: licence not specified\n" % blocklineno) |
| 44 |
ret = False |
| 45 |
|
| 46 |
if 'status' in data: |
| 47 |
status, lineno = data['status'] |
| 48 |
if "Redistributable" not in status: |
| 49 |
sys.stderr.write("WHENCE:%d: binaries are not redistributable\n" % lineno) |
| 50 |
ret = False |
| 51 |
else: |
| 52 |
sys.stderr.write("WHENCE:%d: status not specified\n" % blocklineno) |
| 53 |
ret = False |
| 54 |
|
| 55 |
return ret |
| 56 |
|
| 57 |
def load_whence(): |
| 58 |
data = empty_data() |
| 59 |
|
| 60 |
with open("WHENCE", encoding="utf-8") as file: |
| 61 |
pattern_dir = re.compile("^Dir: (.*)\n$") |
| 62 |
pattern_licence = re.compile("^Licence: (.*)\n$") |
| 63 |
pattern_status = re.compile("^Status: (.*)\n$") |
| 64 |
pattern_break = re.compile("^----") |
| 65 |
|
| 66 |
for (lineno, line) in enumerate(file, start=1): |
| 67 |
match = pattern_dir.match(line) |
| 68 |
if match: |
| 69 |
data['dirs'][match.group(1)] = lineno |
| 70 |
continue |
| 71 |
|
| 72 |
match = pattern_licence.match(line) |
| 73 |
if match: |
| 74 |
data['licence'] = (match.group(1), lineno) |
| 75 |
|
| 76 |
match = pattern_status.match(line) |
| 77 |
if match: |
| 78 |
data['status'] = (match.group(1), lineno) |
| 79 |
|
| 80 |
match = pattern_break.match(line) |
| 81 |
if match: |
| 82 |
yield data |
| 83 |
data = empty_data() |
| 84 |
continue |
| 85 |
|
| 86 |
|
| 87 |
yield data |
| 88 |
|
| 89 |
def load_config(): |
| 90 |
with open("config.txt", encoding="utf-8") as file: |
| 91 |
pattern_empty = re.compile("^#|^$") |
| 92 |
pattern_install = re.compile("Install: ([^ \t]+)[ \t]+([^ \t]+)[ \t]+([^ \t]+)\n") |
| 93 |
pattern_link = re.compile("Link: ([^ \t]+)[ \t]+([^ \t]+)\n") |
| 94 |
|
| 95 |
for (lineno, line) in enumerate(file, start=1): |
| 96 |
if pattern_empty.match(line): |
| 97 |
continue |
| 98 |
|
| 99 |
match = pattern_install.match(line) |
| 100 |
if match: |
| 101 |
yield ("install", lineno, *match.groups()) |
| 102 |
continue |
| 103 |
|
| 104 |
match = pattern_link.match(line) |
| 105 |
if match: |
| 106 |
yield ("link", lineno, *match.groups()) |
| 107 |
continue |
| 108 |
|
| 109 |
raise Exception("config.txt: %d: failed to parse '%s'" % (lineno, line[:-1])) |
| 110 |
|
| 111 |
DSPS = [ "adsp", "cdsp", "sdsp", "cdsp1", "gdsp0", "gdsp1" ] |
| 112 |
|
| 113 |
def check_install_config(data, dirs): |
| 114 |
(lineno, path, dsp, subdir) = data |
| 115 |
ret = True |
| 116 |
|
| 117 |
if path.endswith("/"): |
| 118 |
sys.stderr.write("config.txt: %d: trailing '/' in %s\n" % (lineno, path)) |
| 119 |
ret = False |
| 120 |
|
| 121 |
if subdir.endswith("/"): |
| 122 |
sys.stderr.write("config.txt: %d: trailing '/' in %s\n" % (lineno, subdir)) |
| 123 |
ret = False |
| 124 |
|
| 125 |
full = "%s/%s" % (path, subdir) |
| 126 |
if full not in dirs: |
| 127 |
sys.stderr.write("config.txt: %d: path '%s' not found in WHENCE\n" % (lineno, full)) |
| 128 |
ret = False |
| 129 |
|
| 130 |
if dsp not in DSPS: |
| 131 |
sys.stderr.write("config.txt: %d: unknown DSP type '%s'\n" % (lineno, dsp)) |
| 132 |
ret = False |
| 133 |
|
| 134 |
return ret |
| 135 |
|
| 136 |
def check_link_config(data): |
| 137 |
(lineno, src_path, dst_path) = data |
| 138 |
ret = True |
| 139 |
|
| 140 |
if src_path.endswith("/"): |
| 141 |
sys.stderr.write("config.txt: %d: trailing '/' in %s\n" % (lineno, src_path)) |
| 142 |
ret = False |
| 143 |
|
| 144 |
if dst_path.endswith("/"): |
| 145 |
sys.stderr.write("config.txt: %d: trailing '/' in %s\n" % (lineno, dst_path)) |
| 146 |
ret = False |
| 147 |
|
| 148 |
dsp_path = os.path.dirname(src_path) |
| 149 |
sourcedir = os.path.dirname(dsp_path) |
| 150 |
if not os.path.exists(sourcedir): |
| 151 |
sys.stderr.write("config.txt: %d: dir %s doesn't exist\n" % (lineno, sourcedir)) |
| 152 |
ret = False |
| 153 |
|
| 154 |
if os.path.basename(dsp_path) != "dsp": |
| 155 |
sys.stderr.write("config.txt: %d: DSP type not under 'dsp' dir %s\n" % (lineno, src_path)) |
| 156 |
ret = False |
| 157 |
|
| 158 |
return ret |
| 159 |
|
| 160 |
def check_config(data, dirs): |
| 161 |
ret = True |
| 162 |
|
| 163 |
if data[0] == "install": |
| 164 |
ret = check_install_config(data[1:], dirs) |
| 165 |
elif data[0] == "link": |
| 166 |
ret = check_link_config(data[1:]) |
| 167 |
|
| 168 |
return ret |
| 169 |
|
| 170 |
def list_git(): |
| 171 |
if not os.path.exists(".git"): |
| 172 |
sys.stderr.write("Skipping git ls-files check, no .git dir\n") |
| 173 |
return None |
| 174 |
|
| 175 |
git = os.popen("git ls-files") |
| 176 |
for file in git: |
| 177 |
yield file.rstrip("\n") |
| 178 |
|
| 179 |
if git.close(): |
| 180 |
sys.stderr.write("WHENCE: skipped contents validation, git file listing failed\n") |
| 181 |
|
| 182 |
def check_conf_file_format(path): |
| 183 |
"""Validate properties not visible to the YAML parser.""" |
| 184 |
okay = True |
| 185 |
|
| 186 |
with open(path, "rb") as f: |
| 187 |
raw = f.read() |
| 188 |
|
| 189 |
if len(raw) == 0: |
| 190 |
sys.stderr.write(f"{path}: file is empty\n") |
| 191 |
return False |
| 192 |
|
| 193 |
if not raw.endswith(b"\n"): |
| 194 |
sys.stderr.write( |
| 195 |
f"{path}: missing trailing newline at end of file\n") |
| 196 |
okay = False |
| 197 |
elif raw.endswith(b"\n\n"): |
| 198 |
sys.stderr.write( |
| 199 |
f"{path}: trailing blank line(s) at end of file\n") |
| 200 |
okay = False |
| 201 |
|
| 202 |
first_line = raw.split(b"\n", 1)[0].decode("utf-8", errors="replace") |
| 203 |
SPDX_HEADER_RE = re.compile(r"^# SPDX-License-Identifier: [^\s]+\s*$") |
| 204 |
if not SPDX_HEADER_RE.match(first_line): |
| 205 |
sys.stderr.write( |
| 206 |
f"{path}: missing SPDX-License-Identifier header on first" |
| 207 |
f" line\n") |
| 208 |
okay = False |
| 209 |
|
| 210 |
return okay |
| 211 |
|
| 212 |
def load_schema(): |
| 213 |
with open("conf.d/schema.json", encoding="utf-8") as file: |
| 214 |
return json.load(file) |
| 215 |
|
| 216 |
def check_conf_against_schema(path, data, validator): |
| 217 |
"""Validate YAML contents against conf.d/schema.json.""" |
| 218 |
okay = True |
| 219 |
for err in sorted(validator.iter_errors(data), key=lambda e: e.path): |
| 220 |
loc = "/".join(str(p) for p in err.absolute_path) or "<root>" |
| 221 |
sys.stderr.write(f"{path}: {loc}: {err.message}\n") |
| 222 |
okay = False |
| 223 |
return okay |
| 224 |
|
| 225 |
def load_machine_dsp_paths(): |
| 226 |
"""Validate and parse conf.d/*.yaml files; return the set of base paths.""" |
| 227 |
paths = set() |
| 228 |
okay = True |
| 229 |
|
| 230 |
validator = jsonschema.Draft202012Validator(load_schema()) |
| 231 |
|
| 232 |
for conf in sorted(os.listdir("conf.d")): |
| 233 |
if conf == "schema.json": |
| 234 |
continue |
| 235 |
if not conf.endswith(".yaml"): |
| 236 |
sys.stderr.write( |
| 237 |
f"conf.d/{conf}: unexpected non-yaml file\n") |
| 238 |
okay = False |
| 239 |
continue |
| 240 |
|
| 241 |
full = os.path.join("conf.d", conf) |
| 242 |
|
| 243 |
if not check_conf_file_format(full): |
| 244 |
okay = False |
| 245 |
|
| 246 |
try: |
| 247 |
with open(full, encoding="utf-8") as file: |
| 248 |
data = yaml.safe_load(file) |
| 249 |
except yaml.YAMLError as e: |
| 250 |
sys.stderr.write(f"{full}: YAML parse error: {e}\n") |
| 251 |
okay = False |
| 252 |
continue |
| 253 |
|
| 254 |
if not check_conf_against_schema(full, data, validator): |
| 255 |
okay = False |
| 256 |
continue |
| 257 |
|
| 258 |
|
| 259 |
for machine_name, machine_data in data['machines'].items(): |
| 260 |
dsp_path = machine_data['DSP_LIBRARY_PATH'] |
| 261 |
base_path = dsp_path[:-len('/dsp')] |
| 262 |
paths.add(base_path) |
| 263 |
|
| 264 |
suffix = base_path[base_path.find('/') + 1:] |
| 265 |
name = "hexagon-dsp-binaries-%s.yaml" % \ |
| 266 |
suffix.lower().replace('/', '-') |
| 267 |
if name != conf: |
| 268 |
sys.stderr.write( |
| 269 |
f"Name mismatch, {conf} should be named {name}\n") |
| 270 |
okay = False |
| 271 |
|
| 272 |
return paths if okay else None |
| 273 |
|
| 274 |
def check_config_against_machine_paths(config_data, machine_paths): |
| 275 |
"""Check that config.txt paths are consistent with configuration YAML files.""" |
| 276 |
if machine_paths is None: |
| 277 |
return True |
| 278 |
|
| 279 |
ret = True |
| 280 |
reported_paths = set() |
| 281 |
|
| 282 |
for data in config_data: |
| 283 |
if data[0] == "install": |
| 284 |
(lineno, path, dsp, subdir) = data[1:] |
| 285 |
if path not in machine_paths and path not in reported_paths: |
| 286 |
sys.stderr.write("config.txt: %d: Install path '%s' not found in YAML configs\n" % (lineno, path)) |
| 287 |
reported_paths.add(path) |
| 288 |
ret = False |
| 289 |
elif data[0] == "link": |
| 290 |
(lineno, src_path, dst_path) = data[1:] |
| 291 |
|
| 292 |
|
| 293 |
parts = dst_path.split('/') |
| 294 |
if len(parts) >= 2 and parts[-2] == 'dsp': |
| 295 |
base_path = '/'.join(parts[:-2]) |
| 296 |
if base_path not in machine_paths and base_path not in reported_paths: |
| 297 |
sys.stderr.write("config.txt: %d: Link target base path '%s' not found in YAML configs\n" % (lineno, base_path)) |
| 298 |
reported_paths.add(base_path) |
| 299 |
ret = False |
| 300 |
|
| 301 |
return ret |
| 302 |
|
| 303 |
def check_dir(subdir): |
| 304 |
pattern_shell = re.compile("^fastrpc_shell(_unsigned)?_[0-9]$") |
| 305 |
pattern_library = re.compile("^[-_+0-9a-zA-Z]*\\.so(\\.[0-9]*)?$") |
| 306 |
|
| 307 |
okay = True |
| 308 |
|
| 309 |
|
| 310 |
if not os.path.exists(subdir): |
| 311 |
return False |
| 312 |
|
| 313 |
for file in os.listdir(subdir): |
| 314 |
fullname = os.path.join(subdir, file) |
| 315 |
|
| 316 |
if not os.path.isfile(fullname): |
| 317 |
sys.stderr.write("WHENCE: not a file %s\n" % fullname) |
| 318 |
okay = False |
| 319 |
|
| 320 |
|
| 321 |
if file == "hashes-ignore.txt": |
| 322 |
continue |
| 323 |
|
| 324 |
if not pattern_shell.match(file) and \ |
| 325 |
not pattern_library.match(file): |
| 326 |
sys.stderr.write("WHENCE: unknown file type %s\n" % fullname) |
| 327 |
okay = False |
| 328 |
|
| 329 |
return okay |
| 330 |
|
| 331 |
def main(): |
| 332 |
okay = True |
| 333 |
dirs = {} |
| 334 |
licences = {'LICENSE.MIT' : None} |
| 335 |
|
| 336 |
for data in load_whence(): |
| 337 |
if not verify_data(data): |
| 338 |
okay = False |
| 339 |
|
| 340 |
dirs.update(dict.fromkeys(data['dirs'].keys())) |
| 341 |
if 'licence' in data: |
| 342 |
licences[data['licence'][0]] = None |
| 343 |
|
| 344 |
known_files = ['.gitignore', 'config.txt', 'Makefile', 'TODO', 'README.md', 'WHENCE'] |
| 345 |
|
| 346 |
for file in list_git(): |
| 347 |
if os.path.dirname(file) == "conf.d" and \ |
| 348 |
(os.path.basename(file).endswith(".yaml") or |
| 349 |
os.path.basename(file) == "schema.json"): |
| 350 |
continue |
| 351 |
|
| 352 |
if os.path.dirname(file) in dirs: |
| 353 |
continue |
| 354 |
|
| 355 |
if file in licences: |
| 356 |
continue |
| 357 |
|
| 358 |
if os.path.dirname(file) == 'scripts': |
| 359 |
continue |
| 360 |
|
| 361 |
if os.path.dirname(file) == '.github/workflows': |
| 362 |
continue |
| 363 |
|
| 364 |
if file in known_files: |
| 365 |
continue |
| 366 |
|
| 367 |
sys.stderr.write("WHENCE: file %s is not under a listed directory\n" % file) |
| 368 |
okay = False |
| 369 |
|
| 370 |
|
| 371 |
machine_paths = load_machine_dsp_paths() |
| 372 |
if not machine_paths: |
| 373 |
okay = False |
| 374 |
|
| 375 |
|
| 376 |
config_data = [] |
| 377 |
try: |
| 378 |
for data in load_config(): |
| 379 |
config_data.append(data) |
| 380 |
if not check_config(data, dirs): |
| 381 |
okay = False |
| 382 |
|
| 383 |
except Exception as e: |
| 384 |
sys.stderr.write("%s\n" % e) |
| 385 |
okay = False |
| 386 |
|
| 387 |
|
| 388 |
if not check_config_against_machine_paths(config_data, machine_paths): |
| 389 |
okay = False |
| 390 |
|
| 391 |
for entry in dirs.keys(): |
| 392 |
if not check_dir(entry): |
| 393 |
sys.stderr.write("WHENCE: subdir %s failed the checks\n" % entry) |
| 394 |
okay = False |
| 395 |
|
| 396 |
return 0 if okay else 1 |
| 397 |
|
| 398 |
if __name__ == "__main__": |
| 399 |
sys.exit(main()) |