Blame membership-mwclient-0.3-prod.py

43f4cdc
#!/usr/bin/python
43f4cdc
''' This script is for generating output for
43f4cdc
https://fedoraproject.org/wiki/Ambassadors/MembershipService/Verification.'''
43f4cdc
#
43f4cdc
# Copyright (C) 2010, Susmit Shannigrahi, Susmit AT fedoraproject DOT org
43f4cdc
#
43f4cdc
# This program is free software: you can redistribute it and/or modify
43f4cdc
# it under the terms of the GNU General Public License as published by
43f4cdc
# the Free Software Foundation, either version 3 of the License, or
43f4cdc
# (at your option) any later version.
43f4cdc
#
43f4cdc
# This program is distributed in the hope that it will be useful,
43f4cdc
# but WITHOUT ANY WARRANTY; without even the implied warranty of
43f4cdc
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
43f4cdc
# GNU General Public License for more details.
43f4cdc
#
43f4cdc
# You should have received a copy of the GNU General Public License
43f4cdc
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
43f4cdc
43f4cdc
import getpass
43f4cdc
from fedora.client.fas2 import AccountSystem
43f4cdc
import datetime
43f4cdc
import GeoIP
43f4cdc
from django.utils.encoding import smart_str
43f4cdc
import mwclient
43f4cdc
43f4cdc
#from maxmind array
43f4cdc
#changes are: 'RS': 'EU', 'ME': 'EU', 'AU': 'AS', 'NZ': 'AS'
43f4cdc
#https://fedorahosted.org/fedora-infrastructure/ticket/2921
43f4cdc
#changes are: 'IL': 'EU', 'KG': 'EU', 'LB': 'EU', 'SA': 'EU', 'AE': 'EU'
43f4cdc
43f4cdc
43f4cdc
CONTINENT_MAP = {'AP': 'AS', 'EU': 'EU', 'AD': 'EU', 'AE': 'EU', 'AF': 'AS', \
59c4c74
 'AG': 'SA', 'AI': 'SA', 'AL': 'EU', 'AM': 'AS', 'AN': 'SA', 'AO': 'EU', \
43f4cdc
'AQ': 'AN', 'AR': 'SA', 'AS': 'OC', 'AT': 'EU', 'AU': 'OC', 'AW': 'SA', \
43f4cdc
'AX': 'EU', 'AZ': 'AS', 'BA': 'EU', 'BB': 'SA', 'BD': 'AS', 'BE': 'EU', \
59c4c74
'BF': 'EU', 'BG': 'EU', 'BH': 'AS', 'BI': 'EU', 'BJ': 'EU', 'BM': 'SA', \
59c4c74
'BN': 'AS', 'BO': 'SA', 'BR': 'SA', 'BS': 'SA', 'BT': 'AS', 'BV': 'EU', \
59c4c74
'BW': 'EU', 'BY': 'EU', 'BZ': 'SA', 'CA': 'NA', 'CC': 'AS', 'CD': 'EU', \
59c4c74
'CF': 'EU', 'CG': 'EU', 'CH': 'EU', 'CI': 'EU', 'CK': 'OC', 'CL': 'SA', \
59c4c74
'CM': 'EU', 'CN': 'AS', 'CO': 'SA', 'CR': 'SA', 'CU': 'SA', 'CV': 'EU', \
59c4c74
'CX': 'AS', 'CY': 'AS', 'CZ': 'EU', 'DE': 'EU', 'DJ': 'EU', 'DK': 'EU', \
59c4c74
'DM': 'SA', 'DO': 'SA', 'DZ': 'EU', 'EC': 'SA', 'EE': 'EU', 'EG': 'EU', \
59c4c74
'EH': 'EU', 'ER': 'EU', 'ES': 'EU', 'ET': 'EU', 'FI': 'EU', 'FJ': 'OC', \
59c4c74
'FK': 'SA', 'FM': 'OC', 'FO': 'EU', 'FR': 'EU', 'FX': 'EU', 'GA': 'EU', \
59c4c74
'GB': 'EU', 'GD': 'SA', 'GE': 'AS', 'GF': 'SA', 'GG': 'EU', 'GH': 'EU', \
59c4c74
'GI': 'EU', 'GL': 'SA', 'GM': 'EU', 'GN': 'EU', 'GP': 'SA', 'GQ': 'EU', \
59c4c74
'GR': 'EU', 'GS': 'SA', 'GT': 'SA', 'GU': 'OC', 'GW': 'EU', 'GY': 'SA', \
59c4c74
'HK': 'AS', 'HM': 'EU', 'HN': 'SA', 'HR': 'EU', 'HT': 'SA', 'HU': 'EU', \
43f4cdc
'ID': 'AS', 'IE': 'EU', 'IL': 'EU', 'IM': 'EU', 'IN': 'AS', 'IO': 'AS', \
43f4cdc
'IQ': 'AS', 'IR': 'AS', 'IS': 'EU', 'IT': 'EU', 'JE': 'EU', 'JM': 'SA', \
59c4c74
'JO': 'AS', 'JP': 'AS', 'KE': 'EU', 'KG': 'EU', 'KH': 'AS', 'KI': 'OC', \
59c4c74
'KM': 'EU', 'KN': 'SA', 'KP': 'AS', 'KR': 'AS', 'KW': 'AS', 'KY': 'SA', \
43f4cdc
'KZ': 'AS', 'LA': 'AS', 'LB': 'EU', 'LC': 'SA', 'LI': 'EU', 'LK': 'AS', \
59c4c74
'LR': 'EU', 'LS': 'EU', 'LT': 'EU', 'LU': 'EU', 'LV': 'EU', 'LY': 'EU', \
59c4c74
'MA': 'EU', 'MC': 'EU', 'MD': 'EU', 'MG': 'EU', 'MH': 'OC', 'MK': 'EU', \
59c4c74
'ML': 'EU', 'MM': 'AS', 'MN': 'AS', 'MO': 'AS', 'MP': 'OC', 'MQ': 'SA', \
59c4c74
'MR': 'EU', 'MS': 'SA', 'MT': 'EU', 'MU': 'EU', 'MV': 'AS', 'MW': 'EU', \
59c4c74
'MX': 'NA', 'MY': 'AS', 'MZ': 'EU', 'NA': 'EU', 'NC': 'OC', 'NE': 'EU', \
59c4c74
'NF': 'OC', 'NG': 'EU', 'NI': 'SA', 'NL': 'EU', 'NO': 'EU', 'NP': 'AS', \
43f4cdc
'NR': 'OC', 'NU': 'OC', 'NZ': 'AS', 'OM': 'AS', 'PA': 'SA', 'PE': 'SA', \
43f4cdc
'PF': 'OC', 'PG': 'OC', 'PH': 'AS', 'PK': 'AS', 'PL': 'EU', 'PM': 'SA', \
43f4cdc
'PN': 'OC', 'PR': 'SA', 'PS': 'AS', 'PT': 'EU', 'PW': 'OC', 'PY': 'SA', \
59c4c74
'QA': 'AS', 'RE': 'EU', 'RO': 'EU', 'RU': 'EU', 'RW': 'EU', 'SA': 'AS', \
59c4c74
'SB': 'OC', 'SC': 'EU', 'SD': 'EU', 'SE': 'EU', 'SG': 'AS', 'SH': 'EU', \
59c4c74
'SI': 'EU', 'SJ': 'EU', 'SK': 'EU', 'SL': 'EU', 'SM': 'EU', 'SN': 'EU', \
59c4c74
'SO': 'EU', 'SR': 'SA', 'ST': 'EU', 'SV': 'SA', 'SY': 'AS', 'SZ': 'EU', \
59c4c74
'TC': 'SA', 'TD': 'EU', 'TF': 'EU', 'TG': 'EU', 'TH': 'AS', 'TJ': 'AS', \
59c4c74
'TK': 'OC', 'TM': 'AS', 'TN': 'EU', 'TO': 'OC', 'TP': 'AS', 'TR': 'EU', \
59c4c74
'TT': 'SA', 'TV': 'OC', 'TW': 'AS', 'TZ': 'EU', 'UA': 'EU', 'UG': 'EU', \
43f4cdc
'UM': 'OC', 'US': 'NA', 'UY': 'SA', 'UZ': 'AS', 'VA': 'EU', 'VC': 'SA', \
43f4cdc
'VE': 'SA', 'VG': 'SA', 'VI': 'SA', 'VN': 'AS', 'VU': 'OC', 'WF': 'OC', \
59c4c74
'WS': 'OC', 'YE': 'AS', 'YT': 'EU', 'YU': 'EU', 'ZA': 'EU', 'ZM': 'EU', \
59c4c74
'ZR': 'EU', 'ZW': 'EU', 'RS': 'EU', 'ME': 'EU', 'AU': 'AS'}
43f4cdc
43f4cdc
def calc_list():
43f4cdc
    '''Calculate the contributors list categorised by country'''
