+def sources2removals(source_list, suite_id, session):
+ """Compute removals items given a list of names of source packages
+
+ @type source_list: list
+ @param source_list: A list of names of source packages
+
+ @type suite_id: int
+ @param suite_id: The id of the suite from which these sources should be removed
+
+ @type session: SQLA Session
+ @param session: The database session in use
+
+ @rtype: list
+ @return: A list of items to be removed to remove all sources and their binaries from the given suite
+ """
+ to_remove = []
+ params = {"suite_id": suite_id, "sources": tuple(source_list)}
+ q = session.execute("""
+ SELECT s.source, s.version, 'source', s.id
+ FROM source s
+ JOIN src_associations sa ON sa.source = s.id
+ WHERE sa.suite = :suite_id AND s.source IN :sources""", params)
+ to_remove.extend(q)
+ q = session.execute("""
+ SELECT b.package, b.version, a.arch_string, b.id
+ FROM binaries b
+ JOIN bin_associations ba ON b.id = ba.bin
+ JOIN architecture a ON b.architecture = a.id
+ JOIN source s ON b.source = s.id
+ WHERE ba.suite = :suite_id AND s.source IN :sources""", params)
+ to_remove.extend(q)
+ return to_remove
+
+
+def decruft_newer_version_in(othersuite, suite_name, suite_id, rm_msg, session, dryrun):
+ """Compute removals items given a list of names of source packages
+
+ @type othersuite: str
+ @param othersuite: The name of the suite to compare with (e.g. "unstable" for "NVIU")
+
+ @type suite: str
+ @param suite: The name of the suite from which to do removals (e.g. "experimental" for "NVIU")
+
+ @type suite_id: int
+ @param suite_id: The id of the suite from which these sources should be removed
+
+ @type rm_msg: str
+ @param rm_msg: The removal message (or tag, e.g. "NVIU")
+
+ @type session: SQLA Session
+ @param session: The database session in use
+
+ @type dryrun: bool
+ @param dryrun: If True, just print the actions rather than actually doing them
+ """
+ nvi_list = [x[0] for x in newer_version(othersuite, suite_name, session)]
+ if nvi_list:
+ message = "[auto-cruft] %s" % rm_msg
+ if dryrun:
+ print " dak rm -m \"%s\" -s %s %s" % (message, suite_name, " ".join(nvi_list))
+ else:
+ removals = sources2removals(nvi_list, suite_id, session)
+ remove(session, message, [suite_name], removals, whoami="DAK's auto-decrufter")
+