|
4 | 4 |
|
5 | 5 | """Utilities that are common to multiple CLI commands."""
|
6 | 6 |
|
7 |
| -from dataclasses import dataclass |
8 |
| -from typing import Any, Callable, Mapping, Optional, TypeVar |
| 7 | +from dataclasses import dataclass, field |
| 8 | +from pathlib import Path |
| 9 | +from typing import Any, Mapping, Optional |
9 | 10 |
|
10 | 11 | import click
|
11 | 12 | from boolean.boolean import Expression, ParseError
|
12 | 13 | from license_expression import ExpressionError
|
13 | 14 |
|
14 | 15 | from .._util import _LICENSING
|
| 16 | +from ..global_licensing import GlobalLicensingParseError |
15 | 17 | from ..i18n import _
|
16 |
| -from ..project import Project |
| 18 | +from ..project import GlobalLicensingConflict, Project |
| 19 | +from ..vcs import find_root |
17 | 20 |
|
18 |
| -F = TypeVar("F", bound=Callable) |
19 | 21 |
|
20 |
| - |
21 |
| -def requires_project(f: F) -> F: |
22 |
| - """A decorator to mark subcommands that require a :class:`Project` object. |
23 |
| - Make sure to apply this decorator _first_. |
24 |
| - """ |
25 |
| - setattr(f, "requires_project", True) |
26 |
| - return f |
27 |
| - |
28 |
| - |
29 |
| -@dataclass(frozen=True) |
| 22 | +@dataclass() |
30 | 23 | class ClickObj:
|
31 | 24 | """A dataclass holding necessary context and options."""
|
32 | 25 |
|
33 |
| - no_multiprocessing: bool |
34 |
| - project: Optional[Project] |
| 26 | + root: Optional[Path] = None |
| 27 | + include_submodules: bool = False |
| 28 | + include_meson_subprojects: bool = False |
| 29 | + no_multiprocessing: bool = True |
| 30 | + |
| 31 | + _project: Optional[Project] = field( |
| 32 | + default=None, init=False, repr=False, compare=False |
| 33 | + ) |
| 34 | + |
| 35 | + @property |
| 36 | + def project(self) -> Project: |
| 37 | + """Generate a project object on demand, and cache it.""" |
| 38 | + if self._project: |
| 39 | + return self._project |
| 40 | + |
| 41 | + root = self.root |
| 42 | + if root is None: |
| 43 | + root = find_root() |
| 44 | + if root is None: |
| 45 | + root = Path.cwd() |
| 46 | + |
| 47 | + try: |
| 48 | + project = Project.from_directory( |
| 49 | + root, |
| 50 | + include_submodules=self.include_submodules, |
| 51 | + include_meson_subprojects=self.include_meson_subprojects, |
| 52 | + ) |
| 53 | + # FileNotFoundError and NotADirectoryError don't need to be caught |
| 54 | + # because argparse already made sure of these things. |
| 55 | + except GlobalLicensingParseError as error: |
| 56 | + raise click.UsageError( |
| 57 | + _( |
| 58 | + "'{path}' could not be parsed. We received the" |
| 59 | + " following error message: {message}" |
| 60 | + ).format(path=error.source, message=str(error)) |
| 61 | + ) from error |
| 62 | + |
| 63 | + except (GlobalLicensingConflict, OSError) as error: |
| 64 | + raise click.UsageError(str(error)) from error |
| 65 | + |
| 66 | + self._project = project |
| 67 | + return project |
35 | 68 |
|
36 | 69 |
|
37 | 70 | class MutexOption(click.Option):
|
|
0 commit comments