Do not execute crc check on import
[csit.git] / resources / tools / integrated / check_crc.py
1 # Copyright (c) 2019 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"Please rebase the patch to see if that fixes the problem.",
64             u"If that fails email csit-dev@lists.fd.io for a new",
65             u"operational branch supporting the api changes.",
66             u"",
67             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
68         ]
69         ret_code = 1
70     else:
71         stderr_lines = [
72             u"",
73             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
74             u"",
75             u"VPP CSIT API CHECK PASS!",
76             u"",
77             u"@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@",
78         ]
79         ret_code = 0
80     for stderr_line in stderr_lines:
81         print(stderr_line, file=sys.stderr)
82     return ret_code
83
84 if __name__ == u"__main__":
85     sys.exit(main())