File: circleci_linter.py

package info (click to toggle)
pytorch 1.13.1%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 139,252 kB
  • sloc: cpp: 1,100,274; python: 706,454; ansic: 83,052; asm: 7,618; java: 3,273; sh: 2,841; javascript: 612; makefile: 323; xml: 269; ruby: 185; yacc: 144; objc: 68; lex: 44
file content (159 lines) | stat: -rw-r--r-- 4,526 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
"""
Checks that the configuration in .circleci/config.yml has been properly regenerated.
"""

import argparse
import json
import logging
import os
import subprocess
import sys
import time
from enum import Enum
from typing import List, NamedTuple, Optional


CHECKED_IN_FILE = "config.yml"
REGENERATION_SCRIPT = "regenerate.sh"

PARENT_DIR = os.path.basename(os.path.dirname(os.path.abspath(__file__)))
README_PATH = os.path.join(PARENT_DIR, "README.md")


class LintSeverity(str, Enum):
    ERROR = "error"
    WARNING = "warning"
    ADVICE = "advice"
    DISABLED = "disabled"


class LintMessage(NamedTuple):
    path: Optional[str]
    line: Optional[int]
    char: Optional[int]
    code: str
    severity: LintSeverity
    name: str
    original: Optional[str]
    replacement: Optional[str]
    description: Optional[str]


IS_WINDOWS: bool = os.name == "nt"


def as_posix(name: str) -> str:
    return name.replace("\\", "/") if IS_WINDOWS else name


def run_command(args: List[str], cwd: str) -> "subprocess.CompletedProcess[bytes]":
    logging.debug("$ %s", " ".join(args))
    start_time = time.monotonic()
    try:
        return subprocess.run(
            args,
            cwd=cwd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            check=True,
        )
    finally:
        end_time = time.monotonic()
        logging.debug("took %dms", (end_time - start_time) * 1000)


def run_check(
    regen_script_working_dir: str, regen_script: str, config_file: str
) -> List[LintMessage]:
    try:
        proc = run_command(["python3", regen_script], regen_script_working_dir)
    except Exception as err:
        return [
            LintMessage(
                path=None,
                line=None,
                char=None,
                code="CIRCLECI",
                severity=LintSeverity.ERROR,
                name="command-failed",
                original=None,
                replacement=None,
                description=(
                    f"Failed due to {err.__class__.__name__}:\n{err}"
                    if not isinstance(err, subprocess.CalledProcessError)
                    else (
                        "COMMAND (exit code {returncode})\n"
                        "{command}\n\n"
                        "STDERR\n{stderr}\n\n"
                        "STDOUT\n{stdout}"
                    ).format(
                        returncode=err.returncode,
                        command=" ".join(as_posix(x) for x in err.cmd),
                        stderr=err.stderr.decode("utf-8").strip() or "(empty)",
                        stdout=err.stdout.decode("utf-8").strip() or "(empty)",
                    )
                ),
            )
        ]

    with open(config_file, mode="rb") as f:
        config = f.read()
    if proc.stdout == config:
        return []

    return [
        LintMessage(
            path=config_file,
            line=None,
            char=None,
            code="CIRCLECI",
            severity=LintSeverity.ERROR,
            name="config inconsistency",
            original=config.decode("utf-8"),
            replacement=proc.stdout.decode("utf-8"),
            description=(
                "The checked-in CircleCI config.yml file does not match what was generated by the scripts. "
                "Re-run with '-a' to accept changes."
            ),
        )
    ]


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="circleci consistency linter",
        fromfile_prefix_chars="@",
    )
    parser.add_argument(
        "--config-yml",
        required=True,
        help="location of config.yml",
    )
    parser.add_argument(
        "--regen-script-working-dir",
        required=True,
        help="this script will chdir to this argument before running --regen-script",
    )
    parser.add_argument(
        "--regen-script",
        required=True,
        help="location of the config generation script, relative to --regen-script-working-dir",
    )
    parser.add_argument(
        "--verbose",
        action="store_true",
        help="verbose logging",
    )

    args = parser.parse_args()

    logging.basicConfig(
        format="<%(threadName)s:%(levelname)s> %(message)s",
        level=logging.NOTSET if args.verbose else logging.DEBUG,
        stream=sys.stderr,
    )

    for lint_message in run_check(
        args.regen_script_working_dir, args.regen_script, args.config_yml
    ):
        print(json.dumps(lint_message._asdict()), flush=True)