File: sync_test_files.py

package info (click to toggle)
sqlalchemy 2.0.45%2Bds1-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 26,868 kB
  • sloc: python: 416,938; makefile: 231; sh: 7
file content (78 lines) | stat: -rw-r--r-- 2,018 bytes parent folder | download
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
"""Synchronizes test files that require the future annotation import.

.. versionadded:: 2.0
"""

from __future__ import annotations

from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Any
from typing import Iterable

from sqlalchemy.util.tool_support import code_writer_cmd

header = '''\
"""This file is automatically generated from the file
{source!r}
by the {this_file!r} script.

Do not edit manually, any change will be lost.
"""  # noqa: E501

from __future__ import annotations

'''

home = Path(__file__).parent.parent
this_file = Path(__file__).relative_to(home).as_posix()
remove_str = "# anno only: "


def run_operation(
    name: str, source: str, dest: str, cmd: code_writer_cmd
) -> None:
    source_data = Path(source).read_text().replace(remove_str, "")
    dest_data = header.format(source=source, this_file=this_file) + source_data

    with NamedTemporaryFile(
        mode="w",
        delete=False,
        suffix=".py",
    ) as buf:
        buf.write(dest_data)

    cmd.run_black(buf.name)
    cmd.write_output_file_from_tempfile(buf.name, dest)


def main(file: str, cmd: code_writer_cmd) -> None:
    if file == "all":
        operations: Iterable[Any] = files.items()
    else:
        operations = [(file, files[file])]

    for name, info in operations:
        run_operation(name, info["source"], info["dest"], cmd)


files = {
    "typed_annotation": {
        "source": "test/orm/declarative/test_typed_mapping.py",
        "dest": "test/orm/declarative/test_tm_future_annotations_sync.py",
    },
    "dc_typed_annotation": {
        "source": "test/orm/declarative/test_dc_transforms.py",
        "dest": "test/orm/declarative/test_dc_transforms_future_anno_sync.py",
    },
}

if __name__ == "__main__":
    cmd = code_writer_cmd(__file__)
    with cmd.add_arguments() as parser:
        parser.add_argument(
            "--file", choices=list(files) + ["all"], default="all"
        )

    with cmd.run_program():
        main(cmd.args.file, cmd)