File: reformat-code.py

package info (click to toggle)
libxmlb 0.3.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,052 kB
  • sloc: ansic: 12,692; python: 569; xml: 83; sh: 81; makefile: 2
file content (90 lines) | stat: -rwxr-xr-x 2,486 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python3
#
# Copyright 2017 Dell Inc.
#
# SPDX-License-Identifier: LGPL-2.1-or-later
#

import os
import sys
import subprocess
import argparse

CLANG_DIFF_FORMATTERS = [
    "clang-format-diff-11",
    "clang-format-diff-13",
    "clang-format-diff",
]


def parse_args():
    parser = argparse.ArgumentParser(
        description="Reformat C code to match project style",
        epilog="Call with no argument to reformat uncommitted code.",
    )
    parser.add_argument(
        "commit", nargs="*", default="", help="Reformat all changes since this commit"
    )
    parser.add_argument(
        "--debug", action="store_true", help="Display all launched commands"
    )
    return parser.parse_args()


def select_clang_version(formatters):
    for formatter in formatters:
        try:
            ret = subprocess.check_call(
                [formatter, "--help"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
            )
            if ret == 0:
                return formatter
        except FileNotFoundError:
            continue
    print("No clang formatter installed")
    sys.exit(1)


## Entry Point ##
if __name__ == "__main__":
    args = parse_args()
    base = os.getenv("GITHUB_BASE_REF")
    if base:
        base = "origin/%s" % base
    else:
        if args.commit:
            base = args.commit[0]
        else:
            base = "HEAD"
    cmd = ["git", "describe", base]
    if args.debug:
        print(cmd)
    ret = subprocess.run(cmd, capture_output=True)
    if ret.returncode:
        if args.debug:
            print(ret.stderr)
        base = "HEAD"
    print("Reformatting code against %s" % base)
    formatter = select_clang_version(CLANG_DIFF_FORMATTERS)
    cmd = ["git", "diff", "-U0", base]
    if args.debug:
        print(cmd)
    ret = subprocess.run(cmd, capture_output=True, text=True)
    if ret.returncode:
        print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
        sys.exit(1)
    cmd = [formatter, "-p1"]
    if args.debug:
        print(cmd)
    ret = subprocess.run(cmd, input=ret.stdout, capture_output=True, text=True)
    if ret.returncode:
        print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
        sys.exit(1)
    cmd = ["patch", "-p0"]
    if args.debug:
        print(cmd)
    ret = subprocess.run(cmd, input=ret.stdout, capture_output=True, text=True)
    if ret.returncode:
        print("Failed to run %s\n%s" % (cmd, ret.stderr.strip()))
        sys.exit(1)
    sys.exit(0)