|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# -*- Python -*- |
| 4 | +# |
| 5 | +# $Id: generate-draft-aliases $ |
| 6 | +# |
| 7 | +# Author: Markus Stenberg <markus.stenberg@iki.fi> |
| 8 | +# |
| 9 | +""" |
| 10 | +
|
| 11 | +This code dumps Django model InternetDraft's contents as postfix email |
| 12 | +aliases |
| 13 | +
|
| 14 | +<no suffix> (same as -authors) |
| 15 | +.authors (list of authors) |
| 16 | +.chairs (WG chairs) |
| 17 | +.notify (notify emails(?)) |
| 18 | +.ad (sponsoring AD) |
| 19 | +.all (all of the above) |
| 20 | +
|
| 21 | +TODO: |
| 22 | +
|
| 23 | +- results somewhat inconsistent with the results from the old tool; |
| 24 | + should examine why (ask me for diff tool if interested in fixing it) |
| 25 | +
|
| 26 | +""" |
| 27 | + |
| 28 | +DRAFT_EMAIL_SUFFIX='@tools.ietf.org' |
| 29 | + |
| 30 | +# boilerplate (from various other ietf/bin scripts) |
| 31 | +import os, sys |
| 32 | +basedir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../..")) |
| 33 | +sys.path = [ basedir ] + sys.path |
| 34 | + |
| 35 | +from ietf import settings |
| 36 | +from django.core import management |
| 37 | +management.setup_environ(settings) |
| 38 | + |
| 39 | +from ietf.doc.models import Document |
| 40 | +from ietf.group.utils import get_group_chairs_emails, get_group_ads_emails |
| 41 | +from ietf.utils.aliases import * |
| 42 | +import time |
| 43 | + |
| 44 | +def get_draft_ad_emails(draft): |
| 45 | + " Get AD email for the given draft, if any. " |
| 46 | + # If working group document, return current WG ADs |
| 47 | + wg = draft.group |
| 48 | + if wg and wg.acronym != 'none' and wg.parent and wg.parent.acronym != 'none': |
| 49 | + return get_group_ads_emails(wg) |
| 50 | + # If not, return explicit AD set (whether up to date or not) |
| 51 | + ad = draft.ad |
| 52 | + #return [ad and ad.user and ad.user.email] |
| 53 | + return [ad and ad.email_address()] |
| 54 | + |
| 55 | +def get_draft_authors_emails(draft): |
| 56 | + " Get list of authors for the given draft." |
| 57 | + |
| 58 | + # This feels 'correct'; however, it creates fairly large delta |
| 59 | + return [email.email_address() for email in draft.authors.all()] |
| 60 | + |
| 61 | + # This gives fairly small delta compared to current state, |
| 62 | + # however, it seems to be wrong (doesn't check for emails being |
| 63 | + # active etc). |
| 64 | + #return [email.address for email in draft.authors.all()] |
| 65 | + |
| 66 | +def get_draft_notify_emails(draft): |
| 67 | + " Get list of email addresses to notify for the given draft." |
| 68 | + n = draft.notify |
| 69 | + if not n: |
| 70 | + return |
| 71 | + l = [] |
| 72 | + draft_email = draft.name + DRAFT_EMAIL_SUFFIX |
| 73 | + for e in n.split(','): |
| 74 | + # If the draft name itself is listed as notify list element, we |
| 75 | + # expand it (to make results better verifiable with the old ones) |
| 76 | + e = e.strip() |
| 77 | + if e == draft_email: |
| 78 | + l.extend(get_draft_authors_emails(draft)) |
| 79 | + else: |
| 80 | + l.append(e) |
| 81 | + # Alternative: if we don't want to do expansion, just this would be |
| 82 | + # perhaps better (MTA can do expansion too): |
| 83 | + # l = n.split(',') |
| 84 | + return l |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + import datetime |
| 88 | + import time |
| 89 | + |
| 90 | + # Year ago? |
| 91 | + show_since = datetime.datetime.now() - datetime.timedelta(365) |
| 92 | + # 10 years ago? |
| 93 | + #show_since = datetime.datetime.now() - datetime.timedelta(10 * 365) |
| 94 | + |
| 95 | + modname = 'ietf.generate_draft_aliases' |
| 96 | + date = time.strftime("%Y-%m-%d_%H:%M:%S") |
| 97 | + print '# Generated by python -m %s at %s' % (modname, date) |
| 98 | + |
| 99 | + drafts = Document.objects.all() |
| 100 | + |
| 101 | + # Drafts with active status |
| 102 | + active_drafts = drafts.filter(states__slug='active') |
| 103 | + |
| 104 | + # Drafts that expired within year |
| 105 | + inactive_recent_drafts = drafts.exclude(states__slug='active').filter(expires__gte=show_since) |
| 106 | + |
| 107 | + interesting_drafts = active_drafts | inactive_recent_drafts |
| 108 | + |
| 109 | + for draft in interesting_drafts.distinct().iterator(): |
| 110 | + # Omit RFCs, we care only about drafts |
| 111 | + if draft.docalias_set.filter(name__startswith='rfc'): |
| 112 | + continue |
| 113 | + name = draft.name |
| 114 | + done = [] |
| 115 | + all = [] |
| 116 | + def handle_sublist(name, f, o, is_ad=False): |
| 117 | + r = dump_sublist(name, f, o, is_ad) |
| 118 | + if r: |
| 119 | + done.append(name) |
| 120 | + all.extend(r) |
| 121 | + return r |
| 122 | + #.authors (/and no suffix) = authors |
| 123 | + |
| 124 | + # First, do no suffix case |
| 125 | + # If no authors, don't generate list either |
| 126 | + r = dump_sublist(name, get_draft_authors_emails, draft) |
| 127 | + if not r: |
| 128 | + continue |
| 129 | + handle_sublist('%s%s' % (name, '.authors'), get_draft_authors_emails, draft) |
| 130 | + wg = draft.group |
| 131 | + |
| 132 | + if wg: |
| 133 | + # .chairs = WG chairs |
| 134 | + handle_sublist('%s%s' % (name, '.chairs'), get_group_chairs_emails, wg) |
| 135 | + |
| 136 | + # .ad = sponsoring AD / WG AD (WG document) |
| 137 | + handle_sublist('%s%s' % (name, '.ad'), get_draft_ad_emails, draft, True) |
| 138 | + |
| 139 | + # .notify = notify email list from the Document |
| 140 | + handle_sublist('%s%s' % (name, '.notify'), get_draft_notify_emails, draft) |
| 141 | + |
| 142 | + # .all = everything on 'done' (recursive aliases) |
| 143 | + #dump_sublist('%s%s' % (name, '.all'), None, done) |
| 144 | + # .all = everything on 'all' (expanded aliases) |
| 145 | + dump_sublist('%s%s' % (name, '.all'), None, all) |
| 146 | + |
| 147 | + |
0 commit comments