43f4cdc
    output = []
43f4cdc
    people_list = []
43f4cdc
    country_list = []
43f4cdc
    flag = 0
59c4c74
    final_output_list_as = []
43f4cdc
    final_output_list_eu = []
43f4cdc
    final_output_list_na = []
43f4cdc
    final_output_list_latam = []
43f4cdc
    final_output_list_unknown = []
43f4cdc
    full_name = {'AS' : 'APAC', 'NA' : 'North America', \
59c4c74
'SA' : 'LATAM', 'EU' : 'EMEA', 'Unknown' : 'Unknown'}
43f4cdc
43f4cdc
43f4cdc
    group_name = 'ambassadors'
43f4cdc
#    username = 'fedoradummy'
59c4c74
#    password = '<%= fedoraDummyUserPassword %>'
43f4cdc
    username = raw_input('Username: ').strip()
59c4c74
    password = getpass.getpass('Password: ')
43f4cdc
    fas = AccountSystem(username=username, password=password)
43f4cdc
43f4cdc
    # Call fedoraproject API with mwclient
43f4cdc
    site = mwclient.Site(('https', 'fedoraproject.org'), path='/w/')
43f4cdc
    site.login(username, password) # Optional
43f4cdc
    # Specify which page to edit
43f4cdc
    page = site.Pages['Ambassadors/MembershipService/Verification2']
