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