Show More
@@ -1,169 +1,173 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # |
|
3 | 3 | # checkseclevel - checking section title levels in each online help document |
|
4 | 4 | |
|
5 | 5 | import sys, os |
|
6 | 6 | import optparse |
|
7 | 7 | |
|
8 | 8 | # import from the live mercurial repo |
|
9 | 9 | sys.path.insert(0, "..") |
|
10 | 10 | # fall back to pure modules if required C extensions are not available |
|
11 | 11 | sys.path.append(os.path.join('..', 'mercurial', 'pure')) |
|
12 | 12 | from mercurial import demandimport; demandimport.enable() |
|
13 | 13 | from mercurial.commands import table |
|
14 | 14 | from mercurial.help import helptable |
|
15 | 15 | from mercurial import extensions |
|
16 | 16 | from mercurial import minirst |
|
17 | 17 | |
|
18 | 18 | _verbose = False |
|
19 | 19 | |
|
20 | 20 | def verbose(msg): |
|
21 | 21 | if _verbose: |
|
22 | 22 | print msg |
|
23 | 23 | |
|
24 | 24 | def error(msg): |
|
25 | 25 | sys.stderr.write('%s\n' % msg) |
|
26 | 26 | |
|
27 | 27 | level2mark = ['"', '=', '-', '.', '#'] |
|
28 | 28 | reservedmarks = ['"'] |
|
29 | 29 | |
|
30 | 30 | mark2level = {} |
|
31 | 31 | for m, l in zip(level2mark, xrange(len(level2mark))): |
|
32 | 32 | if m not in reservedmarks: |
|
33 | 33 | mark2level[m] = l |
|
34 | 34 | |
|
35 | 35 | initlevel_topic = 0 |
|
36 | 36 | initlevel_cmd = 1 |
|
37 | 37 | initlevel_ext = 1 |
|
38 | 38 | initlevel_ext_cmd = 3 |
|
39 | 39 | |
|
40 | 40 | def showavailables(initlevel): |
|
41 | 41 | error(' available marks and order of them in this help: %s' % |
|
42 | 42 | (', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1:]]))) |
|
43 | 43 | |
|
44 | 44 | def checkseclevel(doc, name, initlevel): |
|
45 | 45 | verbose('checking "%s"' % name) |
|
46 | 46 | blocks, pruned = minirst.parse(doc, 0, ['verbose']) |
|
47 | 47 | errorcnt = 0 |
|
48 | 48 | curlevel = initlevel |
|
49 | 49 | for block in blocks: |
|
50 | 50 | if block['type'] != 'section': |
|
51 | 51 | continue |
|
52 | 52 | mark = block['underline'] |
|
53 | 53 | title = block['lines'][0] |
|
54 | 54 | if (mark not in mark2level) or (mark2level[mark] <= initlevel): |
|
55 | 55 | error('invalid section mark %r for "%s" of %s' % |
|
56 | 56 | (mark * 4, title, name)) |
|
57 | 57 | showavailables(initlevel) |
|
58 | 58 | errorcnt += 1 |
|
59 | 59 | continue |
|
60 | 60 | nextlevel = mark2level[mark] |
|
61 | 61 | if curlevel < nextlevel and curlevel + 1 != nextlevel: |
|
62 | 62 | error('gap of section level at "%s" of %s' % |
|
63 | 63 | (title, name)) |
|
64 | 64 | showavailables(initlevel) |
|
65 | 65 | errorcnt += 1 |
|
66 | 66 | continue |
|
67 | 67 | verbose('appropriate section level for "%s %s"' % |
|
68 | 68 | (mark * (nextlevel * 2), title)) |
|
69 | 69 | curlevel = nextlevel |
|
70 | 70 | |
|
71 | 71 | return errorcnt |
|
72 | 72 | |
|
73 | 73 | def checkcmdtable(cmdtable, namefmt, initlevel): |
|
74 | 74 | errorcnt = 0 |
|
75 | 75 | for k, entry in cmdtable.items(): |
|
76 | 76 | name = k.split("|")[0].lstrip("^") |
|
77 | 77 | if not entry[0].__doc__: |
|
78 | 78 | verbose('skip checking %s: no help document' % |
|
79 | 79 | (namefmt % name)) |
|
80 | 80 | continue |
|
81 | 81 | errorcnt += checkseclevel(entry[0].__doc__, |
|
82 | 82 | namefmt % name, |
|
83 | 83 | initlevel) |
|
84 | 84 | return errorcnt |
|
85 | 85 | |
|
86 | 86 | def checkhghelps(): |
|
87 | 87 | errorcnt = 0 |
|
88 | 88 | for names, sec, doc in helptable: |
|
89 | 89 | if callable(doc): |
|
90 | 90 | doc = doc() |
|
91 | 91 | errorcnt += checkseclevel(doc, |
|
92 | 92 | '%s help topic' % names[0], |
|
93 | 93 | initlevel_topic) |
|
94 | 94 | |
|
95 | 95 | errorcnt += checkcmdtable(table, '%s command', initlevel_cmd) |
|
96 | 96 | |
|
97 | 97 | for name in sorted(extensions.enabled().keys() + |
|
98 | 98 | extensions.disabled().keys()): |
|
99 | 99 | mod = extensions.load(None, name, None) |
|
100 | 100 | if not mod.__doc__: |
|
101 | 101 | verbose('skip checking %s extension: no help document' % name) |
|
102 | 102 | continue |
|
103 | 103 | errorcnt += checkseclevel(mod.__doc__, |
|
104 | 104 | '%s extension' % name, |
|
105 | 105 | initlevel_ext) |
|
106 | 106 | |
|
107 | 107 | cmdtable = getattr(mod, 'cmdtable', None) |
|
108 | 108 | if cmdtable: |
|
109 | 109 | errorcnt += checkcmdtable(cmdtable, |
|
110 | 110 | '%s command of ' + name + ' extension', |
|
111 | 111 | initlevel_ext_cmd) |
|
112 | 112 | return errorcnt |
|
113 | 113 | |
|
114 | 114 | def checkfile(filename, initlevel): |
|
115 | 115 | if filename == '-': |
|
116 | 116 | filename = 'stdin' |
|
117 | 117 | doc = sys.stdin.read() |
|
118 | 118 | else: |
|
119 | 119 | fp = open(filename) |
|
120 | 120 | try: |
|
121 | 121 | doc = fp.read() |
|
122 | 122 | finally: |
|
123 | 123 | fp.close() |
|
124 | 124 | |
|
125 | 125 | verbose('checking input from %s with initlevel %d' % |
|
126 | 126 | (filename, initlevel)) |
|
127 | 127 | return checkseclevel(doc, 'input from %s' % filename, initlevel) |
|
128 | 128 | |
|
129 | if __name__ == "__main__": | |
|
129 | def main(): | |
|
130 | 130 | optparser = optparse.OptionParser("""%prog [options] |
|
131 | 131 | |
|
132 | 132 | This checks all help documents of Mercurial (topics, commands, |
|
133 | 133 | extensions and commands of them), if no file is specified by --file |
|
134 | 134 | option. |
|
135 | 135 | """) |
|
136 | 136 | optparser.add_option("-v", "--verbose", |
|
137 | 137 | help="enable additional output", |
|
138 | 138 | action="store_true") |
|
139 | 139 | optparser.add_option("-f", "--file", |
|
140 | 140 | help="filename to read in (or '-' for stdin)", |
|
141 | 141 | action="store", default="") |
|
142 | 142 | |
|
143 | 143 | optparser.add_option("-t", "--topic", |
|
144 | 144 | help="parse file as help topic", |
|
145 | 145 | action="store_const", dest="initlevel", const=0) |
|
146 | 146 | optparser.add_option("-c", "--command", |
|
147 | 147 | help="parse file as help of core command", |
|
148 | 148 | action="store_const", dest="initlevel", const=1) |
|
149 | 149 | optparser.add_option("-e", "--extension", |
|
150 | 150 | help="parse file as help of extension", |
|
151 | 151 | action="store_const", dest="initlevel", const=1) |
|
152 | 152 | optparser.add_option("-C", "--extension-command", |
|
153 | 153 | help="parse file as help of extension command", |
|
154 | 154 | action="store_const", dest="initlevel", const=3) |
|
155 | 155 | |
|
156 | 156 | optparser.add_option("-l", "--initlevel", |
|
157 | 157 | help="set initial section level manually", |
|
158 | 158 | action="store", type="int", default=0) |
|
159 | 159 | |
|
160 | 160 | (options, args) = optparser.parse_args() |
|
161 | 161 | |
|
162 | global _verbose | |
|
162 | 163 | _verbose = options.verbose |
|
163 | 164 | |
|
164 | 165 | if options.file: |
|
165 | 166 | if checkfile(options.file, options.initlevel): |
|
166 | 167 | sys.exit(1) |
|
167 | 168 | else: |
|
168 | 169 | if checkhghelps(): |
|
169 | 170 | sys.exit(1) |
|
171 | ||
|
172 | if __name__ == "__main__": | |
|
173 | main() |
General Comments 0
You need to be logged in to leave comments.
Login now