# !/usr/bin/env python # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # """ Mailing list moderator tool used to manage mailing lists from the command line. """ import restkit import sys from asf.cli import entrypoint from asf.data import aliases, lists from asf.utils import committers from asf.utils.emails import email_from_alias, is_apache_email_address, username_from_apache_email EXTRACTORS = { 'fullname': lambda c: c.fullname, 'username': lambda c: c.username, 'member': lambda c: 'TRUE' if c.member else 'FALSE', 'emails': lambda c: ' '.join(c.emails), 'projects': lambda c: ' '.join(c.projects), 'podlings': lambda c: ' '.join(c.mentoring), 'pmcs': lambda c: ' '.join(c.committees), } def cmd_lookup(args): mail_aliases = aliases.get_mail_aliases(args.username, args.password) email_alias = args.email_alias asf_email = email_alias if is_apache_email_address(email_alias) else email_from_alias(email_alias, mail_aliases) if asf_email: committer = committers.get_committer(username_from_apache_email(asf_email), args.username, args.password) if committer: restrict = args.type if not restrict: print EXTRACTORS['fullname'](committer), '-', EXTRACTORS['username'](committer) if committer.member: print ' *ASF member' if committer.emails: print ' E-mails:', EXTRACTORS['emails'](committer) if committer.projects: print ' Projects:', EXTRACTORS['projects'](committer) if committer.mentoring: print ' Mentoring:', EXTRACTORS['podlings'](committer) if committer.committees: print ' PMCs:', EXTRACTORS['pmcs'](committer) else: print EXTRACTORS[restrict.lower()](committer) return raise KeyError('%s is not a registered email alias' % email_alias) def cmd_add(args): try: if args.moderator: lists.add_ezmlm_moderator(args.mailing_list, args.email_address, args.username, args.password) else: lists.add_ezmlm_subscriber(args.mailing_list, args.email_address, args.username, args.password) print 'Added %s to %s' % ('moderator %s' % args.email_address if args.moderator else args.email_address, args.mailing_list) except restkit.ResourceNotFound: print 'Mailing list %s not found' % args.mailing_list def cmd_remove(args): try: if args.moderator: lists.remove_ezmlm_moderator(args.mailing_list, args.email_address, args.username, args.password) else: lists.remove_ezmlm_subscriber(args.mailing_list, args.email_address, args.username, args.password) print 'Removed %s to %s' % ('moderator %s' % args.email_address if args.moderator else args.email_address, args.mailing_list) except restkit.ResourceNotFound: print 'Mailing list %s not found' % args.mailing_list def cmd_list(args): for address, groups in lists.get_ezmlm_lists().iteritems(): for group in groups: print group + '@' + address def cmd_members(args): try: if args.moderator: members = lists.get_ezmlm_moderators(args.mailing_list, args.username, args.password) else: members = lists.get_ezmlm_subscribers(args.mailing_list, args.username, args.password) for member in members: print member except restkit.ResourceNotFound: raise KeyError('Mailing list %s not found' % args.mailing_list) @entrypoint def main(cli): cli.add_username_password(use_store=True) def print_error(error): print >>sys.stderr, '\033[91m' + str(error).replace("'", "") + '\033[0m' cli.register_exception(KeyError, print_error) cli.register_exception(restkit.RequestError, lambda e: print_error('Unable to reach %s' % lists.EZMLM_URL)) parser = cli.argparser subparsers = parser.add_subparsers() lookup_parser = subparsers.add_parser('lookup', description='Lookup the email alias') lookup_parser.add_argument('email_alias', help='The email alias to lookup') lookup_parser.add_argument('-t', '--type', help='Restrict output to information of a specific type', choices=EXTRACTORS.keys()) lookup_parser.set_defaults(func=cmd_lookup) add_parser = subparsers.add_parser('add', description='Add an email address to a mailing list') add_parser.add_argument('mailing_list', help='The mailing list that the email address is attempting to join') add_parser.add_argument('email_address', help='The email address to add') add_parser.add_argument('-m', '--moderator', default=False, action='store_true', help='Email address is for moderator') add_parser.set_defaults(func=cmd_add) remove_parser = subparsers.add_parser('remove', description='Remove an email address to a mailing list') remove_parser.add_argument('mailing_list', help='The mailing list that the email address is being removed from') remove_parser.add_argument('email_address', help='The email address to remove') remove_parser.add_argument('-m', '--moderator', default=False, action='store_true', help='Email address is for moderator') remove_parser.set_defaults(func=cmd_remove) list_parser = subparsers.add_parser('list', description='List the ASF mailing lists') list_parser.set_defaults(func=cmd_list) list_parser = subparsers.add_parser('members', description='List the members of a mailing list') list_parser.add_argument('mailing_list', help='The mailing list whose membership is to be listed') list_parser.add_argument('-m', '--moderator', default=False, action='store_true', help='List moderators of mailing list') list_parser.set_defaults(func=cmd_members) with cli.run(): cli.args.func(cli.args)