You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2026-10-23 21:28:56 +03:00
The build tool in src/build becomes build/build.py with its library in build/lib, and the C part of the documentation tool in doc/src becomes doc/doc.py with its library in doc/lib. They migrate together because doc/src was built on src/build, linking its config and help parsing along with its extensions to the core types. doc.pl runs doc.py rather than building and running a C binary, and meson runs build.py for the code it generates as it builds. The three libraries form the hierarchy the C include path did: build/lib is the base, doc/lib sits on it, and test/lib sits on both. They are implicit namespace packages so a package spans them, and a module lives in exactly one library and holds the union of what the tools need from it, since Python resolves a module name to a single file and cannot extend one the way the C included a core module and added to it. define.yaml declares which libraries each tool may import and the test runner puts only those on the path, so a test of the build library cannot reach the doc or test library any more than the build tool can. The declarations move with the code. config.yaml, error.yaml, and postgres.yaml are now at the root of build, and help.xml becomes doc/xml/reference.xml since it is documentation that the help happens to be generated from, so the generator is told where it is rather than knowing. dist.sh moves to build as well. The documentation tool's own option declarations are gone because they existed only to generate the option parser for the C binary and doc.py parses its command line directly. Three modules in src/build/common were never build tool code. exec, regExp, and string extend the core types for the test harness and the unit tests, which were their only users, so they move to test/src/harness. The json and xml extensions had no users outside the two tools and are removed with them, and the tests for both are trimmed to the core functions that remain. The help unit test built its fixture by calling the generator's C functions directly, which was the last thing outside the tools that depended on them. It now runs build.py help-data to write the packed help and loads that, so it still runs against the current declarations without needing the generated C, which may not have been written when the test runs. The manual page wrapping is a port of the wrapper the binary uses for its own help, so the manual page and pgbackrest help break lines the same way, including where a word longer than a line is left unbroken. libyaml is no longer needed to build pgBackRest since the only thing that read yaml was the C generator. It is dropped from the development container and from every CI platform, and meson no longer looks for it. Code generation needs nothing compiled first either, so a test run no longer builds a generator before it can select the tests. The common source list is merged into the pgbackrest source list since it existed only so the generator could be built from the same files, and the entries that were out of alphabetical order are put in order while the list is being rewritten anyway. Every file the generator writes is byte identical to what the C wrote, for the project declarations and for the documentation's own. The documentation is checked the same way: the HTML rendered on this branch is byte identical to the HTML rendered on integration, every file of it, which exercises the whole pipeline on the real content rather than only the four files the tool writes. Every module in both libraries is declared in define.yaml and has full line and branch coverage. The coverage list is keyed by file rather than by module name, since the same name in two languages is two files and one of them would have silently lost its requirement while both existed. TestError becomes ToolError, since it is the error type for three tools now rather than for the harness alone.
121 lines
3.8 KiB
Python
121 lines
3.8 KiB
Python
"""Represent short strings as integers.
|
|
|
|
Port of the encoding in src/common/type/stringId.c. The maps there are written out as 256 byte tables; here they are built from the
|
|
character ranges they encode, which is the same mapping."""
|
|
|
|
####################################################################################################################################
|
|
from common.error import ToolError
|
|
|
|
# Maximum characters that can be encoded with each number of bits
|
|
STRID5_MAX = 12
|
|
STRID6_MAX = 10
|
|
|
|
# Sequence value meaning no sequence was specified
|
|
STRING_ID_SEQ_NONE = None
|
|
|
|
# Sequence at or above this must be stored in the high bits rather than the header
|
|
_SEQ_HIGH_MIN = 6
|
|
|
|
# Bits used to encode a character, and the mask for the low sequence bits
|
|
_BIT5 = 0
|
|
_BIT6 = 1
|
|
_SEQ_LOW_MASK = 0xE
|
|
|
|
|
|
####################################################################################################################################
|
|
def _map_build(bit):
|
|
"""Build a character to encoding map for the specified number of bits. A character not in the map cannot be encoded."""
|
|
|
|
result = {}
|
|
|
|
for index, char in enumerate("abcdefghijklmnopqrstuvwxyz"):
|
|
result[char] = index + 1
|
|
|
|
result["-"] = 27
|
|
|
|
# 5-bit encoding has room for only a few digits while 6-bit encoding covers all digits and upper case
|
|
if bit == _BIT5:
|
|
result["2"] = 28
|
|
result["5"] = 29
|
|
result["6"] = 30
|
|
else:
|
|
for index, char in enumerate("0123456789"):
|
|
result[char] = index + 28
|
|
|
|
for index, char in enumerate("ABCDEFGHIJKLMNOPQRSTUVWXYZ"):
|
|
result[char] = index + 38
|
|
|
|
return result
|
|
|
|
|
|
_MAP = {_BIT5: _map_build(_BIT5), _BIT6: _map_build(_BIT6)}
|
|
|
|
|
|
####################################################################################################################################
|
|
def _str_id_bit_from_z(bit, value):
|
|
"""Encode with the specified number of bits, or return 0 when the value does not fit the encoding."""
|
|
|
|
if len(value) > (STRID5_MAX if bit == _BIT5 else STRID6_MAX):
|
|
return 0
|
|
|
|
map = _MAP[bit]
|
|
|
|
for char in value:
|
|
if char not in map:
|
|
return 0
|
|
|
|
# Set encoding in the header then shift each character into place
|
|
result = bit
|
|
shift = 5 if bit == _BIT5 else 6
|
|
|
|
for index, char in enumerate(value):
|
|
result |= map[char] << (4 + shift * index)
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def str_id_from_z(value, sequence=STRING_ID_SEQ_NONE):
|
|
"""Encode a string as a StringId, adding a sequence when one is specified."""
|
|
|
|
if value == "":
|
|
raise ToolError("StringId value may not be empty")
|
|
|
|
result = _str_id_bit_from_z(_BIT5, value)
|
|
|
|
# If 5-bit encoding fails try 6-bit
|
|
if result == 0:
|
|
result = _str_id_bit_from_z(_BIT6, value)
|
|
|
|
# Error when 6-bit encoding also fails
|
|
if result == 0:
|
|
raise ToolError("'%s' contains invalid characters" % value)
|
|
|
|
if sequence is not STRING_ID_SEQ_NONE:
|
|
# If the sequence fits in the header
|
|
if sequence < _SEQ_HIGH_MIN:
|
|
result |= (sequence + 1) << 1
|
|
# Else the sequence must be stored in the high bits
|
|
else:
|
|
bit = result & 1
|
|
|
|
# Shift to remove the bit marker (added back below) and make space for the high sequence
|
|
result >>= 1
|
|
result <<= 6
|
|
|
|
result |= (sequence - _SEQ_HIGH_MIN) << 4 | _SEQ_LOW_MASK | bit
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def str_id_seq(str_id):
|
|
"""Extract the sequence from a StringId."""
|
|
|
|
sequence = str_id & _SEQ_LOW_MASK
|
|
|
|
if sequence != _SEQ_LOW_MASK:
|
|
return (sequence >> 1) - 1
|
|
|
|
return ((str_id & 0x1F0) >> 4) + _SEQ_HIGH_MIN
|