#!/usr/bin/env python2
'''This script pushes selected AFS git repositories to github.com/slac-epics.
'''

import argparse
import os
#import os.path
#import shutil
import pycurl
import StringIO
import sys
#import tempfile

githubApiURL = "https://api.github.com"

def perform_rs( curlIO ):
    buffer = StringIO.StringIO()
    curlIO.setopt( curlIO.WRITEDATA, buffer )
    curlIO.perform()
    curlIO.close()
    return buffer.getvalue()

def getOrgRepoList( curlIO, orgName ):
    '''Get the list of repos for the specified github URL.
    URL must be for a github user or organization
    '''
    curlIO.setopt( curlIO.URL, githubApiURL + "/org/%s/repos?per_page=1000" % orgName )
    #return curlIO.perform_rs()
    return perform_rs( curlIO )

def getUserRepoList( curlIO ):
    '''Get the list of repos for the specified github URL.
    URL must be for a github user or organization
    '''
    curlIO.setopt( curlIO.URL, githubApiURL + "/users/repos?per_page=1000" )
    #return curlIO.perform_rs()
    return perform_rs( curlIO )

if __name__ == '__main__':
    parser = argparse.ArgumentParser( description='''This script ...
                                                     Example: ...''')
    defUser = os.environ['USER']
    parser.add_argument( '-u', '--username',     default=defUser,  help="github username, default %s" % defUser )
    parser.add_argument( '-v', '--verbose',      action='store_true',  help='Print more status msgs' )

    args = parser.parse_args( )

    # Validate ...
    
    curlIO = pycurl.Curl()
    if args.username:
        curlIO.setopt( pycurl.USERPWD, args.username )

    print getUserRepoList( curlIO )
    print "Done."

