Coverage for dak/process_upload.py: 82%
320 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"""
2Checks Debian packages from Incoming
3@contact: Debian FTP Master <ftpmaster@debian.org>
4@copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006 James Troup <james@nocrew.org>
5@copyright: 2009 Joerg Jaspert <joerg@debian.org>
6@copyright: 2009 Mark Hymers <mhy@debian.org>
7@copyright: 2009 Frank Lichtenheld <djpig@debian.org>
8@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# based on process-unchecked and process-accepted
27## pu|pa: locking (daily.lock)
28## pu|pa: parse arguments -> list of changes files
29## pa: initialize urgency log
30## pu|pa: sort changes list
32## foreach changes:
33### pa: load dak file
34## pu: copy CHG to tempdir
35## pu: check CHG signature
36## pu: parse changes file
37## pu: checks:
38## pu: check distribution (mappings, rejects)
39## pu: copy FILES to tempdir
40## pu: check whether CHG already exists in CopyChanges
41## pu: check whether FILES already exist in one of the policy queues
42## for deb in FILES:
43## pu: extract control information
44## pu: various checks on control information
45## pu|pa: search for source (in CHG, projectb, policy queues)
46## pu|pa: check whether "Version" fulfills target suite requirements/suite propagation
47## pu|pa: check whether deb already exists in the pool
48## for src in FILES:
49## pu: various checks on filenames and CHG consistency
50## pu: if isdsc: check signature
51## for file in FILES:
52## pu: various checks
53## pu: NEW?
54## //pu: check whether file already exists in the pool
55## pu: store what "Component" the package is currently in
56## pu: check whether we found everything we were looking for in CHG
57## pu: check the DSC:
58## pu: check whether we need and have ONE DSC
59## pu: parse the DSC
60## pu: various checks //maybe drop some of the in favor of lintian
61## pu|pa: check whether "Version" fulfills target suite requirements/suite propagation
62## pu: check whether DSC_FILES is consistent with "Format"
63## for src in DSC_FILES:
64## pu|pa: check whether file already exists in the pool (with special handling for .orig.tar.gz)
65## pu: create new tempdir
66## pu: create symlink mirror of source
67## pu: unpack source
68## pu: extract changelog information for BTS
69## //pu: create missing .orig symlink
70## pu: check with lintian
71## for file in FILES:
72## pu: check checksums and sizes
73## for file in DSC_FILES:
74## pu: check checksums and sizes
75## pu: CHG: check urgency
76## for deb in FILES:
77## pu: extract contents list and check for dubious timestamps
78## pu: check that the uploader is actually allowed to upload the package
79### pa: install:
80### if stable_install:
81### pa: remove from p-u
82### pa: add to stable
83### pa: move CHG to morgue
84### pa: append data to ChangeLog
85### pa: send mail
86### pa: remove .dak file
87### else:
88### pa: add dsc to db:
89### for file in DSC_FILES:
90### pa: add file to file
91### pa: add file to dsc_files
92### pa: create source entry
93### pa: update source associations
94### pa: update src_uploaders
95### for deb in FILES:
96### pa: add deb to db:
97### pa: add file to file
98### pa: find source entry
99### pa: create binaries entry
100### pa: update binary associations
101### pa: .orig component move
102### pa: move files to pool
103### pa: save CHG
104### pa: move CHG to done/
105### pa: change entry in queue_build
106## pu: use dispatch table to choose target queue:
107## if NEW:
108## pu: write .dak file
109## pu: move to NEW
110## pu: send mail
111## elsif AUTOBYHAND:
112## pu: run autobyhand script
113## pu: if stuff left, do byhand or accept
114## elsif targetqueue in (oldstable, stable, embargo, unembargo):
115## pu: write .dak file
116## pu: check overrides
117## pu: move to queue
118## pu: send mail
119## else:
120## pu: write .dak file
121## pu: move to ACCEPTED
122## pu: send mails
123## pu: create files for BTS
124## pu: create entry in queue_build
125## pu: check overrides
127# Integrity checks
128## GPG
129## Parsing changes (check for duplicates)
130## Parse dsc
131## file list checks
133# New check layout (TODO: Implement)
134## Permission checks
135### suite mappings
136### ACLs
137### version checks (suite)
138### override checks
140## Source checks
141### copy orig
142### unpack
143### BTS changelog
144### src contents
145### lintian
146### urgency log
148## Binary checks
149### timestamps
150### control checks
151### src relation check
152### contents
154## Database insertion (? copy from stuff)
155### BYHAND / NEW / Policy queues
156### Pool
158## Queue builds
160import datetime
161import errno
162import fcntl
163import functools
164import os
165import random
166import sys
167import time
168import traceback
169from collections.abc import Callable, Iterable
170from typing import Concatenate, NoReturn
172import apt_pkg
174import daklib.announce
175import daklib.archive
176import daklib.checks
177import daklib.upload
178from daklib import daklog, utils
179from daklib.config import Config
180from daklib.dbconn import DBConn, SignatureHistory, get_active_keyring_files
181from daklib.regexes import re_default_answer
182from daklib.summarystats import SummaryStats
183from daklib.urgencylog import UrgencyLog
185###############################################################################
187Options: apt_pkg.Configuration
188Logger: daklog.Logger
190###############################################################################
193def usage(exit_code=0) -> NoReturn:
194 print(
195 """Usage: dak process-upload [OPTION]... [CHANGES]...
196 -a, --automatic automatic run
197 -d, --directory <DIR> process uploads in <DIR>
198 -h, --help show this help and exit.
199 --max-duration <D> stop processing after duration (e.g. 10m, 1h 5m)
200 -n, --no-action don't do anything
201 -p, --no-lock don't check lockfile !! for cron.daily only !!
202 -s, --no-mail don't send any mail
203 -V, --version display the version number and exit"""
204 )
205 sys.exit(exit_code)
208###############################################################################
210type Handler[**P, R] = Callable[Concatenate[str, daklib.archive.ArchiveUpload, P], R]
213def try_or_reject[**P, R](function: Handler[P, R]) -> Handler[P, R]:
214 """Try to call function or reject the upload if that fails"""
216 @functools.wraps(function)
217 def wrapper(directory: str, upload: daklib.archive.ArchiveUpload, *args, **kwargs):
218 reason = "No exception caught. This should not happen."
220 try:
221 return function(directory, upload, *args, **kwargs)
222 except (daklib.archive.ArchiveException, daklib.checks.Reject) as e:
223 reason = str(e)
224 except Exception:
225 reason = "There was an uncaught exception when processing your upload:\n{0}\nAny original reject reason follows below.".format(
226 traceback.format_exc()
227 )
229 try:
230 upload.rollback()
231 return real_reject(directory, upload, reason=reason)
232 except Exception:
233 reason = "In addition there was an exception when rejecting the package:\n{0}\nPrevious reasons:\n{1}".format(
234 traceback.format_exc(), reason
235 )
236 upload.rollback()
237 return real_reject(directory, upload, reason=reason, notify=False)
239 raise Exception(
240 "Rejecting upload failed after multiple tries. Giving up. Last reason:\n{0}".format(
241 reason
242 )
243 )
245 return wrapper
248def get_processed_upload(
249 upload: daklib.archive.ArchiveUpload,
250) -> daklib.announce.ProcessedUpload:
251 changes = upload.changes
252 control = upload.changes.changes
254 pu = daklib.announce.ProcessedUpload()
256 pu.maintainer = control.get("Maintainer")
257 pu.changed_by = control.get("Changed-By")
258 pu.fingerprint = changes.primary_fingerprint
259 pu.authorized_by_fingerprint = upload.authorized_by_fingerprint.fingerprint
261 pu.suites = upload.final_suites or []
262 pu.from_policy_suites = []
264 with open(upload.changes.path, "r") as fd:
265 pu.changes = fd.read()
266 pu.changes_filename = upload.changes.filename
267 pu.sourceful = upload.changes.sourceful
268 pu.source = control.get("Source")
269 pu.version = control.get("Version")
270 pu.architecture = control.get("Architecture")
271 pu.bugs = changes.closed_bugs
273 pu.program = "process-upload"
275 pu.warnings = upload.warnings
277 return pu
280@try_or_reject
281def accept(directory: str, upload: daklib.archive.ArchiveUpload) -> None:
282 cnf = Config()
284 Logger.log(["ACCEPT", upload.changes.filename])
285 print("ACCEPT")
287 upload.install()
288 utils.process_buildinfos(
289 upload.directory, upload.changes.buildinfo_files, upload.transaction.fs, Logger
290 )
292 assert upload.final_suites is not None
293 accepted_to_real_suite = any(
294 suite.policy_queue is None for suite in upload.final_suites
295 )
296 sourceful_upload = upload.changes.sourceful
298 control = upload.changes.changes
299 if sourceful_upload and not Options["No-Action"]:
300 urgency = control.get("Urgency")
301 # As per policy 5.6.17, the urgency can be followed by a space and a
302 # comment. Extract only the urgency from the string.
303 if " " in urgency: 303 ↛ 304line 303 didn't jump to line 304 because the condition on line 303 was never true
304 urgency, comment = urgency.split(" ", 1)
305 if urgency not in cnf.value_list("Urgency::Valid"): 305 ↛ 306line 305 didn't jump to line 306 because the condition on line 305 was never true
306 urgency = cnf["Urgency::Default"]
307 UrgencyLog().log(control["Source"], control["Version"], urgency)
309 pu = get_processed_upload(upload)
310 daklib.announce.announce_accept(pu)
312 # Move .changes to done, but only for uploads that were accepted to a
313 # real suite. process-policy will handle this for uploads to queues.
314 if accepted_to_real_suite:
315 src = os.path.join(upload.directory, upload.changes.filename)
317 now = datetime.datetime.now(tz=datetime.UTC)
318 donedir = os.path.join(cnf["Dir::Done"], now.strftime("%Y/%m/%d"))
319 dst = os.path.join(donedir, upload.changes.filename)
320 dst = utils.find_next_free(dst)
322 upload.transaction.fs.copy(src, dst, mode=0o644)
324 SummaryStats().accept_count += 1
325 SummaryStats().accept_bytes += upload.changes.bytes
328@try_or_reject
329def accept_to_new(directory: str, upload: daklib.archive.ArchiveUpload) -> None:
331 Logger.log(["ACCEPT-TO-NEW", upload.changes.filename])
332 print("ACCEPT-TO-NEW")
334 upload.install_to_new()
335 # TODO: tag bugs pending
337 pu = get_processed_upload(upload)
338 daklib.announce.announce_new(pu)
340 SummaryStats().accept_count += 1
341 SummaryStats().accept_bytes += upload.changes.bytes
344@try_or_reject
345def reject(
346 directory: str,
347 upload: daklib.archive.ArchiveUpload,
348 reason: str | None = None,
349 notify=True,
350) -> None:
351 real_reject(directory, upload, reason, notify)
354def real_reject(
355 directory: str,
356 upload: daklib.archive.ArchiveUpload,
357 reason: str | None = None,
358 notify=True,
359) -> None:
360 # XXX: rejection itself should go to daklib.archive.ArchiveUpload
361 cnf = Config()
363 Logger.log(["REJECT", upload.changes.filename])
364 print("REJECT")
366 fs = upload.transaction.fs
367 rejectdir = cnf["Dir::Reject"]
369 files = [f.filename for f in upload.changes.files.values()]
370 files.append(upload.changes.filename)
372 for fn in files:
373 src = os.path.join(upload.directory, fn)
374 dst = utils.find_next_free(os.path.join(rejectdir, fn))
375 if not os.path.exists(src):
376 continue
377 fs.copy(src, dst)
379 if upload.reject_reasons is not None: 379 ↛ 384line 379 didn't jump to line 384 because the condition on line 379 was always true
380 if reason is None: 380 ↛ 382line 380 didn't jump to line 382 because the condition on line 380 was always true
381 reason = ""
382 reason = reason + "\n" + "\n".join(upload.reject_reasons)
384 if reason is None: 384 ↛ 385line 384 didn't jump to line 385 because the condition on line 384 was never true
385 reason = "(Unknown reason. Please check logs.)"
387 dst = utils.find_next_free(
388 os.path.join(rejectdir, "{0}.reason".format(upload.changes.filename))
389 )
390 fh = fs.create(dst)
391 fh.write(reason)
392 fh.close()
394 if notify: 394 ↛ 398line 394 didn't jump to line 398 because the condition on line 394 was always true
395 pu = get_processed_upload(upload)
396 daklib.announce.announce_reject(pu, reason)
398 SummaryStats().reject_count += 1
401###############################################################################
404def action(directory: str, upload: daklib.archive.ArchiveUpload) -> bool:
405 changes = upload.changes
406 processed = True
408 global Logger
410 cnf = Config()
412 okay = upload.check()
414 try:
415 summary = changes.changes.get("Changes", "")
416 except UnicodeDecodeError as e:
417 summary = "Reading changes failed: %s" % (e)
418 # the upload checks should have detected this, but make sure this
419 # upload gets rejected in any case
420 upload.reject_reasons.append(summary)
422 package_info = []
423 if okay:
424 if changes.source is not None:
425 package_info.append("source:{0}".format(changes.source.dsc["Source"]))
426 for binary in changes.binaries:
427 package_info.append("binary:{0}".format(binary.control["Package"]))
429 (prompt, answer) = ("", "XXX")
430 if Options["No-Action"] or Options["Automatic"]: 430 ↛ 433line 430 didn't jump to line 433 because the condition on line 430 was always true
431 answer = "S"
433 print(summary)
434 print()
435 print("\n".join(package_info))
436 print()
437 if len(upload.warnings) > 0:
438 print("\n".join(upload.warnings))
439 print()
441 if len(upload.reject_reasons) > 0:
442 print("Reason:")
443 print("\n".join(upload.reject_reasons))
444 print()
446 path = os.path.join(directory, changes.filename)
447 created = os.stat(path).st_mtime
448 now = time.time()
449 too_new = now - created < int(cnf["Dinstall::SkipTime"])
451 if too_new:
452 print("SKIP (too new)")
453 prompt = "[S]kip, Quit ?"
454 else:
455 prompt = "[R]eject, Skip, Quit ?"
456 if Options["Automatic"]: 456 ↛ 467line 456 didn't jump to line 467 because the condition on line 456 was always true
457 answer = "R"
458 elif upload.new:
459 prompt = "[N]ew, Skip, Quit ?"
460 if Options["Automatic"]: 460 ↛ 467line 460 didn't jump to line 467 because the condition on line 460 was always true
461 answer = "N"
462 else:
463 prompt = "[A]ccept, Skip, Quit ?"
464 if Options["Automatic"]: 464 ↛ 467line 464 didn't jump to line 467 because the condition on line 464 was always true
465 answer = "A"
467 while prompt.find(answer) == -1: 467 ↛ 468line 467 didn't jump to line 468 because the condition on line 467 was never true
468 answer = utils.input_or_exit(prompt)
469 m = re_default_answer.match(prompt)
470 if answer == "":
471 assert m is not None
472 answer = m.group(1)
473 answer = answer[:1].upper()
475 if answer == "R":
476 reject(directory, upload)
477 elif answer == "A":
478 # upload.try_autobyhand must not be run with No-Action.
479 if Options["No-Action"] or upload.try_autobyhand(): 479 ↛ 482line 479 didn't jump to line 482 because the condition on line 479 was always true
480 accept(directory, upload)
481 else:
482 print("W: redirecting to BYHAND as automatic processing failed.")
483 accept_to_new(directory, upload)
484 elif answer == "N":
485 accept_to_new(directory, upload)
486 elif answer == "Q": 486 ↛ 487line 486 didn't jump to line 487 because the condition on line 486 was never true
487 sys.exit(0)
488 elif answer == "S": 488 ↛ 491line 488 didn't jump to line 491 because the condition on line 488 was always true
489 processed = False
491 if not Options["No-Action"]: 491 ↛ 494line 491 didn't jump to line 494 because the condition on line 491 was always true
492 upload.commit()
494 return processed
497###############################################################################
500def unlink_if_exists(path: str) -> None:
501 try:
502 os.unlink(path)
503 except OSError as e:
504 if e.errno != errno.ENOENT: 504 ↛ 505line 504 didn't jump to line 505 because the condition on line 504 was never true
505 raise
508def process_it(
509 directory: str, changes: daklib.upload.Changes, keyrings: list[str]
510) -> None:
511 global Logger
513 print("\n{0}\n".format(changes.filename))
514 Logger.log(["Processing changes file", changes.filename])
516 with daklib.archive.ArchiveUpload(directory, changes, keyrings) as upload:
517 processed = action(directory, upload)
518 if processed and not Options["No-Action"]:
519 session = DBConn().session()
520 history = SignatureHistory.from_signed_file(upload.changes)
521 if history.query(session) is None: 521 ↛ 524line 521 didn't jump to line 524 because the condition on line 521 was always true
522 session.add(history)
523 session.commit()
524 session.close()
526 unlink_if_exists(os.path.join(directory, changes.filename))
527 for fn in changes.files:
528 unlink_if_exists(os.path.join(directory, fn))
531###############################################################################
534def _source_group(source: str) -> str:
535 """decide group for given source name
537 This is mostly for Secure Boot signing where "X" should be
538 processed in the same group as (and before) "X-signed-*".
540 As a further special case, "grub2" needs to be processed in the
541 same group as (and before) "grub-efi-*-signed".
542 """
544 group = source.split("-", 1)[0]
545 if group == "grub2": 545 ↛ 546line 545 didn't jump to line 546 because the condition on line 545 was never true
546 return "grub"
547 return group
550def _group_changes_by_source_and_shuffle(
551 changes: list[tuple[str, daklib.upload.Changes]],
552) -> list[tuple[str, daklib.upload.Changes]]:
553 """Group changes by Source, sort each group, and shuffle group order."""
554 grouped: dict[str, list[tuple[str, daklib.upload.Changes]]] = {}
555 for directory, change in changes:
556 source = _source_group(change.changes.get("Source", ""))
557 grouped.setdefault(source, []).append((directory, change))
559 for group in grouped.values():
560 group.sort(key=lambda item: item[1])
562 source_names = list(grouped)
563 random.shuffle(source_names)
564 return [item for source in source_names for item in grouped[source]]
567###############################################################################
570def process_changes(
571 changes_filenames: Iterable[str],
572 max_duration: datetime.timedelta | None = None,
573) -> None:
574 deadline: float | None = None
575 if max_duration is not None: 575 ↛ 576line 575 didn't jump to line 576 because the condition on line 575 was never true
576 deadline = time.monotonic() + max_duration.total_seconds()
578 session = DBConn().session()
579 keyring_files = get_active_keyring_files(session)
580 session.close()
582 changes = []
583 for fn in changes_filenames:
584 try:
585 directory, filename = os.path.split(fn)
586 c = daklib.upload.Changes(directory, filename, keyring_files)
587 changes.append((directory, c))
588 except Exception as e:
589 try:
590 Logger.log(
591 [
592 filename,
593 "Error while loading changes file {0}: {1}".format(fn, e),
594 ]
595 )
596 except Exception as e:
597 Logger.log(
598 [
599 filename,
600 "Error while loading changes file {0}, with additional error while printing exception: {1}".format(
601 fn, repr(e)
602 ),
603 ]
604 )
606 changes = _group_changes_by_source_and_shuffle(changes)
608 for directory, c in changes:
609 if deadline is not None and time.monotonic() >= deadline: 609 ↛ 610line 609 didn't jump to line 610 because the condition on line 609 was never true
610 Logger.log(["Max duration reached; stopping processing loop"])
611 break
612 process_it(directory, c, keyring_files)
615###############################################################################
618def main() -> None:
619 global Options, Logger
621 cnf = Config()
622 summarystats = SummaryStats()
624 Arguments = [
625 ("a", "automatic", "Dinstall::Options::Automatic"),
626 ("h", "help", "Dinstall::Options::Help"),
627 ("\0", "max-duration", "Dinstall::Options::Max-Duration", "HasArg"),
628 ("n", "no-action", "Dinstall::Options::No-Action"),
629 ("p", "no-lock", "Dinstall::Options::No-Lock"),
630 ("s", "no-mail", "Dinstall::Options::No-Mail"),
631 ("d", "directory", "Dinstall::Options::Directory", "HasArg"),
632 ]
634 for i in [
635 "automatic",
636 "help",
637 "max-duration",
638 "no-action",
639 "no-lock",
640 "no-mail",
641 "version",
642 "directory",
643 ]:
644 key = "Dinstall::Options::%s" % i
645 if key not in cnf:
646 cnf[key] = ""
648 changes_files = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv) # type: ignore[attr-defined]
649 Options = cnf.subtree("Dinstall::Options")
651 if Options["Help"]:
652 usage()
654 # -n/--dry-run invalidates some other options which would involve things happening
655 if Options["No-Action"]: 655 ↛ 656line 655 didn't jump to line 656 because the condition on line 655 was never true
656 Options["Automatic"] = "" # type: ignore[index]
658 # Obtain lock if not in no-action mode and initialize the log
659 if not Options["No-Action"]: 659 ↛ 678line 659 didn't jump to line 678 because the condition on line 659 was always true
660 lock_fd = os.open(
661 os.path.join(cnf["Dir::Lock"], "process-upload.lock"),
662 os.O_RDWR | os.O_CREAT,
663 )
664 try:
665 fcntl.flock(lock_fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
666 except OSError as e:
667 if e.errno in (errno.EACCES, errno.EAGAIN):
668 utils.fubar(
669 "Couldn't obtain lock; assuming another 'dak process-upload' is already running."
670 )
671 else:
672 raise
674 # Initialise UrgencyLog() - it will deal with the case where we don't
675 # want to log urgencies
676 urgencylog = UrgencyLog()
678 Logger = daklog.Logger("process-upload", Options["No-Action"])
680 # If we have a directory flag, use it to find our files
681 if cnf["Dinstall::Options::Directory"] != "": 681 ↛ 695line 681 didn't jump to line 695 because the condition on line 681 was always true
682 # Note that we clobber the list of files we were given in this case
683 # so warn if the user has done both
684 if len(changes_files) > 0: 684 ↛ 685line 684 didn't jump to line 685 because the condition on line 684 was never true
685 utils.warn("Directory provided so ignoring files given on command line")
687 changes_files = utils.get_changes_files(cnf["Dinstall::Options::Directory"])
688 Logger.log(
689 [
690 "Using changes files from directory",
691 cnf["Dinstall::Options::Directory"],
692 len(changes_files),
693 ]
694 )
695 elif not len(changes_files) > 0:
696 utils.fubar("No changes files given and no directory specified")
697 else:
698 Logger.log(["Using changes files from command-line", len(changes_files)])
700 max_duration = None
701 if Options["Max-Duration"]: 701 ↛ 702line 701 didn't jump to line 702 because the condition on line 701 was never true
702 try:
703 max_duration = utils.parse_duration(Options["Max-Duration"])
704 except ValueError as e:
705 utils.fubar("Invalid --max-duration: %s" % e)
707 process_changes(changes_files, max_duration=max_duration)
709 if summarystats.accept_count:
710 sets = "set"
711 if summarystats.accept_count > 1:
712 sets = "sets"
713 print(
714 "Installed %d package %s, %s."
715 % (
716 summarystats.accept_count,
717 sets,
718 utils.size_type(int(summarystats.accept_bytes)),
719 )
720 )
721 Logger.log(["total", summarystats.accept_count, summarystats.accept_bytes])
723 if summarystats.reject_count:
724 sets = "set"
725 if summarystats.reject_count > 1:
726 sets = "sets"
727 print("Rejected %d package %s." % (summarystats.reject_count, sets))
728 Logger.log(["rejected", summarystats.reject_count])
730 if not Options["No-Action"]: 730 ↛ 733line 730 didn't jump to line 733 because the condition on line 730 was always true
731 urgencylog.close()
733 Logger.close()