File: changeset_testlist.py

package info (click to toggle)
uhd 4.8.0.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 183,172 kB
  • sloc: cpp: 279,415; python: 109,850; ansic: 103,348; vhdl: 57,230; tcl: 20,007; xml: 8,581; makefile: 2,863; sh: 2,797; pascal: 230; javascript: 120; csh: 94; asm: 20; perl: 11
file content (296 lines) | stat: -rw-r--r-- 10,560 bytes parent folder | download
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python3
#
# Copyright 2023 Ettus Research, a National Instruments Brand
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
"""Changeset analyzer: Create list of required tests based on changes in a branch.

This script reads changes made on a particular branch compared to
another and computes a list of tests that need to be run to validate this branch.
"""

import argparse
import os
import pathlib
import re
import shutil
import subprocess
import sys

import requests
from ruamel import yaml


def parse_args():
    """Parse and return args."""
    parser = argparse.ArgumentParser(
        description=__doc__,
    )
    parser.add_argument(
        "--source-branch", help="Source branch off diff. Defaults to the current branch."
    )
    parser.add_argument(
        "--target-branch", default="master", help="Target branch off diff. Defaults to 'master'."
    )
    parser.add_argument(
        "--repo-path",
        default=".",
        help="Path to the UHD git repository. Defaults to the current directory.",
    )
    parser.add_argument("--rule-file", help="Path to rules file.")
    parser.add_argument("--set-azdo-var", help="Generate output to set an AzDO variable")
    parser.add_argument("--list-tests", action="store_true", help="Show the generated test-list.")
    parser.add_argument(
        "--include-target",
        action="store_true",
        help="Include changes that originate from the target branch.",
    )
    parser.add_argument(
        "--github-label-api-endpoint",
        help="Endpoint to use for GitHub API requests read back labels.",
    )
    parser.add_argument("--github-token", help="GitHub token to use for API requests.")
    parser.add_argument("-v", "--verbose", action="store_true", help="Verbose output")
    return parser.parse_args()


def check_changeset_content(file, **kwargs):
    """Check changeset content for code and/or comments changes

    For example, if a .cpp file only has a comment change, we can use this to
    remove it from the "changed files" list. This is useful when we only want to
    only include tests if the underlying code was actually changed, and not just
    a comment (which cannot change the outcome of a hardware test).

    The values of the include_content argument have the following meaning:
    - 'code': at least one line with code changed
    - 'code-only': all changed lines must be code changes
    - 'comments': at least one line with comments changed
    - 'comments-only': all changed lines must be comment changes
    - 'code-and-comments': either code or comments changes (always True)

    Arguments:
    file: List of files that have been changed.
    repo_path: Path to the UHD repository.
    git_cmd: Path to the git command.
    include_content: allowed values are: code, code-only, comments,
        comments-only, code-and-comments

    Returns: True if the file should be included, False otherwise.
    """

    def identify_comment_line_cpp(line):
        """Identify if a line is a comment in a C++ file."""
        line = line.strip()
        return line.startswith("//") or line.startswith("/*") or line.startswith("*")

    def identify_comment_line_py(line):
        """Identify if a line is a comment in a Python file."""
        line = line.strip()
        return line.startswith("#")

    comment_identifer = {
        ".cpp": identify_comment_line_cpp,
        ".hpp": identify_comment_line_cpp,
        ".c": identify_comment_line_cpp,
        ".h": identify_comment_line_cpp,
        ".py": identify_comment_line_py,
    }

    include_content = kwargs.get("include_content", None)
    if include_content == "code-and-comments":
        # No need to check, result will always be true
        return True
    elif include_content in ["code", "code-only"]:
        invert = True
    elif include_content in ["comments", "comments-only"]:
        invert = False
    else:
        raise ValueError(f"Unsupported argument: include_content={include_content}")

    target_branch = kwargs["target_branch"]
    if kwargs.get("include_target"):
        target_branch += "..."
    get_diff_args = [
        shutil.which("git"),
        "-C",
        kwargs["repo_path"],
        "diff",
        "--no-color",
        "--unified=0",
        target_branch,
        "--",
    ]

    ext = os.path.splitext(file)[1]
    if ext not in comment_identifer:
        # If we have no rule to check, then always include the file.
        return True
    diff_lines = (
        subprocess.check_output(get_diff_args + [file], encoding="utf-8").strip().split("\n")[4:]
    )
    line_matches = [comment_identifer[ext](line[1:])^invert for line in diff_lines if line[0] in ("-", "+")]
    if include_content.endswith("-only"):
        return all(line_matches)
    else:
        return any(line_matches)


