Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
"""WGSL template engine.
 
The public entry point is :func:`build`, which orchestrates the
load -> parse -> generate -> emit pipeline.
"""
 
from __future__ import annotations
 
import os
import shutil
from collections.abc import Sequence
from pathlib import Path
 
from .code_generator import resolve_code_generator
from .errors import (
    WgslTemplateBuildError,
    WgslTemplateError,
    WgslTemplateGenerateError,
    WgslTemplateLoadError,
    WgslTemplateParseError,
)
from .generator import generate_directory
from .loader import load_from_directories
from .parser import parse
from .types import SourceDir
 
 
def build(
    *,
    source_dirs: Sequence[str | os.PathLike[str] | SourceDir],
    out_dir: str | os.PathLike[str],
    template_ext: str = ".wgsl.template",
    generator: str = "static-cpp-literal",
    include_path_prefix: str = "",
    preserve_code_reference: bool = False,
    clean: bool = False,
    verbose: bool = False,
) -> list[str]:
    """Run the full build pipeline: load → parse → generate → emit.
 
    Returns the list of relative output paths that were inspected (one
    per emitted file). Files whose contents are unchanged are not
    rewritten, which keeps CMake's mtime-based incremental rebuild
    tracking honest.
    """
 
    if not source_dirs:
        raise WgslTemplateBuildError(
            "source_dirs must be provided and cannot be empty",
            "invalid-options",
        )
 
    out_path = Path(out_dir).resolve()
 
    # --clean wipes the output directory before building.
    if clean and out_path.exists():
        if verbose:
            print(f"Cleaning output directory: {out_path}")
        if out_path.is_file():
            raise WgslTemplateBuildError(
                f'Output path "{out_path}" is an existing file; expected a directory',
                "invalid-options",
            )
        shutil.rmtree(out_path)
 
    if verbose:
        print("Building WGSL templates...")
        print(f"  Sources: {list(source_dirs)}")
        print(f"  Output: {out_path}")
        print(f"  Generator: {generator}")
        print(f"  Template extension: {template_ext}")
        if include_path_prefix:
            print(f"  Include prefix: {include_path_prefix}")
        if preserve_code_reference:
            print("  Preserve code references: enabled")
 
    # PASS0 - load.
    pass0 = load_from_directories(source_dirs, ext=template_ext)
    # PASS1 - parse.
    pass1 = parse(pass0)
    # PASS2 - generate.
    code_generator = resolve_code_generator(generator)
    pass2 = generate_directory(
        pass1,
        code_generator,
        preserve_code_reference=preserve_code_reference,
    )
    # Build (per-template + index*.h + string_table.h).
    files = code_generator.build(
        pass2,
        template_ext=template_ext,
        include_path_prefix=include_path_prefix,
    )
 
    # Write files. Idempotent: only overwrite when content differs.
    out_path.mkdir(parents=True, exist_ok=True)
 
    written: list[str] = []
    for rel_path, content in files.templates.items():
        assert isinstance(content, str)
        full_path = (out_path / rel_path).resolve()
        # Security: refuse to write outside the output directory.
        try:
            full_path.relative_to(out_path)
        except ValueError as e:
            raise WgslTemplateBuildError(
                f"Security violation: attempted to write file outside output directory: {rel_path}",
                "path-security-violation",
                file_path=str(rel_path),
            ) from e
 
        full_path.parent.mkdir(parents=True, exist_ok=True)
 
        # Idempotent write: only rewrite when content differs. This
        # preserves mtime so CMake's incremental builds stay clean.
        # Force LF line endings unconditionally for stable output
        # across Windows / Linux.
        normalized = content.replace("\r\n", "\n")
        new_bytes = normalized.encode("utf-8")
        existing_bytes: bytes | None = None
        if full_path.exists():
            try:
                existing_bytes = full_path.read_bytes()
            except OSError:
                existing_bytes = None
        if existing_bytes != new_bytes:
            full_path.write_bytes(new_bytes)
        written.append(rel_path)
 
    if verbose:
        print(f"Build completed successfully! ({len(written)} file(s))")
 
    return written
 
 
__all__ = [
    "SourceDir",
    "WgslTemplateBuildError",
    "WgslTemplateError",
    "WgslTemplateGenerateError",
    "WgslTemplateLoadError",
    "WgslTemplateParseError",
    "build",
]