Debian Codegraph @

debian/tests/python/temptest/__main__.py

raw symbols SCIP
1 # SPDX-FileCopyrightText: Peter Pentchev <[email protected]>
2 # SPDX-License-Identifier: BSD-2-Clause
3 """Parse command-line arguments, run a command in a temporary directory."""
4
5 from __future__ import annotations
6
7 import os
8 import pathlib
9 import shutil
10 import subprocess # noqa: S404
11 import sys
12 import tempfile
13 import typing
14
15 import click
16
17 from . import defs
18 from . import util
19
20
21 if typing.TYPE_CHECKING:
22 from typing import Final
23
24
25 def arg_features(_ctx: click.Context, _self: click.Parameter, value: bool) -> bool: # noqa: FBT001
26 """Display program features information and exit."""
27 if not value:
28 return value
29
30 print( # noqa: T201
31 "Features: " + " ".join(f"{name}={value}" for name, value in defs.FEATURES.items()),
32 )
33 sys.exit(0)
34
35
36 def run(cfg: defs.Config) -> None:
37 """Create a temporary directory, copy the test file there, run the test."""
38 testf: Final = cfg.srcdir / "regex_3" / "test_regex.py"
39 if not testf.is_file():
40 sys.exit(f"Not a regular file: {testf}")
41
42 with tempfile.TemporaryDirectory(prefix="regex-temp-test.") as tempd_obj:
43 tempd: Final = pathlib.Path(tempd_obj)
44 cfg.log.debug("Using %(tempd)s as a temporary directory", {"tempd": tempd})
45 destf: Final = tempd / testf.name
46 cfg.log.debug("Copying %(src)s to %(dst)s", {"src": testf, "dst": destf})
47 shutil.copy2(testf, destf)
48
49 pyenv: Final = dict(os.environ)
50
51 def add_pypath(path: pathlib.Path) -> None:
52 """Add the specified path to PYTHONPATH or set it if it isn't set at all."""
53 current: Final = pyenv.get("PYTHONPATH")
54 if current is None:
55 pyenv["PYTHONPATH"] = str(path)
56 else:
57 pyenv["PYTHONPATH"] = f"{path}:{current}"
58
59 if cfg.pybuild is not None:
60 pypath: Final = (
61 cfg.pybuild / f"cpython3_{sys.version_info[0]}.{sys.version_info[1]}" / "build"
62 )
63 if not pypath.is_dir():
64 sys.exit(f"Not a directory: {pypath}")
65 add_pypath(pypath)
66
67 if cfg.pypath is not None:
68 add_pypath(cfg.pypath)
69
70 subprocess.check_call( # noqa: S603
71 [sys.executable, "-B", "-u", "--", destf],
72 cwd=tempd,
73 env=pyenv,
74 )
75
76
77 @click.command(name="temptest")
78 @click.option(
79 "--features",
80 is_flag=True,
81 is_eager=True,
82 callback=arg_features,
83 help="display program features information and exit",
84 )
85 @click.option(
86 "--pybuild",
87 type=click.Path(
88 exists=True,
89 file_okay=False,
90 dir_okay=True,
91 path_type=pathlib.Path,
92 resolve_path=True,
93 ),
94 help="the .pybuild directory to look for the built Python module in",
95 )
96 @click.option(
97 "--pypath",
98 type=click.Path(
99 exists=True,
100 file_okay=False,
101 dir_okay=True,
102 path_type=pathlib.Path,
103 resolve_path=True,
104 ),
105 help="the built Python module parent directory to add to PYTHONPATH",
106 )
107 @click.option(
108 "--quiet",
109 "-q",
110 is_flag=True,
111 help="quiet operation; only display warning and error messages",
112 )
113 @click.option(
114 "--srcdir",
115 type=click.Path(
116 exists=True,
117 file_okay=False,
118 dir_okay=True,
119 path_type=pathlib.Path,
120 resolve_path=True,
121 ),
122 required=True,
123 help="the directory to look for the regex module source code",
124 )
125 @click.option("--verbose", "-v", is_flag=True, help="verbose operation; display diagnostic output")
126 def main( # noqa: PLR0913 # yep, we like command-line arguments
127 *,
128 features: bool,
129 pybuild: pathlib.Path | None,
130 pypath: pathlib.Path | None,
131 quiet: bool,
132 srcdir: pathlib.Path,
133 verbose: bool,
134 ) -> None:
135 """Parse command-line arguments, copy test-regex to a temporary directory, run it."""
136 if features:
137 sys.exit("Internal error: how did we get to main() with features=True?")
138
139 cfg: Final = defs.Config(
140 log=util.build_logger(quiet=quiet, verbose=verbose),
141 pybuild=pybuild,
142 pypath=pypath,
143 srcdir=srcdir,
144 verbose=verbose,
145 )
146 run(cfg)
147
148
149 if __name__ == "__main__":
150 main()