summaryrefslogtreecommitdiff
path: root/util/check_cprints.py
blob: 38de40c5c743a09d5c0f50e4c2bb4f17c507b219 (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
#!/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.

"""Checks files for invalid CPRINTS calls."""

import re
import sys
from typing import List, Optional

import preupload.lib


CPRINTS_RE = re.compile(r'(CPRINTS|cprints)[^"]*"[^"]*\\n"')


def main(argv: Optional[List[str]] = None) -> Optional[int]:
    """Look at all files passed in on commandline for invalid CPRINTS calls."""
    return_code = 0
    parser = preupload.lib.argument_parser()

    args = parser.parse_args(argv)
    for filename in args.filename:
        lines = preupload.lib.cat_file(args, filename).splitlines()
        for linenum, line in enumerate(lines, start=1):
            if CPRINTS_RE.search(line):
                print(
                    "error: CPRINTS strings should not include newline "
                    f"characters\n{filename}:{linenum}: {line}",
                    file=sys.stderr,
                )
                return_code = 1

    return return_code


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