doc/check-seclevel.py
author Augie Fackler <augie@google.com>
Sun, 06 Oct 2019 09:45:02 -0400
changeset 43076 2372284d9457
parent 41355 77763dc5b07b
child 43080 86e4daa2d54c
permissions -rwxr-xr-x
formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     1
#!/usr/bin/env python
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     2
#
26192
67e6e55360d2 check-seclevel: fix file description grammar
timeless@mozdev.org
parents: 21792
diff changeset
     3
# checkseclevel - checking section title levels in each online help document
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     4
28965
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
     5
from __future__ import absolute_import
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
     6
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
     7
import optparse
28965
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
     8
import os
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
     9
import sys
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    10
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    11
# import from the live mercurial repo
27221
ab776610fc6d check-seclevel: set module load policy to Python only
Gregory Szorc <gregory.szorc@gmail.com>
parents: 26413
diff changeset
    12
os.environ['HGMODULEPOLICY'] = 'py'
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    13
sys.path.insert(0, "..")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    14
from mercurial import demandimport
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    15
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    16
demandimport.enable()
28965
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    17
from mercurial import (
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    18
    commands,
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    19
    extensions,
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    20
    help,
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    21
    minirst,
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    22
    ui as uimod,
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    23
)
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    24
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    25
table = commands.table
98153441c8cc py3: make check-seclevel use absolute_import
Pulkit Goyal <7895pulkit@gmail.com>
parents: 27770
diff changeset
    26
