Coverage for src/debputy/dh_migration/models.py: 83%
105 statements
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-28 20:14 +0000
« prev ^ index » next coverage.py v7.16.1, created at 2026-10-28 20:14 +0000
1import dataclasses
2import re
3from collections.abc import Sequence, Iterable
4from typing import cast
6from debputy.architecture_support import DpkgArchitectureBuildProcessValuesTable
7from debputy.commands.debputy_cmd.output import IOBasedOutputStyling
8from debputy.highlevel_manifest import MutableYAMLManifest, HighLevelManifest
9from debputy.packages import BinaryPackage
10from debputy.plugin.api import VirtualPath
11from debputy.plugin.api.spec import DebputyIntegrationMode
12from debputy.pydeb_compat import resolve_dpkg_arch_keyword_for_all_archs
13from debputy.substitution import Substitution
15_DH_VAR_RE = re.compile(r"([$][{])([A-Za-z0-9][-_:0-9A-Za-z]*)([}])")
18class AcceptableMigrationIssues:
19 def __init__(self, values: frozenset[str]):
20 self._values = values
22 def __contains__(self, item: str) -> bool:
23 return item in self._values or "ALL" in self._values
26class UnsupportedFeature(RuntimeError):
27 @property
28 def message(self) -> str:
29 return cast("str", self.args[0])
31 @property
32 def issue_keys(self) -> Sequence[str] | None:
33 if len(self.args) < 2:
34 return None
35 return cast("Sequence[str]", self.args[1])
38class ConflictingChange(RuntimeError):
39 @property
40 def message(self) -> str:
41 return cast("str", self.args[0])
44@dataclasses.dataclass(slots=True)
45class MigrationRequest:
46 debian_dir: VirtualPath
47 manifest: HighLevelManifest
48 acceptable_migration_issues: AcceptableMigrationIssues
49 migration_target: DebputyIntegrationMode | None
51 @property
52 def all_packages(self) -> Iterable[BinaryPackage]:
53 return self.manifest.all_packages
55 @property
56 def is_single_binary_package(self) -> bool:
57 return sum(1 for _ in self.all_packages) == 1
59 @property
60 def main_binary(self) -> BinaryPackage:
61 return next(iter(p for p in self.all_packages if p.is_main_package))
63 def all_arch_names(self) -> frozenset[str] | None:
64 return resolve_dpkg_arch_keyword_for_all_archs(
65 self.manifest.dpkg_arch_query_table, "ARCH"
66 )
68 def all_arch_os_names(self) -> frozenset[str] | None:
69 return resolve_dpkg_arch_keyword_for_all_archs(
70 self.manifest.dpkg_arch_query_table, "ARCH_OS"
71 )
74@dataclasses.dataclass(slots=True)
75class FeatureMigration:
76 tagline: str
77 fo: IOBasedOutputStyling
78 successful_manifest_changes: int = 0
79 warnings: list[str] = dataclasses.field(default_factory=list)
80 remove_paths_on_success: list[str] = dataclasses.field(default_factory=list)
81 rename_paths_on_success: list[tuple[str, str]] = dataclasses.field(
82 default_factory=list
83 )
84 assumed_compat: int | None = None
85 required_plugins: list[str] = dataclasses.field(default_factory=list)
87 def warn(self, msg: str) -> None:
88 self.warnings.append(msg)
90 def rename_on_success(self, source: str, dest: str) -> None:
91 self.rename_paths_on_success.append((source, dest))
93 def remove_on_success(self, path: str) -> None:
94 self.remove_paths_on_success.append(path)
96 def require_plugin(self, debputy_plugin: str) -> None:
97 self.required_plugins.append(debputy_plugin)
99 @property
100 def anything_to_do(self) -> bool:
101 return bool(self.total_changes_involved)
103 @property
104 def performed_changes(self) -> int:
105 return (
106 self.successful_manifest_changes
107 + len(self.remove_paths_on_success)
108 + len(self.rename_paths_on_success)
109 )
111 @property
112 def total_changes_involved(self) -> int:
113 return (
114 self.successful_manifest_changes
115 + len(self.warnings)
116 + len(self.remove_paths_on_success)
117 + len(self.rename_paths_on_success)
118 )
121class DHMigrationSubstitution(Substitution):
122 def __init__(
123 self,
124 dpkg_arch_table: DpkgArchitectureBuildProcessValuesTable,
125 acceptable_migration_issues: AcceptableMigrationIssues,
126 feature_migration: FeatureMigration,
127 mutable_manifest: MutableYAMLManifest,
128 ) -> None:
129 self._acceptable_migration_issues = acceptable_migration_issues
130 self._dpkg_arch_table = dpkg_arch_table
131 self._feature_migration = feature_migration
132 self._mutable_manifest = mutable_manifest
133 # TODO: load 1:1 variables from the real subst instance (less stuff to keep in sync)
134 one2one = [
135 "DEB_SOURCE",
136 "DEB_VERSION",
137 "DEB_VERSION_EPOCH_UPSTREAM",
138 "DEB_VERSION_UPSTREAM_REVISION",
139 "DEB_VERSION_UPSTREAM",
140 "SOURCE_DATE_EPOCH",
141 ]
142 self._builtin_substs = {
143 "Tab": "{{token:TAB}}",
144 "Space": " ",
145 "Newline": "{{token:NEWLINE}}",
146 "Dollar": "${}",
147 }
148 self._builtin_substs.update((x, "{{" + x + "}}") for x in one2one)
150 def _replacement(self, key: str, definition_source: str) -> str:
151 if key in self._builtin_substs: 151 ↛ 152line 151 didn't jump to line 152 because the condition on line 151 was never true
152 return self._builtin_substs[key]
153 if key in self._dpkg_arch_table:
154 return "{{" + key + "}}"
155 if key.startswith("env:"): 155 ↛ 156line 155 didn't jump to line 156 because the condition on line 155 was never true
156 if "dh-subst-env" not in self._acceptable_migration_issues:
157 raise UnsupportedFeature(
158 "Use of environment based substitution variable {{"
159 + key
160 + "}} is not"
161 f" supported in debputy. The variable was spotted at {definition_source}",
162 ["dh-subst-env"],
163 )
164 elif "dh-subst-unknown-variable" not in self._acceptable_migration_issues: 164 ↛ 165line 164 didn't jump to line 165 because the condition on line 164 was never true
165 raise UnsupportedFeature(
166 "Unknown substitution variable {{"
167 + key
168 + "}}, which does not have a known"
169 f" counter part in debputy. The variable was spotted at {definition_source}",
170 ["dh-subst-unknown-variable"],
171 )
172 manifest_definitions = self._mutable_manifest.manifest_definitions(
173 create_if_absent=False
174 )
175 manifest_variables = manifest_definitions.manifest_variables(
176 create_if_absent=False
177 )
178 if key not in manifest_variables.variables: 178 ↛ 189line 178 didn't jump to line 189 because the condition on line 178 was always true
179 manifest_definitions.create_definition_if_missing()
180 manifest_variables[key] = "TODO: Provide variable value for " + key
181 self._feature_migration.warn(
182 "TODO: MANUAL MIGRATION of unresolved substitution variable {{"
183 + key
184 + "}} from"
185 + f" {definition_source}"
186 )
187 self._feature_migration.successful_manifest_changes += 1
189 return "{{" + key + "}}"
191 def substitute(
192 self,
193 value: str,
194 definition_source: str,
195 /,
196 escape_glob_characters: bool = False,
197 ) -> str:
198 if "${" not in value:
199 return value
200 replacement = self._apply_substitution(
201 _DH_VAR_RE,
202 value,
203 definition_source,
204 escape_glob_characters=escape_glob_characters,
205 )
206 return replacement.replace("${}", "$")
208 def with_extra_substitutions(self, **extra_substitutions: str) -> "Substitution":
209 return self