|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Automates the release checklist item in `createrelease`: |
| 4 | +# |
| 5 | +# # Check every isPremiumEnabled call: TODO write helper script |
| 6 | +# # - every id should be in --errorlist |
| 7 | +# # - premiumaddon: check coverage.py |
| 8 | +# |
| 9 | +# Two checks are performed: |
| 10 | +# |
| 11 | +# 1. Every id passed to isPremiumEnabled("...") in lib/ must be a real, |
| 12 | +# known Cppcheck error id (i.e. it must show up in `cppcheck |
| 13 | +# --errorlist`). Otherwise the premium check can never actually be |
| 14 | +# turned on/off since its id does not exist. |
| 15 | +# |
| 16 | +# 2. Every Cppcheck id referenced by the premium addon's compliance |
| 17 | +# mapping tables (MISRA/CERT/AUTOSAR/CWE -> Cppcheck id, generated by |
| 18 | +# `coverage.py --id`) must also show up in `cppcheck --errorlist`. |
| 19 | +# Otherwise the mapping refers to an id that has been renamed or |
| 20 | +# removed. |
| 21 | +# |
| 22 | +# A warning is printed for every id that fails a check; exit status is 1 |
| 23 | +# if any warning was printed, 0 otherwise. |
| 24 | +# |
| 25 | +# Usage: |
| 26 | +# tools/release-check-premium-ids.py [path-to-addon/coverage] |
| 27 | +# |
| 28 | +# The addon/coverage directory defaults to ../addon/coverage (relative to |
| 29 | +# this repo, i.e. a checkout of the premium addon repo next to this one). |
| 30 | +# If it can't be found, check 2 is skipped. |
| 31 | + |
| 32 | +import argparse |
| 33 | +import glob |
| 34 | +import os |
| 35 | +import re |
| 36 | +import subprocess |
| 37 | +import sys |
| 38 | + |
| 39 | +REPO_DIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 40 | +CPPCHECK_BIN = os.path.join(REPO_DIR, 'cppcheck') |
| 41 | + |
| 42 | +ID_RE = re.compile(r' id="([^"]*)"') |
| 43 | +IS_PREMIUM_ENABLED_RE = re.compile(r'isPremiumEnabled\s*\(\s*"([^"]*)"') |
| 44 | + |
| 45 | +warning_count = 0 |
| 46 | + |
| 47 | + |
| 48 | +def warn(message): |
| 49 | + global warning_count |
| 50 | + warning_count += 1 |
| 51 | + print('warning: ' + message, file=sys.stderr) |
| 52 | + |
| 53 | + |
| 54 | +def get_errorlist_ids(): |
| 55 | + out = subprocess.check_output([CPPCHECK_BIN, '--errorlist'], universal_newlines=True) |
| 56 | + return set(ID_RE.findall(out)) |
| 57 | + |
| 58 | + |
| 59 | +def get_is_premium_enabled_ids(): |
| 60 | + ids = set() |
| 61 | + for filename in glob.glob(os.path.join(REPO_DIR, 'lib', '*.cpp')) + glob.glob(os.path.join(REPO_DIR, 'lib', '*.h')): |
| 62 | + with open(filename, 'rt', encoding='utf-8') as f: |
| 63 | + ids.update(IS_PREMIUM_ENABLED_RE.findall(f.read())) |
| 64 | + return ids |
| 65 | + |
| 66 | + |
| 67 | +def get_addon_coverage_ids(addon_coverage_dir): |
| 68 | + coverage_py = os.path.join(addon_coverage_dir, 'coverage.py') |
| 69 | + if not os.path.isfile(coverage_py): |
| 70 | + print(f"(skipped premium addon coverage.py check: '{coverage_py}' not found)", file=sys.stderr) |
| 71 | + return None |
| 72 | + |
| 73 | + ids_files = glob.glob(os.path.join(addon_coverage_dir, 'ids-*.txt')) |
| 74 | + for f in ids_files: |
| 75 | + os.remove(f) |
| 76 | + |
| 77 | + env = dict(os.environ, CPPCHECK_REPO=REPO_DIR) |
| 78 | + subprocess.check_call(['python3', 'coverage.py', '--id'], cwd=addon_coverage_dir, env=env, stdout=subprocess.DEVNULL) |
| 79 | + |
| 80 | + ids = set() |
| 81 | + ids_files = glob.glob(os.path.join(addon_coverage_dir, 'ids-*.txt')) |
| 82 | + for filename in ids_files: |
| 83 | + with open(filename, 'rt', encoding='utf-8') as f: |
| 84 | + ids.update(line.strip() for line in f if line.strip()) |
| 85 | + os.remove(filename) |
| 86 | + return ids |
| 87 | + |
| 88 | + |
| 89 | +def main(): |
| 90 | + parser = argparse.ArgumentParser(description='Check that isPremiumEnabled() ids and premium addon coverage ids are known Cppcheck error ids.') |
| 91 | + parser.add_argument('addon_coverage_dir', nargs='?', default=os.path.join(REPO_DIR, '..', 'addon', 'coverage'), |
| 92 | + help='path to the premium addon coverage/ directory (default: ../addon/coverage)') |
| 93 | + args = parser.parse_args() |
| 94 | + |
| 95 | + if not os.access(CPPCHECK_BIN, os.X_OK): |
| 96 | + print(f"error: '{CPPCHECK_BIN}' not found or not executable, build it first (make -j$(nproc))", file=sys.stderr) |
| 97 | + return 1 |
| 98 | + |
| 99 | + errorlist_ids = get_errorlist_ids() |
| 100 | + |
| 101 | + for id_ in sorted(get_is_premium_enabled_ids() - errorlist_ids): |
| 102 | + warn(f'isPremiumEnabled("{id_}") but there is no such id in --errorlist') |
| 103 | + |
| 104 | + addon_coverage_dir = os.path.realpath(args.addon_coverage_dir) |
| 105 | + addon_ids = get_addon_coverage_ids(addon_coverage_dir) |
| 106 | + if addon_ids is not None: |
| 107 | + for id_ in sorted(addon_ids - errorlist_ids): |
| 108 | + warn(f'premium addon coverage.py references id "{id_}" but there is no such id in --errorlist') |
| 109 | + |
| 110 | + return 1 if warning_count else 0 |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == '__main__': |
| 114 | + sys.exit(main()) |
0 commit comments