dev-isolator 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dev_isolator/__init__.py +13 -0
- dev_isolator/__main__.py +15 -0
- dev_isolator/base_image.py +429 -0
- dev_isolator/config.py +650 -0
- dev_isolator/constants.py +58 -0
- dev_isolator/container_image.py +302 -0
- dev_isolator/container_manager.py +516 -0
- dev_isolator/data/Containerfile +32 -0
- dev_isolator/data/addons/addon.claude.jinja2 +16 -0
- dev_isolator/data/addons/addon.opencode.jinja2 +16 -0
- dev_isolator/data/addons/config.claude.yaml +292 -0
- dev_isolator/data/addons/config.opencode.py +4 -0
- dev_isolator/data/addons/config.opencode.yaml +8 -0
- dev_isolator/data/base/Containerfile.almalinux-10 +5 -0
- dev_isolator/data/base/Containerfile.almalinux-9 +5 -0
- dev_isolator/data/base/Containerfile.archlinux +6 -0
- dev_isolator/data/base/Containerfile.debian-13 +6 -0
- dev_isolator/data/base/Containerfile.fedora-43 +5 -0
- dev_isolator/data/base/Containerfile.fedora-44 +5 -0
- dev_isolator/data/base/Containerfile.fedora-45 +5 -0
- dev_isolator/data/base/Containerfile.fedora-rawhide +6 -0
- dev_isolator/data/base/Containerfile.opensuse-leap +4 -0
- dev_isolator/data/base/Containerfile.opensuse-tumbleweed +5 -0
- dev_isolator/data/base/Containerfile.ubuntu-22.04 +6 -0
- dev_isolator/data/base/Containerfile.ubuntu-24.04 +7 -0
- dev_isolator/data/base/Containerfile.ubuntu-26.04 +8 -0
- dev_isolator/data/config.yaml +136 -0
- dev_isolator/data/setup/add_user.containerfile.jinja2 +36 -0
- dev_isolator/main.py +931 -0
- dev_isolator/podman_utils.py +116 -0
- dev_isolator/utils.py +86 -0
- dev_isolator/waitspinner.py +115 -0
- dev_isolator-0.1.0.dist-info/METADATA +131 -0
- dev_isolator-0.1.0.dist-info/RECORD +38 -0
- dev_isolator-0.1.0.dist-info/WHEEL +5 -0
- dev_isolator-0.1.0.dist-info/entry_points.txt +3 -0
- dev_isolator-0.1.0.dist-info/licenses/LICENSE +674 -0
- dev_isolator-0.1.0.dist-info/top_level.txt +1 -0
dev_isolator/main.py
ADDED
|
@@ -0,0 +1,931 @@
|
|
|
1
|
+
"""CLI entry point for dev-isolator.
|
|
2
|
+
|
|
3
|
+
dev-isolator manages a per-project, podman based development
|
|
4
|
+
environment. For every project that contains a ``.devisolator``
|
|
5
|
+
directory (holding a ``Containerfile`` and optional addon
|
|
6
|
+
configuration) it builds an image named
|
|
7
|
+
``dev-isolator:<slug_of_the_project_path>--<tag>`` and runs commands inside a
|
|
8
|
+
container created from it, with the project directory mounted at
|
|
9
|
+
``/workspace``. This keeps build tools, caches and dependencies isolated
|
|
10
|
+
from the host while the sources stay in place.
|
|
11
|
+
|
|
12
|
+
The sub commands are:
|
|
13
|
+
|
|
14
|
+
``init``
|
|
15
|
+
Initialize a local ``.devisolator`` configuration for the current
|
|
16
|
+
project.
|
|
17
|
+
``build``
|
|
18
|
+
Build the image for the current project.
|
|
19
|
+
``shell``
|
|
20
|
+
Open an interactive shell inside the container (the default when
|
|
21
|
+
no subcommand is given).
|
|
22
|
+
``run <command> ...``
|
|
23
|
+
Run an arbitrary command inside the container.
|
|
24
|
+
``target <name> [args...]``
|
|
25
|
+
Run a predefined target command from the ``targets`` section of
|
|
26
|
+
``config.yaml`` inside the container, passing the arguments
|
|
27
|
+
following the target name to it.
|
|
28
|
+
``opencode`` / ``claude``
|
|
29
|
+
Run the respective AI coding tool inside the container.
|
|
30
|
+
``clean``
|
|
31
|
+
Stop, kill and remove the containers of the current project and
|
|
32
|
+
remove its image (or, with ``--all``, every dev-isolator image and
|
|
33
|
+
container, or with ``--local``, every image and container of the
|
|
34
|
+
current project).
|
|
35
|
+
``list-base``
|
|
36
|
+
List the base image definitions bundled with dev-isolator.
|
|
37
|
+
``list-targets``
|
|
38
|
+
List the targets defined in the ``targets`` section of the active
|
|
39
|
+
``config.yaml``.
|
|
40
|
+
``help``
|
|
41
|
+
Show the help message.
|
|
42
|
+
|
|
43
|
+
The ``--shell/-s`` option, given before the sub command, selects the
|
|
44
|
+
shell used by ``shell`` and by ``run``/``opencode``/``claude`` when they
|
|
45
|
+
start an interactive shell.
|
|
46
|
+
|
|
47
|
+
The ``target`` sub command takes only positional arguments: the first
|
|
48
|
+
one names a target from the ``targets`` section of ``config.yaml``,
|
|
49
|
+
whose command is a jinja2 template in which the extra arguments are
|
|
50
|
+
available as ``args[0]``, ``args[1]``, ... and as ``all_args`` (all of
|
|
51
|
+
them joined with spaces). A placeholder marked with the ``optional``
|
|
52
|
+
filter is only used when the argument was given on the command line;
|
|
53
|
+
the token holding it (and a preceding option token) is dropped
|
|
54
|
+
otherwise. The command may be a single command line (a string) or a
|
|
55
|
+
script given line by line in a list; the script lines run line by line
|
|
56
|
+
in one shell session inside the container, which stops at the first
|
|
57
|
+
failing line.
|
|
58
|
+
|
|
59
|
+
Copyright (C) 2026 Martin Köhler
|
|
60
|
+
|
|
61
|
+
This program comes with ABSOLUTELY NO WARRANTY.
|
|
62
|
+
This is free software, and you are welcome to redistribute it
|
|
63
|
+
under certain conditions; see LICENSE for details.
|
|
64
|
+
SPDX-License-Identifier: GPL-3.0-or-later
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
import argparse
|
|
68
|
+
import sys
|
|
69
|
+
import os
|
|
70
|
+
import re
|
|
71
|
+
import shlex
|
|
72
|
+
import shutil
|
|
73
|
+
|
|
74
|
+
import jinja2
|
|
75
|
+
|
|
76
|
+
from textwrap import dedent
|
|
77
|
+
from .container_manager import ContainerDefintion
|
|
78
|
+
from .container_image import replace_from_images
|
|
79
|
+
from .utils import set_verbosity, search_path, Verbosity, dprint
|
|
80
|
+
from .constants import (BUNDLE_DIR, CONFIG_FILE_NAME, ADDON_DIR,
|
|
81
|
+
DEV_ISOLATOR_DIR, DEFAULT_BASE)
|
|
82
|
+
from .base_image import BaseImage, detect_package_manager
|
|
83
|
+
from pathlib import Path
|
|
84
|
+
from .config import Configuration
|
|
85
|
+
|
|
86
|
+
DEFAULT_SHELL = "bash"
|
|
87
|
+
|
|
88
|
+
#: Sentinel returned by the ``optional`` jinja2 filter for an argument
|
|
89
|
+
#: that was not given on the command line; after rendering, every token
|
|
90
|
+
#: still containing it is dropped from the target's command.
|
|
91
|
+
_OPTIONAL_UNSET = "\x00dev-isolator-unset-optional\x00"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def create_parser() -> argparse.ArgumentParser:
|
|
95
|
+
"""Create and configure the command line argument parser.
|
|
96
|
+
|
|
97
|
+
Defines the global options (``--verbose/-v``, ``--shell/-s`` and
|
|
98
|
+
``--config/-c``) on the top level parser and one subparser per sub
|
|
99
|
+
command. The
|
|
100
|
+
``opencode``, ``claude``, ``run`` and ``target`` parsers disable
|
|
101
|
+
their own help handling and define no arguments, so that unknown
|
|
102
|
+
arguments following them are returned verbatim in the remainder of
|
|
103
|
+
``parse_known_args`` and passed through to the command inside the
|
|
104
|
+
container (for ``target``, the first remainder token names the
|
|
105
|
+
target to run).
|
|
106
|
+
"""
|
|
107
|
+
description = """\
|
|
108
|
+
Manage an isolated, podman based development environment
|
|
109
|
+
for the current project.
|
|
110
|
+
|
|
111
|
+
Images are named dev-isolator:<slug_of_the_project_path> and
|
|
112
|
+
are built from the ``Containerfile`` found in the
|
|
113
|
+
``.devisolator`` directory of the project or form a default one.
|
|
114
|
+
Commands started with dev-isolator run inside a container created
|
|
115
|
+
from this image, with the project directory mounted at
|
|
116
|
+
``/workspace``, so tools and dependencies stay isolated from the
|
|
117
|
+
host.
|
|
118
|
+
"""
|
|
119
|
+
epilog = """\
|
|
120
|
+
examples:
|
|
121
|
+
dev-isolator build build the image of the current project
|
|
122
|
+
dev-isolator --shell zsh open a zsh in the container
|
|
123
|
+
dev-isolator open an interactive shell in the container
|
|
124
|
+
dev-isolator run make test run "make test" inside the container
|
|
125
|
+
dev-isolator target test run the "test" target from the
|
|
126
|
+
"targets" section of config.yaml
|
|
127
|
+
dev-isolator opencode run opencode inside the container
|
|
128
|
+
dev-isolator clean --all remove every dev-isolator image
|
|
129
|
+
dev-isolator list-base list the available base image definitions
|
|
130
|
+
dev-isolator list-targets list the targets of the active config.yaml
|
|
131
|
+
|
|
132
|
+
The dev-isolator command can also be abbreviated by `di`
|
|
133
|
+
"""
|
|
134
|
+
parser = argparse.ArgumentParser(
|
|
135
|
+
prog="dev-isolator",
|
|
136
|
+
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
137
|
+
description=dedent(description),
|
|
138
|
+
epilog=dedent(epilog),
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
subparsers = parser.add_subparsers(dest="command")
|
|
142
|
+
|
|
143
|
+
parser.add_argument(
|
|
144
|
+
"--verbose",
|
|
145
|
+
"-v",
|
|
146
|
+
action="count",
|
|
147
|
+
default=0,
|
|
148
|
+
help="Increase verbosity. Repeat for more details (-vv, -vvv).",
|
|
149
|
+
)
|
|
150
|
+
parser.add_argument(
|
|
151
|
+
"--shell",
|
|
152
|
+
"-s",
|
|
153
|
+
type=str,
|
|
154
|
+
default=DEFAULT_SHELL,
|
|
155
|
+
help="Shell to start inside the container. "
|
|
156
|
+
f"Default: {DEFAULT_SHELL}.",
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
parser.add_argument(
|
|
160
|
+
"-c",
|
|
161
|
+
"--config",
|
|
162
|
+
type=str,
|
|
163
|
+
default=None,
|
|
164
|
+
help="Name of the sub-configuration (a subdirectory of the "
|
|
165
|
+
".devisolator directory holding its own ``Containerfile`` and "
|
|
166
|
+
"``config.yaml``) to use instead of the project's default "
|
|
167
|
+
"one."
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
init_parser = subparsers.add_parser(
|
|
171
|
+
"init",
|
|
172
|
+
help="Initialize a clean local configuration. Sub-configurations"
|
|
173
|
+
" are also allowed by specifying the ``--config``` flag.",
|
|
174
|
+
description="Initialize a local configuration.")
|
|
175
|
+
init_parser.add_argument(
|
|
176
|
+
"-f",
|
|
177
|
+
"--force",
|
|
178
|
+
dest="init_force",
|
|
179
|
+
action="store_true",
|
|
180
|
+
help="Enforce overwriting existing files."
|
|
181
|
+
)
|
|
182
|
+
init_parser.add_argument(
|
|
183
|
+
"-b",
|
|
184
|
+
"--base",
|
|
185
|
+
dest="init_base",
|
|
186
|
+
type=str,
|
|
187
|
+
default=DEFAULT_BASE,
|
|
188
|
+
help="Specify the base image. See ``di list-base`` for possible "
|
|
189
|
+
f"values. Default = {DEFAULT_BASE}"
|
|
190
|
+
)
|
|
191
|
+
subparsers.add_parser(
|
|
192
|
+
"list-base",
|
|
193
|
+
help="List the base image definitions bundled with dev-isolator",
|
|
194
|
+
description="List the base image definitions bundled with "
|
|
195
|
+
"dev-isolator, i.e. the images a project "
|
|
196
|
+
"``Containerfile`` can use as its base."
|
|
197
|
+
)
|
|
198
|
+
subparsers.add_parser(
|
|
199
|
+
"list-targets",
|
|
200
|
+
help="List the targets of the active config.yaml",
|
|
201
|
+
description="List the target names defined in the "
|
|
202
|
+
"``targets`` section of the active "
|
|
203
|
+
"``config.yaml``, i.e. the targets a project "
|
|
204
|
+
"can run with ``target <name>``."
|
|
205
|
+
)
|
|
206
|
+
|
|
207
|
+
build_parser = subparsers.add_parser(
|
|
208
|
+
"build",
|
|
209
|
+
help="Build the current dev-isolator image",
|
|
210
|
+
description="Build the image from the project settings or the "
|
|
211
|
+
"default one."
|
|
212
|
+
)
|
|
213
|
+
build_parser.add_argument(
|
|
214
|
+
"-k",
|
|
215
|
+
"--keep-container-file",
|
|
216
|
+
dest="keep_container_file",
|
|
217
|
+
action="store_true",
|
|
218
|
+
help="Keep the generated Containerfile in case of an error."
|
|
219
|
+
)
|
|
220
|
+
|
|
221
|
+
subparsers.add_parser(
|
|
222
|
+
"shell",
|
|
223
|
+
help="Open a terminal/shell inside a container "
|
|
224
|
+
"dev-isolator-<slug_of_the_current_path>--<tag>-<8 random "
|
|
225
|
+
"characters>.",
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
clean_parser = subparsers.add_parser(
|
|
229
|
+
"clean",
|
|
230
|
+
help="Remove the current dev-isolator image.",
|
|
231
|
+
)
|
|
232
|
+
clean_group = clean_parser.add_mutually_exclusive_group()
|
|
233
|
+
clean_group.add_argument(
|
|
234
|
+
"-a",
|
|
235
|
+
"--all",
|
|
236
|
+
dest="clean_all",
|
|
237
|
+
action="store_true",
|
|
238
|
+
help="Remove all dev-isolator images instead of only "
|
|
239
|
+
"the current one.",
|
|
240
|
+
)
|
|
241
|
+
clean_group.add_argument(
|
|
242
|
+
"-l",
|
|
243
|
+
"--local",
|
|
244
|
+
dest="clean_local",
|
|
245
|
+
action="store_true",
|
|
246
|
+
help="Remove all images from the current .devisolator directory."
|
|
247
|
+
)
|
|
248
|
+
|
|
249
|
+
subparsers.add_parser(
|
|
250
|
+
"opencode",
|
|
251
|
+
add_help=False,
|
|
252
|
+
help="Run opencode inside a container "
|
|
253
|
+
"dev-isolator-<slug_of_the_current_path>--<tag>-<8 random "
|
|
254
|
+
"characters>, passing all following arguments to it "
|
|
255
|
+
"(for example ``opencode --port 4096``).",
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
subparsers.add_parser(
|
|
259
|
+
"claude",
|
|
260
|
+
add_help=False,
|
|
261
|
+
help="Run claude inside a container "
|
|
262
|
+
"dev-isolator-<slug_of_the_current_path>--<tag>-<8 random "
|
|
263
|
+
"characters>, passing all following arguments to it "
|
|
264
|
+
"(for example ``claude --model xyz``).",
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
subparsers.add_parser(
|
|
268
|
+
"run",
|
|
269
|
+
add_help=False,
|
|
270
|
+
help="Run an arbitrary command inside the container, passing all "
|
|
271
|
+
"following arguments through to it "
|
|
272
|
+
"(for example ``run make test``).",
|
|
273
|
+
)
|
|
274
|
+
|
|
275
|
+
subparsers.add_parser(
|
|
276
|
+
"target",
|
|
277
|
+
add_help=False,
|
|
278
|
+
help="Run a predefined target command from the 'targets' section "
|
|
279
|
+
"of config.yaml inside the container. Takes only positional "
|
|
280
|
+
"arguments: the first one names the target, all following "
|
|
281
|
+
"ones are passed to the target's command "
|
|
282
|
+
"(for example ``target test -k foo``).",
|
|
283
|
+
)
|
|
284
|
+
|
|
285
|
+
subparsers.add_parser(
|
|
286
|
+
"help",
|
|
287
|
+
help="Show this help message and exit.",
|
|
288
|
+
description="Show the dev-isolator help message and exit.",
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
return parser
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
def global_options_parser() -> argparse.ArgumentParser:
|
|
295
|
+
"""Return a parser exposing only the global options.
|
|
296
|
+
|
|
297
|
+
The global options (``--verbose``, ``--shell`` and ``--config``)
|
|
298
|
+
live on the top level parser and must precede the sub command. A
|
|
299
|
+
parser with just
|
|
300
|
+
these options and no sub commands consumes the leading global
|
|
301
|
+
options and leaves the sub command — together with everything
|
|
302
|
+
following it and any options that precede it and are not global —
|
|
303
|
+
in the remainder, so the tokens before the sub command can be
|
|
304
|
+
validated on their own.
|
|
305
|
+
"""
|
|
306
|
+
parser = argparse.ArgumentParser(prog="dev-isolator", add_help=False)
|
|
307
|
+
parser.add_argument("--verbose", "-v", action="count", default=0)
|
|
308
|
+
parser.add_argument("--shell", "-s", type=str, default=DEFAULT_SHELL)
|
|
309
|
+
parser.add_argument("--config", "-c", type=str, default=None)
|
|
310
|
+
return parser
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def reject_precommand_options(parser, argv) -> None:
|
|
314
|
+
"""Abort on unknown options that appear before the sub command.
|
|
315
|
+
|
|
316
|
+
The global options precede the sub command; everything after it
|
|
317
|
+
belongs to the sub command (and, for the pass-through commands, to
|
|
318
|
+
the command inside the container). Parsing ``argv`` with the
|
|
319
|
+
global-only parser leaves the sub command and any preceding unknown
|
|
320
|
+
options in the remainder, so the options before the first
|
|
321
|
+
non-option token — the sub command — are rejected as unrecognized.
|
|
322
|
+
"""
|
|
323
|
+
_, remainder = global_options_parser().parse_known_args(argv)
|
|
324
|
+
command_index = next(
|
|
325
|
+
(i for i, token in enumerate(remainder)
|
|
326
|
+
if not token.startswith("-")),
|
|
327
|
+
len(remainder),
|
|
328
|
+
)
|
|
329
|
+
unknown = remainder[:command_index]
|
|
330
|
+
if unknown:
|
|
331
|
+
parser.error("unrecognized arguments: " + " ".join(unknown))
|
|
332
|
+
|
|
333
|
+
|
|
334
|
+
def parse_args(argv=None) -> argparse.Namespace:
|
|
335
|
+
"""Parse command line arguments into a namespace.
|
|
336
|
+
|
|
337
|
+
Parse ``argv`` (by default ``sys.argv[1:]``) with the parser from
|
|
338
|
+
``create_parser``. The global options (``--verbose``,
|
|
339
|
+
``--shell`` and ``--config``) live on the top level parser, so they
|
|
340
|
+
must precede the sub command; unknown options before the sub command
|
|
341
|
+
are rejected.
|
|
342
|
+
|
|
343
|
+
The ``opencode``, ``claude``, ``run`` and ``target`` sub commands
|
|
344
|
+
disable their own help handling and define no arguments, so every
|
|
345
|
+
token following them is returned verbatim in the remainder by
|
|
346
|
+
``parse_known_args`` and stored on the ``opencode_args``,
|
|
347
|
+
``claude_args``, ``run_args`` and ``target_args`` attributes. This
|
|
348
|
+
keeps options of the command inside the container (for example
|
|
349
|
+
``opencode --port 4096``) from clashing with those of dev-isolator.
|
|
350
|
+
Without an explicit subcommand, behave as if ``shell`` was requested
|
|
351
|
+
with the default shell.
|
|
352
|
+
"""
|
|
353
|
+
if argv is None:
|
|
354
|
+
argv = sys.argv[1:]
|
|
355
|
+
|
|
356
|
+
parser = create_parser()
|
|
357
|
+
args, extras = parser.parse_known_args(argv)
|
|
358
|
+
|
|
359
|
+
reject_precommand_options(parser, argv)
|
|
360
|
+
|
|
361
|
+
if args.command is None:
|
|
362
|
+
args.command = "shell"
|
|
363
|
+
|
|
364
|
+
if args.command == "run":
|
|
365
|
+
args.run_args = extras
|
|
366
|
+
elif args.command == "opencode":
|
|
367
|
+
args.opencode_args = extras
|
|
368
|
+
elif args.command == "claude":
|
|
369
|
+
args.claude_args = extras
|
|
370
|
+
elif args.command == "target":
|
|
371
|
+
args.target_args = extras
|
|
372
|
+
return args
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def shell(definition, args) -> int:
|
|
376
|
+
"""Handle the ``shell`` sub command.
|
|
377
|
+
|
|
378
|
+
Look up the container definition of the current project and start
|
|
379
|
+
an interactive shell (``args.shell``) inside its container.
|
|
380
|
+
Returns the exit status of the container, or 1 on error.
|
|
381
|
+
"""
|
|
382
|
+
try:
|
|
383
|
+
return definition.run_container(args.shell)
|
|
384
|
+
except Exception as e:
|
|
385
|
+
print(f"An error occured: {e}", file=sys.stderr)
|
|
386
|
+
return 1
|
|
387
|
+
|
|
388
|
+
|
|
389
|
+
def build(definition, args) -> int:
|
|
390
|
+
"""Handle the ``build`` sub command.
|
|
391
|
+
|
|
392
|
+
Look up the container definition of the current project and build
|
|
393
|
+
its image. Wwhen ``args.keep_container_file`` is set, keep the
|
|
394
|
+
generated Containerfile in the current directory on a build error.
|
|
395
|
+
Returns 0 on success, or 1 on error.
|
|
396
|
+
"""
|
|
397
|
+
try:
|
|
398
|
+
definition.build_container(keep_container_file=args.keep_container_file
|
|
399
|
+
)
|
|
400
|
+
except Exception as e:
|
|
401
|
+
print(f"An error occured: {e}", file=sys.stderr)
|
|
402
|
+
return 1
|
|
403
|
+
return 0
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def clean(definition, args) -> int:
|
|
407
|
+
"""Handle the ``clean`` sub command.
|
|
408
|
+
|
|
409
|
+
Remove the image of the current project, every dev-isolator image
|
|
410
|
+
when ``args.clean_all`` is set, or every image of the current project
|
|
411
|
+
when ``args.clean_local`` is set. Matching containers are stopped
|
|
412
|
+
(eventually killed) and removed as well. Returns 0 on success, or 1
|
|
413
|
+
on error.
|
|
414
|
+
"""
|
|
415
|
+
try:
|
|
416
|
+
if args.clean_all:
|
|
417
|
+
definition.clean_all_images()
|
|
418
|
+
elif args.clean_local:
|
|
419
|
+
definition.clean_local_images()
|
|
420
|
+
else:
|
|
421
|
+
definition.clean_image()
|
|
422
|
+
except Exception as e:
|
|
423
|
+
print(f"An error occured: {e}", file=sys.stderr)
|
|
424
|
+
return 1
|
|
425
|
+
return 0
|
|
426
|
+
|
|
427
|
+
|
|
428
|
+
def init_project(args) -> int:
|
|
429
|
+
"""Handle the ``init`` sub command.
|
|
430
|
+
|
|
431
|
+
Create the ``.devisolator`` directory of the current project
|
|
432
|
+
(or, with ``args.config``, its ``.devisolator/<config>``
|
|
433
|
+
sub-configuration) and fill it with the ``Containerfile`` and
|
|
434
|
+
``config.yaml`` bundled with dev-isolator. The bundled
|
|
435
|
+
``Containerfile``'s ``FROM`` instruction is rewritten to
|
|
436
|
+
reference the selected base image (when one is given):
|
|
437
|
+
``args.init_base`` (default ``ubuntu:26.04``) must match the
|
|
438
|
+
``DI_BASE`` value of one of the bundled base Containerfiles; an
|
|
439
|
+
unknown base image aborts with an error.
|
|
440
|
+
|
|
441
|
+
Existing files are kept and reported unless ``args.init_force``
|
|
442
|
+
(``init -f``) is given, in which case they are overwritten.
|
|
443
|
+
|
|
444
|
+
:param args: The parsed command line arguments (from
|
|
445
|
+
:func:`parse_args`).
|
|
446
|
+
:returns: 0 on success.
|
|
447
|
+
:raises RuntimeError: if the requested base image is not bundled
|
|
448
|
+
with dev-isolator.
|
|
449
|
+
"""
|
|
450
|
+
force = args.init_force
|
|
451
|
+
config = args.config
|
|
452
|
+
|
|
453
|
+
if config:
|
|
454
|
+
container_file = (Path(os.getcwd()) / DEV_ISOLATOR_DIR / config
|
|
455
|
+
/ "Containerfile")
|
|
456
|
+
config_file = (Path(os.getcwd()) / DEV_ISOLATOR_DIR / config
|
|
457
|
+
/ CONFIG_FILE_NAME)
|
|
458
|
+
user_dir = Path(os.getcwd()) / DEV_ISOLATOR_DIR / config
|
|
459
|
+
else:
|
|
460
|
+
container_file = (Path(os.getcwd()) / DEV_ISOLATOR_DIR
|
|
461
|
+
/ "Containerfile")
|
|
462
|
+
config_file = (Path(os.getcwd()) / DEV_ISOLATOR_DIR
|
|
463
|
+
/ CONFIG_FILE_NAME)
|
|
464
|
+
user_dir = Path(os.getcwd()) / DEV_ISOLATOR_DIR
|
|
465
|
+
|
|
466
|
+
user_dir.mkdir(parents=True, exist_ok=True)
|
|
467
|
+
src_container_file = Path(BUNDLE_DIR) / "Containerfile"
|
|
468
|
+
src_config_file = Path(BUNDLE_DIR) / CONFIG_FILE_NAME
|
|
469
|
+
|
|
470
|
+
if container_file.is_file() and not force:
|
|
471
|
+
print(f"Containerfile {container_file} already exists and will not be "
|
|
472
|
+
" overwritten. Use ``init -f`` to overwrite.")
|
|
473
|
+
else:
|
|
474
|
+
if args.init_base:
|
|
475
|
+
base = BaseImage()
|
|
476
|
+
lb = base.list_base_images()
|
|
477
|
+
|
|
478
|
+
base_name = args.init_base
|
|
479
|
+
if base_name not in lb:
|
|
480
|
+
pkg_name = detect_package_manager(base_name)
|
|
481
|
+
if not pkg_name:
|
|
482
|
+
raise RuntimeError(
|
|
483
|
+
f"Base image {args.init_base} unknown or "
|
|
484
|
+
"package manager not supported.")
|
|
485
|
+
new_image = f"{base_name}"
|
|
486
|
+
|
|
487
|
+
file_content = src_container_file.read_text(encoding="utf-8")
|
|
488
|
+
file_content = replace_from_images(file_content, new_image)
|
|
489
|
+
container_file.write_text(file_content)
|
|
490
|
+
else:
|
|
491
|
+
shutil.copy(src_container_file, container_file)
|
|
492
|
+
# Works with python >= 3.14
|
|
493
|
+
# src_container_file.copy(container_file)
|
|
494
|
+
|
|
495
|
+
if config_file.is_file() and not force:
|
|
496
|
+
print(f"Configuration file {config_file} already exists and will not "
|
|
497
|
+
"be overwritten. Use ``init -f`` to overwrite.")
|
|
498
|
+
else:
|
|
499
|
+
shutil.copy(src_config_file, config_file)
|
|
500
|
+
# src_config_file.copy(config_file)
|
|
501
|
+
|
|
502
|
+
print(f"Created new config in {user_dir}")
|
|
503
|
+
|
|
504
|
+
return 0
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+
def list_base() -> int:
|
|
508
|
+
"""Handle the ``list-base`` sub command.
|
|
509
|
+
|
|
510
|
+
Print every base image definition bundled with dev-isolator, one per
|
|
511
|
+
line, as ``<name>:<tag>`` (for example ``ubuntu:24.04``). These are
|
|
512
|
+
the names a
|
|
513
|
+
project ``Containerfile`` may reference as its base image (see
|
|
514
|
+
:mod:`dev_isolator.base_image`). Unlike the project commands, it
|
|
515
|
+
needs no ``.devisolator`` directory. Returns 0.
|
|
516
|
+
"""
|
|
517
|
+
base = BaseImage()
|
|
518
|
+
for bi in base.list_base_images():
|
|
519
|
+
print(f"{bi}")
|
|
520
|
+
return 0
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def list_targets(definition, args) -> int:
|
|
524
|
+
"""Handle the ``list-targets`` sub command.
|
|
525
|
+
|
|
526
|
+
Print the target names defined in the ``targets`` section of the
|
|
527
|
+
active ``config.yaml``, one per line, sorted alphabetically. These
|
|
528
|
+
are the targets a project can run with ``target <name>`` (see
|
|
529
|
+
:func:`target`). It only needs the ``config.yaml`` of the project,
|
|
530
|
+
not a podman connection. Returns 0.
|
|
531
|
+
|
|
532
|
+
:param definition: The container definition of the current project
|
|
533
|
+
(holding the active ``config.yaml``).
|
|
534
|
+
:param args: The parsed command line arguments (from
|
|
535
|
+
:func:`parse_args`).
|
|
536
|
+
:returns: 0 on success.
|
|
537
|
+
"""
|
|
538
|
+
targets = definition.config.config_targets()
|
|
539
|
+
for name in sorted(targets):
|
|
540
|
+
print(name)
|
|
541
|
+
return 0
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def in_container_command(definition, command, remainder, env=None) -> int:
|
|
545
|
+
"""Run ``command`` with ``remainder`` as its arguments inside the
|
|
546
|
+
container of the current project.
|
|
547
|
+
|
|
548
|
+
Used by the ``opencode``, ``claude``, ``run`` and ``target`` sub
|
|
549
|
+
commands, which pass extra arguments through without parsing them.
|
|
550
|
+
Returns the exit status of the container, or 1 on error.
|
|
551
|
+
"""
|
|
552
|
+
try:
|
|
553
|
+
return definition.run_container(
|
|
554
|
+
command=[command, *remainder], extra_environment=env)
|
|
555
|
+
except Exception as e:
|
|
556
|
+
print(f"An error occured: {e}", file=sys.stderr)
|
|
557
|
+
return 1
|
|
558
|
+
|
|
559
|
+
|
|
560
|
+
def _optional_filter(value):
|
|
561
|
+
"""Return the sentinel marking an unset optional argument.
|
|
562
|
+
|
|
563
|
+
Used as the ``optional`` jinja2 filter of the ``target`` sub
|
|
564
|
+
command: a placeholder written as ``{{ args[0] | optional }}``
|
|
565
|
+
renders normally when ``args[0]`` was given on the command line;
|
|
566
|
+
when it was not (the value is undefined or empty), the sentinel is
|
|
567
|
+
returned instead and the token holding the placeholder is dropped
|
|
568
|
+
from the command afterwards (see :func:`_drop_unset_optional`).
|
|
569
|
+
"""
|
|
570
|
+
if value is None or isinstance(value, jinja2.Undefined) or value == "":
|
|
571
|
+
return _OPTIONAL_UNSET
|
|
572
|
+
return value
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
def _target_jinja_environment() -> jinja2.Environment:
|
|
576
|
+
"""Return the jinja2 environment for the ``target`` sub command.
|
|
577
|
+
|
|
578
|
+
Like the addon template environment (see
|
|
579
|
+
:func:`dev_isolator.base_image._jinja_environment`), it does not
|
|
580
|
+
escape the rendered command (shell context, not HTML), but it uses
|
|
581
|
+
strict undefined values, so a mandatory ``{{ args[0] }}`` without
|
|
582
|
+
the ``optional`` filter aborts with an error instead of rendering
|
|
583
|
+
as an empty string. The ``optional`` filter marks arguments that
|
|
584
|
+
may be omitted (see :func:`_optional_filter`).
|
|
585
|
+
"""
|
|
586
|
+
environment = jinja2.Environment(
|
|
587
|
+
autoescape=False,
|
|
588
|
+
undefined=jinja2.StrictUndefined,
|
|
589
|
+
keep_trailing_newline=True,
|
|
590
|
+
)
|
|
591
|
+
environment.filters["optional"] = _optional_filter
|
|
592
|
+
return environment
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
def _drop_unset_optional(tokens: list[str]) -> list[str]:
|
|
596
|
+
"""Drop the tokens of a rendered target command that hold an unset
|
|
597
|
+
optional argument.
|
|
598
|
+
|
|
599
|
+
A token still containing the sentinel of the ``optional`` filter
|
|
600
|
+
(see :func:`_optional_filter`) is dropped; when the token itself is
|
|
601
|
+
not an option but the token before it is one, that preceding
|
|
602
|
+
option is dropped as well, so an optional ``-k <value>`` pair
|
|
603
|
+
disappears as a whole.
|
|
604
|
+
|
|
605
|
+
:param tokens: The rendered command tokens.
|
|
606
|
+
:returns: The tokens without the unset optional ones.
|
|
607
|
+
"""
|
|
608
|
+
result: list[str] = []
|
|
609
|
+
for token in tokens:
|
|
610
|
+
if _OPTIONAL_UNSET in token:
|
|
611
|
+
# An option token holding the unset placeholder itself
|
|
612
|
+
# (for example ``-k={{ args[0] | optional }}``) is dropped
|
|
613
|
+
# alone; a plain value token also drops its preceding
|
|
614
|
+
# option.
|
|
615
|
+
if (not token.startswith("-") and result
|
|
616
|
+
and result[-1].startswith("-")):
|
|
617
|
+
result.pop()
|
|
618
|
+
continue
|
|
619
|
+
result.append(token)
|
|
620
|
+
return result
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
#: Matches a token that is a shell operator or redirection, for example
|
|
624
|
+
#: ``|``, ``||``, ``&&``, ``;``, ``>``, ``>>``, ``2>&1``,
|
|
625
|
+
#: ``2>/dev/null`` or ``>>out.txt``. Such tokens are left unquoted when
|
|
626
|
+
#: a rendered target line is assembled for the shell, so redirections
|
|
627
|
+
#: and pipes in target commands are interpreted instead of being
|
|
628
|
+
#: passed as literal arguments.
|
|
629
|
+
_SHELL_OPERATOR_RE = re.compile(
|
|
630
|
+
r"^(?:"
|
|
631
|
+
r"\|\||&&|[|&;]"
|
|
632
|
+
r"|\d*(?:<<|>>|[<>])(?:&\d*)?"
|
|
633
|
+
r"|\d*(?:<<|>>|[<>])\S+"
|
|
634
|
+
r")$")
|
|
635
|
+
|
|
636
|
+
|
|
637
|
+
def _has_shell_operators(tokens: list[str]) -> bool:
|
|
638
|
+
"""Return whether any of the ``tokens`` is a shell operator or
|
|
639
|
+
redirection (see :data:`_SHELL_OPERATOR_RE`)."""
|
|
640
|
+
return any(_SHELL_OPERATOR_RE.match(token) for token in tokens)
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
def _join_shell_tokens(tokens: list[str]) -> str:
|
|
644
|
+
"""Join the rendered ``tokens`` into one shell command line.
|
|
645
|
+
|
|
646
|
+
Ordinary tokens are quoted safely (like :func:`shlex.quote`); a
|
|
647
|
+
token that is a shell operator or redirection (see
|
|
648
|
+
:data:`_SHELL_OPERATOR_RE`) is left unquoted, so the shell
|
|
649
|
+
interprets it instead of receiving it as a literal argument.
|
|
650
|
+
"""
|
|
651
|
+
return " ".join(token if _SHELL_OPERATOR_RE.match(token)
|
|
652
|
+
else shlex.quote(token)
|
|
653
|
+
for token in tokens)
|
|
654
|
+
|
|
655
|
+
|
|
656
|
+
def render_target_lines(template: str | list[str],
|
|
657
|
+
extra_args: list[str]) -> list[list[str]]:
|
|
658
|
+
"""Render the command template of a target into script lines.
|
|
659
|
+
|
|
660
|
+
The template context provides the extra arguments given after the
|
|
661
|
+
target name on the command line as ``args`` (the list of the
|
|
662
|
+
arguments) and ``all_args`` (all of them joined with spaces). In
|
|
663
|
+
the string form, the whole template is rendered as one command line
|
|
664
|
+
and split like a shell would (``shlex.split``); in the list form,
|
|
665
|
+
every entry is one line of a script (a whole command line) and is
|
|
666
|
+
rendered and split on its own. Tokens holding an unset optional
|
|
667
|
+
argument (see :func:`_drop_unset_optional`) are dropped from every
|
|
668
|
+
line; a line that becomes empty afterwards is skipped.
|
|
669
|
+
|
|
670
|
+
:param template: The target's command template (a string or a list
|
|
671
|
+
of strings, as returned by
|
|
672
|
+
:meth:`dev_isolator.config.Configuration.config_targets`).
|
|
673
|
+
:param extra_args: The arguments given after the target name on
|
|
674
|
+
the command line.
|
|
675
|
+
:returns: The rendered script lines, each a list of command
|
|
676
|
+
tokens.
|
|
677
|
+
:raises RuntimeError: if the template references a mandatory
|
|
678
|
+
argument (a ``{{ args[n] }}`` placeholder without the
|
|
679
|
+
``optional`` filter) that was not given.
|
|
680
|
+
"""
|
|
681
|
+
environment = _target_jinja_environment()
|
|
682
|
+
context = {"args": list(extra_args),
|
|
683
|
+
"all_args": " ".join(extra_args)}
|
|
684
|
+
|
|
685
|
+
if isinstance(template, str):
|
|
686
|
+
template = [template]
|
|
687
|
+
|
|
688
|
+
# List form: every entry is one line of a script.
|
|
689
|
+
lines: list[list[str]] = []
|
|
690
|
+
for entry in template:
|
|
691
|
+
try:
|
|
692
|
+
text = environment.from_string(entry).render(**context)
|
|
693
|
+
except jinja2.UndefinedError as e:
|
|
694
|
+
raise RuntimeError(
|
|
695
|
+
f"Invalid target command {template}: {e}. A mandatory "
|
|
696
|
+
"argument ({{ args[n] }} without the 'optional' filter) "
|
|
697
|
+
"was not given on the command line; mark it optional in "
|
|
698
|
+
"config.yaml with '{{ args[n] | optional }}' if it may "
|
|
699
|
+
"be omitted.") from e
|
|
700
|
+
tokens = _drop_unset_optional(shlex.split(text))
|
|
701
|
+
if tokens:
|
|
702
|
+
lines.append(tokens)
|
|
703
|
+
return lines
|
|
704
|
+
|
|
705
|
+
|
|
706
|
+
def target(definition, args) -> int:
|
|
707
|
+
"""Handle the ``target`` sub command.
|
|
708
|
+
|
|
709
|
+
Look up the target named by the first positional argument
|
|
710
|
+
(``args.target_args[0]``) in the ``targets`` section of the active
|
|
711
|
+
``config.yaml`` and run its command inside the container of the
|
|
712
|
+
current project, with the remaining positional arguments passed to
|
|
713
|
+
the command (see :func:`render_target_lines`). A bit like a
|
|
714
|
+
predefined ``run``. Takes only positional arguments.
|
|
715
|
+
|
|
716
|
+
A command given as a string (or a single-line list) is executed
|
|
717
|
+
directly; a multi-line list is a script: its lines run line by line
|
|
718
|
+
in one shell session (``args.shell -ec script``), which stops at
|
|
719
|
+
the first failing line.
|
|
720
|
+
|
|
721
|
+
A rendered line containing shell operators or redirections (for
|
|
722
|
+
example ``pytest 2>&1 | tee output.txt``) is run through the shell
|
|
723
|
+
(``args.shell -ec line``), so the operators are interpreted; its
|
|
724
|
+
ordinary tokens are quoted safely, operator tokens are left as is.
|
|
725
|
+
|
|
726
|
+
:param definition: The container definition of the current project.
|
|
727
|
+
:param args: The parsed command line arguments (from
|
|
728
|
+
:func:`parse_args`).
|
|
729
|
+
:returns: The exit status of the container, or 1 on error.
|
|
730
|
+
"""
|
|
731
|
+
targets = definition.config.config_targets()
|
|
732
|
+
if not args.target_args:
|
|
733
|
+
print("No target given. Available targets: "
|
|
734
|
+
+ (", ".join(sorted(targets)) if targets else "(none, "
|
|
735
|
+
"define them in the 'targets' section of config.yaml)"),
|
|
736
|
+
file=sys.stderr)
|
|
737
|
+
return 1
|
|
738
|
+
name, *extra_args = args.target_args
|
|
739
|
+
if name not in targets:
|
|
740
|
+
print(f"Unknown target '{name}'. Available targets: "
|
|
741
|
+
+ (", ".join(sorted(targets)) if targets else "(none, "
|
|
742
|
+
"define them in the 'targets' section of config.yaml)"),
|
|
743
|
+
file=sys.stderr)
|
|
744
|
+
return 1
|
|
745
|
+
try:
|
|
746
|
+
lines = render_target_lines(targets[name], extra_args)
|
|
747
|
+
except RuntimeError as e:
|
|
748
|
+
print(f"An error occured: {e}", file=sys.stderr)
|
|
749
|
+
return 1
|
|
750
|
+
if not lines:
|
|
751
|
+
print(f"An error occured: target '{name}' rendered an empty "
|
|
752
|
+
"command.", file=sys.stderr)
|
|
753
|
+
return 1
|
|
754
|
+
if len(lines) == 1:
|
|
755
|
+
command = lines[0]
|
|
756
|
+
# A line with shell operators (for example ``2>&1`` or ``|``)
|
|
757
|
+
# needs a shell to be interpreted; run it through the shell.
|
|
758
|
+
if _has_shell_operators(command):
|
|
759
|
+
return in_container_command(
|
|
760
|
+
definition, args.shell, ["-ec", _join_shell_tokens(command)])
|
|
761
|
+
return in_container_command(definition, command[0], command[1:])
|
|
762
|
+
# Several lines: run the script in one shell session; ``-e`` stops
|
|
763
|
+
# it at the first failing line.
|
|
764
|
+
script = "\n".join(_join_shell_tokens(line) for line in lines)
|
|
765
|
+
return in_container_command(definition, args.shell, ["-ec", script])
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
def get_path(config):
|
|
769
|
+
"""Locate the build context, Containerfile, config file and addon
|
|
770
|
+
directories for a project.
|
|
771
|
+
|
|
772
|
+
The ``.devisolator`` directory is found by walking up from the
|
|
773
|
+
current working directory (see :func:`search_path`); without one,
|
|
774
|
+
dev-isolator's bundled ``data`` directory is used as build
|
|
775
|
+
context. When ``config`` names a sub-configuration (a
|
|
776
|
+
subdirectory of ``.devisolator`` holding its own Containerfile,
|
|
777
|
+
``config.yaml`` and addons), that sub-configuration is preferred
|
|
778
|
+
over the project's own files.
|
|
779
|
+
|
|
780
|
+
Every candidate is listed in precedence order: the
|
|
781
|
+
sub-configuration first, then the ``.devisolator`` directory
|
|
782
|
+
itself, then the bundled files. Only existing files (or
|
|
783
|
+
directories, for the addon search paths) are kept.
|
|
784
|
+
|
|
785
|
+
:param config: Name of the sub-configuration to use, or ``None``.
|
|
786
|
+
:returns: The tuple ``(context_dir, container_file, config_file,
|
|
787
|
+
addon_search_path)`` — build context directory, Containerfile
|
|
788
|
+
path, ``config.yaml`` path and list of addon search
|
|
789
|
+
directories, all resolved to absolute paths.
|
|
790
|
+
:raises FileNotFoundError: if no Containerfile or no config file
|
|
791
|
+
is found.
|
|
792
|
+
:raises RuntimeError: if ``config`` is given but no
|
|
793
|
+
``.devisolator`` directory exists, or it has no such
|
|
794
|
+
sub-directory.
|
|
795
|
+
"""
|
|
796
|
+
di_path = search_path(DEV_ISOLATOR_DIR)
|
|
797
|
+
bundle_path = BUNDLE_DIR
|
|
798
|
+
|
|
799
|
+
if not di_path:
|
|
800
|
+
context_dir = bundle_path
|
|
801
|
+
else:
|
|
802
|
+
context_dir = di_path.parent
|
|
803
|
+
|
|
804
|
+
if (config and not di_path) or (config and
|
|
805
|
+
not (di_path / config).is_dir()):
|
|
806
|
+
raise RuntimeError("--config or DEVISOLATOR_CONFIG specified but "
|
|
807
|
+
f"'{config}' in the .devisolator directory "
|
|
808
|
+
"does not exists.")
|
|
809
|
+
|
|
810
|
+
candidates_container_file: [Path] = list()
|
|
811
|
+
candidates_config_file: [Path] = list()
|
|
812
|
+
candidates_addon: [Path] = list()
|
|
813
|
+
|
|
814
|
+
if di_path and config:
|
|
815
|
+
candidates_container_file.append(di_path / config / "Containerfile")
|
|
816
|
+
candidates_config_file.append(di_path / config / CONFIG_FILE_NAME)
|
|
817
|
+
candidates_addon.append(di_path / config)
|
|
818
|
+
if di_path:
|
|
819
|
+
candidates_container_file.append(di_path / "Containerfile")
|
|
820
|
+
candidates_config_file.append(di_path / CONFIG_FILE_NAME)
|
|
821
|
+
candidates_addon.append(di_path)
|
|
822
|
+
candidates_container_file.append(bundle_path / "Containerfile")
|
|
823
|
+
candidates_config_file.append(bundle_path / CONFIG_FILE_NAME)
|
|
824
|
+
candidates_addon.append(ADDON_DIR)
|
|
825
|
+
|
|
826
|
+
config_file = [x.resolve() for x in candidates_config_file
|
|
827
|
+
if x.resolve().is_file()]
|
|
828
|
+
container_file = [x.resolve() for x in
|
|
829
|
+
candidates_container_file
|
|
830
|
+
if x.resolve().is_file()]
|
|
831
|
+
addon_search_path = [x.resolve() for x in candidates_addon
|
|
832
|
+
if x.resolve().is_dir()]
|
|
833
|
+
|
|
834
|
+
if len(container_file) < 1:
|
|
835
|
+
raise FileNotFoundError("No Containerfile found in "
|
|
836
|
+
f"{candidates_container_file}")
|
|
837
|
+
container_file = container_file[0]
|
|
838
|
+
|
|
839
|
+
if len(config_file) < 1:
|
|
840
|
+
raise FileNotFoundError("No config file found in "
|
|
841
|
+
f"{candidates_config_file}")
|
|
842
|
+
config_file = config_file[0]
|
|
843
|
+
|
|
844
|
+
dprint(Verbosity.INFO, f"Containerfile found at {container_file}")
|
|
845
|
+
dprint(Verbosity.INFO, f"Config file found at {config_file}")
|
|
846
|
+
dprint(Verbosity.INFO, f"Addons seach path: {addon_search_path}")
|
|
847
|
+
dprint(Verbosity.INFO, f"Context directory {context_dir}")
|
|
848
|
+
return context_dir, container_file, config_file, addon_search_path
|
|
849
|
+
|
|
850
|
+
|
|
851
|
+
def main() -> int:
|
|
852
|
+
"""Main CLI entry point.
|
|
853
|
+
|
|
854
|
+
Parse the command line with ``parse_args``, apply the requested
|
|
855
|
+
verbosity and dispatch to the handler of the selected sub command.
|
|
856
|
+
Without an explicit subcommand, ``shell`` is executed with the
|
|
857
|
+
default shell.
|
|
858
|
+
|
|
859
|
+
The exit code of the selected sub command handler is returned
|
|
860
|
+
unchanged (0 on success, 1 on error); any unhandled exception is
|
|
861
|
+
reported as an error message and turned into the exit code 255.
|
|
862
|
+
"""
|
|
863
|
+
try:
|
|
864
|
+
ret_code = main_intern()
|
|
865
|
+
except Exception as e:
|
|
866
|
+
dprint(Verbosity.ERROR, f"{e}")
|
|
867
|
+
ret_code = 255
|
|
868
|
+
return ret_code
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
def main_intern() -> int:
|
|
872
|
+
"""Implement :func:`main`: parse the command line, set the
|
|
873
|
+
verbosity and dispatch to the sub command's handler.
|
|
874
|
+
|
|
875
|
+
Without an explicit subcommand, ``shell`` is executed with the
|
|
876
|
+
default shell. Commands that need no project (``init``,
|
|
877
|
+
``list-base``, ``help``) are handled first; every other command
|
|
878
|
+
needs the ``ContainerDefintion`` of the current project
|
|
879
|
+
(located via :func:`get_path`). The ``DEVISOLATOR_CONFIG``
|
|
880
|
+
environment variable acts as the default for ``--config``.
|
|
881
|
+
"""
|
|
882
|
+
args = parse_args()
|
|
883
|
+
|
|
884
|
+
if args.command == "help":
|
|
885
|
+
create_parser().print_help()
|
|
886
|
+
return 0
|
|
887
|
+
|
|
888
|
+
set_verbosity(Verbosity(args.verbose))
|
|
889
|
+
|
|
890
|
+
if os.getenv("DEVISOLATOR_CONFIG") and not args.config:
|
|
891
|
+
args.config = os.getenv("DEVISOLATOR_CONFIG")
|
|
892
|
+
if args.config == "default":
|
|
893
|
+
args.config = None
|
|
894
|
+
|
|
895
|
+
# Commands that do not require an initialized ContainerDefintion object
|
|
896
|
+
if args.command == "init":
|
|
897
|
+
return init_project(args)
|
|
898
|
+
if args.command == "list-base":
|
|
899
|
+
return list_base()
|
|
900
|
+
|
|
901
|
+
context_dir, container_file, config_file, addon_search_path = get_path(
|
|
902
|
+
args.config)
|
|
903
|
+
config = Configuration(config_file, addon_search_path)
|
|
904
|
+
definition = ContainerDefintion(context_dir, container_file, config,
|
|
905
|
+
args.config)
|
|
906
|
+
|
|
907
|
+
if args.command == "clean":
|
|
908
|
+
return clean(definition, args)
|
|
909
|
+
if args.command == "shell":
|
|
910
|
+
return shell(definition, args)
|
|
911
|
+
if args.command == "build":
|
|
912
|
+
return build(definition, args)
|
|
913
|
+
if args.command == "opencode":
|
|
914
|
+
return in_container_command(definition, "opencode", args.opencode_args)
|
|
915
|
+
if args.command == "claude":
|
|
916
|
+
return in_container_command(definition, "claude", args.claude_args)
|
|
917
|
+
if args.command == "run":
|
|
918
|
+
if args.run_args:
|
|
919
|
+
return in_container_command(definition, args.run_args[0],
|
|
920
|
+
args.run_args[1:])
|
|
921
|
+
# Without a command, behave like ``shell``.
|
|
922
|
+
return shell(definition, args)
|
|
923
|
+
if args.command == "target":
|
|
924
|
+
return target(definition, args)
|
|
925
|
+
if args.command == "list-targets":
|
|
926
|
+
return list_targets(definition, args)
|
|
927
|
+
return 0
|
|
928
|
+
|
|
929
|
+
|
|
930
|
+
if __name__ == "__main__":
|
|
931
|
+
sys.exit(main())
|