Debian Codegraph @

dhpython/build/options.py

raw symbols SCIP
1 # Copyright © 2025 Stefano Rivera <[email protected]>
2 #
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
9 #
10 # The above copyright notice and this permission notice shall be included in
11 # all copies or substantial portions of the Software.
12 #
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 # THE SOFTWARE.
20
21 from argparse import ArgumentParser, Action, Namespace
22 from collections.abc import Sequence
23 from dataclasses import dataclass, field
24 from typing import Any, override
25 from os import environ, getcwd
26
27 from dhpython.version import Version
28
29 DEFAULT_EXT_PATTERN = r"\.so(\.[^/]*)?$"
30 DEFAULT_INSTALL_DIR = "/usr/lib/python{version}/dist-packages"
31
32 @dataclass
33 class PybuildOptions:
34 verbose: bool = environ.get("PYBUILD_VERBOSE") == "1"
35 quiet: bool = environ.get("PYBUILD_QUIET") == "1"
36 really_quiet: bool = environ.get("PYBUILD_RQUIET") == "1"
37 detect_only: bool = False
38 clean_only: bool = False
39 configure_only: bool = False
40 build_only: bool = False
41 install_only: bool = False
42 test_only: bool = False
43 autopkgtest_only: bool = False
44 list_systems: bool = False
45 print_args: list[str] = field(default_factory=list)
46 before_clean: str | None = None
47 clean_args: str | None = None
48 after_clean: str | None = None
49 before_configure: str | None = None
50 configure_args: str | None = None
51 after_configure: str | None = None
52 before_build: str | None = None
53 build_args: str | None = None
54 after_build: str | None = None
55 before_install: str | None = None
56 install_args: str | None = None
57 after_install: str | None = None
58 before_test: str | None = None
59 test_args: str | None = None
60 after_test: str | None = None
61 test_nose: bool = environ.get("PYBUILD_TEST_NOSE") == "1"
62 test_nose2: bool = environ.get("PYBUILD_TEST_NOSE2") == "1"
63 test_pytest: bool = environ.get("PYBUILD_TEST_PYTEST") == "1"
64 test_tox: bool = environ.get("PYBUILD_TEST_TOX") == "1"
65 test_stestr: bool = environ.get("PYBUILD_TEST_STESTR") == "1"
66 test_unittest: bool = environ.get("PYBUILD_TEST_UNITTEST") == "1"
67 test_custom: bool = environ.get("PYBUILD_TEST_CUSTOM") == "1"
68 dir: str = environ.get("PYBUILD_DIR", getcwd())
69 # We handle this default manually, because --name affects it
70 destdir: str | None = None
71 ext_destdir: str | None = environ.get("PYBUILD_EXT_DESTDIR")
72 ext_pattern: str = environ.get("PYBUILD_EXT_PATTERN", DEFAULT_EXT_PATTERN)
73 ext_sub_pattern: str | None = environ.get("PYBUILD_EXT_SUB_PATTERN")
74 ext_sub_repl: str | None = environ.get("PYBUILD_EXT_SUB_REPL")
75 install_dir: str = DEFAULT_INSTALL_DIR
76 name: str | None = environ.get("PYBUILD_NAME")
77 system: str | None = environ.get("PYBUILD_SYSTEM")
78 versions: list[Version] = field(default_factory=list)
79 interpreter: list[str] = field(default_factory=list)
80 disable: str | None = None
81 parallel: int = 1
82 custom_tests: bool = False
83
84
85 class AppendVersions(Action):
86 """
87 Append possibly-space-separated versions.
88
89 This allows the use of -p "$(py3versions -rv)".
90 """
91
92 @override
93 def __call__(
94 self,
95 parser: ArgumentParser,
96 namespace: Namespace,
97 values: str | Sequence[Any] | None,
98 option_string: str | None = None,
99 ) -> None:
100 assert isinstance(values, str)
101 for value in values.split():
102 getattr(namespace, self.dest).append(Version(value))
103
104
105 def build_parser() -> ArgumentParser:
106 usage = "%(prog)s [ACTION] [BUILD SYSTEM ARGS] [DIRECTORIES] [OPTIONS]"
107 parser = ArgumentParser(usage=usage)
108 parser.add_argument(
109 "-v",
110 "--verbose",
111 action="store_true",
112 help="turn verbose mode on",
113 )
114 parser.add_argument(
115 "-q",
116 "--quiet",
117 action="store_true",
118 help="doesn't show external command's output",
119 )
120 parser.add_argument(
121 "-qq",
122 "--really-quiet",
123 action="store_true",
124 help="be quiet",
125 )
126 parser.add_argument("--version", action="version", version="%(prog)s DEVELV")
127
128 action = parser.add_argument_group(
129 "ACTION",
130 """The default is to build,
131 install and test the library using detected build system version by
132 version. Selecting one of following actions, will invoke given action
133 for all versions - one by one - which (contrary to the default action)
134 in some build systems can overwrite previous results.""",
135 )
136 action.add_argument(
137 "--detect",
138 action="store_true",
139 dest="detect_only",
140 help="return the name of detected build system",
141 )
142 action.add_argument(
143 "--clean",
144 action="store_true",
145 dest="clean_only",
146 help="clean files using auto-detected build system specific methods",
147 )
148 action.add_argument(
149 "--configure",
150 action="store_true",
151 dest="configure_only",
152 help="invoke configure step for all requested Python versions",
153 )
154 action.add_argument(
155 "--build",
156 action="store_true",
157 dest="build_only",
158 help="invoke build step for all requested Python versions",
159 )
160 action.add_argument(
161 "--install",
162 action="store_true",
163 dest="install_only",
164 help="invoke install step for all requested Python versions",
165 )
166 action.add_argument(
167 "--test",
168 action="store_true",
169 dest="test_only",
170 help="invoke tests for auto-detected build system",
171 )
172 action.add_argument(
173 "--autopkgtest",
174 action="store_true",
175 dest="autopkgtest_only",
176 help="invoke autopkgtests for auto-detected build system",
177 )
178 action.add_argument(
179 "--list-systems",
180 action="store_true",
181 help="list available build systems and exit",
182 )
183 action.add_argument(
184 "--print",
185 action="append",
186 dest="print_args",
187 help="print pybuild's internal parameters",
188 )
189
190 arguments = parser.add_argument_group(
191 "BUILD SYSTEM ARGS",
192 """
193 Additional arguments passed to the build system.
194 --system=custom requires complete command.""",
195 )
196 arguments.add_argument(
197 "--before-clean", metavar="CMD", help="invoked before the clean command"
198 )
199 arguments.add_argument("--clean-args", metavar="ARGS")
200 arguments.add_argument(
201 "--after-clean", metavar="CMD", help="invoked after the clean command"
202 )
203
204 arguments.add_argument(
205 "--before-configure", metavar="CMD", help="invoked before the configure command"
206 )
207 arguments.add_argument("--configure-args", metavar="ARGS")
208 arguments.add_argument(
209 "--after-configure", metavar="CMD", help="invoked after the configure command"
210 )
211
212 arguments.add_argument(
213 "--before-build", metavar="CMD", help="invoked before the build command"
214 )
215 arguments.add_argument("--build-args", metavar="ARGS")
216 arguments.add_argument(
217 "--after-build", metavar="CMD", help="invoked after the build command"
218 )
219
220 arguments.add_argument(
221 "--before-install", metavar="CMD", help="invoked before the install command"
222 )
223 arguments.add_argument("--install-args", metavar="ARGS")
224 arguments.add_argument(
225 "--after-install", metavar="CMD", help="invoked after the install command"
226 )
227
228 arguments.add_argument(
229 "--before-test", metavar="CMD", help="invoked before the test command"
230 )
231 arguments.add_argument("--test-args", metavar="ARGS")
232 arguments.add_argument(
233 "--after-test", metavar="CMD", help="invoked after the test command"
234 )
235
236 tests = parser.add_argument_group(
237 "TESTS",
238 """\
239 unittest\'s discover is used by default (if available)""",
240 )
241 tests.add_argument(
242 "--test-nose",
243 action="store_true",
244 help="use nose module in --test step",
245 )
246 tests.add_argument(
247 "--test-nose2",
248 action="store_true",
249 help="use nose2 module in --test step",
250 )
251 tests.add_argument(
252 "--test-pytest",
253 action="store_true",
254 help="use pytest module in --test step",
255 )
256 tests.add_argument(
257 "--test-tox",
258 action="store_true",
259 help="use tox in --test step",
260 )
261 tests.add_argument(
262 "--test-stestr",
263 action="store_true",
264 help="use stestr in --test step",
265 )
266 tests.add_argument(
267 "--test-unittest",
268 action="store_true",
269 help="use unittest in --test step",
270 )
271 tests.add_argument(
272 "--test-custom",
273 action="store_true",
274 help="use custom command in --test step",
275 )
276
277 dirs = parser.add_argument_group("DIRECTORIES")
278 dirs.add_argument(
279 "-d",
280 "--dir",
281 action="store",
282 metavar="DIR",
283 help="source files directory - base for other relative dirs [default: CWD]",
284 )
285 dirs.add_argument(
286 "--dest-dir",
287 action="store",
288 metavar="DIR",
289 dest="destdir",
290 help="destination directory [default: debian/tmp]",
291 )
292 dirs.add_argument(
293 "--ext-dest-dir",
294 action="store",
295 metavar="DIR",
296 dest="ext_destdir",
297 help="destination directory for .so files",
298 )
299 dirs.add_argument(
300 "--ext-pattern",
301 action="store",
302 metavar="PATTERN",
303 help="regular expression for files that should be moved"
304 " if --ext-dest-dir is set [default: .so files]",
305 )
306 dirs.add_argument(
307 "--ext-sub-pattern",
308 action="store",
309 metavar="PATTERN",
310 help="pattern to change --ext-pattern's filename or path",
311 )
312 dirs.add_argument(
313 "--ext-sub-repl",
314 action="store",
315 metavar="PATTERN",
316 help="replacement for match from --ext-sub-pattern," " empty string by default",
317 )
318 dirs.add_argument(
319 "--install-dir",
320 action="store",
321 metavar="DIR",
322 help="installation directory [default: .../dist-packages]",
323 )
324 dirs.add_argument(
325 "--name",
326 action="store",
327 help="use this name to guess destination directories",
328 )
329
330 limit = parser.add_argument_group("LIMITATIONS")
331 limit.add_argument(
332 "-s",
333 "--system",
334 help="select a build system [default: auto-detection]",
335 )
336 limit.add_argument(
337 "-p",
338 "--pyver",
339 action=AppendVersions,
340 dest="versions",
341 help="""build for Python VERSION.
342 This option can be used multiple times
343 [default: all supported Python 3.X versions]""",
344 )
345 limit.add_argument(
346 "-i",
347 "--interpreter",
348 action="append",
349 help="change interpreter [default: python{version}]",
350 )
351 limit.add_argument(
352 "--disable", metavar="ITEMS", help="disable action, interpreter or version"
353 )
354 limit.add_argument(
355 "-j",
356 "--parallel",
357 help="specify number of parallel jobs [default: 1]",
358 )
359 return parser