xref: /aosp_15_r20/external/toolchain-utils/llvm_tools/custom_script_example.py (revision 760c253c1ed00ce9abd48f8546f08516e57485fe)
1#!/usr/bin/env python3
2# Copyright 2019 The ChromiumOS Authors
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""A custom script example that utilizes the .JSON contents of the tryjob."""
7
8import json
9import sys
10
11import update_tryjob_status
12
13
14def main():
15    """Determines the exit code based off of the contents of the .JSON file."""
16
17    # Index 1 in 'sys.argv' is the path to the .JSON file which contains
18    # the contents of the tryjob.
19    #
20    # Format of the tryjob contents:
21    #   {
22    #     "status" : [TRYJOB_STATUS],
23    #     "buildbucket_id" : [BUILDBUCKET_ID],
24    #     "extra_cls" : [A_LIST_OF_EXTRA_CLS_PASSED_TO_TRYJOB],
25    #     "url" : [GERRIT_URL],
26    #     "builder" : [TRYJOB_BUILDER_LIST],
27    #     "rev" : [REVISION],
28    #     "link" : [LINK_TO_TRYJOB],
29    #     "options" : [A_LIST_OF_OPTIONS_PASSED_TO_TRYJOB]
30    #   }
31    abs_path_json_file = sys.argv[1]
32
33    with open(abs_path_json_file, encoding="utf-8") as f:
34        tryjob_contents = json.load(f)
35
36    CUTOFF_PENDING_REVISION = 369416
37
38    SKIP_REVISION_CUTOFF_START = 369420
39    SKIP_REVISION_CUTOFF_END = 369428
40
41    if (
42        tryjob_contents["status"]
43        == update_tryjob_status.TryjobStatus.PENDING.value
44    ):
45        if tryjob_contents["rev"] <= CUTOFF_PENDING_REVISION:
46            # Exit code 0 means to set the tryjob 'status' as 'good'.
47            sys.exit(0)
48
49        # Exit code 124 means to set the tryjob 'status' as 'bad'.
50        sys.exit(124)
51
52    if tryjob_contents["status"] == update_tryjob_status.TryjobStatus.BAD.value:
53        # Need to take a closer look at the contents of the tryjob to then
54        # decide what that tryjob's 'status' value should be.
55        #
56        # Since the exit code is not in the mapping, an exception will occur
57        # which will save the file in the directory of this custom script
58        # example.
59        sys.exit(1)
60
61    if (
62        tryjob_contents["status"]
63        == update_tryjob_status.TryjobStatus.SKIP.value
64    ):
65        # Validate that the 'skip value is really set between the cutoffs.
66        if (
67            SKIP_REVISION_CUTOFF_START
68            < tryjob_contents["rev"]
69            < SKIP_REVISION_CUTOFF_END
70        ):
71            # Exit code 125 means to set the tryjob 'status' as 'skip'.
72            sys.exit(125)
73
74        if tryjob_contents["rev"] >= SKIP_REVISION_CUTOFF_END:
75            sys.exit(124)
76
77
78if __name__ == "__main__":
79    main()
80