Coverage for dak/update_suite.py: 78%
125 statements
« prev ^ index » next coverage.py v7.6.0, created at 2026-11-21 08:51 +0000
« prev ^ index » next coverage.py v7.6.0, created at 2026-11-21 08:51 +0000
1# Copyright (C) 2015, Ansgar Burchardt <ansgar@debian.org>
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License along
14# with this program; if not, write to the Free Software Foundation, Inc.,
15# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17import sys
18from collections.abc import Collection, Sequence
19from typing import TYPE_CHECKING, NoReturn
21from sqlalchemy import select, sql
23import daklib.daklog
24import daklib.utils
25from daklib.archive import ArchiveTransaction
26from daklib.dbconn import ArchiveFile, Component, DBBinary, DBSource, PoolFile, Suite
28if TYPE_CHECKING:
29 from sqlalchemy.engine import ScalarResult
31"""
32Idea:
34dak update-suite testing testing-kfreebsd
35 -> grab all source & binary packages from testing with a higher version
36 than in testing-kfreebsd (or not in -kfreebsd) and copy them
37 -> limited to architectures in testing-kfreebsd
38 -> obeys policy queues
39 -> copies to build queues
41dak update-suite --create-in=ftp-master stable testing
42 -> create suite "testing" based on "stable" in archive "ftp-master"
44Additional switches:
45 --skip-policy-queue: skip target suite's policy queue
46 --skip-build-queues: do not copy to build queue
47 --no-new-packages: do not copy new packages
48 -> source-based, new binaries from existing sources will be added
49 --only-new-packages: do not update existing packages
50 -> source-based, do not copy new binaries w/o source!
51 --also-policy-queue: also copy pending packages from policy queue
52 --update-overrides: update overrides as well (if different overrides are used)
53 --no-act
54"""
57def usage() -> NoReturn:
58 print("dak update-suite [-n|--no-act] <origin> <target>")
59 sys.exit(0)
62class SuiteUpdater:
63 def __init__(
64 self,
65 transaction: ArchiveTransaction,
66 origin: Suite,
67 target: Suite,
68 new_packages=True,
69 also_from_policy_queue=False,
70 obey_policy_queue=True,
71 obey_build_queues=True,
72 update_overrides=False,
73 dry_run=False,
74 ):
75 self.transaction = transaction
76 self.origin = origin
77 self.target = target
78 self.new_packages = new_packages
79 self.also_from_policy_queue = also_from_policy_queue
80 self.obey_policy_queue = obey_policy_queue
81 self.obey_build_queues = obey_build_queues
82 self.update_overrides = update_overrides
83 self.dry_run = dry_run
85 if obey_policy_queue and target.policy_queue_id is not None: 85 ↛ 86line 85 didn't jump to line 86 because the condition on line 85 was never true
86 raise Exception("Not implemented...")
87 self.logger = None if dry_run else daklib.daklog.Logger("update-suite")
89 def query_new_binaries(
90 self, additional_sources: Collection[int]
91 ) -> "Sequence[DBBinary]":
92 # Candidates are binaries in the origin suite, and optionally in its policy queue.
93 query = """
94 SELECT b.*
95 FROM binaries b
96 JOIN bin_associations ba ON b.id = ba.bin AND ba.suite = :origin
97 """
98 if self.also_from_policy_queue: 98 ↛ 99line 98 didn't jump to line 99 because the condition on line 98 was never true
99 query += """
100 UNION
101 SELECT b.*
102 FROM binaries b
103 JOIN policy_queue_upload_binaries_map pqubm ON pqubm.binary_id = b.id
104 JOIN policy_queue_upload pqu ON pqu.id = pqubm.policy_queue_upload_id
105 WHERE pqu.target_suite_id = :origin
106 AND pqu.policy_queue_id = (SELECT policy_queue_id FROM suite WHERE id = :origin)
107 """
109 # Only take binaries that are for a architecture part of the target suite,
110 # and whose source was just added to the target suite (i.e. listed in additional_sources)
111 # or that have the source already available in the target suite
112 # or in the target suite's policy queue if we obey policy queues,
113 # and filter out binaries with a lower version than already in the target suite.
114 if self.obey_policy_queue: 114 ↛ 123line 114 didn't jump to line 123 because the condition on line 114 was always true
115 cond_source_in_policy_queue = """
116 EXISTS (SELECT 1
117 FROM policy_queue_upload pqu
118 WHERE tmp.source = pqu.source_id
119 AND pqu.target_suite_id = :target
120 AND pqu.policy_queue_id = (SELECT policy_queue_id FROM suite WHERE id = :target))
121 """
122 else:
123 cond_source_in_policy_queue = "FALSE"
124 query = """
125 WITH tmp AS ({0})
126 SELECT DISTINCT *
127 FROM tmp
128 WHERE tmp.architecture IN (SELECT architecture FROM suite_architectures WHERE suite = :target)
129 AND (tmp.source = ANY(:additional_sources)
130 OR EXISTS (SELECT 1
131 FROM src_associations sa
132 WHERE tmp.source = sa.source AND sa.suite = :target)
133 OR {1})
134 AND NOT EXISTS (SELECT 1
135 FROM binaries b2
136 JOIN bin_associations ba2 ON b2.id = ba2.bin AND ba2.suite = :target
137 WHERE tmp.package = b2.package AND tmp.architecture = b2.architecture AND b2.version >= tmp.version)
138 ORDER BY package, version, architecture
139 """.format(
140 query, cond_source_in_policy_queue
141 )
143 params = {
144 "origin": self.origin.suite_id,
145 "target": self.target.suite_id,
146 "additional_sources": list(additional_sources),
147 }
149 return self.transaction.session.scalars(
150 select(DBBinary).from_statement(sql.text(query)), params
151 ).all()
153 def query_new_sources(self) -> "Sequence[DBSource]":
154 # Candidates are source packages in the origin suite, and optionally in its policy queue.
155 query = """
156 SELECT s.*
157 FROM source s
158 JOIN src_associations sa ON s.id = sa.source AND sa.suite = :origin
159 """
160 if self.also_from_policy_queue: 160 ↛ 161line 160 didn't jump to line 161 because the condition on line 160 was never true
161 query += """
162 UNION
163 SELECT s.*
164 FROM source s
165 JOIN policy_queue_upload pqu ON pqu.source_id = s.id
166 WHERE pqu.target_suite_id = :origin
167 AND pqu.policy_queue_id = (SELECT policy_queue_id FROM suite WHERE id = :origin)
168 """
170 # Filter out source packages with a lower version than already in the target suite.
171 query = """
172 WITH tmp AS ({0})
173 SELECT DISTINCT *
174 FROM tmp
175 WHERE NOT EXISTS (SELECT 1
176 FROM source s2
177 JOIN src_associations sa2 ON s2.id = sa2.source AND sa2.suite = :target
178 WHERE s2.source = tmp.source AND s2.version >= tmp.version)
179 """.format(
180 query
181 )
183 # Optionally filter out source packages that are not already in the target suite.
184 if not self.new_packages: 184 ↛ 185line 184 didn't jump to line 185 because the condition on line 184 was never true
185 query += """
186 AND EXISTS (SELECT 1
187 FROM source s2
188 JOIN src_associations sa2 ON s2.id = sa2.source AND sa2.suite = :target
189 WHERE s2.source = tmp.source)
190 """
192 query += "ORDER BY source, version"
194 params = {"origin": self.origin.suite_id, "target": self.target.suite_id}
196 return self.transaction.session.scalars(
197 select(DBSource).from_statement(sql.text(query)), params
198 ).all()
200 def _components_for_binary(
201 self, binary: DBBinary, suite: Suite
202 ) -> "ScalarResult[Component]":
203 session = self.transaction.session
204 return session.scalars(
205 select(Component)
206 .join(ArchiveFile, Component.component_id == ArchiveFile.component_id)
207 .join(ArchiveFile.file)
208 .where(PoolFile.file_id == binary.poolfile_id)
209 .where(ArchiveFile.archive_id == suite.archive_id)
210 )
212 def install_binaries(self, binaries: Collection[DBBinary], suite: Suite) -> None:
213 if len(binaries) == 0: 213 ↛ 214line 213 didn't jump to line 214 because the condition on line 213 was never true
214 return
215 # If origin and target suites are in the same archive, we can skip the
216 # overhead from ArchiveTransaction.copy_binary()
217 if self.origin.archive_id == suite.archive_id: 217 ↛ 223line 217 didn't jump to line 223 because the condition on line 217 was always true
218 query = "INSERT INTO bin_associations (bin, suite) VALUES (:bin, :suite)"
219 target_id = suite.suite_id
220 params = [{"bin": b.binary_id, "suite": target_id} for b in binaries]
221 self.transaction.session.execute(sql.text(query), params)
222 else:
223 for b in binaries:
224 for c in self._components_for_binary(b, suite):
225 self.transaction.copy_binary(b, suite, c)
227 def _components_for_source(
228 self, source: DBSource, suite: Suite
229 ) -> "ScalarResult[Component]":
230 session = self.transaction.session
231 return session.scalars(
232 select(Component)
233 .join(ArchiveFile, Component.component_id == ArchiveFile.component_id)
234 .join(ArchiveFile.file)
235 .where(PoolFile.file_id == source.poolfile_id)
236 .where(ArchiveFile.archive_id == suite.archive_id)
237 )
239 def install_sources(self, sources: Collection[DBSource], suite: Suite) -> None:
240 if len(sources) == 0: 240 ↛ 241line 240 didn't jump to line 241 because the condition on line 240 was never true
241 return
242 # If origin and target suites are in the same archive, we can skip the
243 # overhead from ArchiveTransaction.copy_source()
244 if self.origin.archive_id == suite.archive_id: 244 ↛ 252line 244 didn't jump to line 252 because the condition on line 244 was always true
245 query = (
246 "INSERT INTO src_associations (source, suite) VALUES (:source, :suite)"
247 )
248 target_id = suite.suite_id
249 params = [{"source": s.source_id, "suite": target_id} for s in sources]
250 self.transaction.session.execute(sql.text(query), params)
251 else:
252 for s in sources:
253 for c in self._components_for_source(s, suite):
254 self.transaction.copy_source(s, suite, c)
256 def update_suite(self) -> None:
257 targets = {self.target}
258 if self.obey_build_queues: 258 ↛ 260line 258 didn't jump to line 260 because the condition on line 258 was always true
259 targets.update([bq.suite for bq in self.target.copy_queues])
260 target_names = sorted(s.suite_name for s in targets)
261 target_name = ",".join(target_names)
263 new_sources = self.query_new_sources()
264 additional_sources = tuple(s.source_id for s in new_sources)
265 for s in new_sources:
266 self.log(["add-source", target_name, s.source, s.version])
267 if not self.dry_run:
268 for target in targets:
269 self.install_sources(new_sources, target)
271 new_binaries = self.query_new_binaries(additional_sources)
272 for b in new_binaries:
273 self.log(
274 [
275 "add-binary",
276 target_name,
277 b.package,
278 b.version,
279 b.architecture.arch_string,
280 ]
281 )
282 if not self.dry_run:
283 for target in targets:
284 self.install_binaries(new_binaries, target)
286 def log(self, args: list[object]) -> None:
287 if self.logger:
288 self.logger.log(args)
289 else:
290 print(*args, sep="|")
293def main() -> None:
294 from daklib.config import Config
296 config = Config()
298 import apt_pkg
300 arguments = [
301 ("h", "help", "Update-Suite::Options::Help"),
302 ("n", "no-act", "Update-Suite::options::NoAct"),
303 ]
304 argv = apt_pkg.parse_commandline(config.Cnf, arguments, sys.argv) # type: ignore[attr-defined]
305 try:
306 options = config.subtree("Update-Suite::Options")
307 except KeyError:
308 options = {}
310 if "Help" in options or len(argv) != 2:
311 usage()
313 origin_name = argv[0]
314 target_name = argv[1]
315 dry_run = True if "NoAct" in options else False
317 with ArchiveTransaction() as transaction:
318 session = transaction.session
320 origin = session.execute(
321 select(Suite).filter_by(suite_name=origin_name)
322 ).scalar_one_or_none()
323 if origin is None: 323 ↛ 324line 323 didn't jump to line 324 because the condition on line 323 was never true
324 daklib.utils.fubar("Origin suite '{0}' is unknown.".format(origin_name))
325 target = session.execute(
326 select(Suite).filter_by(suite_name=target_name)
327 ).scalar_one_or_none()
328 if target is None: 328 ↛ 329line 328 didn't jump to line 329 because the condition on line 328 was never true
329 daklib.utils.fubar("Target suite '{0}' is unknown.".format(target_name))
331 su = SuiteUpdater(transaction, origin, target, dry_run=dry_run)
332 su.update_suite()
334 if dry_run:
335 transaction.rollback()
336 else:
337 transaction.commit()