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.
145 lines
5.6 KiB
Python
145 lines
5.6 KiB
Python
"""Parse PostgreSQL Interface Declaration.
|
|
|
|
The versions come from postgres.yaml, but the types, defines, and functions that make up an interface are read out of the vendored
|
|
PostgreSQL headers rather than declared. A version interface is the same headers compiled again with different names, so scanning
|
|
them is what keeps the generated code in step with a header update."""
|
|
|
|
####################################################################################################################################
|
|
import os
|
|
|
|
from common.error import ToolError
|
|
from common.storage import file_read
|
|
from common.yaml import yaml_bool, yaml_load
|
|
|
|
# Defines that every interface undefines but that no vendored header declares, so they are added to the list explicitly
|
|
_DEFINE_EXTRA_LIST = ("CATALOG_VERSION_NO_MAX", "PG_VERSION")
|
|
|
|
|
|
####################################################################################################################################
|
|
class BldPgVersion:
|
|
"""A supported PostgreSQL version."""
|
|
|
|
def __init__(self, version, release):
|
|
self.version = version
|
|
self.release = release # Has the version been released?
|
|
|
|
|
|
####################################################################################################################################
|
|
class BldPg:
|
|
"""The PostgreSQL interface declaration."""
|
|
|
|
def __init__(self, pg_list, type_list, define_list, function_list):
|
|
self.pg_list = pg_list # Supported versions, oldest first
|
|
self.type_list = type_list # Interface types, sorted
|
|
self.define_list = define_list # Interface defines, sorted
|
|
self.function_list = function_list # Functions defined by macros, in the order they are declared
|
|
|
|
|
|
####################################################################################################################################
|
|
def _version_list(path):
|
|
"""Parse the version list."""
|
|
|
|
result = []
|
|
|
|
for key, value in yaml_load(file_read(path), path):
|
|
if key != "version":
|
|
raise ToolError("unknown postgres definition '%s'" % key)
|
|
|
|
for version in value:
|
|
# A scalar is the version on its own, else a map naming the version and its attributes
|
|
if isinstance(version, str):
|
|
result.append(BldPgVersion(version, True))
|
|
|
|
continue
|
|
|
|
for name, detail in version:
|
|
release = True
|
|
|
|
for def_key, def_value in detail:
|
|
if def_key != "release":
|
|
raise ToolError("unknown postgres definition '%s'" % def_key)
|
|
|
|
release = yaml_bool(def_value, "version '%s' release" % name)
|
|
|
|
result.append(BldPgVersion(name, release))
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def _define_list(header):
|
|
"""Scan the defines out of a header."""
|
|
|
|
result = []
|
|
|
|
for line in header.split("\n"):
|
|
line = line.strip()
|
|
|
|
if not line.startswith("#define"):
|
|
continue
|
|
|
|
token = line.split(" ")[1].strip()
|
|
|
|
if token == "":
|
|
raise ToolError("unable to find define -- are there extra spaces on '%s'" % line)
|
|
|
|
# The define name may be followed by a parameter list or separated from its value by a tab
|
|
define = token.split("(")[0] if "(" in token else token.split("\t")[0]
|
|
define = define.strip()
|
|
|
|
if define not in result:
|
|
result.append(define)
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def _type_list(header):
|
|
"""Scan the types out of a header.
|
|
|
|
A typedef of a struct or an enum names the type after the block rather than before it, so the scan carries on to the closing
|
|
brace, collecting the values of an enum on the way since each is a name the interface has to undefine."""
|
|
|
|
result = []
|
|
scan_enum = False
|
|
|
|
def add(value):
|
|
if value not in result:
|
|
result.append(value)
|
|
|
|
for line in header.split("\n"):
|
|
token_list = line.strip().split(" ")
|
|
|
|
if token_list[0] == "typedef":
|
|
# A struct or an enum is named at the end of the block, so keep scanning
|
|
if token_list[1] in ("struct", "enum"):
|
|
scan_enum = token_list[1] == "enum"
|
|
else:
|
|
add(token_list[-1].split(";")[0])
|
|
elif token_list[0] == "}":
|
|
add(token_list[-1].split(";")[0])
|
|
scan_enum = False
|
|
elif scan_enum and token_list[0] != "{":
|
|
add(token_list[0].split(",")[0])
|
|
|
|
return result
|
|
|
|
|
|
####################################################################################################################################
|
|
def bld_pg_parse(path_repo):
|
|
"""Parse the PostgreSQL interface declaration."""
|
|
|
|
path_vendor = os.path.join(path_repo, "src/postgres/interface/version.vendor.h")
|
|
header_vendor = file_read(path_vendor)
|
|
|
|
# The interface is generated from the vendored header, so its types and defines are whatever that header declares
|
|
type_list = sorted(_type_list(header_vendor))
|
|
define_list = sorted(_define_list(header_vendor) + list(_DEFINE_EXTRA_LIST))
|
|
|
|
# Functions are defined as macros, which each interface expands for its own version
|
|
function_list = _define_list(file_read(os.path.join(path_repo, "src/postgres/interface/version.intern.h")))
|
|
|
|
path_yaml = os.path.join(path_repo, "build/postgres.yaml")
|
|
|
|
return BldPg(_version_list(path_yaml), type_list, define_list, function_list)
|