ngompa / centos-git-common

Forked from centos-git-common 3 years ago
Clone
Blob Blame History Raw
#!/usr/bin/env python
#
#  License: GPLv3
#
#  Initial Author: Bonnie King <bonniek@fnal.gov>
#         Updates:
#                  Pat Riehecky <riehecky@fnal.gov>
#
'''Get list of repos from gitblit RPC, to grab CentOS sources'''

import optparse
import requests
import simplejson as json
import sys

RPCURL = "https://git.centos.org/rpc/?req=LIST_REPOSITORIES"

def read_args():
    '''
        read in the command line args and set things up
    '''

    desc = '''Get list of git repositories from the GitBlit json RPC
           '''

    usage = "usage: %prog [options] "
    parser = optparse.OptionParser(usage=usage, description=desc)
    parser.add_option('-p', '--project', metavar="<PROJECTS>",
                      help='''project path (default 'rpms', could be 'all', 'core-sig'...)''',
                      default='rpms')

    parser.add_option('-b', '--branch', metavar="<branch name>",
                      help='Only list repos with this branch (default master)',
                      default = 'master')

    parser.add_option('-u', '--url', metavar="<URL>",
                      help='URL to check (default %s)' % (RPCURL),
                      default = RPCURL)

    (options, args) = parser.parse_args()
    return options

def get_repo_list(url, branch, projectpath):
    '''return a list of repo URLs'''
    try:
        req = requests.get(url)
    except requests.exceptions.RequestException as err_msg:
        print err_msg
        sys.exit(1)

    if req.status_code != 200:
        print "Unable to access gitblit api at " + url
        sys.exit(1)


    payload = req.text
    repos = json.loads(payload)
    branchname = 'refs/heads/' + branch

    for repo in repos.keys():
        if projectpath != 'all':
            if repos[repo]['projectPath'] != projectpath:
                del repos[repo]
                continue
        if branchname not in repos[repo]['availableRefs']:
            del repos[repo]

    return repos.keys()

def main():
    '''Broken out so it can be inherited if someone wants'''
    options = read_args()
    repos = get_repo_list(url=options.url, branch=options.branch, projectpath=options.project)
    if repos:
        print '\n'.join(repos)

if __name__ == "__main__":
    main()