vppapigen: crcchecker: report in-progress messages
[vpp.git] / extras / scripts / crcchecker.py
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 import json
6 import argparse
7 from subprocess import run, PIPE, check_output, CalledProcessError
8
9 rootdir = os.path.dirname(os.path.realpath(__file__)) + '/../..'
10
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)
14         return {}
15     apigen_bin = f'{rootdir}/src/tools/vppapigen/vppapigen.py'
16     if revision:
17         apigen = (f'{apigen_bin} --git-revision {revision} --includedir src '
18                   f'--input {filename} CRC')
19     else:
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)
24         return {}
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)
28         sys.exit(-2)
29
30     return json.loads(rv.stdout)
31
32
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
42
43
44 def filelist_from_git_ls():
45     filelist = []
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)
50
51     for l in rv.stdout.decode('ascii').split('\n'):
52         if len(l):
53             filelist.append(l)
54     return filelist
55
56
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)
62
63     if len(rv.stdout):
64         return True
65     return False
66
67
68 def filelist_from_git_grep(filename):
69     filelist = []
70     try:
71         rv = check_output(f'git grep -e "import .*{filename}" -- *.api', shell=True)
72     except CalledProcessError as err:
73         return []
74         print('RV', err.returncode)
75     for l in rv.decode('ascii').split('\n'):
76         if l:
77             f, p = l.split(':')
78             filelist.append(f)
79     return filelist
80
81
82 def filelist_from_patchset():
83     filelist = []
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':
88             filelist.append(l)
89
90     # Check for dependencies (imports)
91     imported_files = []
92     for f in filelist:
93         imported_files.extend(filelist_from_git_grep(os.path.basename(f)))
94
95     filelist.extend(imported_files)
96     return set(filelist)
97
98 def is_deprecated(d, k):
99     if 'options' in d[k] and 'deprecated' in d[k]['options']:
100         return True
101     return False
102
103 def is_in_progress(d, k):
104     try:
105         if d[k]['options']['status'] == 'in_progress':
106             return True
107     except:
108         return False
109
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
115     for k in new.keys():
116         newversion = int(new[k]['version'])
117         if newversion == 0 or is_in_progress(new, k):
118             print(f'in-progress: {k}')
119     for k in added:
120         print(f'added: {k}')
121     for k in removed:
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}')
126         else:
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}')
134         else:
135             print(f'modified: {k}')
136
137     # check which messages are still there but were marked for deprecation
138     for k in new.keys():
139         newversion = int(new[k]['version'])
140         if newversion > 0 and is_deprecated(new, k):
141             if k in old:
142                 if not is_deprecated(old, k):
143                     print(f'deprecated: {k}')
144             else:
145                 print(f'added+deprecated: {k}')
146
147     return backwards_incompatible
148
149
150 def main():
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)
160
161     args = parser.parse_args()
162
163     if args.diff and args.files:
164         parser.print_help()
165         sys.exit(-1)
166
167     # Diff two files
168     if args.diff:
169         oldcrcs = crc_from_apigen(None, args.diff[0])
170         newcrcs = crc_from_apigen(None, args.diff[1])
171         backwards_incompatible = report(newcrcs, oldcrcs)
172         sys.exit(0)
173
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()
177         crcs = {}
178         for f in files:
179             crcs.update(crc_from_apigen(args.git_revision, f))
180         for k, v in crcs.items():
181             print(f'{k}: {v}')
182         sys.exit(0)
183
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)
191             sys.exit(-1)
192         files = filelist_from_patchset()
193     else:
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()
197
198     revision = args.git_revision if args.git_revision else 'HEAD~1'
199
200     oldcrcs = {}
201     newcrcs = {}
202     for f in files:
203         newcrcs.update(crc_from_apigen(None, f))
204         oldcrcs.update(crc_from_apigen(revision, f))
205
206     backwards_incompatible = report(newcrcs, oldcrcs)
207
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)
212             sys.exit(-1)
213         else:
214             print('*' * 67)
215             print('* VPP CHECKAPI SUCCESSFULLY COMPLETED')
216             print('*' * 67)
217
218 if __name__ == '__main__':
219     main()