Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/env python3
"""pgBackRest Release Manager.
 
Builds the documentation for a release, deploys it to the website, and builds the copy that ships in the distribution. A separate
program from the documentation builder because it is about a release rather than about a document: it decides which documentation to
build, for which platform, and where it goes.
 
The documentation is built for each platform it documents, since the commands a reader runs differ between them, and the copy of the
user guide for each is kept under a name of its own.
 
All output, including errors, goes to stdout so the run reads in the order it happened."""
 
####################################################################################################################################
import argparse
import os
import re
import shutil
import signal
import sys
 
# Send everything written to stderr to stdout instead so the output is in the order it happened
sys.stderr = sys.stdout
 
# Do not cache bytecode. The tool runs from the source tree, where a __pycache__ would show up as an unexpected binary file in the
# linter and in the distribution. This must be set before the library modules are imported below.
sys.dont_write_bytecode = True
 
# Each tool keeps its library beside itself and may use the libraries below it in the hierarchy. Insert them first, lowest last, so
# the doc modules are found before anything else on the path.
for lib in ("build", "doc"):
    sys.path.insert(0, os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), lib, "lib"))
 
from command.render.release import release_last  # noqa: E402
from common.error import EXIT_ERROR, EXIT_TERM, ToolError, error_trace  # noqa: E402
from common.exec import exec_result  # noqa: E402
from common.log import *  # noqa: E402
from common.storage import file_read, file_remove, path_list, path_list_recurse  # noqa: E402
from common.xml import xml_document_parse, xml_node_attribute, xml_node_normalize  # noqa: E402
from config.project import PROJECT_NAME, project_version  # noqa: E402
 
# Where the documentation is built and where the release keeps what it needs
_FILE_CACHE = "resource/exe.cache"
_PATH_HTML = "output/html"
_PATH_SITE = "site"
 
# Files at the top of the site that belong to the site rather than to a build of the documentation, so a deploy leaves them alone.
# CNAME is what points the domain at the site, so removing it takes the site off its domain until someone puts it back.
_SITE_KEEP_LIST = ("CNAME",)
 
# The footer of a page, and a release date as date_render() writes it or a release year within the footer
_FOOTER_EXP = re.compile(r'<div class="page-footer">.*?</div>', re.DOTALL)
_FOOTER_DATE_EXP = re.compile(r"[A-Z][a-z]+ [0-9]{1,2}, [0-9]{4}|[0-9]{4}")
 
# The distributions the documentation is built for, named for the os type the documentation generates commands for. The user guide
# for each is kept under a name of its own, since a reader picks the one for the system they are on.
_DISTRO_LIST = ("debian", "rhel")
 
_FILE_USER_GUIDE = "user-guide.html"
_FILE_USER_GUIDE_RHEL = "user-guide-rhel.html"
 
 
####################################################################################################################################
def cfg_load(arg_list, path_repo):
    """Parse the command line and apply the rules that cannot be expressed in the parser."""
 
    parser = argparse.ArgumentParser(prog="release.py", description="pgBackRest Release Manager")
 
    parser.add_argument("--version", action="version", version="pgBackRest %s Release Manager" % project_version(path_repo))
    parser.add_argument("--build", action="store_true", help="build the cache before release (include in the release commit)")
    parser.add_argument("--deploy", action="store_true", help="deploy the documentation to the website")
    parser.add_argument("--dist", action="store_true", help="build the documentation that ships in the distribution")
    parser.add_argument("--no-gen", dest="gen", action="store_false", help="do not regenerate the coverage summary")
    parser.add_argument("--no-exe", dest="exe", action="store_false", help="do not run the commands (only applies to --dist)")
    parser.add_argument(
        "--distro", choices=_DISTRO_LIST, help="build the documentation for one distribution rather than for every one"
    )
    parser.add_argument("--quiet", action="store_true", help="set the log level to error")
    parser.add_argument(
        "--log-level", default="info", choices=sorted(LEVEL_NAME.values()), metavar="LEVEL", help="console log level"
    )
 
    config = parser.parse_args(arg_list)
    config.repo_path = path_repo
    config.doc_path = os.path.join(path_repo, "doc")
 
    if not (config.build or config.deploy or config.dist):
        raise ToolError("neither --build nor --deploy nor --dist requested, nothing to do")
 
    config.log_level = OFF if config.quiet else log_level_parse(config.log_level)
 
    return config
 
 
####################################################################################################################################
def doc_build(config, arg_list, show_output=False):
    """Build the documentation."""
 
    exec_result(
        "%s %s" % (os.path.join(config.doc_path, "doc.py"), " ".join(arg_list)),
        suppress_stderr=True,
        show_output=show_output,
    )
 
 
