Coverage for dak/check_overrides.py: 78%
160 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"""Cruft checker and hole filler for overrides
3@contact: Debian FTPMaster <ftpmaster@debian.org>
4@copyright: 2000, 2001, 2002, 2004, 2006 James Troup <james@nocrew.org>
5@opyright: 2005 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6@copyright: 2011 Joerg Jaspert <joerg@debian.org>
7@license: GNU General Public License version 2 or later
9"""
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25################################################################################
27######################################################################
28# NB: dak check-overrides is not a good idea with New Incoming as it #
29# doesn't take into account accepted. You can minimize the impact #
30# of this by running it immediately after dak process-accepted but #
31# that's still racy because 'dak process-new' doesn't lock with 'dak #
32# process-accepted'. A better long term fix is the evil plan for #
33# accepted to be in the DB. #
34######################################################################
36# dak check-overrides should now work fine being done during
37# cron.daily, for example just before 'dak make-overrides' (after 'dak
38# process-accepted' and 'dak make-suite-file-list'). At that point,
39# queue/accepted should be empty and installed, so... dak
40# check-overrides does now take into account suites sharing overrides
42# TODO:
43# * Only update out-of-sync overrides when corresponding versions are equal to
44# some degree
45# * consistency checks like:
46# - section=debian-installer only for udeb and # dsc
47# - priority=optional if dsc
48# - (suite, package, 'dsc') is unique,
49# - just as (suite, package, (u)deb) (yes, across components!)
50# - sections match their component (each component has an own set of sections,
51# could probably be reduced...)
53################################################################################
55import sys
56from typing import TYPE_CHECKING, NoReturn
58import apt_pkg
59from sqlalchemy import select, sql
61from daklib import daklog, utils
62from daklib.config import Config
63from daklib.dbconn import (
64 Component,
65 DBConn,
66 OverrideType,
67 Suite,
68 get_component,
69 get_override_type,
70 get_priorities,
71 get_priority,
72 get_sections,
73 get_suite,
74)
76if TYPE_CHECKING:
77 from sqlalchemy.orm import Session
79################################################################################
81Options: apt_pkg.Configuration #: Commandline arguments parsed into this
82Logger: daklog.Logger #: Our logging object
83sections: dict[int, str] = {}
84priorities: dict[int, str] = {}
85blacklist: set[str] = set()
87################################################################################
90def usage(exit_code=0) -> NoReturn:
91 print(
92 """Usage: dak check-overrides
93Check for cruft in overrides.
95 -n, --no-action don't do anything
96 -h, --help show this help and exit"""
97 )
99 sys.exit(exit_code)
102################################################################################
105def process(
106 osuite: str,
107 affected_suites: list[int],
108 originosuite: str | None,
109 component: str,
110 otype: str,
111 session: "Session",
112) -> None:
113 global Logger, Options, sections, priorities
115 o = get_suite(osuite, session)
116 if o is None: 116 ↛ 117line 116 didn't jump to line 117 because the condition on line 116 was never true
117 utils.fubar("Suite '%s' not recognised." % (osuite))
118 osuite_id = o.suite_id
120 originosuite_id = None
121 if originosuite:
122 oo = get_suite(originosuite, session)
123 if oo is None: 123 ↛ 124line 123 didn't jump to line 124 because the condition on line 123 was never true
124 utils.fubar("Suite '%s' not recognised." % (originosuite))
125 originosuite_id = oo.suite_id
127 c = get_component(component, session)
128 if c is None: 128 ↛ 129line 128 didn't jump to line 129 because the condition on line 128 was never true
129 utils.fubar("Component '%s' not recognised." % (component))
130 component_id = c.component_id
132 ot = get_override_type(otype, session)
133 if ot is None: 133 ↛ 134line 133 didn't jump to line 134 because the condition on line 133 was never true
134 utils.fubar(
135 "Type '%s' not recognised. (Valid types are deb, udeb and dsc)" % (otype)
136 )
137 type_id = ot.overridetype_id
138 dsc_type = get_override_type("dsc", session)
139 assert dsc_type is not None
140 dsc_type_id = dsc_type.overridetype_id
142 source_priority = get_priority("optional", session)
143 assert source_priority is not None
144 source_priority_id = source_priority.priority_id
146 if otype == "deb" or otype == "udeb":
147 packages = {}
148 q = session.execute(
149 sql.text(
150 """
151SELECT b.package
152 FROM binaries b
153 JOIN bin_associations ba ON b.id = ba.bin
154 JOIN suite ON ba.suite = suite.id
155 JOIN files_archive_map af ON b.file = af.file_id AND suite.archive_id = af.archive_id
156 WHERE b.type = :otype AND ba.suite = ANY(:affected_suites) AND af.component_id = :component_id
157"""
158 ),
159 {
160 "otype": otype,
161 "affected_suites": affected_suites,
162 "component_id": component_id,
163 },
164 )
165 for i in q.fetchall():
166 packages[i[0]] = 0
168 src_packages = {}
169 q = session.execute(
170 sql.text(
171 """
172SELECT s.source FROM source s
173 JOIN src_associations sa ON s.id = sa.source
174 JOIN suite ON sa.suite = suite.id
175 JOIN files_archive_map af ON s.file = af.file_id AND suite.archive_id = af.archive_id
176 WHERE sa.suite = ANY(:affected_suites) AND af.component_id = :component_id
177"""
178 ),
179 {"affected_suites": affected_suites, "component_id": component_id},
180 )
181 for i in q.fetchall():
182 src_packages[i[0]] = 0
184 # -----------
185 # Drop unused overrides
187 q = session.execute(
188 sql.text(
189 """SELECT package, priority, section, maintainer
190 FROM override WHERE suite = :suite_id
191 AND component = :component_id AND type = :type_id"""
192 ),
193 {"suite_id": osuite_id, "component_id": component_id, "type_id": type_id},
194 )
195 # We're already within a transaction
196 if otype == "dsc":
197 for i in q.fetchall():
198 package = i[0]
199 if package in src_packages: 199 ↛ 202line 199 didn't jump to line 202 because the condition on line 199 was always true
200 src_packages[package] = 1
201 else:
202 if package in blacklist:
203 utils.warn("%s in incoming, not touching" % package)
204 continue
205 Logger.log(
206 [
207 "removing unused override",
208 osuite,
209 component,
210 otype,
211 package,
212 priorities[i[1]],
213 sections[i[2]],
214 i[3],
215 ]
216 )
217 if not Options["No-Action"]:
218 session.execute(
219 sql.text(
220 """DELETE FROM override WHERE package = :package
221 AND suite = :suite_id AND component = :component_id
222 AND type = :type_id
223 AND created < now() - interval '14 days'"""
224 ),
225 {
226 "package": package,
227 "suite_id": osuite_id,
228 "component_id": component_id,
229 "type_id": type_id,
230 },
231 )
232 # create source overrides based on binary overrides, as source
233 # overrides not always get created
234 q = session.execute(
235 sql.text(
236 """SELECT package, priority, section, maintainer
237 FROM override WHERE suite = :suite_id AND component = :component_id"""
238 ),
239 {"suite_id": osuite_id, "component_id": component_id},
240 )
241 for i in q.fetchall():
242 package = i[0]
243 if package not in src_packages or src_packages[package]: 243 ↛ 245line 243 didn't jump to line 245 because the condition on line 243 was always true
244 continue
245 src_packages[package] = 1
247 Logger.log(
248 [
249 "add missing override",
250 osuite,
251 component,
252 otype,
253 package,
254 "source",
255 sections[i[2]],
256 i[3],
257 ]
258 )
259 if not Options["No-Action"]:
260 session.execute(
261 sql.text(
262 """INSERT INTO override (package, suite, component,
263 priority, section, type, maintainer)
264 VALUES (:package, :suite_id, :component_id,
265 :priority_id, :section_id, :type_id, :maintainer)"""
266 ),
267 {
268 "package": package,
269 "suite_id": osuite_id,
270 "component_id": component_id,
271 "priority_id": source_priority_id,
272 "section_id": i[2],
273 "type_id": dsc_type_id,
274 "maintainer": i[3],
275 },
276 )
277 # Check whether originosuite has an override for us we can
278 # copy
279 if originosuite:
280 q = session.execute(
281 sql.text(
282 """SELECT origin.package, origin.priority, origin.section,
283 origin.maintainer, target.priority, target.section,
284 target.maintainer
285 FROM override origin
286 LEFT JOIN override target ON (origin.package = target.package
287 AND target.suite = :suite_id
288 AND origin.component = target.component
289 AND origin.type = target.type)
290 WHERE origin.suite = :originsuite_id
291 AND origin.component = :component_id
292 AND origin.type = :type_id"""
293 ),
294 {
295 "suite_id": osuite_id,
296 "originsuite_id": originosuite_id,
297 "component_id": component_id,
298 "type_id": type_id,
299 },
300 )
301 for i in q.fetchall():
302 package = i[0]
303 if package not in src_packages or src_packages[package]:
304 if i[4] and (i[1] != i[4] or i[2] != i[5] or i[3] != i[6]): 304 ↛ 305line 304 didn't jump to line 305 because the condition on line 304 was never true
305 Logger.log(
306 [
307 "syncing override",
308 osuite,
309 component,
310 otype,
311 package,
312 "source",
313 sections[i[5]],
314 i[6],
315 "source",
316 sections[i[2]],
317 i[3],
318 ]
319 )
320 if not Options["No-Action"]:
321 session.execute(
322 sql.text(
323 """UPDATE override
324 SET priority = :priority,
325 section = :section,
326 maintainer = :maintainer
327 WHERE package = :package AND suite = :suite_id
328 AND component = :component_id AND type = :type_id"""
329 ),
330 {
331 "priority": i[1],
332 "section": i[2],
333 "maintainer": i[3],
334 "package": package,
335 "suite_id": osuite_id,
336 "component_id": component_id,
337 "type_id": dsc_type_id,
338 },
339 )
340 continue
342 # we can copy
343 src_packages[package] = 1
344 Logger.log(
345 [
346 "copying missing override",
347 osuite,
348 component,
349 otype,
350 package,
351 "source",
352 sections[i[2]],
353 i[3],
354 ]
355 )
356 if not Options["No-Action"]: 356 ↛ 301line 356 didn't jump to line 301 because the condition on line 356 was always true
357 session.execute(
358 sql.text(
359 """INSERT INTO override (package, suite, component,
360 priority, section, type, maintainer)
361 VALUES (:package, :suite_id, :component_id,
362 :priority_id, :section_id, :type_id,
363 :maintainer)"""
364 ),
365 {
366 "package": package,
367 "suite_id": osuite_id,
368 "component_id": component_id,
369 "priority_id": source_priority_id,
370 "section_id": i[2],
371 "type_id": dsc_type_id,
372 "maintainer": i[3],
373 },
374 )
376 for package, hasoverride in list(src_packages.items()):
377 if not hasoverride: 377 ↛ 378line 377 didn't jump to line 378 because the condition on line 377 was never true
378 utils.warn("%s has no override!" % package)
380 else: # binary override
381 for i in q.fetchall():
382 package = i[0]
383 if package in packages:
384 packages[package] = 1
385 else:
386 if package in blacklist: 386 ↛ 387line 386 didn't jump to line 387 because the condition on line 386 was never true
387 utils.warn("%s in incoming, not touching" % package)
388 continue
389 Logger.log(
390 [
391 "removing unused override",
392 osuite,
393 component,
394 otype,
395 package,
396 priorities[i[1]],
397 sections[i[2]],
398 i[3],
399 ]
400 )
401 if not Options["No-Action"]: 401 ↛ 381line 401 didn't jump to line 381 because the condition on line 401 was always true
402 session.execute(
403 sql.text(
404 """DELETE FROM override
405 WHERE package = :package AND suite = :suite_id
406 AND component = :component_id AND type = :type_id
407 AND created < now() - interval '14 days'"""
408 ),
409 {
410 "package": package,
411 "suite_id": osuite_id,
412 "component_id": component_id,
413 "type_id": type_id,
414 },
415 )
417 # Check whether originosuite has an override for us we can
418 # copy
419 if originosuite:
420 q = session.execute(
421 sql.text(
422 """SELECT origin.package, origin.priority, origin.section,
423 origin.maintainer, target.priority, target.section,
424 target.maintainer
425 FROM override origin LEFT JOIN override target
426 ON (origin.package = target.package
427 AND target.suite = :suite_id
428 AND origin.component = target.component
429 AND origin.type = target.type)
430 WHERE origin.suite = :originsuite_id
431 AND origin.component = :component_id
432 AND origin.type = :type_id"""
433 ),
434 {
435 "suite_id": osuite_id,
436 "originsuite_id": originosuite_id,
437 "component_id": component_id,
438 "type_id": type_id,
439 },
440 )
441 for i in q.fetchall():
442 package = i[0]
443 if package not in packages or packages[package]:
444 if i[4] and (i[1] != i[4] or i[2] != i[5] or i[3] != i[6]): 444 ↛ 445line 444 didn't jump to line 445 because the condition on line 444 was never true
445 Logger.log(
446 [
447 "syncing override",
448 osuite,
449 component,
450 otype,
451 package,
452 priorities[i[4]],
453 sections[i[5]],
454 i[6],
455 priorities[i[1]],
456 sections[i[2]],
457 i[3],
458 ]
459 )
460 if not Options["No-Action"]:
461 session.execute(
462 sql.text(
463 """UPDATE override
464 SET priority = :priority_id,
465 section = :section_id,
466 maintainer = :maintainer
467 WHERE package = :package
468 AND suite = :suite_id
469 AND component = :component_id
470 AND type = :type_id"""
471 ),
472 {
473 "priority_id": i[1],
474 "section_id": i[2],
475 "maintainer": i[3],
476 "package": package,
477 "suite_id": osuite_id,
478 "component_id": component_id,
479 "type_id": type_id,
480 },
481 )
482 continue
483 # we can copy
484 packages[package] = 1
485 Logger.log(
486 [
487 "copying missing override",
488 osuite,
489 component,
490 otype,
491 package,
492 priorities[i[1]],
493 sections[i[2]],
494 i[3],
495 ]
496 )
497 if not Options["No-Action"]: 497 ↛ 441line 497 didn't jump to line 441 because the condition on line 497 was always true
498 session.execute(
499 sql.text(
500 """INSERT INTO override (package, suite, component,
501 priority, section, type, maintainer)
502 VALUES (:package, :suite_id, :component_id,
503 :priority_id, :section_id, :type_id, :maintainer)"""
504 ),
505 {
506 "package": package,
507 "suite_id": osuite_id,
508 "component_id": component_id,
509 "priority_id": i[1],
510 "section_id": i[2],
511 "type_id": type_id,
512 "maintainer": i[3],
513 },
514 )
516 for package, hasoverride in list(packages.items()):
517 if not hasoverride: 517 ↛ 518line 517 didn't jump to line 518 because the condition on line 517 was never true
518 utils.warn("%s has no override!" % package)
520 session.commit()
521 sys.stdout.flush()
524################################################################################
527def main() -> None:
528 global Logger, Options, sections, priorities
530 cnf = Config()
532 Arguments = [
533 ("h", "help", "Check-Overrides::Options::Help"),
534 ("n", "no-action", "Check-Overrides::Options::No-Action"),
535 ]
536 for i in ["help", "no-action"]:
537 key = "Check-Overrides::Options::%s" % i
538 if key not in cnf: 538 ↛ 536line 538 didn't jump to line 536 because the condition on line 538 was always true
539 cnf[key] = ""
540 apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv) # type: ignore[attr-defined]
541 Options = cnf.subtree("Check-Overrides::Options")
543 if Options["Help"]:
544 usage()
546 session = DBConn().session()
548 # init sections, priorities:
550 # We need forward and reverse
551 sections = {entry: name for name, entry in get_sections(session).items()}
552 priorities = {entry: name for name, entry in get_priorities(session).items()}
554 if not Options["No-Action"]: 554 ↛ 557line 554 didn't jump to line 557 because the condition on line 554 was always true
555 Logger = daklog.Logger("check-overrides")
556 else:
557 Logger = daklog.Logger("check-overrides", 1)
559 for suite in session.scalars(select(Suite).where(Suite.overrideprocess)):
560 originosuite_name: str | None = None
561 originremark = ""
563 if suite.overrideorigin is not None:
564 originosuite = get_suite(suite.overrideorigin, session)
565 if originosuite is None: 565 ↛ 566line 565 didn't jump to line 566 because the condition on line 565 was never true
566 utils.fubar(
567 "%s has an override origin suite of %s but it doesn't exist!"
568 % (suite.suite_name, suite.overrideorigin)
569 )
570 originosuite_name = originosuite.suite_name
571 originremark = " taking missing from %s" % originosuite_name
573 print("Processing %s%s..." % (suite.suite_name, originremark))
575 # Get a list of all suites that use the override file of 'suite.suite_name' as
576 # well as the suite
577 ocodename = suite.codename
578 suiteids = list(
579 session.scalars(
580 select(Suite.suite_id).where(Suite.overridecodename == ocodename)
581 )
582 )
583 if suite.suite_id not in suiteids: 583 ↛ 586line 583 didn't jump to line 586 because the condition on line 583 was always true
584 suiteids.append(suite.suite_id)
586 if len(suiteids) < 1: 586 ↛ 587line 586 didn't jump to line 587 because the condition on line 586 was never true
587 utils.fubar("Couldn't find id's of all suites: %s" % suiteids)
589 for component_name in session.scalars(select(Component.component_name)):
590 # It is crucial for the dsc override creation based on binary
591 # overrides that 'dsc' goes first
592 otypes = ["dsc"]
593 for otype_name in session.scalars(select(OverrideType.overridetype)):
594 if otype_name == "dsc":
595 continue
596 otypes.append(otype_name)
598 for otype in otypes:
599 print(
600 "Processing %s [%s - %s]"
601 % (suite.suite_name, component_name, otype)
602 )
603 sys.stdout.flush()
604 process(
605 suite.suite_name,
606 suiteids,
607 originosuite_name,
608 component_name,
609 otype,
610 session,
611 )
613 Logger.close()