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.
377 lines
12 KiB
Perl
Executable File
377 lines
12 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
####################################################################################################################################
|
|
# doc.pl - PgBackRest Doc Builder
|
|
####################################################################################################################################
|
|
|
|
####################################################################################################################################
|
|
# Perl includes
|
|
####################################################################################################################################
|
|
use strict;
|
|
use warnings FATAL => qw(all);
|
|
use Carp qw(confess);
|
|
use English '-no_match_vars';
|
|
|
|
$SIG{__DIE__} = sub { Carp::confess @_ };
|
|
|
|
use Cwd qw(abs_path);
|
|
use File::Basename qw(dirname);
|
|
use Getopt::Long qw(GetOptions);
|
|
use Pod::Usage qw(pod2usage);
|
|
use Storable;
|
|
|
|
use lib dirname(abs_path($0)) . '/lib';
|
|
use lib dirname(dirname(abs_path($0))) . '/lib';
|
|
use lib dirname(dirname(abs_path($0))) . '/build/lib';
|
|
|
|
use pgBackRestDoc::Common::Execute;
|
|
use pgBackRestDoc::Common::Storage;
|
|
use pgBackRestDoc::Common::StoragePosix;
|
|
|
|
use pgBackRestDoc::Common::Doc;
|
|
use pgBackRestDoc::Common::DocContainer;
|
|
use pgBackRestDoc::Common::DocManifest;
|
|
use pgBackRestDoc::Common::Exception;
|
|
use pgBackRestDoc::Common::Log;
|
|
use pgBackRestDoc::Common::String;
|
|
use pgBackRestDoc::Html::DocHtmlSite;
|
|
use pgBackRestDoc::Markdown::DocMarkdown;
|
|
use pgBackRestDoc::ProjectInfo;
|
|
|
|
####################################################################################################################################
|
|
# Usage
|
|
####################################################################################################################################
|
|
|
|
=head1 NAME
|
|
|
|
doc.pl - Generate pgBackRest documentation
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
doc.pl [options]
|
|
|
|
General Options:
|
|
--help Display usage and exit
|
|
--version Display pgBackRest version
|
|
--quiet Sets log level to ERROR
|
|
--log-level Log level for execution (e.g. ERROR, WARN, INFO, DEBUG)
|
|
--deploy Write exe.cache into resource for persistence
|
|
--no-exe Should commands be executed when building help? (for testing only)
|
|
--no-cache Don't use execution cache
|
|
--cache-only Only use the execution cache - don't attempt to generate it
|
|
--pre Pre-build containers for execute elements marked pre
|
|
--var Override defined variable
|
|
--key-var Override defined variable and use in cache key
|
|
--doc-path Document path to render (manifest.xml should be located here)
|
|
--out Output types (html, markdown)
|
|
--out-preserve Don't clean output directory
|
|
--require Require only certain sections of the document (to speed testing)
|
|
--include Include source in generation (links will reference website)
|
|
--exclude Exclude source from generation (links will reference website)
|
|
|
|
Variable Options:
|
|
--dev Set 'dev' variable to 'y'
|
|
--debug Set 'debug' variable to 'y'
|
|
=cut
|
|
|
|
####################################################################################################################################
|
|
# Load command line parameters and config (see usage above for details)
|
|
####################################################################################################################################
|
|
my $bHelp = false;
|
|
my $bVersion = false;
|
|
my $bQuiet = false;
|
|
my $strLogLevel = 'info';
|
|
my $bNoExe = false;
|
|
my $bNoCache = false;
|
|
my $bCacheOnly = false;
|
|
my $rhVariableOverride = {};
|
|
my $rhKeyVariableOverride = {};
|
|
my $strDocPath;
|
|
my @stryOutput;
|
|
my $bOutPreserve = false;
|
|
my @stryRequire;
|
|
my @stryInclude;
|
|
my @stryExclude;
|
|
my $bDeploy = false;
|
|
my $bDev = false;
|
|
my $bDebug = false;
|
|
my $bPre = false;
|
|
|
|
GetOptions ('help' => \$bHelp,
|
|
'version' => \$bVersion,
|
|
'quiet' => \$bQuiet,
|
|
'log-level=s' => \$strLogLevel,
|
|
'out=s@' => \@stryOutput,
|
|
'out-preserve' => \$bOutPreserve,
|
|
'require=s@' => \@stryRequire,
|
|
'include=s@' => \@stryInclude,
|
|
'exclude=s@' => \@stryExclude,
|
|
'no-exe', \$bNoExe,
|
|
'deploy', \$bDeploy,
|
|
'no-cache', \$bNoCache,
|
|
'dev', \$bDev,
|
|
'debug', \$bDebug,
|
|
'pre', \$bPre,
|
|
'cache-only', \$bCacheOnly,
|
|
'key-var=s%', $rhKeyVariableOverride,
|
|
'var=s%', $rhVariableOverride,
|
|
'doc-path=s', \$strDocPath)
|
|
or pod2usage(2);
|
|
|
|
####################################################################################################################################
|
|
# Run in eval block to catch errors
|
|
####################################################################################################################################
|
|
eval
|
|
{
|
|
# Display version and exit if requested
|
|
if ($bHelp || $bVersion)
|
|
{
|
|
print PROJECT_NAME . ' ' . PROJECT_VERSION . " Documentation Builder\n";
|
|
|
|
if ($bHelp)
|
|
{
|
|
print "\n";
|
|
pod2usage();
|
|
}
|
|
|
|
exit 0;
|
|
}
|
|
|
|
# Disable cache when no exe
|
|
if ($bNoExe)
|
|
{
|
|
$bNoCache = true;
|
|
}
|
|
|
|
# Make sure options are set correctly for deploy
|
|
if ($bDeploy)
|
|
{
|
|
my $strError = 'cannot be specified for deploy';
|
|
|
|
!$bNoExe
|
|
or confess "--no-exe ${strError}";
|
|
|
|
!@stryRequire
|
|
or confess "--require ${strError}";
|
|
}
|
|
|
|
# one --include must be specified when --required is
|
|
if (@stryRequire && @stryInclude != 1)
|
|
{
|
|
confess "one --include is required when --require is specified";
|
|
}
|
|
|
|
# Set console log level
|
|
if ($bQuiet)
|
|
{
|
|
$strLogLevel = 'error';
|
|
}
|
|
|
|
# If --dev passed then set the dev var to 'y'
|
|
if ($bDev)
|
|
{
|
|
$rhVariableOverride->{'dev'} = 'y';
|
|
}
|
|
|
|
# If --debug passed then set the debug var to 'y'
|
|
if ($bDebug)
|
|
{
|
|
$rhVariableOverride->{'debug'} = 'y';
|
|
}
|
|
|
|
# Doesn't make sense to pass include and exclude
|
|
if (@stryInclude > 0 && @stryExclude > 0)
|
|
{
|
|
confess "cannot specify both --include and --exclude";
|
|
}
|
|
|
|
logLevelSet(undef, uc($strLogLevel), OFF);
|
|
|
|
# Get the base path
|
|
my $strBasePath = abs_path(dirname($0));
|
|
|
|
my $oStorageDoc = new pgBackRestDoc::Common::Storage(
|
|
$strBasePath, new pgBackRestDoc::Common::StoragePosix({bFileSync => false, bPathSync => false}));
|
|
|
|
if (!defined($strDocPath))
|
|
{
|
|
$strDocPath = $strBasePath;
|
|
}
|
|
|
|
my $strOutputPath = "${strDocPath}/output";
|
|
|
|
# Create the out path if it does not exist
|
|
if (!-e $strOutputPath)
|
|
{
|
|
mkdir($strOutputPath)
|
|
or confess &log(ERROR, "unable to create path ${strOutputPath}");
|
|
}
|
|
|
|
# Merge key variables into the variable list and ensure there are no duplicates
|
|
foreach my $strKey (sort(keys(%{$rhKeyVariableOverride})))
|
|
{
|
|
if (defined($rhVariableOverride->{$strKey}))
|
|
{
|
|
confess &log(ERROR, "'${strKey}' cannot be passed as --var and --key-var");
|
|
}
|
|
|
|
$rhVariableOverride->{$strKey} = $rhKeyVariableOverride->{$strKey};
|
|
}
|
|
|
|
# Build the reference documents, the user guide, and the manual page
|
|
my $strRepoPath = dirname($strBasePath);
|
|
my $strBuildVar = "";
|
|
|
|
foreach my $strVar (sort(keys(%{$rhVariableOverride})))
|
|
{
|
|
$strBuildVar .= " --var=${strVar}=\"" . $rhVariableOverride->{$strVar} . '"';
|
|
}
|
|
|
|
$strBuildVar .= " --var=debug=" . ($bDebug ? 'y' : 'n');
|
|
|
|
executeTest(
|
|
"${strBasePath}/doc.py --repo-path=${strRepoPath}${strBuildVar}" .
|
|
($strLogLevel ne 'info' ? " --log-level=${strLogLevel}" : ''),
|
|
{bShowOutputAsync => true});
|
|
|
|
# Load the manifest
|
|
my $oManifest = new pgBackRestDoc::Common::DocManifest(
|
|
$oStorageDoc, \@stryRequire, \@stryInclude, \@stryExclude, $rhKeyVariableOverride, $rhVariableOverride,
|
|
$strDocPath, $bDeploy, $bCacheOnly, $bPre);
|
|
|
|
if (!$bNoCache)
|
|
{
|
|
$oManifest->cacheRead();
|
|
}
|
|
|
|
# If no outputs were given
|
|
if (@stryOutput == 0)
|
|
{
|
|
@stryOutput = $oManifest->renderList();
|
|
|
|
if ($oManifest->isBackRest())
|
|
{
|
|
push(@stryOutput, 'man');
|
|
}
|
|
}
|
|
|
|
# Build host containers
|
|
if (!$bCacheOnly && !$bNoExe)
|
|
{
|
|
foreach my $strSource ($oManifest->sourceList())
|
|
{
|
|
if ((@stryInclude == 0 || grep(/$strSource/, @stryInclude)) && !grep(/$strSource/, @stryExclude))
|
|
{
|
|
&log(INFO, "source $strSource");
|
|
|
|
foreach my $oHostDefine ($oManifest->sourceGet($strSource)->{doc}->nodeList('host-define', false))
|
|
{
|
|
if ($oManifest->evaluateIf($oHostDefine))
|
|
{
|
|
my $strImage = $oManifest->variableReplace($oHostDefine->paramGet('image'));
|
|
my $strFrom = $oManifest->variableReplace($oHostDefine->paramGet('from'));
|
|
my $strRevision = $oHostDefine->paramGet('revision', false, '0');
|
|
my $strDockerfile = "${strOutputPath}/doc-host.dockerfile";
|
|
my $strScript =
|
|
"FROM ${strFrom}\n\n" . trim($oManifest->variableReplace($oHostDefine->valueGet())) . "\n";
|
|
|
|
&log(INFO, "Build vm '${strImage}' from '${strFrom}'");
|
|
|
|
dockerBuildCached($oStorageDoc, $strDockerfile, $strImage, $strScript, $strBasePath, $strRevision);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
# Render output
|
|
for my $strOutput (@stryOutput)
|
|
{
|
|
&log(INFO, "render ${strOutput} output");
|
|
|
|
# Man output has already been generated in C so do not remove it
|
|
next if ($strOutput eq 'man');
|
|
|
|
# Clean contents of out directory
|
|
if (!$bOutPreserve)
|
|
{
|
|
my $strOutputPath = "${strOutputPath}/$strOutput";
|
|
|
|
# Clean the current out path if it exists
|
|
if (-e $strOutputPath)
|
|
{
|
|
executeTest("rm -rf ${strOutputPath}/*");
|
|
}
|
|
# Else create the html path
|
|
else
|
|
{
|
|
mkdir($strOutputPath)
|
|
or confess &log(ERROR, "unable to create path ${strOutputPath}");
|
|
}
|
|
}
|
|
|
|
$oManifest->renderGet($strOutput);
|
|
|
|
if ($strOutput eq 'markdown')
|
|
{
|
|
my $oMarkdown =
|
|
new pgBackRestDoc::Markdown::DocMarkdown
|
|
(
|
|
$oManifest,
|
|
"${strBasePath}/xml",
|
|
"${strOutputPath}/markdown",
|
|
!$bNoExe
|
|
);
|
|
|
|
$oMarkdown->process();
|
|
}
|
|
elsif ($strOutput eq 'html')
|
|
{
|
|
my $oHtmlSite =
|
|
new pgBackRestDoc::Html::DocHtmlSite
|
|
(
|
|
$oManifest,
|
|
"${strBasePath}/xml",
|
|
"${strOutputPath}/html",
|
|
"${strBasePath}/resource/html/default.css",
|
|
defined($oManifest->variableGet('project-favicon')) ?
|
|
"${strBasePath}/resource/" . $oManifest->variableGet('project-favicon') : undef,
|
|
defined($oManifest->variableGet('project-logo')) ?
|
|
"${strBasePath}/resource/" . $oManifest->variableGet('project-logo') : undef,
|
|
"${strBasePath}/resource/sponsor",
|
|
!$bNoExe
|
|
);
|
|
|
|
$oHtmlSite->process();
|
|
}
|
|
}
|
|
|
|
# Verify page/section links now that all pages have been rendered and their section anchors are known
|
|
$oManifest->linkVerify();
|
|
|
|
# Cache the manifest (mostly useful for testing rendering changes in the code)
|
|
if (!$bNoCache && !$bCacheOnly)
|
|
{
|
|
$oManifest->cacheWrite();
|
|
}
|
|
|
|
# Exit with success
|
|
exit 0;
|
|
}
|
|
|
|
####################################################################################################################################
|
|
# Check for errors
|
|
####################################################################################################################################
|
|
or do
|
|
{
|
|
# If a backrest exception then return the code
|
|
exit $EVAL_ERROR->code() if (isException(\$EVAL_ERROR));
|
|
|
|
# Else output the unhandled error
|
|
print $EVAL_ERROR;
|
|
exit ERROR_UNHANDLED;
|
|
};
|
|
|
|
# It shouldn't be possible to get here
|
|
&log(ASSERT, 'execution reached invalid location in ' . __FILE__ . ', line ' . __LINE__);
|
|
exit ERROR_ASSERT;
|