doc/check-seclevel.py
author Raphaël Gomès <rgomes@octobus.net>
Wed, 04 May 2022 18:04:46 +0200
branchstable
changeset 49163 9889bf0fd960
parent 45830 c102b704edb5
child 48875 6000f5b25c9b
child 49205 45e71954612c
permissions -rwxr-xr-x
Added signature for changeset 0ddd5e1f5f67
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45830
c102b704edb5 global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents: 43094
diff changeset
     1
#!/usr/bin/env python3
17648
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):
43080
86e4daa2d54c cleanup: mark some ui.(status|note|warn|write) calls as not needing i18n
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    50
    ui.notenoi18n('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:
43094
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
    73
            ui.warnnoi18n(
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
    74
                'gap of section level at "%s" of %s\n' % (title, name)
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
    75
            )
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    76
            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
    77
            errorcnt += 1
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    78
            continue
43080
86e4daa2d54c cleanup: mark some ui.(status|note|warn|write) calls as not needing i18n
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    79
        ui.notenoi18n(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    80
            'appropriate section level for "%s %s"\n'
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    81
            % (mark * (nextlevel * 2), title)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    82
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    83
        curlevel = nextlevel
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    84
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    85
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    86
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    87
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
    88
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
    89
    errorcnt = 0
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
    90
    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
    91
        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
    92
        if not entry[0].__doc__:
43094
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
    93
            ui.notenoi18n(
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
    94
                'skip checking %s: no help document\n' % (namefmt % name)
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
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
            continue
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    97
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    98
            ui, entry[0].__doc__, namefmt % name, initlevel
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
    99
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   100
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   101
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   102
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   103
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
   104
    errorcnt = 0
40292
9c6473d2038b help: splitting the topics by category
Rodrigo Damazio <rdamazio@google.com>
parents: 32544
diff changeset
   105
    for h in helptable:
9c6473d2038b help: splitting the topics by category
Rodrigo Damazio <rdamazio@google.com>
parents: 32544
diff changeset
   106
        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
   107
        if callable(doc):
26413
e0c572d4d112 help: pass around ui to doc loader (API)
Yuya Nishihara <yuya@tcha.org>
parents: 26411
diff changeset
   108
            doc = doc(ui)
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   109
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   110
            ui, doc, '%s help topic' % names[0], initlevel_topic
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   111
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   112
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   113
    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
   114
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   115
    for name in sorted(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   116
        list(extensions.enabled()) + list(extensions.disabled())
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   117
    ):
27511
44a596a8bed1 check-seclevel: pass a ui to the extension loader
Bryan O'Sullivan <bos@serpentine.com>
parents: 27510
diff changeset
   118
        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
   119
        if not mod.__doc__:
43094
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
   120
            ui.notenoi18n(
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
   121
                'skip checking %s extension: no help document\n' % name
e8cf9ad52a78 formatting: run black on all file again
Pierre-Yves David <pierre-yves.david@octobus.net>
parents: 43080
diff changeset
   122
            )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   123
            continue
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   124
        errorcnt += checkseclevel(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   125
            ui, mod.__doc__, '%s extension' % name, initlevel_ext
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   126
        )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   127
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   128
        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
   129
        if cmdtable:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   130
            errorcnt += checkcmdtable(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   131
                ui,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   132
                cmdtable,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   133
                '%%s command of %s extension' % name,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   134
                initlevel_ext_cmd,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   135
            )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   136
    return errorcnt
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   137
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   138
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   139
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
   140
    if filename == '-':
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   141
        filename = 'stdin'
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   142
        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
   143
    else:
27770
1b8c7d59be43 check-seclevel: use a context manager for file I/O
Bryan O'Sullivan <bryano@fb.com>
parents: 27511
diff changeset
   144
        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
   145
            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
   146
43080
86e4daa2d54c cleanup: mark some ui.(status|note|warn|write) calls as not needing i18n
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
   147
    ui.notenoi18n(
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   148
        'checking input from %s with initlevel %d\n' % (filename, initlevel)
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   149
    )
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   150
    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
   151
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   152
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   153
def main():
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   154
    optparser = optparse.OptionParser(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   155
        """%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
   156
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   157
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
   158
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
   159
option.
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   160
"""
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
        "-v", "--verbose", help="enable additional output", action="store_true"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   164
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   165
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   166
        "-d", "--debug", help="debug mode", action="store_true"
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   167
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   168
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   169
        "-f",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   170
        "--file",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   171
        help="filename to read in (or '-' for stdin)",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   172
        action="store",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   173
        default="",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   174
    )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   175
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   176
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   177
        "-t",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   178
        "--topic",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   179
        help="parse file as help topic",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   180
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   181
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   182
        const=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   183
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   184
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   185
        "-c",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   186
        "--command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   187
        help="parse file as help of core command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   188
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   189
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   190
        const=1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   191
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   192
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   193
        "-e",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   194
        "--extension",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   195
        help="parse file as help of extension",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   196
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   197
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   198
        const=1,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   199
    )
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   200
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   201
        "-C",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   202
        "--extension-command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   203
        help="parse file as help of extension command",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   204
        action="store_const",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   205
        dest="initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   206
        const=3,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   207
    )
17648
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   208
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   209
    optparser.add_option(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   210
        "-l",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   211
        "--initlevel",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   212
        help="set initial section level manually",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   213
        action="store",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   214
        type="int",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   215
        default=0,
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   216
    )
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
    (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
   219
30559
d83ca854fa21 ui: factor out ui.load() to create a ui without loading configs (API)
Yuya Nishihara <yuya@tcha.org>
parents: 28965
diff changeset
   220
    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
   221
    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
   222
    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
   223
07f1ac17b722 doc: add the tool to check section marks in help documents
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
diff changeset
   224
    if options.file:
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   225
        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
   226
            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
   227
    else:
26411
dd62eaa82cbe check-seclevel: use ui to show status and error messages
Yuya Nishihara <yuya@tcha.org>
parents: 26398
diff changeset
   228
        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
   229
            sys.exit(1)
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   230
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 41355
diff changeset
   231
26398
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   232
if __name__ == "__main__":
70abba798098 check-seclevel: wrap entry point by function
Yuya Nishihara <yuya@tcha.org>
parents: 26192
diff changeset
   233
    main()