pingou / centos-git-common

Forked from centos-git-common 2 years ago
Clone

Blame centos.git.repolist.py

bonniek 7a56e5
#!/usr/bin/env python
a6a2bc
#
a6a2bc
#  License: GPLv3
a6a2bc
#
a6a2bc
#  Initial Author: Bonnie King <bonniek@fnal.gov>
a6a2bc
#         Updates:
a6a2bc
#                  Pat Riehecky <riehecky@fnal.gov>
a6a2bc
#
bonniek 7a56e5
'''Get list of repos from gitblit RPC, to grab CentOS sources'''
bonniek 7a56e5
bonniek 7a56e5
import optparse
bonniek 7a56e5
import requests
bonniek 7a56e5
import simplejson as json
bonniek 7a56e5
import sys
bonniek 7a56e5
4ec6d6
RPCURL = "https://git.centos.org/rpc/?req=LIST_REPOSITORIES"
bonniek 7a56e5
bonniek 7a56e5
def read_args():
bonniek 7a56e5
    '''
bonniek 7a56e5
        read in the command line args and set things up
bonniek 7a56e5
    '''
bonniek 7a56e5
bonniek 7a56e5
    desc = '''Get list of git repositories from the GitBlit json RPC
bonniek 7a56e5
           '''
bonniek 7a56e5
bonniek 7a56e5
    usage = "usage: %prog [options] "
bonniek 7a56e5
    parser = optparse.OptionParser(usage=usage, description=desc)
bonniek 7a56e5
    parser.add_option('-p', '--project', metavar="<PROJECTS>",
bonniek dbddad
                      help='''project path (default 'rpms', could be 'all', 'core-sig'...)''',
bonniek 7a56e5
                      default='rpms')
bonniek 7a56e5
a6a2bc
    parser.add_option('-b', '--branch', metavar="<branch name>",
a6a2bc
                      help='Only list repos with this branch (default master)',
a6a2bc
                      default = 'master')
a6a2bc
bonniek 7a56e5
    parser.add_option('-u', '--url', metavar="<URL>",
bonniek 7a56e5
                      help='URL to check (default %s)' % (RPCURL),
a6a2bc
                      default = RPCURL)
bonniek 7a56e5
bonniek 7a56e5
    (options, args) = parser.parse_args()
bonniek 7a56e5
    return options
bonniek 7a56e5
a6a2bc
def get_repo_list(url, branch, projectpath):
bonniek 7a56e5
    '''return a list of repo URLs'''
bonniek 7a56e5
    try:
bonniek 7a56e5
        req = requests.get(url)
a6a2bc
    except requests.exceptions.RequestException as err_msg:
a6a2bc
        print err_msg
bonniek 7a56e5
        sys.exit(1)
bonniek 7a56e5
4ec6d6
    if req.status_code != 200:
4ec6d6
        print "Unable to access gitblit api at " + url
4ec6d6
        sys.exit(1)
4ec6d6
4ec6d6
bonniek 7a56e5
    payload = req.text
bonniek 7a56e5
    repos = json.loads(payload)
a6a2bc
    branchname = 'refs/heads/' + branch
bonniek 7a56e5
a6a2bc
    for repo in repos.keys():
a6a2bc
        if projectpath != 'all':
bonniek 7a56e5
            if repos[repo]['projectPath'] != projectpath:
bonniek 7a56e5
                del repos[repo]
a6a2bc
                continue
a6a2bc
        if branchname not in repos[repo]['availableRefs']:
a6a2bc
            del repos[repo]
bonniek 7a56e5
bonniek 7a56e5
    return repos.keys()
bonniek 7a56e5
bonniek 7a56e5
def main():
a6a2bc
    '''Broken out so it can be inherited if someone wants'''
bonniek 7a56e5
    options = read_args()
a6a2bc
    repos = get_repo_list(url=options.url, branch=options.branch, projectpath=options.project)
a6a2bc
    if repos:
a6a2bc
        print '\n'.join(repos)
4ec6d6
bonniek 7a56e5
if __name__ == "__main__":
bonniek 7a56e5
    main()