summaryrefslogtreecommitdiffstats
path: root/src/debputy/commands/debputy_cmd/context.py
blob: 3363e9615e5710e6d3248ea3e169c349dfef6ae1 (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import argparse
import dataclasses
import errno
import os
from typing import (
    Optional,
    Tuple,
    Mapping,
    FrozenSet,
    Set,
    Union,
    Sequence,
    Iterable,
    Callable,
    Dict,
    TYPE_CHECKING,
)

from debian.debian_support import DpkgArchTable

from debputy._deb_options_profiles import DebBuildOptionsAndProfiles
from debputy.architecture_support import (
    DpkgArchitectureBuildProcessValuesTable,
    dpkg_architecture_table,
)
from debputy.exceptions import DebputyRuntimeError
from debputy.filesystem_scan import FSROOverlay
from debputy.highlevel_manifest import HighLevelManifest
from debputy.highlevel_manifest_parser import YAMLManifestParser
from debputy.packages import SourcePackage, BinaryPackage, parse_source_debian_control
from debputy.plugin.api import VirtualPath
from debputy.plugin.api.impl import load_plugin_features
from debputy.plugin.api.feature_set import PluginProvidedFeatureSet
from debputy.substitution import (
    Substitution,
    VariableContext,
    SubstitutionImpl,
    NULL_SUBSTITUTION,
)
from debputy.util import _error, PKGNAME_REGEX, resolve_source_date_epoch, setup_logging

if TYPE_CHECKING:
    from argparse import _SubParsersAction


CommandHandler = Callable[["CommandContext"], None]
ArgparserConfigurator = Callable[[argparse.ArgumentParser], None]


def add_arg(
    *name_or_flags: str,
    **kwargs,
) -> Callable[[argparse.ArgumentParser], None]:
    def _configurator(argparser: argparse.ArgumentParser) -> None:
        argparser.add_argument(
            *name_or_flags,
            **kwargs,
        )

    return _configurator


@dataclasses.dataclass(slots=True, frozen=True)
class CommandArg:
    parsed_args: argparse.Namespace
    plugin_search_dirs: Sequence[str]


@dataclasses.dataclass
class Command:
    handler: Callable[["CommandContext"], None]
    require_substitution: bool = True
    requested_plugins_only: bool = False


class CommandContext:
    def __init__(
        self,
        parsed_args: argparse.Namespace,
        plugin_search_dirs: Sequence[str],
        require_substitution: bool = True,
        requested_plugins_only: bool = False,
    ) -> None:
        self.parsed_args = parsed_args
        self.plugin_search_dirs = plugin_search_dirs
        self._require_substitution = require_substitution
        self._requested_plugins_only = requested_plugins_only
        self._debputy_plugin_feature_set: PluginProvidedFeatureSet = (
            PluginProvidedFeatureSet()
        )
        self._debian_dir = FSROOverlay.create_root_dir("debian", "debian")
        self._mtime: Optional[int] = None
        self._source_variables: Optional[Mapping[str, str]] = None
        self._substitution: Optional[Substitution] = None
        self._requested_plugins: Optional[Sequence[str]] = None
        self._plugins_loaded = False
        self._dctrl_data: Optional[
            Tuple[
                DpkgArchitectureBuildProcessValuesTable,
                DpkgArchTable,
                DebBuildOptionsAndProfiles,
                "SourcePackage",
                Mapping[str, "BinaryPackage"],
            ]
        ] = None

    @property
    def debian_dir(self) -> VirtualPath:
        return self._debian_dir

    @property
    def mtime(self) -> int:
        if self._mtime is None:
            self._mtime = resolve_source_date_epoch(
                None,
                substitution=self.substitution,
            )
        return self._mtime

    def source_package(self) -> SourcePackage:
        _a, _b, _c, source, _d = self._parse_dctrl()
        return source

    def binary_packages(self) -> Mapping[str, "BinaryPackage"]:
        _a, _b, _c, _source, binary_package_table = self._parse_dctrl()
        return binary_package_table

    def requested_plugins(self) -> Sequence[str]:
        if self._requested_plugins is None:
            self._requested_plugins = self._resolve_requested_plugins()
        return self._requested_plugins

    def required_plugins(self) -> Set[str]:
        return set(getattr(self.parsed_args, "required_plugins") or [])

    @property
    def deb_build_options_and_profiles(self) -> "DebBuildOptionsAndProfiles":
        _a, _b, deb_build_options_and_profiles, _c, _d = self._parse_dctrl()
        return deb_build_options_and_profiles

    @property
    def deb_build_options(self) -> Mapping[str, Optional[str]]:
        return self.deb_build_options_and_profiles.deb_build_options

    def _create_substitution(
        self,
        parsed_args: argparse.Namespace,
        plugin_feature_set: PluginProvidedFeatureSet,
        debian_dir: VirtualPath,
    ) -> Substitution:
        requested_subst = self._require_substitution
        if hasattr(parsed_args, "substitution"):
            requested_subst = parsed_args.substitution
        if requested_subst is False and self._require_substitution:
            _error(f"--no-substitution cannot be used with {parsed_args.command}")
        if self._require_substitution or requested_subst is not False:
            variable_context = VariableContext(debian_dir)
            return SubstitutionImpl(
                plugin_feature_set=plugin_feature_set,
                unresolvable_substitutions=frozenset(["PACKAGE"]),
                variable_context=variable_context,
            )
        return NULL_SUBSTITUTION

    def load_plugins(self) -> PluginProvidedFeatureSet:
        if not self._plugins_loaded:
            requested_plugins = None
            required_plugins = self.required_plugins()
            if self._requested_plugins_only:
                requested_plugins = self.requested_plugins()
            debug_mode = getattr(self.parsed_args, "debug_mode", False)
            load_plugin_features(
                self.plugin_search_dirs,
                self.substitution,
                requested_plugins_only=requested_plugins,
                required_plugins=required_plugins,
                plugin_feature_set=self._debputy_plugin_feature_set,
                debug_mode=debug_mode,
            )
            self._plugins_loaded = True
        return self._debputy_plugin_feature_set

    @staticmethod
    def _plugin_from_dependency_field(dep_field: str) -> Iterable[str]:
        package_prefix = "debputy-plugin-"
        for dep_clause in (d.strip() for d in dep_field.split(",")):
            dep = dep_clause.split("|")[0].strip()
            if not dep.startswith(package_prefix):
                continue
            m = PKGNAME_REGEX.search(dep)
            assert m
            package_name = m.group(0)
            plugin_name = package_name[len(package_prefix) :]
            yield plugin_name

    def _resolve_requested_plugins(self) -> Sequence[str]:
        _a, _b, _c, source_package, _d = self._parse_dctrl()
        bd = source_package.fields.get("Build-Depends", "")
        plugins = list(self._plugin_from_dependency_field(bd))
        for field_name in ("Build-Depends-Arch", "Build-Depends-Indep"):
            f = source_package.fields.get(field_name)
            if not f:
                continue
            for plugin in self._plugin_from_dependency_field(f):
                raise DebputyRuntimeError(
                    f"Cannot load plugins via {field_name}:"
                    f" Please move debputy-plugin-{plugin} dependency to Build-Depends."
                )

        return plugins

    @property
    def substitution(self) -> Substitution:
        if self._substitution is None:
            self._substitution = self._create_substitution(
                self.parsed_args,
                self._debputy_plugin_feature_set,
                self.debian_dir,
            )
        return self._substitution

    def _parse_dctrl(
        self,
    ) -> Tuple[
        DpkgArchitectureBuildProcessValuesTable,
        DpkgArchTable,
        DebBuildOptionsAndProfiles,
        "SourcePackage",
        Mapping[str, "BinaryPackage"],
    ]:
        if self._dctrl_data is None:
            build_env = DebBuildOptionsAndProfiles.instance()
            dpkg_architecture_variables = dpkg_architecture_table()
            dpkg_arch_query_table = DpkgArchTable.load_arch_table()

            packages: Union[Set[str], FrozenSet[str]] = frozenset()
            if hasattr(self.parsed_args, "packages"):
                packages = self.parsed_args.packages

            try:
                debian_control = self.debian_dir.get("control")
                if debian_control is None:
                    raise FileNotFoundError(
                        errno.ENOENT,
                        os.strerror(errno.ENOENT),
                        os.path.join(self.debian_dir.fs_path, "control"),
                    )
                source_package, binary_packages = parse_source_debian_control(
                    debian_control,
                    packages,  # -p/--package
                    set(),  # -N/--no-package
                    False,  # -i
                    False,  # -a
                    dpkg_architecture_variables=dpkg_architecture_variables,
                    dpkg_arch_query_table=dpkg_arch_query_table,
                    build_env=build_env,
                )
                assert packages <= binary_packages.keys()
            except FileNotFoundError:
                _error(
                    "This subcommand must be run from a source package root; expecting debian/control to exist."
                )

            self._dctrl_data = (
                dpkg_architecture_variables,
                dpkg_arch_query_table,
                build_env,
                source_package,
                binary_packages,
            )

        return self._dctrl_data

    @property
    def has_dctrl_file(self) -> bool:
        debian_control = self.debian_dir.get("control")
        return debian_control is not None

    def manifest_parser(
        self,
        *,
        manifest_path: Optional[str] = None,
    ) -> YAMLManifestParser:
        substitution = self.substitution

        (
            dpkg_architecture_variables,
            dpkg_arch_query_table,
            build_env,
            source_package,
            binary_packages,
        ) = self._parse_dctrl()

        if self.parsed_args.debputy_manifest is not None:
            manifest_path = self.parsed_args.debputy_manifest
        if manifest_path is None:
            manifest_path = os.path.join(self.debian_dir.fs_path, "debputy.manifest")
        return YAMLManifestParser(
            manifest_path,
            source_package,
            binary_packages,
            substitution,
            dpkg_architecture_variables,
            dpkg_arch_query_table,
            build_env,
            self.load_plugins(),
            debian_dir=self.debian_dir,
        )

    def parse_manifest(
        self,
        *,
        manifest_path: Optional[str] = None,
    ) -> HighLevelManifest:
        substitution = self.substitution
        manifest_required = False

        (
            dpkg_architecture_variables,
            dpkg_arch_query_table,
            build_env,
            _,
            binary_packages,
        ) = self._parse_dctrl()

        if self.parsed_args.debputy_manifest is not None:
            manifest_path = self.parsed_args.debputy_manifest
            manifest_required = True
        if manifest_path is None:
            manifest_path = os.path.join(self.debian_dir.fs_path, "debputy.manifest")
        parser = self.manifest_parser(manifest_path=manifest_path)

        os.environ["SOURCE_DATE_EPOCH"] = substitution.substitute(
            "{{SOURCE_DATE_EPOCH}}",
            "Internal resolution",
        )
        if os.path.isfile(manifest_path):
            return parser.parse_manifest()
        if manifest_required:
            _error(f'The path "{manifest_path}" is not a file!')
        return parser.build_manifest()


class CommandBase:
    __slots__ = ()

    def configure(self, argparser: argparse.ArgumentParser) -> None:
        # Does nothing by default
        pass

    def __call__(self, command_arg: CommandArg) -> None:
        raise NotImplementedError


class SubcommandBase(CommandBase):
    __slots__ = ("name", "aliases", "help_description")

    def __init__(
        self,
        name: str,
        *,
        aliases: Sequence[str] = tuple(),
        help_description: Optional[str] = None,
    ) -> None:
        self.name = name
        self.aliases = aliases
        self.help_description = help_description

    def add_subcommand_to_subparser(
        self,
        subparser: "_SubParsersAction",
    ) -> argparse.ArgumentParser:
        parser = subparser.add_parser(
            self.name,
            aliases=self.aliases,
            help=self.help_description,
            allow_abbrev=False,
        )
        self.configure(parser)
        return parser


class GenericSubCommand(SubcommandBase):
    __slots__ = (
        "_handler",
        "_configure_handler",
        "_require_substitution",
        "_requested_plugins_only",
        "_log_only_to_stderr",
    )

    def __init__(
        self,
        name: str,
        handler: Callable[[CommandContext], None],
        *,
        aliases: Sequence[str] = tuple(),
        help_description: Optional[str] = None,
        configure_handler: Optional[Callable[[argparse.ArgumentParser], None]] = None,
        require_substitution: bool = True,
        requested_plugins_only: bool = False,
        log_only_to_stderr: bool = False,
    ) -> None:
        super().__init__(name, aliases=aliases, help_description=help_description)
        self._handler = handler
        self._configure_handler = configure_handler
        self._require_substitution = require_substitution
        self._requested_plugins_only = requested_plugins_only
        self._log_only_to_stderr = log_only_to_stderr

    def configure_handler(
        self,
        handler: Callable[[argparse.ArgumentParser], None],
    ) -> None:
        if self._configure_handler is not None:
            raise TypeError("Only one argument handler can be provided")
        self._configure_handler = handler

    def configure(self, argparser: argparse.ArgumentParser) -> None:
        handler = self._configure_handler
        if handler is not None:
            handler(argparser)

    def __call__(self, command_arg: CommandArg) -> None:
        context = CommandContext(
            command_arg.parsed_args,
            command_arg.plugin_search_dirs,
            self._require_substitution,
            self._requested_plugins_only,
        )
        if self._log_only_to_stderr:
            setup_logging(reconfigure_logging=True, log_only_to_stderr=True)
        return self._handler(context)


class DispatchingCommandMixin(CommandBase):
    __slots__ = ()

    def add_subcommand(self, subcommand: SubcommandBase) -> None:
        raise NotImplementedError

    def add_dispatching_subcommand(
        self,
        name: str,
        dest: str,
        *,
        aliases: Sequence[str] = tuple(),
        help_description: Optional[str] = None,
        metavar: str = "command",
        default_subcommand: Optional[str] = None,
    ) -> "DispatcherCommand":
        ds = DispatcherCommand(
            name,
            dest,
            aliases=aliases,
            help_description=help_description,
            metavar=metavar,
            default_subcommand=default_subcommand,
        )
        self.add_subcommand(ds)
        return ds

    def register_subcommand(
        self,
        name: Union[str, Sequence[str]],
        *,
        help_description: Optional[str] = None,
        argparser: Optional[
            Union[ArgparserConfigurator, Sequence[ArgparserConfigurator]]
        ] = None,
        require_substitution: bool = True,
        requested_plugins_only: bool = False,
        log_only_to_stderr: bool = False,
    ) -> Callable[[CommandHandler], GenericSubCommand]:
        if isinstance(name, str):
            cmd_name = name
            aliases = []
        else:
            cmd_name = name[0]
            aliases = name[1:]

        if argparser is not None and not callable(argparser):
            args = argparser

            def _wrapper(parser: argparse.ArgumentParser) -> None:
                for configurator in args:
                    configurator(parser)

            argparser = _wrapper

        def _annotation_impl(func: CommandHandler) -> GenericSubCommand:
            subcommand = GenericSubCommand(
                cmd_name,
                func,
                aliases=aliases,
                help_description=help_description,
                require_substitution=require_substitution,
                requested_plugins_only=requested_plugins_only,
                log_only_to_stderr=log_only_to_stderr,
            )
            self.add_subcommand(subcommand)
            if argparser is not None:
                subcommand.configure_handler(argparser)

            return subcommand

        return _annotation_impl


class DispatcherCommand(SubcommandBase, DispatchingCommandMixin):
    __slots__ = (
        "_subcommands",
        "_aliases",
        "_dest",
        "_metavar",
        "_required",
        "_default_subcommand",
        "_argparser",
    )

    def __init__(
        self,
        name: str,
        dest: str,
        *,
        aliases: Sequence[str] = tuple(),
        help_description: Optional[str] = None,
        metavar: str = "command",
        default_subcommand: Optional[str] = None,
    ) -> None:
        super().__init__(name, aliases=aliases, help_description=help_description)
        self._aliases: Dict[str, SubcommandBase] = {}
        self._subcommands: Dict[str, SubcommandBase] = {}
        self._dest = dest
        self._metavar = metavar
        self._default_subcommand = default_subcommand
        self._argparser: Optional[argparse.ArgumentParser] = None

    def add_subcommand(self, subcommand: SubcommandBase) -> None:
        all_names = [subcommand.name]
        if subcommand.aliases:
            all_names.extend(subcommand.aliases)
        aliases = self._aliases
        for n in all_names:
            if n in aliases:
                raise ValueError(
                    f"Internal error: Multiple handlers for {n} on topic {self.name}"
                )

            aliases[n] = subcommand
        self._subcommands[subcommand.name] = subcommand

    def configure(self, argparser: argparse.ArgumentParser) -> None:
        if self._argparser is not None:
            raise TypeError("Cannot configure twice!")
        self._argparser = argparser
        subcommands = self._subcommands
        if not subcommands:
            raise ValueError(
                f"Internal error: No subcommands for subcommand {self.name} (then why do we have it?)"
            )
        default_subcommand = self._default_subcommand
        required = default_subcommand is None
        if (
            default_subcommand is not None
            and default_subcommand not in ("--help", "-h")
            and default_subcommand not in subcommands
        ):
            raise ValueError(
                f"Internal error: Subcommand {self.name} should have {default_subcommand} as default,"
                " but it was not registered?"
            )
        subparser = argparser.add_subparsers(
            dest=self._dest,
            required=required,
            metavar=self._metavar,
        )
        for subcommand in subcommands.values():
            subcommand.add_subcommand_to_subparser(subparser)

    def has_command(self, command: str) -> bool:
        return command in self._aliases

    def __call__(self, command_arg: CommandArg) -> None:
        argparser = self._argparser
        assert argparser is not None
        v = getattr(command_arg.parsed_args, self._dest, None)
        if v is None:
            v = self._default_subcommand
            if v in ("--help", "-h"):
                argparser.parse_args([v])
                _error("Missing command", prog=argparser.prog)

        assert (
            v is not None
        ), f"Internal error: No default subcommand and argparse did not provide the required subcommand {self._dest}?"
        assert (
            v in self._aliases
        ), f"Internal error: {v} was accepted as a topic, but it was not registered?"
        self._aliases[v](command_arg)


ROOT_COMMAND = DispatcherCommand(
    "root",
    dest="command",
    metavar="COMMAND",
)