announce.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. """module to send announcements for processed packages
  2. @contact: Debian FTP Master <ftpmaster@debian.org>
  3. @copyright: 2012, Ansgar Burchardt <ansgar@debian.org>
  4. @license: GPL-2+
  5. """
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License along
  17. # with this program; if not, write to the Free Software Foundation, Inc.,
  18. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. import os
  20. from typing import Optional, Union
  21. from daklib.config import Config
  22. from daklib.textutils import fix_maintainer
  23. from daklib.utils import mail_addresses_for_upload, TemplateSubst, send_mail
  24. class ProcessedUpload:
  25. """Contains data of a processed upload.
  26. """
  27. # people
  28. maintainer = None #: Maintainer: field contents
  29. changed_by = None #: Changed-By: field contents
  30. fingerprint = None #: Fingerprint of upload signer
  31. # suites
  32. suites = [] #: Destination suites
  33. from_policy_suites = [] #: Policy suites
  34. # package
  35. changes = None #: Contents of .changes file from upload
  36. changes_filename = None #: Changes Filename
  37. sourceful = None #: Did upload contain source
  38. source = None #: Source value from changes
  39. architecture = None #: Architectures from changes
  40. version = None #: Version from changes
  41. bugs = None #: Bugs closed in upload
  42. # program
  43. program = "unknown-program" #: Which dak program was in use
  44. warnings = [] #: Eventual warnings for upload
  45. def _subst_for_upload(upload: ProcessedUpload) -> dict:
  46. """ Prepare substitutions used for announce mails.
  47. :param upload: upload to handle
  48. :return: A dict of substition values for use by :func:`daklib.utils.TemplateSubst`
  49. """
  50. cnf = Config()
  51. maintainer = upload.maintainer or cnf['Dinstall::MyEmailAddress']
  52. changed_by = upload.changed_by or maintainer
  53. if upload.sourceful:
  54. maintainer_to = mail_addresses_for_upload(maintainer, changed_by, upload.fingerprint)
  55. else:
  56. maintainer_to = mail_addresses_for_upload(maintainer, maintainer, upload.fingerprint)
  57. bcc = 'X-DAK: dak {0}'.format(upload.program)
  58. if 'Dinstall::Bcc' in cnf:
  59. bcc = '{0}\nBcc: {1}'.format(bcc, cnf['Dinstall::Bcc'])
  60. subst = {
  61. '__DISTRO__': cnf['Dinstall::MyDistribution'],
  62. '__BUG_SERVER__': cnf.get('Dinstall::BugServer'),
  63. '__ADMIN_ADDRESS__': cnf['Dinstall::MyAdminAddress'],
  64. '__DAK_ADDRESS__': cnf['Dinstall::MyEmailAddress'],
  65. '__REJECTOR_ADDRESS__': cnf['Dinstall::MyEmailAddress'],
  66. '__MANUAL_REJECT_MESSAGE__': '',
  67. '__BCC__': bcc,
  68. '__MAINTAINER__': changed_by,
  69. '__MAINTAINER_FROM__': fix_maintainer(changed_by)[1],
  70. '__MAINTAINER_TO__': ', '.join(maintainer_to),
  71. '__CHANGES_FILENAME__': upload.changes_filename,
  72. '__FILE_CONTENTS__': upload.changes,
  73. '__SOURCE__': upload.source,
  74. '__VERSION__': upload.version,
  75. '__ARCHITECTURE__': upload.architecture,
  76. '__WARNINGS__': '\n'.join(upload.warnings),
  77. }
  78. override_maintainer = cnf.get('Dinstall::OverrideMaintainer')
  79. if override_maintainer:
  80. subst['__MAINTAINER_FROM__'] = subst['__MAINTAINER_TO__'] = override_maintainer
  81. return subst
  82. def _whitelists(upload: ProcessedUpload):
  83. return [s.mail_whitelist for s in upload.suites]
  84. def announce_reject(upload: ProcessedUpload, reason: str, rejected_by: Optional[str] = None) -> None:
  85. """ Announce a reject.
  86. :param upload: upload to handle
  87. :param reason: Reject reason
  88. :param rejected_by: Who is doing the reject.
  89. """
  90. cnf = Config()
  91. subst = _subst_for_upload(upload)
  92. whitelists = _whitelists(upload)
  93. automatic = rejected_by is None
  94. subst['__CC__'] = 'X-DAK-Rejection: {0}'.format('automatic' if automatic else 'manual')
  95. subst['__REJECT_MESSAGE__'] = reason
  96. if rejected_by:
  97. subst['__REJECTOR_ADDRESS__'] = rejected_by
  98. if not automatic:
  99. subst['__BCC__'] = '{0}\nBcc: {1}'.format(subst['__BCC__'], subst['__REJECTOR_ADDRESS__'])
  100. message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'queue.rejected'))
  101. send_mail(message, whitelists=whitelists)
  102. def announce_accept(upload: ProcessedUpload) -> None:
  103. """ Announce an upload.
  104. :param upload: upload to handle
  105. """
  106. cnf = Config()
  107. subst = _subst_for_upload(upload)
  108. whitelists = _whitelists(upload)
  109. accepted_to_real_suite = any(suite.policy_queue is None or suite in upload.from_policy_suites for suite in upload.suites)
  110. subst['__ACTION__'] = 'accept' if accepted_to_real_suite else 'policy'
  111. suite_names = []
  112. for suite in upload.suites:
  113. if suite.policy_queue and suite not in upload.from_policy_suites:
  114. suite_names.append("{0}->{1}".format(suite.suite_name, suite.policy_queue.queue_name))
  115. else:
  116. suite_names.append(suite.suite_name)
  117. subst['__SUITE__'] = ', '.join(suite_names) or '(none)'
  118. message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.accepted'))
  119. send_mail(message, whitelists=whitelists)
  120. if accepted_to_real_suite and upload.sourceful:
  121. # send mail to announce lists and tracking server
  122. announce = set()
  123. for suite in upload.suites:
  124. if suite.policy_queue is None or suite in upload.from_policy_suites:
  125. announce.update(suite.announce or [])
  126. announce_list_address = ", ".join(announce)
  127. # according to #890944 this email shall be sent to dispatch@<TrackingServer> to avoid
  128. # bouncing emails
  129. # the package email alias is not yet created shortly after accepting the package
  130. tracker = cnf.get('Dinstall::TrackingServer')
  131. if tracker:
  132. announce_list_address = "{0}\nBcc: dispatch@{1}".format(announce_list_address, tracker)
  133. if len(announce_list_address) != 0:
  134. my_subst = subst.copy()
  135. my_subst['__ANNOUNCE_LIST_ADDRESS__'] = announce_list_address
  136. message = TemplateSubst(my_subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.announce'))
  137. send_mail(message, whitelists=whitelists)
  138. close_bugs_default = cnf.find_b('Dinstall::CloseBugs')
  139. close_bugs = any(s.close_bugs if s.close_bugs is not None else close_bugs_default for s in upload.suites)
  140. if accepted_to_real_suite and upload.sourceful and close_bugs:
  141. for bug in upload.bugs:
  142. my_subst = subst.copy()
  143. my_subst['__BUG_NUMBER__'] = str(bug)
  144. message = TemplateSubst(my_subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.bug-close'))
  145. send_mail(message, whitelists=whitelists)
  146. def announce_new(upload: ProcessedUpload) -> None:
  147. """ Announce an upload going to NEW.
  148. :param upload: upload to handle
  149. """
  150. cnf = Config()
  151. subst = _subst_for_upload(upload)
  152. whitelists = _whitelists(upload)
  153. message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.new'))
  154. send_mail(message, whitelists=whitelists)