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