author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Sat, 17 Jun 2017 13:17:10 +0200 | |
changeset 32990 | 1d5d7e2b7ab5 |
parent 32849 | e9fc5550be46 |
child 33195 | 5d8942dbe49e |
permissions | -rwxr-xr-x |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
2 |
# |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
3 |
# check-config - a config flag documentation checker for Mercurial |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
4 |
# |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
5 |
# Copyright 2015 Matt Mackall <mpm@selenic.com> |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
6 |
# |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
7 |
# This software may be used and distributed according to the terms of the |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
8 |
# GNU General Public License version 2 or any later version. |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
9 |
|
28352
a92ee4d8a574
check-config: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27992
diff
changeset
|
10 |
from __future__ import absolute_import, print_function |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
11 |
import re |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
12 |
import sys |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
13 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
14 |
foundopts = {} |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
15 |
documented = {} |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
16 |
|
32847
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
17 |
configre = re.compile(r''' |
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
18 |
# Function call |
32848
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
19 |
ui\.config(?P<ctype>|int|bool|list)\( |
32847
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
20 |
# First argument. |
32848
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
21 |
['"](?P<section>\S+)['"],\s* |
32847
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
22 |
# Second argument |
32848
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
23 |
['"](?P<option>\S+)['"](,\s+ |
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
24 |
(?:default=)?(?P<default>\S+?))? |
32847
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
25 |
\)''', re.VERBOSE | re.MULTILINE) |
e5a6a540ae63
check-config: use compiled regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28352
diff
changeset
|
26 |
|
32849
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
27 |
configwithre = re.compile(''' |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
28 |
ui\.config(?P<ctype>with)\( |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
29 |
# First argument is callback function. This doesn't parse robustly |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
30 |
# if it is e.g. a function call. |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
31 |
[^,]+,\s* |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
32 |
['"](?P<section>\S+)['"],\s* |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
33 |
['"](?P<option>\S+)['"](,\s+ |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
34 |
(?:default=)?(?P<default>\S+?))? |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
35 |
\)''', re.VERBOSE | re.MULTILINE) |
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
36 |
|
27313
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
37 |
configpartialre = (r"""ui\.config""") |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
38 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
39 |
def main(args): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
40 |
for f in args: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
41 |
sect = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
42 |
prevname = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
43 |
confsect = '' |
27313
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
44 |
carryover = '' |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
45 |
for l in open(f): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
46 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
47 |
# check topic-like bits |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
48 |
m = re.match('\s*``(\S+)``', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
49 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
50 |
prevname = m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
51 |
if re.match('^\s*-+$', l): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
52 |
sect = prevname |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
53 |
prevname = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
54 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
55 |
if sect and prevname: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
56 |
name = sect + '.' + prevname |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
57 |
documented[name] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
58 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
59 |
# check docstring bits |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
60 |
m = re.match(r'^\s+\[(\S+)\]', l) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
61 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
62 |
confsect = m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
63 |
continue |
27311
24a1c24fad6e
check-config: allow numbers in configs
timeless <timeless@mozdev.org>
parents:
27310
diff
changeset
|
64 |
m = re.match(r'^\s+(?:#\s*)?(\S+) = ', l) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
65 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
66 |
name = confsect + '.' + m.group(1) |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
67 |
documented[name] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
68 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
69 |
# like the bugzilla extension |
27311
24a1c24fad6e
check-config: allow numbers in configs
timeless <timeless@mozdev.org>
parents:
27310
diff
changeset
|
70 |
m = re.match(r'^\s*(\S+\.\S+)$', l) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
71 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
72 |
documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
73 |
|
27310
9c98fe1416c2
check-config: recognize convert style documentation
timeless <timeless@mozdev.org>
parents:
25849
diff
changeset
|
74 |
# like convert |
9c98fe1416c2
check-config: recognize convert style documentation
timeless <timeless@mozdev.org>
parents:
25849
diff
changeset
|
75 |
m = re.match(r'^\s*:(\S+\.\S+):\s+', l) |
9c98fe1416c2
check-config: recognize convert style documentation
timeless <timeless@mozdev.org>
parents:
25849
diff
changeset
|
76 |
if m: |
9c98fe1416c2
check-config: recognize convert style documentation
timeless <timeless@mozdev.org>
parents:
25849
diff
changeset
|
77 |
documented[m.group(1)] = 1 |
9c98fe1416c2
check-config: recognize convert style documentation
timeless <timeless@mozdev.org>
parents:
25849
diff
changeset
|
78 |
|
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
79 |
# quoted in help or docstrings |
27311
24a1c24fad6e
check-config: allow numbers in configs
timeless <timeless@mozdev.org>
parents:
27310
diff
changeset
|
80 |
m = re.match(r'.*?``(\S+\.\S+)``', l) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
81 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
82 |
documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
83 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
84 |
# look for ignore markers |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
85 |
m = re.search(r'# (?:internal|experimental|deprecated|developer)' |
27312
f925d492113a
check-config: escape period in regexp for inline comments
timeless <timeless@mozdev.org>
parents:
27311
diff
changeset
|
86 |
' config: (\S+\.\S+)$', l) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
87 |
if m: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
88 |
documented[m.group(1)] = 1 |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
89 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
90 |
# look for code-like bits |
27313
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
91 |
line = carryover + l |
32849
e9fc5550be46
check-config: look for ui.configwith
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32848
diff
changeset
|
92 |
m = configre.search(line) or configwithre.search(line) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
93 |
if m: |
32848
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
94 |
ctype = m.group('ctype') |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
95 |
if not ctype: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
96 |
ctype = 'str' |
32848
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
97 |
name = m.group('section') + "." + m.group('option') |
485b8e87e244
check-config: use named groups in regexp
Gregory Szorc <gregory.szorc@gmail.com>
parents:
32847
diff
changeset
|
98 |
default = m.group('default') |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
99 |
if default in (None, 'False', 'None', '0', '[]', '""', "''"): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
100 |
default = '' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
101 |
if re.match('[a-z.]+$', default): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
102 |
default = '<variable>' |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
103 |
if name in foundopts and (ctype, default) != foundopts[name]: |
28352
a92ee4d8a574
check-config: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27992
diff
changeset
|
104 |
print(l) |
a92ee4d8a574
check-config: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27992
diff
changeset
|
105 |
print("conflict on %s: %r != %r" % (name, (ctype, default), |
a92ee4d8a574
check-config: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27992
diff
changeset
|
106 |
foundopts[name])) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
107 |
foundopts[name] = (ctype, default) |
27313
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
108 |
carryover = '' |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
109 |
else: |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
110 |
m = re.search(configpartialre, line) |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
111 |
if m: |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
112 |
carryover = line |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
113 |
else: |
9d155accd8f1
check-config: handle multiline config
timeless <timeless@mozdev.org>
parents:
27312
diff
changeset
|
114 |
carryover = '' |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
115 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
116 |
for name in sorted(foundopts): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
117 |
if name not in documented: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
118 |
if not (name.startswith("devel.") or |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
119 |
name.startswith("experimental.") or |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
120 |
name.startswith("debug.")): |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
121 |
ctype, default = foundopts[name] |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
122 |
if default: |
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
123 |
default = ' [%s]' % default |
28352
a92ee4d8a574
check-config: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents:
27992
diff
changeset
|
124 |
print("undocumented: %s (%s)%s" % (name, ctype, default)) |
25790
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
125 |
|
db5b6a1c064d
check-config: add config option checker
Matt Mackall <mpm@selenic.com>
parents:
diff
changeset
|
126 |
if __name__ == "__main__": |
27992
8f244b75cc5e
tests: execute check-config.py without xargs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
27313
diff
changeset
|
127 |
if len(sys.argv) > 1: |
8f244b75cc5e
tests: execute check-config.py without xargs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
27313
diff
changeset
|
128 |
sys.exit(main(sys.argv[1:])) |
8f244b75cc5e
tests: execute check-config.py without xargs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
27313
diff
changeset
|
129 |
else: |
8f244b75cc5e
tests: execute check-config.py without xargs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
27313
diff
changeset
|
130 |
sys.exit(main([l.rstrip() for l in sys.stdin])) |