helptable = help.helptable
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    27
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    28
level2mark = [b'"', b'=', b'-', b'.', b'#']
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    29
reservedmarks = [b'"']
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    30
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    31
mark2level = {}
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    32
for m, l in zip(level2mark, range(len(level2mark))):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    33
    if m not in reservedmarks:
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    34
        mark2level[m] = l
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    35
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    36
initlevel_topic = 0
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    37
initlevel_cmd = 1
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    38
initlevel_ext = 1
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    39
initlevel_ext_cmd = 3
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    40
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    41
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    42
def showavailables(ui, initlevel):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    43
    avail = '    available marks and order of them in this help: %s\n' % (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    44
        ', '.join(['%r' % (m * 4) for m in level2mark[initlevel + 1 :]])
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    45
    )
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    46
    ui.warn(avail.encode('utf-8'))
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    47
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    48
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    49
def checkseclevel(ui, doc, name, initlevel):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    50
    ui.note('checking "%s"\n' % name)
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    51
    if not isinstance(doc, bytes):
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    52
        doc = doc.encode('utf-8')
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    53
    blocks, pruned = minirst.parse(doc, 0, ['verbose'])
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    54
    errorcnt = 0
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    55
    curlevel = initlevel
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    56
    for block in blocks:
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    57
        if block[b'type'] != b'section':
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    58
            continue
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    59
        mark = block[b'underline']
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    60
        title = block[b'lines'][0]
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    61
        if (mark not in mark2level) or (mark2level[mark] <= initlevel):
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    62
            ui.warn(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    63
                (
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    64
                    'invalid section mark %r for "%s" of %s\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    65
                    % (mark * 4, title, name)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    66
                ).encode('utf-8')
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    67
            )
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    68
            showavailables(ui, initlevel)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    69
            errorcnt += 1
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    70
            continue
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    71
        nextlevel = mark2level[mark]
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    72
        if curlevel < nextlevel and curlevel + 1 != nextlevel:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    73
            ui.warn('gap of section level at "%s" of %s\n' % (title, name))
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    74
            showavailables(ui, initlevel)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    75
            errorcnt += 1
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    76
            continue
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    77
        ui.note(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    78
            'appropriate section level for "%s %s"\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    79
            % (mark * (nextlevel * 2), title)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    80
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    81
        curlevel = nextlevel
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    82
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    83
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    84
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    85
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    86
def checkcmdtable(ui, cmdtable, namefmt, initlevel):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    87
    errorcnt = 0
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    88
    for k, entry in cmdtable.items():
32544
e9f456183402 doc: port check-seclevel.py to be Python 2/3 portable
Augie Fackler <raf@durin42.com>
parents: 30559
diff changeset
    89
        name = k.split(b"|")[0].lstrip(b"^")
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    90
        if not entry[0].__doc__:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    91
            ui.note('skip checking %s: no help document\n' % (namefmt % name))
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    92
            continue
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    93
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    94
            ui, entry[0].__doc__, namefmt % name, initlevel
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    95
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    96
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    97
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    98
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    99
def checkhghelps(ui):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   100
    errorcnt = 0
40292
9c6473d2038b help: splitting the topics by category
Rodrigo Damazio <rdamazio@google.com>
parents: 32544
diff changeset
   101
    for h in helptable:
9c6473d2038b help: splitting the topics by category
Rodrigo Damazio <rdamazio@google.com>
parents: 32544
diff changeset
   102
        names, sec, doc = h[0:3]
21792
e15c991fe2ec check-seclevel: restore use of callable() since it was readded in Python 3.2
Augie Fackler <raf@durin42.com>
parents: 17648
diff changeset
   103
        if callable(doc):
26413
e0c572d4d112 help: pass around ui to doc loader (API)
Yuya Nishihara <yuya@tcha.org>
parents: 26411
diff changeset
   104
            doc = doc(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   105
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   106
            ui, doc, '%s help topic' % names[0], initlevel_topic
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   107
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   108
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   109
    errorcnt += checkcmdtable(ui, table, '%s command', initlevel_cmd)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   110
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   111
    for name in sorted(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   112
        list(extensions.enabled()) + list(extensions.disabled())
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   113
    ):
27511
44a596a8bed1 check-seclevel: pass a ui to the extension loader
Bryan O'Sullivan <bos@serpentine.com>
parents: 27510
diff changeset
   114
        mod = extensions.load(ui, name, None)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   115
        if not mod.__doc__:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   116
            ui.note('skip checking %s extension: no help document\n' % name)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   117
            continue
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   118
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   119
            ui, mod.__doc__, '%s extension' % name, initlevel_ext
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   120
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   121
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   122
        cmdtable = getattr(mod, 'cmdtable', None)
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
        if cmdtable:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   124
            errorcnt += checkcmdtable(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   125
                ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   126
                cmdtable,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   127
                '%%s command of %s extension' % name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   128
                initlevel_ext_cmd,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   129
            )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   130
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   131
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   132
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   133
def checkfile(ui, filename, initlevel):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   134
    if filename == '-':
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   135
        filename = 'stdin'
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
        doc = sys.stdin.read()
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   137
    else:
27770
1b8c7d59be43 check-seclevel: use a context manager for file I/O
Bryan O'Sullivan <bryano@fb.com>
parents: 27511
diff changeset
   138
        with open(filename) as fp:
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   139
            doc = fp.read()
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   140
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   141
    ui.note(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   142
        'checking input from %s with initlevel %d\n' % (filename, initlevel)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   143
    )
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   144
    return checkseclevel(ui, doc, 'input from %s' % filename, initlevel)
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   145
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   146
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   147
def main():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   148
    optparser = optparse.OptionParser(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   149
        """%prog [options]
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   150
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   151
This checks all help documents of Mercurial (topics, commands,
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   152
extensions and commands of them), if no file is specified by --file
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   153
option.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   154
"""
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   155
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   156
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   157
        "-v", "--verbose", help="enable additional output", action="store_true"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   158
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   159
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   160
        "-d", "--debug", help="debug mode", action="store_true"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   161
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   162
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   163
        "-f",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   164
        "--file",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   165
        help="filename to read in (or '-' for stdin)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   166
        action="store",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   167
        default="",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   168
    )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   169
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   170
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   171
        "-t",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   172
        "--topic",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   173
        help="parse file as help topic",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   174
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   175
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   176
        const=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   177
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   178
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   179
        "-c",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   180
        "--command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   181
        help="parse file as help of core command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   182
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   183
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   184
        const=1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   185
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   186
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   187
        "-e",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   188
        "--extension",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   189
        help="parse file as help of extension",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   190
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   191
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   192
        const=1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   193
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   194
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   195
        "-C",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   196
        "--extension-command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   197
        help="parse file as help of extension command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   198
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   199
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   200
        const=3,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   201
    )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   202
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   203
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   204
        "-l",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   205
        "--initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   206
        help="set initial section level manually",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   207
        action="store",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   208
        type="int",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   209
        default=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   210
    )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   211
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   212
    (options, args) = optparser.parse_args()
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   213
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 28965
diff changeset
   214
    ui = uimod.ui.load()
41355
77763dc5b07b py3: add b'' prefixes in doc/check-seclevel.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40292
diff changeset
   215
    ui.setconfig(b'ui', b'verbose', options.verbose, b'--verbose')
77763dc5b07b py3: add b'' prefixes in doc/check-seclevel.py
Pulkit Goyal <pulkit@yandex-team.ru>
parents: 40292
diff changeset
   216
    ui.setconfig(b'ui', b'debug', options.debug, b'--debug')
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   217
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   218
    if options.file:
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   219
        if checkfile(ui, options.file, options.initlevel):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   220
            sys.exit(1)
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   221
    else:
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   222
        if checkhghelps(ui):
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   223
            sys.exit(1)
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   224
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   225
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   226
if __name__ == "__main__":
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   227
    main()