summaryrefslogtreecommitdiff
path: root/util/zephyr_check_compliance.py
blob: 24fea8594e30b091e2613992f99aaf397836a52c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env vpython3

# Copyright 2023 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Check a single commit using the Zephyr check_compliance.py script."""

# [VPYTHON:BEGIN]
# python_version: "3.8"
# wheel: <
#   name: "infra/python/wheels/junitparser-py2_py3"
#   version: "version:2.8.0"
# >
# wheel: <
#   name: "infra/python/wheels/future-py2_py3"
#   version: "version:0.18.2"
# >
# wheel: <
#   name: "infra/python/wheels/python-magic-py2_py3"
#   version: "version:0.4.24"
# >
# wheel: <
#   name: "infra/python/wheels/pyyaml-py3"
#   version: "version:5.3.1"
# >
# wheel: <
#   name: "infra/python/wheels/yamllint-py3"
#   version: "version:1.29.0"
# >
# wheel: <
#   name: "infra/python/wheels/pathspec-py3"
#   version: "version:0.9.0"
# >
# wheel: <
#   name: "infra/python/wheels/lxml/${vpython_platform}"
#   version: "version:4.6.3"
# >
# [VPYTHON:END]

import argparse
import os
import pathlib
import site
import sys


EC_BASE = pathlib.Path(__file__).parent.parent

if "ZEPHYR_BASE" in os.environ:
    ZEPHYR_BASE = pathlib.Path(os.environ.get("ZEPHYR_BASE"))
else:
    ZEPHYR_BASE = pathlib.Path(
        EC_BASE.resolve().parent.parent / "third_party" / "zephyr" / "main"
    )

site.addsitedir(ZEPHYR_BASE / "scripts" / "ci")
# pylint:disable=import-error,wrong-import-position
import check_compliance


# pylint:enable=import-error,wrong-import-position


# Fake ref used by "pre-upload.py --pre-submit"
PRE_SUBMIT_REF = "pre-submit"


def _parse_args(argv):
    parser = argparse.ArgumentParser(description=__doc__)

    parser.add_argument(
        "commit",
        help="Git commit to be checked, hash or reference.",
    )

    return parser.parse_args(argv)


def _patch_get_files():
    """Patch compliance get_files() to exclude non zephyr files."""
    original_get_files = check_compliance.get_files

    def patched_get_files(**kwargs):
        out = []
        for file in original_get_files(**kwargs):
            if file.startswith("zephyr/"):
                out.append(file)
        return out

    check_compliance.get_files = patched_get_files


def _changed_files(commit_range):
    check_compliance.COMMIT_RANGE = commit_range
    check_compliance.GIT_TOP = EC_BASE

    files = check_compliance.get_files(filter="d")
    if len(files) > 0:
        return True

    return False


def main(argv):
    """Main function"""
    args = _parse_args(argv)

    if args.commit == PRE_SUBMIT_REF:
        # Skip if there's no actual commit
        return

    commit_range = f"{args.commit}~1..{args.commit}"

    _patch_get_files()

    if not _changed_files(commit_range):
        # Exit early if nothing changed
        return

    # TODO: also enable DevicetreeBindings
    check_compliance.main(
        [
            "--output=",
            "--no-case-output",
            "-m",
            "YAMLLint",
            "-c",
            commit_range,
        ]
    )
    # Never returns, check_compliance.main() calls sys.exit()


if __name__ == "__main__":
    main(sys.argv[1:])