#!/usr/bin/env python # -*- mode: python; coding: utf-8 -*- # Copyright © 2002, 2007 Translation Project. # Copyright © 2000 Progiciels Bourbeau-Pinard inc. # François Pinard , 2000. """\ Produce a '.mailrc' or '.procmailrc' file that aliases teams and domains. The most important are the "team-leaders" and "all-maintainers" aliases. Usage: create-mail-aliases [-mpDTf] -m produce output for .mailrc -p produce output for .procmailrc -D make aliases or rules to reach domain maintainers -T make aliases or rules to reach team leaders -f fall back on the team list when team has no leader -t make aliases or rules to reach the team lists """ import os, sys, getopt, string sys.path.insert(0, sys.path[0]+'/../lib') import registry def main(*arguments): if not arguments: sys.stdout.write(__doc__) sys.exit(0) options, arguments = getopt.getopt(arguments, 'mpDTft') mailrc = procmailrc = domains = leaders = teams = fallback = 0 for option, value in options: if option == '-m': mailrc = 1 elif option == '-p': procmailrc = 1 elif option == '-D': domains = 1 elif option == '-T': leaders = 1 elif option == '-f': fallback = 1 elif option == '-t': teams = 1 if mailrc and procmailrc: sys.stderr.write("***Cannot produce both mailrc AND procmailrc.\n") sys.exit(2) if not mailrc and not procmailrc: sys.stderr.write("***Use either option -m or -p.\n") sys.exit(2) if domains: if mailrc: alias_maintainers() else: rules_for_maintainers() if leaders: if mailrc: alias_leaders(fallback) else: rules_for_leaders(fallback) if teams: if mailrc: sys.stderr.write("***Not implemented.\n") else: rules_for_teams() def alias_leaders(fallback_on_lists): all_addresses = [] no_mailto = [] for team in registry.team_list(): mailto = None comment = None if team.leader: mailto = team.leader.mailto[0] comment = "%s for %s team" % (team.leader.name[0], team.language) if not mailto and fallback_on_lists: mailto = team.mailto[0] comment = "%s team" % team.language if mailto and mailto != "(nothing)": sys.stdout.write('alias %s "%s <%s>"\n' % (team.code, comment, mailto)) all_addresses.append(team.code) else: no_mailto.append(team.code) sys.stdout.write('alias team-leaders %s\n' % string.join(prune(all_addresses), ', ')) if no_mailto: sys.stderr.write('* No aliases for: %s\n' % string.join(no_mailto, ', ')) def rules_for_leaders(fallback_on_lists): all_addresses = [] no_mailto = [] for team in registry.team_list(): mailto = None if team.leader: mailto = team.leader.mailto[0] if not mailto and fallback_on_lists: mailto = team.mailto[0] if mailto and mailto != "(nothing)": sys.stdout.write(':0\n* ^To:.*[ <]%s@\n! <%s>\n' % (team.code, mailto)) all_addresses.append('<%s>' % mailto) else: no_mailto.append(team.code) group_rule('team-leaders', all_addresses) if no_mailto: sys.stderr.write('* No rules for: %s\n' % string.join(no_mailto, ', ')) def rules_for_teams(): all_addresses = [] no_mailto = [] for team in registry.team_list(): mailto = team.mailto[0] if mailto and mailto != "(nothing)": sys.stdout.write(':0\n* ^To:.*[ <]%s@\n! <%s>\n' % (team.code, mailto)) all_addresses.append('<%s>' % mailto) else: no_mailto.append(team.code) group_rule('all-teams', all_addresses) if no_mailto: sys.stderr.write('* No rules for: %s\n' % string.join(no_mailto, ', ')) def alias_maintainers(): all_maintainers = [] for domain in registry.domain_list(): try: mailto = domain.mailto[0] except IndexError: mailto = domain.nomailto[0] sys.stdout.write('alias %s "Textual domain for \'%s\' <%s>"\n' % (domain.name, domain.name, mailto)) all_maintainers.append(domain.name) sys.stdout.write('alias all-maintainers %s\n' % string.join(prune(all_maintainers), ', ')) def rules_for_maintainers(): all_maintainers = [] for domain in registry.domain_list(): try: mailto = domain.mailto[0] except IndexError: pass else: sys.stdout.write(':0\n* ^To:.*[ <]%s@\n! <%s>\n' % (domain.name, mailto)) all_maintainers.append('<%s>' % mailto) group_rule('all-maintainers', all_maintainers) def prune(list): list.sort() index = 1 while index < len(list): if list[index-1] == list[index]: del list[index] else: index += 1 return list def group_rule(target, list): list = prune(list) sys.stdout.write('\n:0\n* ^TO.*%s@\n* ^Subject: .*\(TP\) *$\n{\n' % target) for item in list: sys.stdout.write(' :0 c\n ! %s\n' % item) sys.stdout.write('}\n') if __name__ == '__main__': apply(main, tuple(sys.argv[1:]))