From a3bb8f4b75f1812f14f4df676b16fb3fc0e38be9 Mon Sep 17 00:00:00 2001 From: Ansgar Burchardt Date: Mon, 17 Sep 2012 10:51:26 +0200 Subject: [PATCH] Add per-suite mail whitelists. --- dak/rm.py | 4 +++- daklib/announce.py | 16 +++++++++++----- daklib/queue.py | 3 ++- daklib/utils.py | 27 ++++++++++++++++++--------- 4 files changed, 34 insertions(+), 16 deletions(-) diff --git a/dak/rm.py b/dak/rm.py index e4f4a6cb..ec36bf4e 100755 --- a/dak/rm.py +++ b/dak/rm.py @@ -210,6 +210,7 @@ def main (): # Additional suite checks suite_ids_list = [] + whitelists = [] suites = utils.split_args(Options["Suite"]) suites_list = utils.join_with_commas_and(suites) if not Options["No-Action"]: @@ -217,6 +218,7 @@ def main (): s = get_suite(suite, session=session) if s is not None: suite_ids_list.append(s.suite_id) + whitelists.append(s.mail_whitelist) if suite in ("oldstable", "stable"): print "**WARNING** About to remove from the (old)stable suite!" print "This should only be done just prior to a (point) release and not at" @@ -498,7 +500,7 @@ def main (): mail_message = utils.TemplateSubst(Subst_close_rm,cnf["Dir::Templates"]+"/rm.bug-close-with-related") else: mail_message = utils.TemplateSubst(Subst_close_rm,cnf["Dir::Templates"]+"/rm.bug-close") - utils.send_mail(mail_message) + utils.send_mail(mail_message, whitelists=whitelists) # close associated bug reports if Options["Do-Close"]: diff --git a/daklib/announce.py b/daklib/announce.py index 6c0fd374..fdc3e468 100644 --- a/daklib/announce.py +++ b/daklib/announce.py @@ -87,9 +87,13 @@ def _subst_for_upload(upload): return subst +def _whitelists(upload): + return [ s.mail_whitelist for s in upload.suites ] + def announce_reject(upload, reason, rejected_by=None): cnf = Config() subst = _subst_for_upload(upload) + whitelists = _whitelists(upload) automatic = rejected_by is None @@ -103,11 +107,12 @@ def announce_reject(upload, reason, rejected_by=None): subst['__BCC__'] = '{0}\nBcc: {1}'.format(subst['__BCC__'], cnf['Dinstall::MyEmailAddress']) message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'queue.rejected')) - send_mail(message) + send_mail(message, whitelists=whitelists) def announce_accept(upload): cnf = Config() subst = _subst_for_upload(upload) + whitelists = _whitelists(upload) accepted_to_real_suite = any(suite.policy_queue is None or suite in upload.from_policy_suites for suite in upload.suites) @@ -121,7 +126,7 @@ def announce_accept(upload): subst['__SUITE__'] = ', '.join(suite_names) or '(none)' message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.accepted')) - send_mail(message) + send_mail(message, whitelists=whitelists) if accepted_to_real_suite and upload.sourceful: # senf mail to announce lists and tracking server @@ -141,7 +146,7 @@ def announce_accept(upload): my_subst['__ANNOUNCE_LIST_ADDRESS__'] = announce_list_address message = TemplateSubst(my_subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.announce')) - send_mail(message) + send_mail(message, whitelists=whitelists) close_bugs_default = cnf.find_b('Dinstall::CloseBugs') close_bugs = any(s.close_bugs if s.close_bugs is not None else close_bugs_default for s in upload.suites) @@ -151,11 +156,12 @@ def announce_accept(upload): my_subst['__BUG_NUMBER__'] = str(bug) message = TemplateSubst(my_subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.bug-close')) - send_mail(message) + send_mail(message, whitelists=whitelists) def announce_new(upload): cnf = Config() subst = _subst_for_upload(upload) + whitelists = _whitelists(upload) message = TemplateSubst(subst, os.path.join(cnf['Dir::Templates'], 'process-unchecked.new')) - send_mail(message) + send_mail(message, whitelists=whitelists) diff --git a/daklib/queue.py b/daklib/queue.py index 547972ed..9dd2702c 100644 --- a/daklib/queue.py +++ b/daklib/queue.py @@ -104,6 +104,7 @@ def check_valid(overrides, session): def prod_maintainer(notes, upload): cnf = Config() changes = upload.changes + whitelists = [ upload.target_suite.mail_whitelist ] # Here we prepare an editor and get them ready to prod... (fd, temp_filename) = utils.temp_filename() @@ -154,7 +155,7 @@ def prod_maintainer(notes, upload): Subst,cnf["Dir::Templates"]+"/process-new.prod") # Send the prod mail - utils.send_mail(prod_mail_message) + utils.send_mail(prod_mail_message, whitelists=whitelists) print "Sent prodding message" diff --git a/daklib/utils.py b/daklib/utils.py index 03cd5894..fd50cb1c 100755 --- a/daklib/utils.py +++ b/daklib/utils.py @@ -603,8 +603,16 @@ def build_package_list(dsc, session = None): ################################################################################ -def send_mail (message, filename=""): - """sendmail wrapper, takes _either_ a message string or a file as arguments""" +def send_mail (message, filename="", whitelists=None): + """sendmail wrapper, takes _either_ a message string or a file as arguments + + @type whitelists: list of (str or None) + @param whitelists: path to whitelists. C{None} or an empty list whitelists + everything, otherwise an address is whitelisted if it is + included in any of the lists. + In addition a global whitelist can be specified in + Dinstall::MailWhiteList. + """ maildir = Cnf.get('Dir::Mail') if maildir: @@ -624,23 +632,24 @@ def send_mail (message, filename=""): os.write (fd, message) os.close (fd) - if Cnf.has_key("Dinstall::MailWhiteList") and \ - Cnf["Dinstall::MailWhiteList"] != "": + if whitelists is None or None in whitelists: + whitelists = [] + if Cnf.get('Dinstall::MailWhiteList', ''): + whitelists.append(Cnf['Dinstall::MailWhiteList']) + if len(whitelists) != 0: message_in = open_file(filename) message_raw = modemail.message_from_file(message_in) message_in.close(); whitelist = []; - whitelist_in = open_file(Cnf["Dinstall::MailWhiteList"]) - try: + for path in whitelists: + with open_file(path, 'r') as whitelist_in: for line in whitelist_in: if not re_whitespace_comment.match(line): if re_re_mark.match(line): whitelist.append(re.compile(re_re_mark.sub("", line.strip(), 1))) else: whitelist.append(re.compile(re.escape(line.strip()))) - finally: - whitelist_in.close() # Fields to check. fields = ["To", "Bcc", "Cc"] @@ -657,7 +666,7 @@ def send_mail (message, filename=""): mail_whitelisted = 1 break if not mail_whitelisted: - print "Skipping %s since it's not in %s" % (item, Cnf["Dinstall::MailWhiteList"]) + print "Skipping {0} since it's not whitelisted".format(item) continue match.append(item) -- 2.39.5