Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 3 | """ |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 4 | crcchecker is a tool to used to enforce that .api messages do not change. |
| 5 | API files with a semantic version < 1.0.0 are ignored. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 6 | """ |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 7 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 8 | import sys |
| 9 | import os |
| 10 | import json |
| 11 | import argparse |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 12 | import re |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 13 | from subprocess import run, PIPE, check_output, CalledProcessError |
| 14 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 15 | # pylint: disable=subprocess-run-check |
| 16 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 17 | ROOTDIR = os.path.dirname(os.path.realpath(__file__)) + "/../.." |
| 18 | APIGENBIN = f"{ROOTDIR}/src/tools/vppapigen/vppapigen.py" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 19 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 20 | |
| 21 | def crc_from_apigen(revision, filename): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 22 | """Runs vppapigen with crc plugin returning a JSON object with CRCs for |
| 23 | all APIs in filename""" |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 24 | if not revision and not os.path.isfile(filename): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 25 | print(f"skipping: {filename}", file=sys.stderr) |
Dave Barach | 592dbd0 | 2021-03-11 15:12:29 -0500 | [diff] [blame] | 26 | # Return <class 'set'> instead of <class 'dict'> |
| 27 | return {-1} |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 28 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 29 | if revision: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 30 | apigen = ( |
| 31 | f"{APIGENBIN} --git-revision {revision} --includedir src " |
| 32 | f"--input {filename} CRC" |
| 33 | ) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 34 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 35 | apigen = f"{APIGENBIN} --includedir src --input {filename} CRC" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 36 | returncode = run(apigen.split(), stdout=PIPE, stderr=PIPE) |
| 37 | if returncode.returncode == 2: # No such file |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 38 | print(f"skipping: {revision}:{filename} {returncode}", file=sys.stderr) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 39 | return {} |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 40 | if returncode.returncode != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 41 | print( |
| 42 | f"vppapigen failed for {revision}:{filename} with " |
| 43 | "command\n {apigen}\n error: {rv}", |
| 44 | returncode.stderr.decode("ascii"), |
| 45 | file=sys.stderr, |
| 46 | ) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 47 | sys.exit(-2) |
| 48 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 49 | return json.loads(returncode.stdout) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 50 | |
| 51 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 52 | def dict_compare(dict1, dict2): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 53 | """Compare two dictionaries returning added, removed, modified |
| 54 | and equal entries""" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 55 | d1_keys = set(dict1.keys()) |
| 56 | d2_keys = set(dict2.keys()) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 57 | intersect_keys = d1_keys.intersection(d2_keys) |
| 58 | added = d1_keys - d2_keys |
| 59 | removed = d2_keys - d1_keys |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 60 | modified = { |
| 61 | o: (dict1[o], dict2[o]) |
| 62 | for o in intersect_keys |
| 63 | if dict1[o]["crc"] != dict2[o]["crc"] |
| 64 | } |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 65 | same = set(o for o in intersect_keys if dict1[o] == dict2[o]) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 66 | return added, removed, modified, same |
| 67 | |
| 68 | |
| 69 | def filelist_from_git_ls(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 70 | """Returns a list of all api files in the git repository""" |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 71 | filelist = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 72 | git_ls = "git ls-files *.api" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 73 | returncode = run(git_ls.split(), stdout=PIPE, stderr=PIPE) |
| 74 | if returncode.returncode != 0: |
| 75 | sys.exit(returncode.returncode) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 76 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 77 | for line in returncode.stdout.decode("ascii").split("\n"): |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 78 | if line: |
| 79 | filelist.append(line) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 80 | return filelist |
| 81 | |
| 82 | |
| 83 | def is_uncommitted_changes(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 84 | """Returns true if there are uncommitted changes in the repo""" |
| 85 | git_status = "git status --porcelain -uno" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 86 | returncode = run(git_status.split(), stdout=PIPE, stderr=PIPE) |
| 87 | if returncode.returncode != 0: |
| 88 | sys.exit(returncode.returncode) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 89 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 90 | if returncode.stdout: |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 91 | return True |
| 92 | return False |
| 93 | |
| 94 | |
| 95 | def filelist_from_git_grep(filename): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 96 | """Returns a list of api files that this <filename> api files imports.""" |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 97 | filelist = [] |
| 98 | try: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 99 | returncode = check_output( |
| 100 | f'git grep -e "import .*{filename}"' " -- *.api", shell=True |
| 101 | ) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 102 | except CalledProcessError: |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 103 | return [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 104 | for line in returncode.decode("ascii").split("\n"): |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 105 | if line: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 106 | filename, _ = line.split(":") |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 107 | filelist.append(filename) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 108 | return filelist |
| 109 | |
| 110 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 111 | def filelist_from_patchset(pattern): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 112 | """Returns list of api files in changeset and the list of api |
| 113 | files they import.""" |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 114 | filelist = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 115 | git_cmd = ( |
| 116 | "((git diff HEAD~1.. --name-only;git ls-files -m) | " |
| 117 | 'sort -u | grep "\\.api$")' |
| 118 | ) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 119 | try: |
| 120 | res = check_output(git_cmd, shell=True) |
| 121 | except CalledProcessError: |
| 122 | return [] |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 123 | |
| 124 | # Check for dependencies (imports) |
| 125 | imported_files = [] |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 126 | for line in res.decode("ascii").split("\n"): |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 127 | if not line: |
| 128 | continue |
| 129 | if not re.search(pattern, line): |
| 130 | continue |
| 131 | filelist.append(line) |
| 132 | imported_files.extend(filelist_from_git_grep(os.path.basename(line))) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 133 | |
| 134 | filelist.extend(imported_files) |
| 135 | return set(filelist) |
| 136 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 137 | |
| 138 | def is_deprecated(message): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 139 | """Given a message, return True if message is deprecated""" |
| 140 | if "options" in message: |
| 141 | if "deprecated" in message["options"]: |
Andrew Yourtchenko | 6a3d4cc | 2020-09-22 15:11:51 +0000 | [diff] [blame] | 142 | return True |
| 143 | # recognize the deprecated format |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 144 | if ( |
| 145 | "status" in message["options"] |
| 146 | and message["options"]["status"] == "deprecated" |
| 147 | ): |
Andrew Yourtchenko | 6a3d4cc | 2020-09-22 15:11:51 +0000 | [diff] [blame] | 148 | print("WARNING: please use 'option deprecated;'") |
| 149 | return True |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 150 | return False |
| 151 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 152 | |
| 153 | def is_in_progress(message): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 154 | """Given a message, return True if message is marked as in_progress""" |
| 155 | if "options" in message: |
| 156 | if "in_progress" in message["options"]: |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 157 | return True |
Andrew Yourtchenko | 6a3d4cc | 2020-09-22 15:11:51 +0000 | [diff] [blame] | 158 | # recognize the deprecated format |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 159 | if ( |
| 160 | "status" in message["options"] |
| 161 | and message["options"]["status"] == "in_progress" |
| 162 | ): |
Andrew Yourtchenko | 6a3d4cc | 2020-09-22 15:11:51 +0000 | [diff] [blame] | 163 | print("WARNING: please use 'option in_progress;'") |
| 164 | return True |
| 165 | return False |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 166 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 167 | |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 168 | def report(new, old): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 169 | """Given a dictionary of new crcs and old crcs, print all the |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 170 | added, removed, modified, in-progress, deprecated messages. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 171 | Return the number of backwards incompatible changes made.""" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 172 | |
| 173 | # pylint: disable=too-many-branches |
| 174 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 175 | new.pop("_version", None) |
| 176 | old.pop("_version", None) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 177 | added, removed, modified, _ = dict_compare(new, old) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 178 | backwards_incompatible = 0 |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 179 | |
Andrew Yourtchenko | 8b0cd69 | 2020-09-16 09:48:59 +0000 | [diff] [blame] | 180 | # print the full list of in-progress messages |
| 181 | # they should eventually either disappear of become supported |
| 182 | for k in new.keys(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 183 | newversion = int(new[k]["version"]) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 184 | if newversion == 0 or is_in_progress(new[k]): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 185 | print(f"in-progress: {k}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 186 | for k in added: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 187 | print(f"added: {k}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 188 | for k in removed: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 189 | oldversion = int(old[k]["version"]) |
| 190 | if oldversion > 0 and not is_deprecated(old[k]) and not is_in_progress(old[k]): |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 191 | backwards_incompatible += 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 192 | print(f"removed: ** {k}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 193 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 194 | print(f"removed: {k}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 195 | for k in modified.keys(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 196 | oldversion = int(old[k]["version"]) |
| 197 | newversion = int(new[k]["version"]) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 198 | if oldversion > 0 and not is_in_progress(old[k]): |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 199 | backwards_incompatible += 1 |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 200 | print(f"modified: ** {k}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 201 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 202 | print(f"modified: {k}") |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 203 | |
| 204 | # check which messages are still there but were marked for deprecation |
| 205 | for k in new.keys(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 206 | newversion = int(new[k]["version"]) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 207 | if newversion > 0 and is_deprecated(new[k]): |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 208 | if k in old: |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 209 | if not is_deprecated(old[k]): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 210 | print(f"deprecated: {k}") |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 211 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 212 | print(f"added+deprecated: {k}") |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 213 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 214 | return backwards_incompatible |
| 215 | |
| 216 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 217 | def check_patchset(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 218 | """Compare the changes to API messages in this changeset. |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 219 | Ignores API files with version < 1.0.0. |
| 220 | Only considers API files located under the src directory in the repo. |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 221 | """ |
| 222 | files = filelist_from_patchset("^src/") |
| 223 | revision = "HEAD~1" |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 224 | |
| 225 | oldcrcs = {} |
| 226 | newcrcs = {} |
| 227 | for filename in files: |
| 228 | # Ignore files that have version < 1.0.0 |
| 229 | _ = crc_from_apigen(None, filename) |
Dave Barach | 592dbd0 | 2021-03-11 15:12:29 -0500 | [diff] [blame] | 230 | # Ignore removed files |
| 231 | if isinstance(_, set) == 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 232 | if isinstance(_, set) == 0 and _["_version"]["major"] == "0": |
Dave Barach | 592dbd0 | 2021-03-11 15:12:29 -0500 | [diff] [blame] | 233 | continue |
| 234 | newcrcs.update(_) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 235 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 236 | oldcrcs.update(crc_from_apigen(revision, filename)) |
| 237 | |
| 238 | backwards_incompatible = report(newcrcs, oldcrcs) |
| 239 | if backwards_incompatible: |
| 240 | # alert on changing production API |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 241 | print( |
| 242 | "crcchecker: Changing production APIs in an incompatible way", |
| 243 | file=sys.stderr, |
| 244 | ) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 245 | sys.exit(-1) |
| 246 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 247 | print("*" * 67) |
| 248 | print("* VPP CHECKAPI SUCCESSFULLY COMPLETED") |
| 249 | print("*" * 67) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 250 | |
| 251 | |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 252 | def main(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 253 | """Main entry point.""" |
| 254 | parser = argparse.ArgumentParser(description="VPP CRC checker.") |
| 255 | parser.add_argument("--git-revision", help="Git revision to compare against") |
| 256 | parser.add_argument( |
| 257 | "--dump-manifest", action="store_true", help="Dump CRC for all messages" |
| 258 | ) |
| 259 | parser.add_argument( |
| 260 | "--check-patchset", |
| 261 | action="store_true", |
| 262 | help="Check patchset for backwards incompatbile changes", |
| 263 | ) |
| 264 | parser.add_argument("files", nargs="*") |
| 265 | parser.add_argument("--diff", help="Files to compare (on filesystem)", nargs=2) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 266 | |
| 267 | args = parser.parse_args() |
| 268 | |
| 269 | if args.diff and args.files: |
| 270 | parser.print_help() |
| 271 | sys.exit(-1) |
| 272 | |
| 273 | # Diff two files |
| 274 | if args.diff: |
| 275 | oldcrcs = crc_from_apigen(None, args.diff[0]) |
| 276 | newcrcs = crc_from_apigen(None, args.diff[1]) |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 277 | backwards_incompatible = report(newcrcs, oldcrcs) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 278 | sys.exit(0) |
| 279 | |
| 280 | # Dump CRC for messages in given files / revision |
| 281 | if args.dump_manifest: |
| 282 | files = args.files if args.files else filelist_from_git_ls() |
| 283 | crcs = {} |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 284 | for filename in files: |
| 285 | crcs.update(crc_from_apigen(args.git_revision, filename)) |
| 286 | for k, value in crcs.items(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 287 | print(f"{k}: {value}") |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 288 | sys.exit(0) |
| 289 | |
| 290 | # Find changes between current patchset and given revision (previous) |
| 291 | if args.check_patchset: |
| 292 | if args.git_revision: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 293 | print("Argument git-revision ignored", file=sys.stderr) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 294 | # Check there are no uncomitted changes |
| 295 | if is_uncommitted_changes(): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 296 | print("Please stash or commit changes in workspace", file=sys.stderr) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 297 | sys.exit(-1) |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 298 | check_patchset() |
| 299 | sys.exit(0) |
| 300 | |
| 301 | # Find changes between current workspace and revision |
| 302 | # Find changes between a given file and a revision |
| 303 | files = args.files if args.files else filelist_from_git_ls() |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 304 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 305 | revision = args.git_revision if args.git_revision else "HEAD~1" |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 306 | |
| 307 | oldcrcs = {} |
| 308 | newcrcs = {} |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 309 | for file in files: |
| 310 | newcrcs.update(crc_from_apigen(None, file)) |
| 311 | oldcrcs.update(crc_from_apigen(revision, file)) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 312 | |
Andrew Yourtchenko | 62bd50d | 2020-09-11 17:40:52 +0000 | [diff] [blame] | 313 | backwards_incompatible = report(newcrcs, oldcrcs) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 314 | |
| 315 | if args.check_patchset: |
| 316 | if backwards_incompatible: |
| 317 | # alert on changing production API |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 318 | print( |
| 319 | "crcchecker: Changing production APIs in an incompatible way", |
| 320 | file=sys.stderr, |
| 321 | ) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 322 | sys.exit(-1) |
| 323 | else: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 324 | print("*" * 67) |
| 325 | print("* VPP CHECKAPI SUCCESSFULLY COMPLETED") |
| 326 | print("*" * 67) |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 327 | |
Ole Troan | ab9f573 | 2020-12-15 10:19:25 +0100 | [diff] [blame] | 328 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame^] | 329 | if __name__ == "__main__": |
Ole Troan | 5c318c7 | 2020-05-05 12:23:47 +0200 | [diff] [blame] | 330 | main() |