####################################################################################################################################
def user_guide_rename(config):
    """Keep the user guide that was just built under a name of its own, so the next one does not replace it."""
 
    path_html = os.path.join(config.doc_path, _PATH_HTML)
 
    os.replace(os.path.join(path_html, _FILE_USER_GUIDE), os.path.join(path_html, _FILE_USER_GUIDE_RHEL))
 
 
####################################################################################################################################
def cmd_build(config):
    """Build the documentation for a release, along with the cache that lets it be built again without running anything."""
 
    if config.gen:
        log(INFO, "generate coverage summary")
 
        exec_result(
            "%s --vm=u24 --no-valgrind --clean --coverage-summary" % os.path.join(config.repo_path, "test/test.py"),
            suppress_stderr=True,
            show_output=True,
        )
 
    # The cache is built from nothing, since what it holds is what this release does rather than what the last one did
    file_remove(os.path.join(config.doc_path, _FILE_CACHE))
 
    # Remove the hosts of any previous build so the addresses they are given are the same every time
    host_remove()
 
    if config.distro is None or config.distro == "rhel":
        log(INFO, "generate RHEL documentation")
 
        doc_build(config, ["--deploy", "--key-var=os-type=rhel", "--out=html"], show_output=True)
 
    if config.distro is None or config.distro == "debian":
        log(INFO, "generate Debian/Ubuntu documentation")
 
        doc_build(config, ["--deploy", "--out=man", "--out=html", "--out=markdown"], show_output=True)
 
    # A copy of everything for review, which is the documentation as the website will have it
    if config.distro is None:
        log(INFO, "generate full documentation for review")
 
        doc_build(
            config,
            [
                "--deploy",
                "--out-preserve",
                "--cache-only",
                "--key-var=os-type=rhel",
                "--out=html",
                "--var=project-url-root=index.html",
            ],
        )
        user_guide_rename(config)
 
        doc_build(
            config, ["--deploy", "--out-preserve", "--cache-only", "--out=man", "--out=html", "--var=project-url-root=index.html"]
        )
 
 
####################################################################################################################################
def host_remove():
    """Remove the hosts of a documentation build.
 
    The addresses hosts are given depend on what else is running, and the documentation shows those addresses, so a build starts
    from nothing."""
 
    name_list = exec_result("docker ps -a --format '{{.Names}}'", suppress_stderr=True)[1].split()
    doc_list = [name for name in name_list if name.startswith("doc-")]
 
    if len(doc_list) > 0:
        exec_result("docker rm -f %s" % " ".join(doc_list), suppress_error=True)
 
 
####################################################################################################################################
def footer_date_remove(content):
    """Remove the release date and year from the footer of a page."""
 
    return _FOOTER_EXP.sub(lambda match: _FOOTER_DATE_EXP.sub("", match.group(0)), content)
 
 
####################################################################################################################################
def site_file_copy(source, target):
    """Copy a file to the site unless it is a page that differs from the copy on the site only in the release date and year."""
 
    if not (
        source.endswith(".html")
        and os.path.isfile(target)
        and footer_date_remove(file_read(source)) == footer_date_remove(file_read(target))
    ):
        shutil.copy2(source, target)
 
 
####################################################################################################################################
def site_path_clean(path_site, path_html):
    """Remove the files in a path of the site that the build did not write."""
 
    for name in path_list_recurse(path_site):
        if not os.path.isfile(os.path.join(path_html, name)):
            file_remove(os.path.join(path_site, name))
 
 