def get_changed_files(repo_path, target_branch, source_branch, include_target):
    """Return a list of paths in the UHD repository that have are different between two branches.

    Arguments:
    repo_path: Path to the UHD repository.
    target_branch: Branch to compare against (e.g., master).
    source_branch: Branch to compare from. Defaults to the current branch.
    include_target: Include changes that originate from the target branch.
    """
    assert target_branch
    # If include_target is false, then current (unstaged/uncommited) changes are
    # not included. If we want to change this, then couple this with
    # git diff --name-only (no further arguments)
    if not include_target:
        target_branch += "..."
    git_cmd = shutil.which("git")
    get_diff_args = [git_cmd, "diff", "--name-only", target_branch]
    if source_branch:
        get_diff_args.append(source_branch)
    files = subprocess.check_output(get_diff_args, cwd=repo_path, encoding="utf-8")
    return files.strip().split("\n")


def load_rules(rule_file):
    """Return the rules as a Python list."""
    with open(rule_file, "r", encoding="utf-8") as rfd:
        y = yaml.YAML(typ="safe", pure=True)
        return y.load(rfd)


class RuleApplier:
    """Helper class to update an internal test list based on a set of rules."""

    def __init__(self, rules, labels, **kwargs):
        """Initialize.

        Arguments:
        rules: List of rules to apply.
        labels: List of labels relevant to the current changeset.
        """
        self.rules = rules
        self.labels = labels
        self.args = kwargs
        self.test_list = set()

    def apply(self, filename, verbose=False):
        """Apply rules against a file."""
        for rule in self.rules:
            if self._apply_rule(rule, filename, verbose):
                break

    def apply_labels(self):
        """Apply rules based only on labels."""
        for rule in self.rules:
            if (
                "label" in rule
                and rule["label"] in self.labels
                and ("re" not in rule and "name" not in rule)
            ):
                self.test_list.update(rule.get("add", []))
                self.test_list.difference_update(rule.get("remove", []))
                for remove_re in rule.get("remove_re", []):
                    self.test_list.difference_update(
                        set([test for test in self.test_list if re.search(remove_re, test)])
                    )

    def _apply_rule(self, rule, filename, verbose=False):
        """Apply a single rule.

        Returns True if you can stop applying rules against this filename.
        """
        # First: Check if this rule even applies to this file
        if (
            ("re" in rule and not re.search(rule["re"], filename))
            or ("name" in rule and rule["name"] != filename)
            or ("name" not in rule and "re" not in rule)
        ):
            return False
        if verbose:
            sys.stderr.write(f"Filename {filename} matches rule: {rule}\n")
        if "label" in rule and not rule["label"] in self.labels:
            if verbose:
                sys.stderr.write(f"Skipping file based on missing label: {rule['label']}\n")
            return False
        if "label" in rule and verbose:
            sys.stderr.write(f"Label {rule['label']} found\n")
        include_content = rule.get("include_content", "code")
        if not check_changeset_content(
            filename, **self.args, include_content=include_content
        ):
            if verbose:
                sys.stderr.write(
                    f"Skipping {filename} based on content rule: include_content='{include_content}'\n"
                )
            return False
        if "add" in rule:
            self.test_list.update(rule["add"])
        if "remove" in rule:
            self.test_list.difference_update(rule["remove"])
        # If stop is specified as False, then we can still apply more rules.
        if "stop" in rule and not rule["stop"]:
            return False
        return True


def get_labels(api_endpoint, token):
    """Get a list of labels from the GitHub API."""
    if not api_endpoint:
        return []
    if token:
        headers = {"Authorization": f"Bearer {token}"}
    else:
        headers = {}
    data = requests.get(api_endpoint, headers=headers).json()
    labels = [label["name"] for label in data]
    print("Labels:", ", ".join(labels))
    return labels


def main():
    """Gogogo! Run script."""
    args = parse_args()
    rule_file = args.rule_file
    if not rule_file:
        rule_file = os.path.join(pathlib.Path(__file__).parent.resolve(), "changeset_testlist.yaml")
    file_list = get_changed_files(
        args.repo_path,
        args.target_branch,
        args.source_branch,
        args.include_target,
    )
    labels = get_labels(args.github_label_api_endpoint, args.github_token)
    rule_applier = RuleApplier(
        load_rules(rule_file),
        labels,
        repo_path=args.repo_path,
        target_branch=args.target_branch,
        source_branch=args.source_branch,
        include_target=args.include_target,
        verbose=args.verbose,
    )
    for filename in file_list:
        rule_applier.apply(filename, args.verbose)
    rule_applier.apply_labels()
    if args.set_azdo_var:
        print(
            f"##vso[task.setvariable variable={args.set_azdo_var};isoutput=true]"
            + ";".join(rule_applier.test_list)
        )
    if args.list_tests:
        print("Required tests:")
        print("---------------", end="")
        print("\n* ".join(sorted([""] + list(rule_applier.test_list))))


if __name__ == "__main__":
    sys.exit(main())