Flag day: requirements for candidates
[csit.git] / resources / tools / integrated / check_crc.py
1 # Copyright (c) 2020 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """
15 Script that fails if .api.json files downloaded to a hardcoded place
16 do not match CRC checksusms currently supported by CSIT.
17
18 No executable flag, nor shebang, as most users do not have .api.json
19 files downloaded to the correct place.
20 """
21
22 import os.path as op
23 import sys
24
25 from resources.libraries.python.VppApiCrc import VppApiCrcChecker
26
27
28 def main():
29     """Execute the logic, return the return code.
30
31     From current location, construct path to .api file subtree,
32     initialize and run the CRC checker, print result consequences
33     to stderr, return the return code to return from the script.
34
35     :returns: Return code to return. 0 if OK, 1 if CRC mismatch.
36     :rtype: int
37     """
38
39     # TODO: Read FDIO_VPP_DIR environment variable, or some other input,
40     # instead of using hardcoded relative path?
41
42     api_dir = op.normpath(op.join(
43         op.dirname(op.abspath(__file__)), u"..", u"..", u"..", u"..",
44         u"build-root", u"install-vpp-native", u"vpp", u"share", u"vpp",
45         u"api"
46     ))
47     checker = VppApiCrcChecker(api_dir)
48     try:
49         checker.report_initial_conflicts(report_missing=True)
50     except RuntimeError as err:
51         stderr_lines = [
52             f"{err!r}",
53             u"",
54             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
55             u"",
56             u"VPP CSIT API CHECK FAIL!",
57             u"",
58             u"This means the patch under test has missing messages,",
59             u"or messages with unexpected CRCs compared to what CSIT needs.",
60             u"Either this Change and/or its ancestors were editing .api files,",
61             u"or your chain is not rebased upon a recent enough VPP codebase.",
62             u"",
63             u"In the former case, please consult the following document",
64             u"to see how to make CSIT accept the .api editing change.",
65             u"https://github.com/FDio/csit/blob/master/docs/automating_vpp_api_flag_day.rst"
66             u"",
67             u"For the latter case, please rebase the patch to see",
68             u"if that fixes the problem. If repeated rebases do not help",
69             u"send and email to csit-dev@lists.fd.io asking to investigate.",
70             u"",
71             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
72         ]
73         ret_code = 1
74     else:
75         stderr_lines = [
76             u"",
77             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
78             u"",
79             u"VPP CSIT API CHECK PASS!",
80             u"",
81             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
82         ]
83         ret_code = 0
84     for stderr_line in stderr_lines:
85         print(stderr_line, file=sys.stderr)
86     return ret_code
87
88 if __name__ == u"__main__":
89     sys.exit(main())