|
| 1 | +import getopt |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +# to query CVE list from Lineage tracker |
| 6 | +import json |
| 7 | +import requests |
| 8 | + |
| 9 | + |
| 10 | +def parse_patches_from_tracker(): |
| 11 | + |
| 12 | + patches = [] |
| 13 | + URL = "https://cve.lineageos.org/api/cves" |
| 14 | + |
| 15 | + response = requests.get(URL) |
| 16 | + |
| 17 | + for key in response.json(): |
| 18 | + patches.append(key) |
| 19 | + |
| 20 | + return patches |
| 21 | + |
| 22 | + |
| 23 | +def find_missing_patches(patches, patches_dir): |
| 24 | + |
| 25 | + missing_patches = {} |
| 26 | + |
| 27 | + subdirs = [node for node in os.listdir(patches_dir) |
| 28 | + if os.path.isdir(os.path.join(patches_dir, node))] |
| 29 | + |
| 30 | + for subdir in subdirs: |
| 31 | + missing_patches[subdir] = list() |
| 32 | + for patch in patches: |
| 33 | + patch_path = os.path.join(patches_dir, subdir, patch) |
| 34 | + if not os.path.isfile(patch_path): |
| 35 | + missing_patches[subdir].append(patch) |
| 36 | + |
| 37 | + return missing_patches |
| 38 | + |
| 39 | + |
| 40 | +""" |
| 41 | +Print usage information about this program. |
| 42 | +""" |
| 43 | + |
| 44 | + |
| 45 | +def print_usage(): |
| 46 | + |
| 47 | + print("usage: check_patches.py <OPTIONS> patches_dir") |
| 48 | + |
| 49 | + |
| 50 | +def main(): |
| 51 | + |
| 52 | + try: |
| 53 | + opts, args = getopt.getopt(sys.argv[1:], "h", ["help"]) |
| 54 | + except getopt.GetoptError as err: |
| 55 | + # print help information and exit |
| 56 | + print(str(err)) |
| 57 | + print_usage() |
| 58 | + sys.exit(2) |
| 59 | + |
| 60 | + # check for required args |
| 61 | + if len(sys.argv) != 2: |
| 62 | + print("[E] Invalid number of args (required: 2, found: " |
| 63 | + + str(len(sys.argv)) + ")!") |
| 64 | + print_usage() |
| 65 | + sys.exit(2) |
| 66 | + |
| 67 | + # directory containing the git patchfiles |
| 68 | + patches_dir = sys.argv[-1] |
| 69 | + |
| 70 | + for o, a in opts: |
| 71 | + if o in ("-h", "--help"): |
| 72 | + print_usage() |
| 73 | + sys.exit() |
| 74 | + else: |
| 75 | + print("[E] unhandled option: " + o) |
| 76 | + sys.exit(2) |
| 77 | + |
| 78 | + if not patches_dir or not os.path.isdir(patches_dir): |
| 79 | + print("[E] invalid patchfile directory: " + patches_dir) |
| 80 | + return |
| 81 | + |
| 82 | + # query CVE list from Lineage tracker |
| 83 | + patches = parse_patches_from_tracker() |
| 84 | + missing_patches = find_missing_patches(patches, patches_dir) |
| 85 | + |
| 86 | + print("missing patchfiles:\n") |
| 87 | + for subdir in missing_patches: |
| 88 | + print(subdir) |
| 89 | + for patch in missing_patches[subdir]: |
| 90 | + print("\t" + patch) |
| 91 | + print("") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments