Debian Codegraph @

contrib/check-config.py

raw
1 #!/usr/bin/env python3
2 #
3 # check-config - a config flag documentation checker for Mercurial
4 #
5 # Copyright 2015 Olivia Mackall <[email protected]>
6 #
7 # This software may be used and distributed according to the terms of the
8 # GNU General Public License version 2 or any later version.
9
10 import re
11 import sys
12
13 foundopts = {}
14 documented = {}
15 allowinconsistent = set()
16
17 configre = re.compile(
18 br'''
19 # Function call
20 ui\.config(?P<ctype>|int|bool|list)\(
21 # First argument.
22 ['"](?P<section>\S+)['"],\s*
23 # Second argument
24 ['"](?P<option>\S+)['"](,\s+
25 (?:default=)?(?P<default>\S+?))?
26 \)''',
27 re.VERBOSE | re.MULTILINE,
28 )
29
30 configwithre = re.compile(
31 br'''
32 ui\.config(?P<ctype>with)\(
33 # First argument is callback function. This doesn't parse robustly
34 # if it is e.g. a function call.
35 [^,]+,\s*
36 ['"](?P<section>\S+)['"],\s*
37 ['"](?P<option>\S+)['"](,\s+
38 (?:default=)?(?P<default>\S+?))?
39 \)''',
40 re.VERBOSE | re.MULTILINE,
41 )
42
43 configpartialre = br"""ui\.config"""
44
45 ignorere = re.compile(
46 br'''
47 \#\s(?P<reason>internal|experimental|deprecated|developer|inconsistent)\s
48 config:\s(?P<config>\S+\.\S+)$
49 ''',
50 re.VERBOSE | re.MULTILINE,
51 )
52
53 if sys.version_info[0] > 2:
54
55 def mkstr(b):
56 if isinstance(b, str):
57 return b
58 return b.decode('utf8')
59
60 else:
61 mkstr = lambda x: x
62
63
64 def main(args):
65 for f in args:
66 sect = b''
67 prevname = b''
68 confsect = b''
69 carryover = b''
70 linenum = 0
71 for l in open(f, 'rb'):
72 linenum += 1
73
74 # check topic-like bits
75 m = re.match(br'\s*``(\S+)``', l)
76 if m:
77 prevname = m.group(1)
78 if re.match(br'^\s*-+$', l):
79 sect = prevname
80 prevname = b''
81
82 if sect and prevname:
83 name = sect + b'.' + prevname
84 documented[name] = 1
85
86 # check docstring bits
87 m = re.match(br'^\s+\[(\S+)\]', l)
88 if m:
89 confsect = m.group(1)
90 continue
91 m = re.match(br'^\s+(?:#\s*)?(\S+) = ', l)
92 if m:
93 name = confsect + b'.' + m.group(1)
94 documented[name] = 1
95
96 # like the bugzilla extension
97 m = re.match(br'^\s*(\S+\.\S+)$', l)
98 if m:
99 documented[m.group(1)] = 1
100
101 # like convert
102 m = re.match(br'^\s*:(\S+\.\S+):\s+', l)
103 if m:
104 documented[m.group(1)] = 1
105
106 # quoted in help or docstrings
107 m = re.match(br'.*?``(\S+\.\S+)``', l)
108 if m:
109 documented[m.group(1)] = 1
110
111 # look for ignore markers
112 m = ignorere.search(l)
113 if m:
114 if m.group('reason') == b'inconsistent':
115 allowinconsistent.add(m.group('config'))
116 else:
117 documented[m.group('config')] = 1
118
119 # look for code-like bits
120 line = carryover + l
121 m = configre.search(line) or configwithre.search(line)
122 if m:
123 ctype = m.group('ctype')
124 if not ctype:
125 ctype = 'str'
126 name = m.group('section') + b"." + m.group('option')
127 default = m.group('default')
128 if default in (
129 None,
130 b'False',
131 b'None',
132 b'0',
133 b'[]',
134 b'""',
135 b"''",
136 ):
137 default = b''
138 if re.match(b'[a-z.]+$', default):
139 default = b'<variable>'
140 if (
141 name in foundopts
142 and (ctype, default) != foundopts[name]
143 and name not in allowinconsistent
144 ):
145 print(mkstr(l.rstrip()))
146 fctype, fdefault = foundopts[name]
147 print(
148 "conflict on %s: %r != %r"
149 % (
150 mkstr(name),
151 (mkstr(ctype), mkstr(default)),
152 (mkstr(fctype), mkstr(fdefault)),
153 )
154 )
155 print("at %s:%d:" % (mkstr(f), linenum))
156 foundopts[name] = (ctype, default)
157 carryover = b''
158 else:
159 m = re.search(configpartialre, line)
160 if m:
161 carryover = line
162 else:
163 carryover = b''
164
165 for name in sorted(foundopts):
166 if name not in documented:
167 if not (
168 name.startswith(b"devel.")
169 or name.startswith(b"experimental.")
170 or name.startswith(b"debug.")
171 ):
172 ctype, default = foundopts[name]
173 if default:
174 if isinstance(default, bytes):
175 default = mkstr(default)
176 default = ' [%s]' % default
177 elif isinstance(default, bytes):
178 default = mkstr(default)
179 print(
180 "undocumented: %s (%s)%s"
181 % (mkstr(name), mkstr(ctype), default)
182 )
183
184
185 if __name__ == "__main__":
186 if len(sys.argv) > 1:
187 sys.exit(main(sys.argv[1:]))
188 else:
189 sys.exit(main([l.rstrip() for l in sys.stdin]))