コード例 #1
0
ファイル: main.py プロジェクト: kmwenja/github_commit_times
def main(user='', repo='', logfile='', frmt='json'):
    """Entry point
    :param user: username
    :param repo: reponame (if username is provided), fullname (if username isn't provided) 
    :param logfile: logfile 
    :param frmt: csv, json (not yet implemented, only does csv)
    """
    repo_url = ''
    single_repo = False  #flag : if fetching one repo (True) or many (False)
    public_repos = False  #multiple repos are public repos
    count = 0  #max repos to fetch if public_repos is selected
    if user != '' and repo != '':
        fullname = "{0}/{1}".format(user, repo)
        repo_url = REPO_URL.format(full_name=fullname)
        single_repo = True
    elif user != '':
        repo_url = USER_REPO_LIST.format(user=user)
        single_repo = False
    elif repo != '':  #fullname of repo is provided
        repo_url = REPO_URL.format(full_name=repo)
        single_repo = True
    else:  #fetch public repos
        ans = raw_input('fetch all repos [y/n]? ')
        if ans in ('y', 'Y'):
            count = raw_input('maximum number of repos [{0}]? '.format(DEFAULT_MAX_PUBLIC_REPOS))
            if count == '':
                count = DEFAULT_MAX_PUBLIC_REPOS
            else:
                try:
                    count = int(count)
                except:
                    print 'Invalid integer'

            repo_url = ALL_REPO_LIST
            single_repo = False
            public_repos = True
        else:
            game_over('no repo/user selected')

    fetcher = Fetcher()
    repo_url = fetcher.get_full_url(repo_url)
    repo_dets = None
    if single_repo:
        repo_dets = fetcher.process_repo(repo_url)
    elif public_repos:
        repo_dets = fetcher.get_public_repos(count)
    else:
        repo_dets = fetcher.process_repo(repo_url, multiple=True)

    if logfile == '': #TODO use reponame if logfile is not present
        logfile = stdout
    else:
        logfile = open(logfile, 'w')

    if type(logfile) is not file:
        game_over('nowhere to log')

    print 'gotten', len(repo_dets), 'repos'

    for i in repo_dets:
        commits = fetcher.extract_commits(i)
        fetcher.write_commits(logfile, i, commits) #TODO write file in JSON

    if logfile != stdout:
        logfile.close()