####################################################################################################################################
def cmd_deploy(config, dev):
    """Deploy the documentation to the website."""
 
    path_html = os.path.join(config.doc_path, _PATH_HTML)
    path_site = os.path.join(config.doc_path, _PATH_SITE)
    version = "dev" if dev else project_version(config.repo_path)
 
    log(INFO, "generate website %s documentation" % ("dev" if dev else "history"))
 
    # A development build is rendered as it is, and a release is rendered from the cache so it comes out the same every time
    arg_list = (["--dev"] if dev else ["--deploy", "--cache-only"]) + ["--var=project-url-root=index.html", "--out=html"]
 
    doc_build(config, arg_list + ["--out-preserve", "--key-var=os-type=rhel"])
    user_guide_rename(config)
 
    file_remove(os.path.join(path_html, "release.html"))
    doc_build(config, arg_list + ["--out-preserve", "--exclude=release"])
 
    log(INFO, "...deploy to repository")
 
    path_prior = os.path.join(path_site, "prior", version)
 
    site_path_clean(path_prior, path_html)
 
    shutil.copytree(path_html, path_prior, dirs_exist_ok=True, copy_function=site_file_copy)
 
    # The main website is only replaced by a release, since a development build is of what is not out yet
    if not dev:
        log(INFO, "generate website documentation")
 
        doc_build(config, ["--var=analytics=y", "--deploy", "--cache-only", "--key-var=os-type=rhel", "--out=html"])
        user_guide_rename(config)
        doc_build(config, ["--var=analytics=y", "--deploy", "--out-preserve", "--cache-only", "--out=html"])
 
        log(INFO, "...deploy to repository")
 
        shutil.rmtree(os.path.join(path_site, "dev"), ignore_errors=True)
 
        for name in os.listdir(path_site):
            path = os.path.join(path_site, name)
 
            # Only what this build writes, which is regular files the site does not own. A link is the site's too, e.g. a retired
            # platform name pointed at the guide that replaced it, so it is left where it is. A file the build wrote again is not
            # removed.
            if (
                name not in _SITE_KEEP_LIST
                and os.path.isfile(path)
                and not os.path.islink(path)
                and not os.path.isfile(os.path.join(path_html, name))
            ):
                os.remove(path)
 
        # Remove files the build did not write from the paths the build writes, e.g. sponsor
        for name in path_list(path_html):
            if os.path.isdir(os.path.join(path_html, name)):
                site_path_clean(os.path.join(path_site, name), os.path.join(path_html, name))
 
        shutil.copytree(path_html, path_site, dirs_exist_ok=True, copy_function=site_file_copy)
 
        for name in ("README.md", "LICENSE"):
            shutil.copyfile(os.path.join(config.repo_path, name), os.path.join(path_site, name))
 
    exec_result("find %s -path .git -prune -type d -exec chmod 750 {} +" % path_site)
    exec_result("find %s -path .git -prune -type f -exec chmod 640 {} +" % path_site)
 
 
####################################################################################################################################
def cmd_dist(config, dev):
    """Build the documentation that ships in the distribution.
 
    What ships is the documentation of the project rather than of the website, so what is only of interest on the website is left
    out."""
 
    log(INFO, "generate dist documentation")
 
    arg_list = [
        "--var=project-url-root=index.html",
        "--exclude=metric",
        "--exclude=news",
        "--var=card=n",
        "--var=news=n",
        "--var=sponsor=n",
    ]
 
    if not config.exe:
        arg_list.append("--no-exe")
    elif not dev:
        arg_list += ["--cache-only", "--var=release-date-static=y"]
 
    doc_build(config, arg_list + ["--key-var=os-type=rhel", "--out=html"])
    user_guide_rename(config)
    doc_build(config, arg_list + ["--out-preserve", "--out=html", "--out=man"])
 
    # The readme the distribution tarball ships is generated rather than committed, so it is rendered on its own
    doc_build(config, ["--out=markdown", "--include=distribution"])
 
 
####################################################################################################################################
def command_run(config):
    """Do what was asked for."""
 
    version = project_version(config.repo_path)
    dev = version.endswith("dev")
 
    # The version the code says it is must be the version the release notes say is most recent, since the documentation reports it
    log(INFO, "check version info")
 
    path_release = os.path.join(config.doc_path, "xml/release.xml")
    release = xml_document_parse(file_read(path_release), path_release)
 
    xml_node_normalize(release)
 
    if xml_node_attribute(release_last(release), "version", True) != version:
        raise ToolError("unable to find version %s as the most recent release in %s" % (version, path_release))
 
    if config.build:
        cmd_build(config)
 
    if config.deploy:
        cmd_deploy(config, dev)
 
    if config.dist:
        cmd_dist(config, dev)
 
    log(INFO, "release complete")
 
    return 0
 
 
####################################################################################################################################
def main():
    """Main."""
 
    # Die silently on SIGPIPE as C programs do, rather than raising when output is piped to a command that exits early
    signal.signal(signal.SIGPIPE, signal.SIG_DFL)
 
    path_repo = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 
    # Load the configuration. Logging is not initialized until this succeeds so print any error directly.
    try:
        config = cfg_load(sys.argv[1:], path_repo)
    except ToolError as error:
        print(error)
 
        return EXIT_ERROR
 
    log_init(config.log_level, True)
    log(INFO, "%s %s Release Manager" % (PROJECT_NAME, project_version(path_repo)))
 
    try:
        return command_run(config)
    except KeyboardInterrupt:
        # A ctrl-c is what was asked for, so report it the way the C reports a signal rather than as a stack trace
        log(ERROR, "terminated on signal SIGINT")
 
        return EXIT_TERM
    except ToolError as error:
        log(ERROR, error)
 
        return error.status
    except Exception as error:
        # An unexpected exception is a bug here rather than a problem with the release, so show the stack trace
        log(ERROR, error_trace(error, config.log_level >= DEBUG))
 
        return EXIT_ERROR
 
 
if __name__ == "__main__":
    sys.exit(main())