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