Blame files/load_from_disk.py

Pierre-Yves Chibon e8bd26
#!/usr/bin/env python
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
import argparse
Pierre-Yves Chibon e8bd26
import requests
Pierre-Yves Chibon e8bd26
import os
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
if 'PAGURE_CONFIG' not in os.environ \
Pierre-Yves Chibon e8bd26
        and os.path.exists('/etc/pagure/pagure.cfg'):
Pierre-Yves Chibon e8bd26
    print 'Using configuration file `/etc/pagure/pagure.cfg`'
Pierre-Yves Chibon e8bd26
    os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
import pagure
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
def get_poc_of_pkgs(debug=False):
Pierre-Yves Chibon e8bd26
    """ Retrieve a dictionary giving the point of contact of each package
Pierre-Yves Chibon e8bd26
    in pkgdb.
Pierre-Yves Chibon e8bd26
    """
Pierre-Yves Chibon e8bd26
    if debug:
Pierre-Yves Chibon e8bd26
        print 'Querying pkgdb'
Pierre-Yves Chibon e8bd26
    PKGDB_URL = 'https://admin.fedoraproject.org/pkgdb/api/'
Pierre-Yves Chibon e8bd26
    req = requests.get(PKGDB_URL + 'bugzilla').text
Pierre-Yves Chibon e8bd26
    if debug:
Pierre-Yves Chibon e8bd26
        print 'Pkgdb data retrieved, getting POC'
Pierre-Yves Chibon e8bd26
    pkgs = {}
Pierre-Yves Chibon e8bd26
    for line in req.split('\n'):
Pierre-Yves Chibon e8bd26
        line = line.strip()
Pierre-Yves Chibon e8bd26
        if not line or line.startswith('#'):
Pierre-Yves Chibon e8bd26
            continue
Pierre-Yves Chibon e8bd26
        line = line.split('|')
Pierre-Yves Chibon e8bd26
        if len(line) < 4:
Pierre-Yves Chibon e8bd26
            continue
Pierre-Yves Chibon e8bd26
        pkgs[line[1]] = line[3]
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
    return pkgs
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
def main(folder, debug=False):
Pierre-Yves Chibon e8bd26
    """
Pierre-Yves Chibon e8bd26
    Logic:
Pierre-Yves Chibon e8bd26
    - Query the list of maintainer/PoC from pkgdb
Pierre-Yves Chibon e8bd26
    - Browse the directory
Pierre-Yves Chibon e8bd26
    - For each git in the directory, create the project with the correct POC
Pierre-Yves Chibon e8bd26
    """
Pierre-Yves Chibon e8bd26
    pocs = get_poc_of_pkgs(debug=debug)
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
    if debug:
Pierre-Yves Chibon e8bd26
        print 'Adding the user to the DB'
Pierre-Yves Chibon e8bd26
    for user in sorted(set(pocs.values())):
Pierre-Yves Chibon e8bd26
        if debug:
Pierre-Yves Chibon e8bd26
            print user
Pierre-Yves Chibon e8bd26
        try:
Pierre-Yves Chibon e8bd26
            pagure.lib.set_up_user(
Pierre-Yves Chibon e8bd26
                session=pagure.SESSION,
Pierre-Yves Chibon e8bd26
                username=user,
Pierre-Yves Chibon e8bd26
                fullname=user,
Pierre-Yves Chibon e8bd26
                default_email='%s@fedoraproject.org' % user,
Patrick Uiterwijk 0e5f4a
                keydir=pagure.APP.config.get('GITOLITE_KEYDIR', None),
Pierre-Yves Chibon e8bd26
            )
Pierre-Yves Chibon e8bd26
            pagure.SESSION.commit()
Pierre-Yves Chibon e8bd26
        except SQLAlchemyError, err:
Pierre-Yves Chibon e8bd26
            pagure.SESSION.rollback()
Pierre-Yves Chibon e8bd26
            print 'ERROR with user %s' % user
Pierre-Yves Chibon e8bd26
            print err
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
    for project in sorted(os.listdir(folder)):
Pierre-Yves Chibon e8bd26
        if debug:
Pierre-Yves Chibon e8bd26
            print project
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
        if not project.endswith('.git'):
Pierre-Yves Chibon e8bd26
            if debug:
Pierre-Yves Chibon f6632a
                print '  -skip: not a git repository'
Pierre-Yves Chibon e8bd26
            continue
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
        if project.split('.git')[0] not in pocs:
Pierre-Yves Chibon e8bd26
            if debug:
Pierre-Yves Chibon e8bd26
                print '  -skip: no pocs'
Pierre-Yves Chibon e8bd26
            continue
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
        try:
Pierre-Yves Chibon 8248d1
            name = project.split('.git')[0]
Pierre-Yves Chibon e8bd26
            pagure.lib.new_project(
Pierre-Yves Chibon e8bd26
                session=pagure.SESSION,
Pierre-Yves Chibon 8248d1
                user=pocs[name],
Pierre-Yves Chibon 8248d1
                name=name,
Pierre-Yves Chibon e8bd26
                blacklist=pagure.APP.config['BLACKLISTED_PROJECTS'],
Pierre-Yves Chibon e8bd26
                gitfolder=pagure.APP.config['GIT_FOLDER'],
Pierre-Yves Chibon e8bd26
                docfolder=pagure.APP.config['DOCS_FOLDER'],
Pierre-Yves Chibon e8bd26
                ticketfolder=pagure.APP.config['TICKETS_FOLDER'],
Pierre-Yves Chibon e8bd26
                requestfolder=pagure.APP.config['REQUESTS_FOLDER'],
Pierre-Yves Chibon e8bd26
            )
Pierre-Yves Chibon e8bd26
            pagure.SESSION.commit()
Pierre-Yves Chibon e8bd26
        except pagure.exceptions.PagureException, err:
Pierre-Yves Chibon e8bd26
            print 'ERROR with project %s' % project
Pierre-Yves Chibon e8bd26
            print err
Pierre-Yves Chibon e8bd26
        except SQLAlchemyError, err:  # pragma: no cover
Pierre-Yves Chibon e8bd26
            pagure.SESSION.rollback()
Pierre-Yves Chibon e8bd26
            print 'ERROR (DB) with project %s' % project
Pierre-Yves Chibon e8bd26
            print err
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
if __name__ == '__main__':
Pierre-Yves Chibon e8bd26
    parser = argparse.ArgumentParser(
Pierre-Yves Chibon e8bd26
        description='Script creating projects on pagure based on the git '
Pierre-Yves Chibon e8bd26
        'repos present in the specified folder and the pkgdb information.'
Pierre-Yves Chibon e8bd26
    )
Pierre-Yves Chibon e8bd26
    parser.add_argument(
Pierre-Yves Chibon e8bd26
        'folder',
Pierre-Yves Chibon e8bd26
        help='Folder containing all the git repos of the projects to create')
Pierre-Yves Chibon e8bd26
    parser.add_argument(
Pierre-Yves Chibon e8bd26
        '--debug', dest='debug', action='store_true', default=False,
Pierre-Yves Chibon e8bd26
        help='Print the debugging output')
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
    args = parser.parse_args()
Pierre-Yves Chibon e8bd26
Pierre-Yves Chibon e8bd26
    main(args.folder, debug=args.debug)