forked from leanprover-community/mathlib4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix-comments.py
executable file
·162 lines (135 loc) · 4.43 KB
/
fix-comments.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
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
#!/usr/bin/env python3
import sys
import os
from pathlib import Path
import subprocess
import re
if len(sys.argv) != 2 or not sys.argv[1].endswith('.lean'):
print("usage: fix-comments.py X.lean")
sys.exit(1)
leanfile = sys.argv[1]
is_clean = subprocess.run(
['git', 'status', '--untracked-files=no', '--porcelain'],
capture_output=True,
check=True,
encoding='utf-8').stdout.rstrip()
if is_clean != "":
print("Certain files tracked by git have uncommitted changes.\n")
os.system("git status --untracked-files=no")
print("\n")
s = input("Type y to continue. ")
if s != 'y':
sys.exit(1)
root_dir = subprocess.run(
['git', 'rev-parse', '--show-toplevel'],
capture_output=True,
check=True,
encoding='utf-8').stdout.rstrip()
align_files = subprocess.run(
['git', 'grep', '-l', '^#align'],
cwd=root_dir,
capture_output=True,
check=True,
encoding='utf-8')
name_map = dict()
for f in align_files.stdout.splitlines():
with open(os.path.join(root_dir, f), encoding="utf-8") as fh:
contents = fh.read()
for p in contents.split(sep='\n#align')[1:]:
n3, n4, *_ = p.split(maxsplit=2)
name_map[n3] = n4
def replace_names(s):
# Terrible hack to treat `.` as a word character
# (to match qualified names)
s = s.replace('.', 'Ᾰ')
# re.DOTALL means that `.` can also match a newline.
# `\A` and `\Z` match only at the start/end of the string respectively.
w = re.findall(r'(?:\b|\A).+?(?:\b|\Z)', s, flags=re.DOTALL)
for i in range(len(w)):
w[i] = w[i].replace('Ᾰ', '.')
w[i] = name_map.get(w[i], w[i])
return ''.join(w)
def process_backticked_names(s):
w = s.split(sep='`')
for i in range(len(w)):
if i % 2 == 1:
w[i] = replace_names(w[i])
return '`'.join(w)
rewritten_contents = ''
in_block_comment = False
in_line_comment = False
prev_char = None
comment_so_far = None # contains end marker but not begin marker
def finish_comment():
global rewritten_contents
global in_block_comment
global in_line_comment
global comment_so_far
if comment_so_far is not None:
rewritten_contents += process_backticked_names(comment_so_far)
in_block_comment = False
in_line_comment = False
comment_so_far = None
with open(leanfile, encoding="utf-8") as F:
while 1:
char = F.read(1)
if not char:
finish_comment()
break
if in_block_comment or in_line_comment:
comment_so_far = comment_so_far + char
else:
rewritten_contents += char
if in_block_comment and prev_char == '-' and char == '/':
finish_comment()
if in_line_comment and char == '\n':
finish_comment()
if comment_so_far is None and prev_char == '/' and char == '-':
in_block_comment = True
comment_so_far = ''
if comment_so_far is None and prev_char == '-' and char == '-':
in_line_comment = True
comment_so_far = ''
prev_char = char
def mktree(path, sha, tree=True):
if path == Path('.'):
return sha
if tree:
inp = f"040000 tree {sha}\t{path.name}"
else:
inp = f"100644 blob {sha}\t{path.name}"
tree_sha = subprocess.run(
['git', 'mktree'],
cwd=root_dir,
input=inp,
capture_output=True,
check=True,
encoding='utf8').stdout.rstrip()
return mktree(path.parent, tree_sha)
path = Path(subprocess.run(
['git', 'ls-files', '--full-name', leanfile],
capture_output=True,
check=True,
encoding='utf-8').stdout.rstrip())
blob_sha = subprocess.run(
['git', 'hash-object', '-w', '--stdin'],
input=rewritten_contents,
cwd=root_dir,
capture_output=True,
check=True,
encoding='utf-8').stdout.rstrip()
tree_sha = mktree(path, blob_sha, tree=False)
print(f"The script will now interactively suggest changes to {leanfile}.\n")
s = input("Type y to continue. ")
if s != 'y':
sys.exit(1)
subprocess.run(['git', 'restore', '--patch', '--source=' + tree_sha, '--', leanfile], check=True)
r = subprocess.run(['git', 'diff', '--quiet', leanfile])
if r.returncode == 0:
pass
elif r.returncode == 1: # file was changed
print("\nPerhaps you would now like to run:")
print(f"git add {leanfile} && git commit -m 'auto: naming'")
else:
# something went wrong
r.check_returncode()