7 from subprocess import run, PIPE, check_output, CalledProcessError
9 rootdir = os.path.dirname(os.path.realpath(__file__)) + '/../..'
11 def crc_from_apigen(revision, filename):
12 if not revision and not os.path.isfile(filename):
13 print(f'skipping: {filename}', file=sys.stderr)
15 apigen_bin = f'{rootdir}/src/tools/vppapigen/vppapigen.py'
17 apigen = (f'{apigen_bin} --git-revision {revision} --includedir src '
18 f'--input {filename} CRC')
20 apigen = (f'{apigen_bin} --includedir src --input {filename} CRC')
21 rv = run(apigen.split(), stdout=PIPE, stderr=PIPE)
22 if rv.returncode == 2: # No such file
23 print(f'skipping: {revision}:{filename} {rv}', file=sys.stderr)
25 if rv.returncode != 0:
26 print(f'vppapigen failed for {revision}:{filename} with command\n {apigen}\n error: {rv}',
27 rv.stderr.decode('ascii'), file=sys.stderr)
30 return json.loads(rv.stdout)
33 def dict_compare(d1, d2):
34 d1_keys = set(d1.keys())
35 d2_keys = set(d2.keys())
36 intersect_keys = d1_keys.intersection(d2_keys)
37 added = d1_keys - d2_keys
38 removed = d2_keys - d1_keys
39 modified = {o: (d1[o], d2[o]) for o in intersect_keys if d1[o]['crc'] != d2[o]['crc']}
40 same = set(o for o in intersect_keys if d1[o] == d2[o])
41 return added, removed, modified, same
44 def filelist_from_git_ls():
46 git_ls = 'git ls-files *.api'
47 rv = run(git_ls.split(), stdout=PIPE, stderr=PIPE)
48 if rv.returncode != 0:
49 sys.exit(rv.returncode)
51 for l in rv.stdout.decode('ascii').split('\n'):
57 def is_uncommitted_changes():
58 git_status = 'git status --porcelain -uno'
59 rv = run(git_status.split(), stdout=PIPE, stderr=PIPE)
60 if rv.returncode != 0:
61 sys.exit(rv.returncode)
68 def filelist_from_git_grep(filename):
71 rv = check_output(f'git grep -e "import .*{filename}" -- *.api', shell=True)
72 except CalledProcessError as err:
74 print('RV', err.returncode)
75 for l in rv.decode('ascii').split('\n'):
82 def filelist_from_patchset():
84 git_cmd = '((git diff HEAD~1.. --name-only;git ls-files -m) | sort -u)'
85 rv = check_output(git_cmd, shell=True)
86 for l in rv.decode('ascii').split('\n'):
87 if len(l) and os.path.splitext(l)[1] == '.api':
90 # Check for dependencies (imports)
93 imported_files.extend(filelist_from_git_grep(os.path.basename(f)))
95 filelist.extend(imported_files)
98 def is_deprecated(d, k):
99 if 'options' in d[k] and 'deprecated' in d[k]['options']:
103 def is_in_progress(d, k):
105 if d[k]['options']['status'] == 'in_progress':
110 def report(new, old):
111 added, removed, modified, same = dict_compare(new, old)
112 backwards_incompatible = 0
113 # print the full list of in-progress messages
114 # they should eventually either disappear of become supported
116 newversion = int(new[k]['version'])
117 if newversion == 0 or is_in_progress(new, k):
118 print(f'in-progress: {k}')
122 oldversion = int(old[k]['version'])
123 if oldversion > 0 and not is_deprecated(old, k) and not is_in_progress(old, k):
124 backwards_incompatible += 1
125 print(f'removed: ** {k}')
127 print(f'removed: {k}')
128 for k in modified.keys():
129 oldversion = int(old[k]['version'])
130 newversion = int(new[k]['version'])
131 if oldversion > 0 and not is_in_progress(old, k):
132 backwards_incompatible += 1
133 print(f'modified: ** {k}')
135 print(f'modified: {k}')
137 # check which messages are still there but were marked for deprecation
139 newversion = int(new[k]['version'])
140 if newversion > 0 and is_deprecated(new, k):
142 if not is_deprecated(old, k):
143 print(f'deprecated: {k}')
145 print(f'added+deprecated: {k}')
147 return backwards_incompatible
151 parser = argparse.ArgumentParser(description='VPP CRC checker.')
152 parser.add_argument('--git-revision',
153 help='Git revision to compare against')
154 parser.add_argument('--dump-manifest', action='store_true',
155 help='Dump CRC for all messages')
156 parser.add_argument('--check-patchset', action='store_true',
157 help='Dump CRC for all messages')
158 parser.add_argument('files', nargs='*')
159 parser.add_argument('--diff', help='Files to compare (on filesystem)', nargs=2)
161 args = parser.parse_args()
163 if args.diff and args.files:
169 oldcrcs = crc_from_apigen(None, args.diff[0])
170 newcrcs = crc_from_apigen(None, args.diff[1])
171 backwards_incompatible = report(newcrcs, oldcrcs)
174 # Dump CRC for messages in given files / revision
175 if args.dump_manifest:
176 files = args.files if args.files else filelist_from_git_ls()
179 crcs.update(crc_from_apigen(args.git_revision, f))
180 for k, v in crcs.items():
184 # Find changes between current patchset and given revision (previous)
185 if args.check_patchset:
186 if args.git_revision:
187 print('Argument git-revision ignored', file=sys.stderr)
188 # Check there are no uncomitted changes
189 if is_uncommitted_changes():
190 print('Please stash or commit changes in workspace', file=sys.stderr)
192 files = filelist_from_patchset()
194 # Find changes between current workspace and revision
195 # Find changes between a given file and a revision
196 files = args.files if args.files else filelist_from_git_ls()
198 revision = args.git_revision if args.git_revision else 'HEAD~1'
203 newcrcs.update(crc_from_apigen(None, f))
204 oldcrcs.update(crc_from_apigen(revision, f))
206 backwards_incompatible = report(newcrcs, oldcrcs)
208 if args.check_patchset:
209 if backwards_incompatible:
210 # alert on changing production API
211 print("crcchecker: Changing production APIs in an incompatible way", file=sys.stderr)
215 print('* VPP CHECKAPI SUCCESSFULLY COMPLETED')
218 if __name__ == '__main__':