queue.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #!/usr/bin/env python
  2. # vim:set et sw=4:
  3. """
  4. Queue utility functions for dak
  5. @contact: Debian FTP Master <ftpmaster@debian.org>
  6. @copyright: 2001 - 2006 James Troup <james@nocrew.org>
  7. @copyright: 2009, 2010 Joerg Jaspert <joerg@debian.org>
  8. @license: GNU General Public License version 2 or later
  9. """
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 2 of the License, or
  13. # (at your option) any later version.
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. ###############################################################################
  22. import os
  23. import utils
  24. from types import *
  25. from dak_exceptions import *
  26. from changes import *
  27. from regexes import *
  28. from config import Config
  29. from dbconn import *
  30. ################################################################################
  31. def check_valid(overrides, session):
  32. """Check if section and priority for new overrides exist in database.
  33. Additionally does sanity checks:
  34. - debian-installer packages have to be udeb (or source)
  35. - non debian-installer packages cannot be udeb
  36. @type overrides: list of dict
  37. @param overrides: list of overrides to check. The overrides need
  38. to be given in form of a dict with the following keys:
  39. - package: package name
  40. - priority
  41. - section
  42. - component
  43. - type: type of requested override ('dsc', 'deb' or 'udeb')
  44. All values are strings.
  45. @rtype: bool
  46. @return: C{True} if all overrides are valid, C{False} if there is any
  47. invalid override.
  48. """
  49. all_valid = True
  50. for o in overrides:
  51. o['valid'] = True
  52. if session.query(Priority).filter_by(priority=o['priority']).first() is None:
  53. o['valid'] = False
  54. if session.query(Section).filter_by(section=o['section']).first() is None:
  55. o['valid'] = False
  56. if get_mapped_component(o['component'], session) is None:
  57. o['valid'] = False
  58. if o['type'] not in ('dsc', 'deb', 'udeb'):
  59. raise Exception('Unknown override type {0}'.format(o['type']))
  60. if o['type'] == 'udeb' and o['section'] != 'debian-installer':
  61. o['valid'] = False
  62. if o['section'] == 'debian-installer' and o['type'] not in ('dsc', 'udeb'):
  63. o['valid'] = False
  64. all_valid = all_valid and o['valid']
  65. return all_valid
  66. ###############################################################################
  67. def prod_maintainer(notes, upload):
  68. cnf = Config()
  69. changes = upload.changes
  70. whitelists = [ upload.target_suite.mail_whitelist ]
  71. # Here we prepare an editor and get them ready to prod...
  72. (fd, temp_filename) = utils.temp_filename()
  73. temp_file = os.fdopen(fd, 'w')
  74. temp_file.write("\n\n=====\n\n".join([note.comment for note in notes]))
  75. temp_file.close()
  76. editor = os.environ.get("EDITOR","vi")
  77. answer = 'E'
  78. while answer == 'E':
  79. os.system("%s %s" % (editor, temp_filename))
  80. temp_fh = utils.open_file(temp_filename)
  81. prod_message = "".join(temp_fh.readlines())
  82. temp_fh.close()
  83. print "Prod message:"
  84. print utils.prefix_multi_line_string(prod_message," ",include_blank_lines=1)
  85. prompt = "[P]rod, Edit, Abandon, Quit ?"
  86. answer = "XXX"
  87. while prompt.find(answer) == -1:
  88. answer = utils.our_raw_input(prompt)
  89. m = re_default_answer.search(prompt)
  90. if answer == "":
  91. answer = m.group(1)
  92. answer = answer[:1].upper()
  93. os.unlink(temp_filename)
  94. if answer == 'A':
  95. return
  96. elif answer == 'Q':
  97. return 0
  98. # Otherwise, do the proding...
  99. user_email_address = utils.whoami() + " <%s>" % (
  100. cnf["Dinstall::MyAdminAddress"])
  101. changed_by = changes.changedby or changes.maintainer
  102. maintainer = changes.maintainer
  103. maintainer_to = utils.mail_addresses_for_upload(maintainer, changed_by, changes.fingerprint)
  104. Subst = {
  105. '__SOURCE__': upload.changes.source,
  106. '__CHANGES_FILENAME__': upload.changes.changesname,
  107. '__MAINTAINER_TO__': ", ".join(maintainer_to),
  108. }
  109. Subst["__FROM_ADDRESS__"] = user_email_address
  110. Subst["__PROD_MESSAGE__"] = prod_message
  111. Subst["__CC__"] = "Cc: " + cnf["Dinstall::MyEmailAddress"]
  112. prod_mail_message = utils.TemplateSubst(
  113. Subst,cnf["Dir::Templates"]+"/process-new.prod")
  114. # Send the prod mail
  115. utils.send_mail(prod_mail_message, whitelists=whitelists)
  116. print "Sent prodding message"
  117. ################################################################################
  118. def edit_note(note, upload, session, trainee=False):
  119. # Write the current data to a temporary file
  120. (fd, temp_filename) = utils.temp_filename()
  121. editor = os.environ.get("EDITOR","vi")
  122. answer = 'E'
  123. while answer == 'E':
  124. os.system("%s %s" % (editor, temp_filename))
  125. temp_file = utils.open_file(temp_filename)
  126. newnote = temp_file.read().rstrip()
  127. temp_file.close()
  128. print "New Note:"
  129. print utils.prefix_multi_line_string(newnote," ")
  130. prompt = "[D]one, Edit, Abandon, Quit ?"
  131. answer = "XXX"
  132. while prompt.find(answer) == -1:
  133. answer = utils.our_raw_input(prompt)
  134. m = re_default_answer.search(prompt)
  135. if answer == "":
  136. answer = m.group(1)
  137. answer = answer[:1].upper()
  138. os.unlink(temp_filename)
  139. if answer == 'A':
  140. return
  141. elif answer == 'Q':
  142. return 0
  143. comment = NewComment()
  144. comment.policy_queue = upload.policy_queue
  145. comment.package = upload.changes.source
  146. comment.version = upload.changes.version
  147. comment.comment = newnote
  148. comment.author = utils.whoami()
  149. comment.trainee = trainee
  150. session.add(comment)
  151. session.commit()
  152. ###############################################################################
  153. def get_suite_version_by_source(source, session):
  154. 'returns a list of tuples (suite_name, version) for source package'
  155. q = session.query(Suite.suite_name, DBSource.version). \
  156. join(Suite.sources).filter_by(source = source)
  157. return q.all()
  158. def get_suite_version_by_package(package, arch_string, session):
  159. '''
  160. returns a list of tuples (suite_name, version) for binary package and
  161. arch_string
  162. '''
  163. return session.query(Suite.suite_name, DBBinary.version). \
  164. join(Suite.binaries).filter_by(package = package). \
  165. join(DBBinary.architecture). \
  166. filter(Architecture.arch_string.in_([arch_string, 'all'])).all()
  167. class Upload(object):
  168. """
  169. Everything that has to do with an upload processed.
  170. """
  171. def __init__(self):
  172. self.logger = None
  173. self.pkg = Changes()
  174. self.reset()
  175. ###########################################################################
  176. def update_subst(self):
  177. """ Set up the per-package template substitution mappings """
  178. raise Exception('to be removed')
  179. cnf = Config()
  180. # If 'dak process-unchecked' crashed out in the right place, architecture may still be a string.
  181. if not self.pkg.changes.has_key("architecture") or not \
  182. isinstance(self.pkg.changes["architecture"], dict):
  183. self.pkg.changes["architecture"] = { "Unknown" : "" }
  184. # and maintainer2047 may not exist.
  185. if not self.pkg.changes.has_key("maintainer2047"):
  186. self.pkg.changes["maintainer2047"] = cnf["Dinstall::MyEmailAddress"]
  187. self.Subst["__ARCHITECTURE__"] = " ".join(self.pkg.changes["architecture"].keys())
  188. self.Subst["__CHANGES_FILENAME__"] = os.path.basename(self.pkg.changes_file)
  189. self.Subst["__FILE_CONTENTS__"] = self.pkg.changes.get("filecontents", "")
  190. # For source uploads the Changed-By field wins; otherwise Maintainer wins.
  191. if self.pkg.changes["architecture"].has_key("source") and \
  192. self.pkg.changes["changedby822"] != "" and \
  193. (self.pkg.changes["changedby822"] != self.pkg.changes["maintainer822"]):
  194. self.Subst["__MAINTAINER_FROM__"] = self.pkg.changes["changedby2047"]
  195. self.Subst["__MAINTAINER_TO__"] = "%s, %s" % (self.pkg.changes["changedby2047"], self.pkg.changes["maintainer2047"])
  196. self.Subst["__MAINTAINER__"] = self.pkg.changes.get("changed-by", "Unknown")
  197. else:
  198. self.Subst["__MAINTAINER_FROM__"] = self.pkg.changes["maintainer2047"]
  199. self.Subst["__MAINTAINER_TO__"] = self.pkg.changes["maintainer2047"]
  200. self.Subst["__MAINTAINER__"] = self.pkg.changes.get("maintainer", "Unknown")
  201. # Process policy doesn't set the fingerprint field and I don't want to make it
  202. # do it for now as I don't want to have to deal with the case where we accepted
  203. # the package into PU-NEW, but the fingerprint has gone away from the keyring in
  204. # the meantime so the package will be remarked as rejectable. Urgh.
  205. # TODO: Fix this properly
  206. if self.pkg.changes.has_key('fingerprint'):
  207. session = DBConn().session()
  208. fpr = get_fingerprint(self.pkg.changes['fingerprint'], session)
  209. if fpr and self.check_if_upload_is_sponsored("%s@debian.org" % fpr.uid.uid, fpr.uid.name):
  210. if self.pkg.changes.has_key("sponsoremail"):
  211. self.Subst["__MAINTAINER_TO__"] += ", %s" % self.pkg.changes["sponsoremail"]
  212. session.close()
  213. if cnf.has_key("Dinstall::TrackingServer") and self.pkg.changes.has_key("source"):
  214. self.Subst["__MAINTAINER_TO__"] += "\nBcc: %s@%s" % (self.pkg.changes["source"], cnf["Dinstall::TrackingServer"])
  215. # Apply any global override of the Maintainer field
  216. if cnf.get("Dinstall::OverrideMaintainer"):
  217. self.Subst["__MAINTAINER_TO__"] = cnf["Dinstall::OverrideMaintainer"]
  218. self.Subst["__MAINTAINER_FROM__"] = cnf["Dinstall::OverrideMaintainer"]
  219. self.Subst["__REJECT_MESSAGE__"] = self.package_info()
  220. self.Subst["__SOURCE__"] = self.pkg.changes.get("source", "Unknown")
  221. self.Subst["__VERSION__"] = self.pkg.changes.get("version", "Unknown")
  222. self.Subst["__SUITE__"] = ", ".join(self.pkg.changes["distribution"])
  223. ###########################################################################
  224. def check_if_upload_is_sponsored(self, uid_email, uid_name):
  225. for key in "maintaineremail", "changedbyemail", "maintainername", "changedbyname":
  226. if not self.pkg.changes.has_key(key):
  227. return False
  228. uid_email = '@'.join(uid_email.split('@')[:2])
  229. if uid_email in [self.pkg.changes["maintaineremail"], self.pkg.changes["changedbyemail"]]:
  230. sponsored = False
  231. elif uid_name in [self.pkg.changes["maintainername"], self.pkg.changes["changedbyname"]]:
  232. sponsored = False
  233. if uid_name == "":
  234. sponsored = True
  235. else:
  236. sponsored = True
  237. sponsor_addresses = utils.gpg_get_key_addresses(self.pkg.changes["fingerprint"])
  238. debian_emails = filter(lambda addr: addr.endswith('@debian.org'), sponsor_addresses)
  239. if uid_email not in debian_emails:
  240. if debian_emails:
  241. uid_email = debian_emails[0]
  242. if ("source" in self.pkg.changes["architecture"] and uid_email and utils.is_email_alias(uid_email)):
  243. if (self.pkg.changes["maintaineremail"] not in sponsor_addresses and
  244. self.pkg.changes["changedbyemail"] not in sponsor_addresses):
  245. self.pkg.changes["sponsoremail"] = uid_email
  246. return sponsored
  247. ###########################################################################
  248. # End check_signed_by_key checks
  249. ###########################################################################
  250. def announce(self, short_summary, action):
  251. """
  252. Send an announce mail about a new upload.
  253. @type short_summary: string
  254. @param short_summary: Short summary text to include in the mail
  255. @type action: bool
  256. @param action: Set to false no real action will be done.
  257. @rtype: string
  258. @return: Textstring about action taken.
  259. """
  260. cnf = Config()
  261. # Skip all of this if not sending mail to avoid confusing people
  262. if cnf.has_key("Dinstall::Options::No-Mail") and cnf["Dinstall::Options::No-Mail"]:
  263. return ""
  264. # Only do announcements for source uploads with a recent dpkg-dev installed
  265. if float(self.pkg.changes.get("format", 0)) < 1.6 or not \
  266. self.pkg.changes["architecture"].has_key("source"):
  267. return ""
  268. announcetemplate = os.path.join(cnf["Dir::Templates"], 'process-unchecked.announce')
  269. lists_todo = {}
  270. summary = ""
  271. # Get a unique list of target lists
  272. for dist in self.pkg.changes["distribution"].keys():
  273. suite = get_suite(dist)
  274. if suite is None: continue
  275. for tgt in suite.announce:
  276. lists_todo[tgt] = 1
  277. self.Subst["__SHORT_SUMMARY__"] = short_summary
  278. for announce_list in lists_todo.keys():
  279. summary += "Announcing to %s\n" % (announce_list)
  280. if action:
  281. self.update_subst()
  282. self.Subst["__ANNOUNCE_LIST_ADDRESS__"] = announce_list
  283. if cnf.get("Dinstall::TrackingServer") and \
  284. self.pkg.changes["architecture"].has_key("source"):
  285. trackingsendto = "Bcc: %s@%s" % (self.pkg.changes["source"], cnf["Dinstall::TrackingServer"])
  286. self.Subst["__ANNOUNCE_LIST_ADDRESS__"] += "\n" + trackingsendto
  287. mail_message = utils.TemplateSubst(self.Subst, announcetemplate)
  288. utils.send_mail(mail_message)
  289. del self.Subst["__ANNOUNCE_LIST_ADDRESS__"]
  290. if cnf.find_b("Dinstall::CloseBugs") and cnf.has_key("Dinstall::BugServer"):
  291. summary = self.close_bugs(summary, action)
  292. del self.Subst["__SHORT_SUMMARY__"]
  293. return summary
  294. ###########################################################################
  295. def check_override(self):
  296. """
  297. Checks override entries for validity. Mails "Override disparity" warnings,
  298. if that feature is enabled.
  299. Abandons the check if
  300. - override disparity checks are disabled
  301. - mail sending is disabled
  302. """
  303. cnf = Config()
  304. # Abandon the check if override disparity checks have been disabled
  305. if not cnf.find_b("Dinstall::OverrideDisparityCheck"):
  306. return
  307. summary = self.pkg.check_override()
  308. if summary == "":
  309. return
  310. overridetemplate = os.path.join(cnf["Dir::Templates"], 'process-unchecked.override-disparity')
  311. self.update_subst()
  312. self.Subst["__SUMMARY__"] = summary
  313. mail_message = utils.TemplateSubst(self.Subst, overridetemplate)
  314. utils.send_mail(mail_message)
  315. del self.Subst["__SUMMARY__"]