| 1 |
#!/usr/bin/env python3 |
| 2 |
# |
| 3 |
# checkseclevel - checking section title levels in each online help document |
| 4 |
|
| 5 |
|
| 6 |
import optparse |
| 7 |
import os |
| 8 |
import sys |
| 9 |
|
| 10 |
# import from the live mercurial repo |
| 11 |
os.environ['HGMODULEPOLICY'] = 'py' |
| 12 |
sys.path.insert(0, os.path.abspath("..")) |
| 13 |
from mercurial import demandimport |
| 14 |
|
| 15 |
demandimport.enable() |
| 16 |
from mercurial import ( |
| 17 |
commands, |
| 18 |
extensions, |
| 19 |
help, |
| 20 |
initialization, |
| 21 |
minirst, |
| 22 |
ui as uimod, |
| 23 |
) |
| 24 |
|
| 25 |
initialization.init() |
| 26 |
|
| 27 |
table = commands.table |
| 28 |
helptable = help.helptable |
| 29 |
|
| 30 |
level2mark = [b'"', b'=', b'-', b'.', b'#'] |
| 31 |
reservedmarks = [b'"'] |
| 32 |
|
| 33 |
mark2level = {} |
| 34 |
for m, l in zip(level2mark, range(len(level2mark))): |
| 35 |
if m not in reservedmarks: |
| 36 |
mark2level[m] = l |
| 37 |
|
| 38 |
initlevel_topic = 0 |
| 39 |
initlevel_cmd = 1 |
| 40 |
initlevel_ext = 1 |
| 41 |
initlevel_ext_cmd = 3 |
| 42 |
|
| 43 |
|
| 44 |
def showavailables(ui, initlevel): |
| 45 |
avail = ' available marks and order of them in this help: %s\n' % ( |
| 46 |
', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1 :]]) |
| 47 |
) |
| 48 |
ui.warn(avail.encode('utf-8')) |
| 49 |
|
| 50 |
|
| 51 |
def checkseclevel(ui, doc, name, initlevel): |
| 52 |
ui.notenoi18n(('checking "%s"\n' % name).encode('utf-8')) |
| 53 |
if not isinstance(doc, bytes): |
| 54 |
doc = doc.encode('utf-8') |
| 55 |
blocks, pruned = minirst.parse(doc, 0, ['verbose']) |
| 56 |
errorcnt = 0 |
| 57 |
curlevel = initlevel |
| 58 |
for block in blocks: |
| 59 |
if block[b'type'] != b'section': |
| 60 |
continue |
| 61 |
mark = block[b'underline'] |
| 62 |
title = block[b'lines'][0] |
| 63 |
if (mark not in mark2level) or (mark2level[mark] <= initlevel): |
| 64 |
ui.warn( |
| 65 |
( |
| 66 |
'invalid section mark %r for "%s" of %s\n' |
| 67 |
% (mark * 4, title, name) |
| 68 |
).encode('utf-8') |
| 69 |
) |
| 70 |
showavailables(ui, initlevel) |
| 71 |
errorcnt += 1 |
| 72 |
continue |
| 73 |
nextlevel = mark2level[mark] |
| 74 |
if curlevel < nextlevel and curlevel + 1 != nextlevel: |
| 75 |
ui.warnnoi18n( |
| 76 |
('gap of section level at "%s" of %s\n' % (title, name)).encode( |
| 77 |
'utf-8' |
| 78 |
) |
| 79 |
) |
| 80 |
showavailables(ui, initlevel) |
| 81 |
errorcnt += 1 |
| 82 |
continue |
| 83 |
ui.notenoi18n( |
| 84 |
( |
| 85 |
'appropriate section level for "%s %s"\n' |
| 86 |
% (mark * (nextlevel * 2), title) |
| 87 |
).encode('utf-8') |
| 88 |
) |
| 89 |
curlevel = nextlevel |
| 90 |
|
| 91 |
return errorcnt |
| 92 |
|
| 93 |
|
| 94 |
def checkcmdtable(ui, cmdtable, namefmt, initlevel): |
| 95 |
errorcnt = 0 |
| 96 |
for k, entry in cmdtable.items(): |
| 97 |
name = k.split(b"|")[0].lstrip(b"^") |
| 98 |
if not entry[0].__doc__: |
| 99 |
ui.notenoi18n( |
| 100 |
( |
| 101 |
'skip checking %s: no help document\n' % (namefmt % name) |
| 102 |
).encode('utf-8') |
| 103 |
) |
| 104 |
continue |
| 105 |
errorcnt += checkseclevel( |
| 106 |
ui, entry[0].__doc__, namefmt % name, initlevel |
| 107 |
) |
| 108 |
return errorcnt |
| 109 |
|
| 110 |
|
| 111 |
def checkhghelps(ui): |
| 112 |
errorcnt = 0 |
| 113 |
for h in helptable: |
| 114 |
names, sec, doc = h[0:3] |
| 115 |
if callable(doc): |
| 116 |
doc = doc(ui) |
| 117 |
errorcnt += checkseclevel( |
| 118 |
ui, doc, '%s help topic' % names[0], initlevel_topic |
| 119 |
) |
| 120 |
|
| 121 |
errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd) |
| 122 |
|
| 123 |
for name in sorted( |
| 124 |
list(extensions.enabled()) + list(extensions.disabled()) |
| 125 |
): |
| 126 |
mod = extensions.load(ui, name, None) |
| 127 |
if not mod.__doc__: |
| 128 |
ui.notenoi18n( |
| 129 |
( |
| 130 |
'skip checking %s extension: no help document\n' % name |
| 131 |
).encode('utf-8') |
| 132 |
) |
| 133 |
continue |
| 134 |
errorcnt += checkseclevel( |
| 135 |
ui, mod.__doc__, '%s extension' % name, initlevel_ext |
| 136 |
) |
| 137 |
|
| 138 |
cmdtable = getattr(mod, 'cmdtable', None) |
| 139 |
if cmdtable: |
| 140 |
errorcnt += checkcmdtable( |
| 141 |
ui, |
| 142 |
cmdtable, |
| 143 |
'%%s command of %s extension' % name, |
| 144 |
initlevel_ext_cmd, |
| 145 |
) |
| 146 |
return errorcnt |
| 147 |
|
| 148 |
|
| 149 |
def checkfile(ui, filename, initlevel): |
| 150 |
if filename == '-': |
| 151 |
filename = 'stdin' |
| 152 |
doc = sys.stdin.read() |
| 153 |
else: |
| 154 |
with open(filename) as fp: |
| 155 |
doc = fp.read() |
| 156 |
|
| 157 |
ui.notenoi18n( |
| 158 |
( |
| 159 |
'checking input from %s with initlevel %d\n' % (filename, initlevel) |
| 160 |
).encode('utf-8') |
| 161 |
) |
| 162 |
return checkseclevel(ui, doc, 'input from %s' % filename, initlevel) |
| 163 |
|
| 164 |
|
| 165 |
def main(): |
| 166 |
optparser = optparse.OptionParser( |
| 167 |
"""%prog [options] |
| 168 |
|
| 169 |
This checks all help documents of Mercurial (topics, commands, |
| 170 |
extensions and commands of them), if no file is specified by --file |
| 171 |
option. |
| 172 |
""" |
| 173 |
) |
| 174 |
optparser.add_option( |
| 175 |
"-v", "--verbose", help="enable additional output", action="store_true" |
| 176 |
) |
| 177 |
optparser.add_option( |
| 178 |
"-d", "--debug", help="debug mode", action="store_true" |
| 179 |
) |
| 180 |
optparser.add_option( |
| 181 |
"-f", |
| 182 |
"--file", |
| 183 |
help="filename to read in (or '-' for stdin)", |
| 184 |
action="store", |
| 185 |
default="", |
| 186 |
) |
| 187 |
|
| 188 |
optparser.add_option( |
| 189 |
"-t", |
| 190 |
"--topic", |
| 191 |
help="parse file as help topic", |
| 192 |
action="store_const", |
| 193 |
dest="initlevel", |
| 194 |
const=0, |
| 195 |
) |
| 196 |
optparser.add_option( |
| 197 |
"-c", |
| 198 |
"--command", |
| 199 |
help="parse file as help of core command", |
| 200 |
action="store_const", |
| 201 |
dest="initlevel", |
| 202 |
const=1, |
| 203 |
) |
| 204 |
optparser.add_option( |
| 205 |
"-e", |
| 206 |
"--extension", |
| 207 |
help="parse file as help of extension", |
| 208 |
action="store_const", |
| 209 |
dest="initlevel", |
| 210 |
const=1, |
| 211 |
) |
| 212 |
optparser.add_option( |
| 213 |
"-C", |
| 214 |
"--extension-command", |
| 215 |
help="parse file as help of extension command", |
| 216 |
action="store_const", |
| 217 |
dest="initlevel", |
| 218 |
const=3, |
| 219 |
) |
| 220 |
|
| 221 |
optparser.add_option( |
| 222 |
"-l", |
| 223 |
"--initlevel", |
| 224 |
help="set initial section level manually", |
| 225 |
action="store", |
| 226 |
type="int", |
| 227 |
default=0, |
| 228 |
) |
| 229 |
|
| 230 |
(options, args) = optparser.parse_args() |
| 231 |
|
| 232 |
ui = uimod.ui.load() |
| 233 |
ui.setconfig(b'ui', b'verbose', options.verbose, b'--verbose') |
| 234 |
ui.setconfig(b'ui', b'debug', options.debug, b'--debug') |
| 235 |
|
| 236 |
if options.file: |
| 237 |
if checkfile(ui, options.file, options.initlevel): |
| 238 |
sys.exit(1) |
| 239 |
else: |
| 240 |
if checkhghelps(ui): |
| 241 |
sys.exit(1) |
| 242 |
|
| 243 |
|
| 244 |
if __name__ == "__main__": |
| 245 |
main() |