-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathautoformat.py
executable file
·96 lines (80 loc) · 2.67 KB
/
autoformat.py
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
#!/usr/bin/env python3
import argparse
import datetime
import getpass
import subprocess
import sys
import tempfile
if __name__ == "__main__":
parser = argparse.ArgumentParser(
"Automated script for applying code reformatting. Automatically commits reformatting changes as 'VTR Robot' and adds them to .git-blame-ignore-revs (so they can be skipped with git hyper-blame). Should be run from the root of the source tree."
)
parser.add_argument(
"--skip_commit",
action="store_true",
default=False,
help="Don't commit the autoformat changes. Default: %(default)s",
)
parser.add_argument(
"--python",
action="store_true",
default=False,
help="Format Python code instead of C/C++. Default: %(default)s",
)
args = parser.parse_args()
git_commit_args = [
"-a",
"--no-verify",
'--author="VTR Robot <robot@verilogtorouting.org>"',
]
# Run `make format`
if args.python:
cmd = "make format-py"
else:
cmd = "make format"
subprocess.check_call(cmd, shell=True)
if not args.skip_commit:
# Commit the changes caused by `make format`
with tempfile.NamedTemporaryFile("w") as msg:
msg.write(
"""\
🤖 - Automated code reformat.
I'm an auto code reformatting bot. Beep boop...
"""
)
msg.flush()
subprocess.check_call(
"git commit {} --file={msg}".format(" ".join(git_commit_args), msg=msg.name),
shell=True,
)
# Append the format change to the .git-blame-ignore-revs file.
format_hash = subprocess.check_output("git rev-parse --verify HEAD", shell=True)
format_hash = format_hash.decode("utf-8").strip()
now = datetime.datetime.utcnow().isoformat()
with open(".git-blame-ignore-revs", "a+") as bf:
bf.write(
"""\
# Autoformat run on {}
{}
""".format(
now, format_hash
)
)
subprocess.check_call("git add .git-blame-ignore-revs", shell=True)
with tempfile.NamedTemporaryFile("w") as msg:
msg.write(
"""\
🤖 - Ignore automated code reformat in blame.
Adding the reformatting change
{}
(which I just committed!) to the ignore list.
I'm an auto code reformatting bot. Beep boop...
""".format(
format_hash
)
)
msg.flush()
subprocess.check_call(
"git commit {} --file={msg}".format(" ".join(git_commit_args), msg=msg.name),
shell=True,
)