You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2026-10-23 21:28:56 +03:00
main.c included the generated help data and passed it to cmdHelp() as a buffer, so no unit build ever compiled it and the help test had to run the generator and load the packed help from a file to have anything to render. Including it in help.c puts the data in the module that reads it and lets the test render exactly what the binary carries. The generated file is added to the files the repository copy needs, since git does not list it, and the test command generates it along with the PostgreSQL interfaces. Those are the two files that are generated rather than committed, and a unit build needs both from the copy because it does not run the code generation the build does. The file is regenerated at the start of every run, so the test still runs against the current declarations without generating them itself. cmdHelp() and helpRender() no longer take the data as a parameter and help.h no longer needs the buffer type. The help-data generator command is removed along with its only caller.
248 lines
8.1 KiB
Python
248 lines
8.1 KiB
Python
"""Render Help.
|
|
|
|
Writes the help the binary carries for itself. The text is rendered from the same xml the documentation is built from, packed, and
|
|
compressed, so the help for every command and option costs about as much as one page of it would uncompressed.
|
|
|
|
The result is a C string literal rather than a list of byte values, which is far more compact, and the data is not terminated so the
|
|
array is declared as not being a string and sized to exactly what is there."""
|
|
|
|
####################################################################################################################################
|
|
import bz2
|
|
import os
|
|
|
|
from common.error import ToolError
|
|
from common.pack import PackWrite
|
|
from common.render import LINE_LENGTH, bld_header
|
|
from common.storage import file_write_differs
|
|
|
|
_MODULE = "help"
|
|
_DESCRIPTION = "Help Data"
|
|
|
|
# Compression level, which is the most bzip2 offers since the data is compressed once and read forever
|
|
_COMPRESS_LEVEL = 9
|
|
|
|
# Tags that render as the text they stand for
|
|
_TAG_TEXT = {"backrest": "pgBackRest", "postgres": "PostgreSQL"}
|
|
|
|
# Tags that render as what they contain, followed by what they add. A tag that adds nothing renders as its content alone.
|
|
_TAG_WRAP = {
|
|
"admonition": ("NOTE: ", "\n\n"),
|
|
"list": ("", "\n"),
|
|
"list-item": ("* ", "\n"),
|
|
"p": ("", "\n\n"),
|
|
"quote": ('"', '"'),
|
|
}
|
|
|
|
# Tags that render as what they contain
|
|
_TAG_CONTENT = (
|
|
"b",
|
|
"br-option",
|
|
"cmd",
|
|
"code",
|
|
"exe",
|
|
"file",
|
|
"host",
|
|
"i",
|
|
"id",
|
|
"link",
|
|
"path",
|
|
"pg-setting",
|
|
"proper",
|
|
"setting",
|
|
)
|
|
|
|
# Bytes that have a short escape in C, which are written as that rather than as an octal escape
|
|
_ESCAPE = {
|
|
0x07: "\\a",
|
|
0x08: "\\b",
|
|
0x09: "\\t",
|
|
0x0A: "\\n",
|
|
0x0B: "\\v",
|
|
0x0C: "\\f",
|
|
0x0D: "\\r",
|
|
ord('"'): '\\"',
|
|
ord("\\"): "\\\\",
|
|
ord("?"): "\\?",
|
|
}
|
|
|
|
|
|
####################################################################################################################################
|
|
def _render_text(text):
|
|
"""Render a run of text.
|
|
|
|
Text that runs over more than one line is how the xml is laid out to be read rather than something to render, so it is dropped
|
|
once it is known to be only whitespace."""
|
|
|
|
if text is None:
|
|
return ""
|
|
|
|
if "\n" in text:
|
|
if text.strip() != "":
|
|
raise ToolError("text '%s' is invalid" % text)
|
|
|
|
return ""
|
|
|
|
# The dash is written as a variable in the xml so it does not get treated as markup by the documentation renderers
|
|
return text.replace("{[dash]}", "-")
|
|
|
|
|
|
####################################################################################################################################
|
|
def _render_node(node):
|
|
"""Render the content of a node as console text."""
|
|
|
|
result = _render_text(node.text)
|
|
|
|
for child in node:
|
|
if child.tag in _TAG_TEXT:
|
|
result += _TAG_TEXT[child.tag]
|
|
elif child.tag in _TAG_WRAP:
|
|
begin, end = _TAG_WRAP[child.tag]
|
|
result += begin + _render_node(child) + end
|
|
elif child.tag in _TAG_CONTENT:
|
|
result += _render_node(child)
|
|
else:
|
|
raise ToolError("unknown tag '%s'" % child.tag)
|
|
|
|
result += _render_text(child.tail)
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def bld_hlp_render_xml(node):
|
|
"""Render a help node as the console text the help shows."""
|
|
|
|
return _render_node(node).strip()
|
|
|
|
|
|
####################################################################################################################################
|
|
def _find(item_list, name):
|
|
"""Find an entry in a list by name, or None when it is not there."""
|
|
|
|
for item in item_list or []:
|
|
if item.name == name:
|
|
return item
|
|
|
|
return None
|
|
|
|
|
|
####################################################################################################################################
|
|
def _pack(bld_cfg, bld_hlp):
|
|
"""Pack the help, which is one entry per command and one per option, in the order the code looks them up in."""
|
|
|
|
pack = PackWrite()
|
|
|
|
pack.array_begin()
|
|
|
|
for cmd in bld_cfg.cmd_list:
|
|
cmd_hlp = _find(bld_hlp.cmd_list, cmd.name)
|
|
|
|
pack.bool_write(cmd.internal)
|
|
pack.str_write(bld_hlp_render_xml(cmd_hlp.summary))
|
|
pack.str_write(bld_hlp_render_xml(cmd_hlp.description))
|
|
|
|
pack.array_end()
|
|
|
|
pack.array_begin()
|
|
|
|
for opt in bld_cfg.opt_list:
|
|
opt_hlp = _find(bld_hlp.opt_list, opt.name)
|
|
|
|
pack.bool_write(opt.internal)
|
|
|
|
# An option documented only by the commands that take it has no help of its own here
|
|
pack.str_write(opt_hlp.section if opt_hlp is not None else None)
|
|
pack.str_write(bld_hlp_render_xml(opt_hlp.summary) if opt_hlp is not None else None)
|
|
pack.str_write(bld_hlp_render_xml(opt_hlp.description) if opt_hlp is not None else None)
|
|
|
|
# Deprecated names, less any that is the option name itself since that is not a name to report as deprecated
|
|
deprecate_list = [deprecate.name for deprecate in opt.deprecate_list or [] if deprecate.name != opt.name]
|
|
|
|
if len(deprecate_list) > 0:
|
|
pack.array_begin()
|
|
|
|
for name in deprecate_list:
|
|
pack.str_write(name)
|
|
|
|
pack.array_end()
|
|
else:
|
|
pack.null_write()
|
|
|
|
# What a command says about the option that differs from what the option says, i.e. its own help or being internal
|
|
found = False
|
|
|
|
for cmd_idx, cmd in enumerate(bld_cfg.cmd_list):
|
|
opt_cmd = _find(opt.cmd_list, cmd.name)
|
|
|
|
if opt_cmd is None:
|
|
continue
|
|
|
|
cmd_opt_hlp = _find(_find(bld_hlp.cmd_list, cmd.name).opt_list, opt.name)
|
|
|
|
if opt.internal == opt_cmd.internal and cmd_opt_hlp is None:
|
|
continue
|
|
|
|
if not found:
|
|
pack.array_begin()
|
|
found = True
|
|
|
|
pack.obj_begin(id=cmd_idx + 1)
|
|
|
|
if opt.internal != opt_cmd.internal:
|
|
pack.bool_write(opt_cmd.internal, default_write=True)
|
|
else:
|
|
pack.null_write()
|
|
|
|
if cmd_opt_hlp is not None:
|
|
pack.str_write(bld_hlp_render_xml(cmd_opt_hlp.summary))
|
|
pack.str_write(bld_hlp_render_xml(cmd_opt_hlp.description))
|
|
|
|
pack.obj_end()
|
|
|
|
if found:
|
|
pack.array_end()
|
|
else:
|
|
pack.null_write()
|
|
|
|
pack.array_end()
|
|
|
|
return pack.end()
|
|
|
|
|
|
####################################################################################################################################
|
|
def _render_help_auto_c(bld_cfg, bld_hlp):
|
|
"""Render help.auto.c.inc, which is the compressed help as a C string literal."""
|
|
|
|
# The packed and compressed help, which is what the binary carries and decompresses at run time
|
|
data = bz2.compress(_pack(bld_cfg, bld_hlp), _COMPRESS_LEVEL)
|
|
|
|
result = bld_header(_MODULE, _DESCRIPTION)
|
|
result += 'VR_NON_STRING static const char helpData[%u] =\n"' % len(data)
|
|
|
|
# Break the physical lines at a fixed width with a continuation, which the preprocessor removes before the string is tokenized.
|
|
# Breaking here rather than between escapes keeps every line the same width even when the break falls inside an escape sequence.
|
|
# The opening quote takes the first column of the first line.
|
|
line = [""]
|
|
line_size = 1
|
|
|
|
for byte in data:
|
|
escape = _ESCAPE.get(byte, chr(byte) if 0x20 <= byte <= 0x7E else "\\%03o" % byte)
|
|
|
|
for char in escape:
|
|
# Counting the continuation the line is as wide as the rest of the source
|
|
if line_size == LINE_LENGTH - 1:
|
|
line.append("")
|
|
line_size = 0
|
|
|
|
line[-1] += char
|
|
line_size += 1
|
|
|
|
return result + "\\\n".join(line) + '";\n'
|
|
|
|
|
|
####################################################################################################################################
|
|
def bld_hlp_render(path_build, bld_cfg, bld_hlp):
|
|
"""Render the help."""
|
|
|
|
file_write_differs(os.path.join(path_build, "src/command/help/help.auto.c.inc"), _render_help_auto_c(bld_cfg, bld_hlp))
|