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):
100 if 'deprecated' in d[k]['options']:
102 # recognize the deprecated format
103 if 'status' in d[k]['options'] and d[k]['options']['status'] == 'deprecated':
104 print("WARNING: please use 'option deprecated;'")
108 def is_in_progress(d, k):
109 if 'options' in d[k]:
110 if 'in_progress' in d[k]['options']:
112 # recognize the deprecated format
113 if 'status' in d[k]['options'] and d[k]['options']['status'] == 'in_progress':
114 print("WARNING: please use 'option in_progress;'")
118 def report(new, old):
119 added, removed, modified, same = dict_compare(new, old)
120 backwards_incompatible = 0
121 # print the full list of in-progress messages
122 # they should eventually either disappear of become supported
124 newversion = int(new[k]['version'])
125 if newversion == 0 or is_in_progress(new, k):
126 print(f'in-progress: {k}')
130 oldversion = int(old[k]['version'])
131 if oldversion > 0 and not is_deprecated(old, k) and not is_in_progress(old, k):
132 backwards_incompatible += 1
133 print(f'removed: ** {k}')
135 print(f'removed: {k}')
136 for k in modified.keys():
137 oldversion = int(old[k]['version'])
138 newversion = int(new[k]['version'])
139 if oldversion > 0 and not is_in_progress(old, k):
140 backwards_incompatible += 1
141 print(f'modified: ** {k}')
143 print(f'modified: {k}')
145 # check which messages are still there but were marked for deprecation
147 newversion = int(new[k]['version'])
148 if newversion > 0 and is_deprecated(new, k):
150 if not is_deprecated(old, k):
151 print(f'deprecated: {k}')
153 print(f'added+deprecated: {k}')
155 return backwards_incompatible
159 parser = argparse.ArgumentParser(description='VPP CRC checker.')
160 parser.add_argument('--git-revision',
161 help='Git revision to compare against')
162 parser.add_argument('--dump-manifest', action='store_true',
163 help='Dump CRC for all messages')
164 parser.add_argument('--check-patchset', action='store_true',
165 help='Dump CRC for all messages')
166 parser.add_argument('files', nargs='*')
167 parser.add_argument('--diff', help='Files to compare (on filesystem)', nargs=2)
169 args = parser.parse_args()
171 if args.diff and args.files:
177 oldcrcs = crc_from_apigen(None, args.diff[0])
178 newcrcs = crc_from_apigen(None, args.diff[1])
179 backwards_incompatible = report(newcrcs, oldcrcs)
182 # Dump CRC for messages in given files / revision
183 if args.dump_manifest:
184 files = args.files if args.files else filelist_from_git_ls()
187 crcs.update(crc_from_apigen(args.git_revision, f))
188 for k, v in crcs.items():
192 # Find changes between current patchset and given revision (previous)
193 if args.check_patchset:
194 if args.git_revision:
195 print('Argument git-revision ignored', file=sys.stderr)
196 # Check there are no uncomitted changes
197 if is_uncommitted_changes():
198 print('Please stash or commit changes in workspace', file=sys.stderr)
200 files = filelist_from_patchset()
202 # Find changes between current workspace and revision
203 # Find changes between a given file and a revision
204 files = args.files if args.files else filelist_from_git_ls()
206 revision = args.git_revision if args.git_revision else 'HEAD~1'
211 newcrcs.update(crc_from_apigen(None, f))
212 oldcrcs.update(crc_from_apigen(revision, f))
214 backwards_incompatible = report(newcrcs, oldcrcs)
216 if args.check_patchset:
217 if backwards_incompatible:
218 # alert on changing production API
219 print("crcchecker: Changing production APIs in an incompatible way", file=sys.stderr)
223 print('* VPP CHECKAPI SUCCESSFULLY COMPLETED')
226 if __name__ == '__main__':