43f4cdc
43f4cdc
    #delete all the erswhile contents.
43f4cdc
    page.edit()
43f4cdc
    blank_page = "updating..."
43f4cdc
    page.save(blank_page)
59c4c74
43f4cdc
    print 'This takes loooooong time to execute...'
59c4c74
43f4cdc
    #get all data from a group
43f4cdc
    group_people = fas.group_members(group_name)
43f4cdc
    #make a list of usernames of a group
43f4cdc
    for item in group_people:
43f4cdc
        people_list.append(item['username'])
43f4cdc
43f4cdc
    # get the country list from GeoIP
43f4cdc
    countries = dict(GeoIP.country_names.items())
43f4cdc
43f4cdc
    # Get a dictinary of all people in FAS
43f4cdc
    data = fas.people_by_key(key='id', search=u'*', \
43f4cdc
fields=['human_name', 'username', 'email', 'status', 'country_code'])
43f4cdc
43f4cdc
    for person in people_list:
43f4cdc
#        break
43f4cdc
        for item in data.values():
43f4cdc
            user_name = item['username']
43f4cdc
            human_name = item['human_name']
43f4cdc
            country_code = item['country_code']
43f4cdc
            status = item['status']
43f4cdc
            email = item['username'] + '@fedoraproject.org'
59c4c74
59c4c74
43f4cdc
43f4cdc
            if person == user_name:
43f4cdc
            #print 'match'
43f4cdc
                if status == 'active': #filter out all inactive accounts
43f4cdc
                    if country_code is None or country_code == 'O1' \
43f4cdc
or country_code == '  ':
59c4c74
                        continent_code = 'Unknown'
43f4cdc
                    else:
43f4cdc
                        continent_code = CONTINENT_MAP[country_code]
43f4cdc
43f4cdc
                   #different values for blank or non-blank fields are
43f4cdc
                   #_____________________________________________________#
43f4cdc
                   # Country Code  || Human Name || Number of tickets||  #
43f4cdc
                   #_____________________________________________________#
43f4cdc
                   # None          || None/Name  ||  0/n                 #
43f4cdc
                   # '   '         || None/Name  ||                      #
43f4cdc
                   # 'IN','FR' etc.|| None/Name  ||                      #
43f4cdc
                   #_____________________________________________________#
43f4cdc
43f4cdc
                    if country_code is None:
43f4cdc
                        if human_name is None:
43f4cdc
                            flag = 1
43f4cdc
                        else:
43f4cdc
                            flag = 2
43f4cdc
                    elif country_code == '  ':
43f4cdc
                        if human_name is None:
43f4cdc
                            flag = 1
43f4cdc
                        else:
43f4cdc
                            flag = 2
43f4cdc
43f4cdc
                    else: #if there is a country code available
43f4cdc
                        country = countries[country_code]
43f4cdc
                        if human_name is None:
43f4cdc
                            flag = 3
43f4cdc
                        else:
43f4cdc
                            flag = 4
43f4cdc
                    #check flag to decide o/p
43f4cdc
                    if flag == 1:
43f4cdc
                        entry = [user_name, user_name, 'Unknown', email, \
43f4cdc
'Unknown']
43f4cdc
                        output.append(entry)
43f4cdc
                    elif flag == 2:
43f4cdc
                        entry = [user_name, human_name, 'Unknown', email, \
43f4cdc
'Unknown']
43f4cdc
                        output.append(entry)
43f4cdc
                    elif flag == 3:
43f4cdc
                        entry = [user_name, user_name, country, email, \
43f4cdc
continent_code]
43f4cdc
                        output.append(entry)
43f4cdc
                    else:
