#!/usr/bin/python

import urllib2
import libxml2
import sys
import re

#download vosummary (for oasis enabled)
response = urllib2.urlopen("https://myosg.grid.iu.edu/vosummary/xml?summary_attrs_showoasis=on&start_type=7daysago&start_date=11%2F27%2F2012&end_type=now&end_date=11%2F27%2F2012&all_vos=on&active_value=1&oasis=on&oasis_value=1&sort_key=name")
xml = response.read()
doc = libxml2.parseDoc(xml)

dns = {}
#parse out the VO names
for vo in doc.xpathEval("//VO"):

        #create good unixname
        voname = vo.xpathEval("Name")[0].content
        voname = re.sub(r'[^/a-z0-9]+', '', voname.lower())

        username = "ouser."+voname
        groupname = "cvmfs"

        for dn in vo.xpathEval("OASIS/Managers/Manager/DNs/DN"):
            dn = dn.content

            ##sanitize dn
            original_dn = dn
            dn = re.sub(r'[^/ ()=\.\-a-zA-Z0-9\':@]+', '', dn)
            
            if original_dn != dn:
                print >> sys.stderr, "invalid character in dn:",original_dn
                continue

            #escape
            dn = re.sub(r'["]+', '\\\"', dn)
            
            #print voname,username,dn
            if dn in dns:
                usernames = dns[dn]
                usernames.append(username)
            else:
                dns[dn] = [username]
    

for dn in dns:
    usernames = dns[dn]
    print "\""+dn+"\"",",".join(usernames)

if len(dns) < 10:
    print >> sys.stderr, "dn count is too small:",len(dns)
    sys.exit(2)

sys.exit(0)