43f4cdc
                        entry = [user_name, human_name, country, email, \
43f4cdc
continent_code]
43f4cdc
                        output.append(entry)
43f4cdc
59c4c74
# Now we have a output list like
43f4cdc
#[['rdsharma4u', 'Ravi Datta Sharma','India','rdsharma4u@gmail.com','1','AS'],
43f4cdc
#['red', 'Sandro Mathys', 'Switzerland', 'sm@sandro-mathys.ch', '10', 'EU']]
43f4cdc
59c4c74
59c4c74
43f4cdc
    for item in output:
43f4cdc
        #break
43f4cdc
        continent_code = item[4]
43f4cdc
        if continent_code == 'AS' or continent_code == 'AU':
43f4cdc
            final_output_list_as.append(item)
43f4cdc
        elif continent_code == 'NA':
43f4cdc
            final_output_list_na.append(item)
43f4cdc
        elif continent_code == 'SA':
43f4cdc
            final_output_list_latam.append(item)
43f4cdc
        elif continent_code == 'EU':
43f4cdc
            final_output_list_eu.append(item)
43f4cdc
        elif continent_code == 'Unknown':
43f4cdc
            final_output_list_unknown.append(item)
43f4cdc
   # sort the list according to countries
43f4cdc
    final_string = ''
59c4c74
    for final_output_list in [final_output_list_as, \
43f4cdc
final_output_list_na, final_output_list_latam, final_output_list_eu, \
43f4cdc
final_output_list_unknown]:
43f4cdc
        country_list = []
43f4cdc
        #print final_output_list
59c4c74
43f4cdc
        # print the full continent name from entry zero of list
43f4cdc
        try:
43f4cdc
            continent_code = final_output_list[0][4]
59c4c74
43f4cdc
            full_cont_name = full_name[continent_code]
59c4c74
43f4cdc
            full_cont_name = "

%s (%s)

" \
43f4cdc
% (full_cont_name, len(final_output_list))
59c4c74
43f4cdc
            final_string = final_string + full_cont_name
43f4cdc
            #print final_string
43f4cdc
            for item in final_output_list:
59c4c74
43f4cdc
                #print item
59c4c74
43f4cdc
                country = item[2]
43f4cdc
                if country_list.count(country) == 0:
43f4cdc
                    country_list.append(country)
43f4cdc
                country_list.sort()
59c4c74
43f4cdc
        except IndexError:
43f4cdc
            pass
43f4cdc
        #print country_list
43f4cdc
        final_output_country_list = \
43f4cdc
[[]*len(country_list) for i in range(len(country_list))]
43f4cdc
        for item in final_output_list:
43f4cdc
            # find the index of this entry (country) from county_list
43f4cdc
            index = country_list.index(item[2])
43f4cdc
            final_output_country_list[index].append(item)
43f4cdc
        #print final_output_country_list
43f4cdc
43f4cdc
43f4cdc
43f4cdc
43f4cdc
        #final_string = ''
59c4c74
43f4cdc
        for item in final_output_country_list:
43f4cdc
            #reverse mapping to print the country
43f4cdc
            pos = final_output_country_list.index(item)
43f4cdc
            num = len(item) #number of ambassador in a country.
59c4c74
43f4cdc
            cnt_lst = "

%s (%s)

" % (country_list[pos], num)
43f4cdc
            # add country and table formatting to string.
43f4cdc
            final_string = final_string + cnt_lst + ''
IndexNameEmail
59c4c74
43f4cdc
            index = 0
43f4cdc
            for entries in item:
43f4cdc
                #print entries
43f4cdc
                #break
43f4cdc
                index = index + 1
43f4cdc
                user_name = entries[0]
43f4cdc
                human_name = entries[1]
43f4cdc
                email = user_name + ' AT fedoraproject DOT org'
59c4c74
43f4cdc
                #add detals.
43f4cdc
                final = smart_str("" + str(index) +" [[User:" + user_name + "| " + human_name + "]]" + "" + email + "")
43f4cdc
43f4cdc
                final_string = final_string + final
43f4cdc
            final_string = final_string + ''
43f4cdc
43f4cdc
    time = datetime.datetime.utcnow()
43f4cdc
    page.edit()
43f4cdc
    page.save(final_string, summary = time)
43f4cdc
59c4c74
43f4cdc
    note = "{{admon/note | Last Updated : %s UTC. %s active contributors \
43f4cdc
listed here against %s total.}}" % (time, len(output), len(people_list))
43f4cdc
    page = site.Pages['Ambassadors/MembershipService/VerificationStats']
43f4cdc
    page.edit()
43f4cdc
    page.save(note, summary = time)
43f4cdc
43f4cdc
if __name__ == "__main__":
43f4cdc
    calc_list()