Blame dev-data.py

skrzepto 779aa7
#!/usr/bin/env python
Alex Gleason 6656f7
# -*- coding: utf-8 -*-
Alex Gleason 6656f7
skrzepto 779aa7
""" Populate the pagure db with some dev data. """
skrzepto 779aa7
Aurélien Bompard dcf6f6
from __future__ import print_function, unicode_literals
Aurélien Bompard dcf6f6
Pierre-Yves Chibon b130e5
import argparse
skrzepto 779aa7
import os
Pierre-Yves Chibon b130e5
import sys
Alex Gleason 633b65
import tempfile
Alex Gleason 633b65
import pygit2
Alex Gleason 633b65
import shutil
Pierre-Yves Chibon b130e5
Pierre-Yves Chibon b130e5
from sqlalchemy import create_engine, MetaData
Pierre-Yves Chibon b130e5
skrzepto 779aa7
import pagure
skrzepto 779aa7
import tests
Aurélien Bompard 831553
from pagure.lib import create_session
Alex Gleason 6656f7
from pagure.lib.login import generate_hashed_value
Alex Gleason 6656f7
from pagure.lib.model import create_default_status
Alex Gleason 633b65
from pagure.lib.repo import PagureRepo
Alex Gleason 633b65
skrzepto 779aa7
skrzepto 779aa7
'''
skrzepto 779aa7
Usage:
skrzepto 779aa7
python dev-data.py --init
skrzepto 779aa7
python dev-data.py --clean
skrzepto 779aa7
python dev-data.py --populate
skrzepto 779aa7
python dev-data.py --all
skrzepto 779aa7
'''
skrzepto 779aa7
Aurélien Bompard 831553
_config = pagure.config.reload_config()
Aurélien Bompard 831553
skrzepto 779aa7
skrzepto 779aa7
def init_database():
Pierre-Yves Chibon b130e5
    DB_URL = _config['DB_URL']
skrzepto 779aa7
skrzepto 779aa7
    # create the table if it doesnt exist
skrzepto 779aa7
    pagure.lib.model.create_tables(
skrzepto 779aa7
        DB_URL,
Pierre-Yves Chibon b130e5
        _config.get('PATH_ALEMBIC_INI', None),
Pierre-Yves Chibon b130e5
        acls=_config.get('ACLS', {}),
skrzepto 779aa7
        debug=True)
skrzepto 779aa7
skrzepto 779aa7
    engine = create_engine('%s' % DB_URL, echo=True)
skrzepto 779aa7
skrzepto 779aa7
    metadata = MetaData(engine)
skrzepto 779aa7
    metadata.reflect(bind=engine)
skrzepto 779aa7
    return engine, metadata
skrzepto 779aa7
skrzepto 779aa7
skrzepto 779aa7
def empty_dev_db(metadata, engine):
skrzepto 779aa7
    print('')
skrzepto 779aa7
    print('')
Pierre-Yves Chibon b130e5
    print('WARNING: Deleting all data from ', _config['DB_URL'])
skrzepto 779aa7
    # Dangerous: this will wipe the data from the table but keep the schema
skrzepto 779aa7
    print('')
Alex Gleason 6656f7
    response = raw_input('Do you want to continue? (yes/no)    ')
skrzepto 779aa7
    if 'yes'.startswith(response.lower()):
skrzepto 779aa7
        for tbl in reversed(metadata.sorted_tables):
skrzepto 779aa7
            if tbl.fullname != 'acls':
skrzepto 779aa7
                engine.execute(tbl.delete())
Alex Gleason 6656f7
    else:
Alex Gleason 6656f7
        exit("Aborting.")
skrzepto 779aa7
skrzepto 779aa7
skrzepto 779aa7
def insert_data(session, username, user_email):
Pierre-Yves Chibon b130e5
    _config['EMAIL_SEND'] = False
Pierre-Yves Chibon b130e5
    _config['TESTING'] = True
skrzepto 779aa7
Alex Gleason 6656f7
    # Populate with default statuses
Alex Gleason 6656f7
    create_default_status(session)
Alex Gleason 6656f7
    print('Default statuses populated')
Alex Gleason 6656f7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tags
skrzepto 779aa7
    item = pagure.lib.model.Tag(
skrzepto 779aa7
        tag='tag1',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # Users
skrzepto 779aa7
    # Create a couple of users
Alex Gleason 633b65
    pingou = item = pagure.lib.model.User(
skrzepto 779aa7
        user='pingou',
skrzepto 779aa7
        fullname='PY C',
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email='bar@pingou.com',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
Alex Gleason 633b65
    foo = item = pagure.lib.model.User(
skrzepto 779aa7
        user='foo',
skrzepto 779aa7
        fullname='foo bar',
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email='foo@bar.com',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
Alex Gleason 633b65
    you = item = pagure.lib.model.User(
skrzepto 779aa7
        user=username,
skrzepto 779aa7
        fullname=username,
Alex Gleason 6656f7
        password=generate_hashed_value(u'testing123'),
Alex Gleason 6656f7
        token=None,
skrzepto 779aa7
        default_email=user_email,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print("User created: {} <{}>, {}".format(item.user, item.default_email, 'testing123'))
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pagure_group
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
skrzepto 779aa7
        group_name='admin',
Alex Gleason 6656f7
        group_type='admin',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='admin',
Ryan Lerch 6aa425
        description='Admin Group',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "admin" group. Pingou is a member.')
skrzepto 779aa7
skrzepto 779aa7
    # Add a couple of groups so that we can list them
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
skrzepto 779aa7
        group_name='group',
skrzepto 779aa7
        group_type='user',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='group group',
Ryan Lerch 6aa425
        description='this is a group group',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "group" group. Pingou is a member.')
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureGroup(
skrzepto 779aa7
        group_name='rel-eng',
skrzepto 779aa7
        group_type='user',
Alex Gleason 633b65
        user_id=pingou.id,
Ryan Lerch 6aa425
        display_name='Release Engineering',
Ryan Lerch 6aa425
        description='The group of release engineers',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
Alex Gleason 6656f7
    print('Created "rel-eng" group. Pingou is a member.')
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # projects
skrzepto 779aa7
skrzepto 779aa7
    import shutil
skrzepto 779aa7
    # delete folder from local instance to start from a clean slate
Alex Gleason 6656f7
    if os.path.exists(_config['GIT_FOLDER']):
Pierre-Yves Chibon b130e5
        shutil.rmtree(_config['GIT_FOLDER'])
skrzepto 779aa7
Alex Gleason 633b65
    # Create projects
Alex Gleason 633b65
    item = project1 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Alex Gleason 633b65
        name='test',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='test project #1',
Alex Gleason 633b65
        hook_token='aaabbbccc',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
    session.flush()
Alex Gleason 633b65
    tests.create_locks(session, item)
Alex Gleason 633b65
Alex Gleason 633b65
    item = project2 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Alex Gleason 633b65
        name='test2',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='test project #2',
Alex Gleason 633b65
        hook_token='aaabbbddd',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
Alex Gleason 633b65
    item = project3 = pagure.lib.model.Project(
Alex Gleason 633b65
        user_id=pingou.id,
Alex Gleason 633b65
        name='test3',
Alex Gleason 633b65
        is_fork=False,
Alex Gleason 633b65
        parent_id=None,
Alex Gleason 633b65
        description='namespaced test project',
Alex Gleason 633b65
        hook_token='aaabbbeee',
Alex Gleason 633b65
        namespace='somenamespace',
Alex Gleason 633b65
    )
Alex Gleason 633b65
    item.close_status = ['Invalid', 'Insufficient data', 'Fixed', 'Duplicate']
Alex Gleason 633b65
    session.add(item)
Alex Gleason 633b65
Alex Gleason 633b65
    session.commit()
Alex Gleason 633b65
Pierre-Yves Chibon b130e5
    tests.create_projects_git(_config['GIT_FOLDER'], bare=True)
Alex Gleason 633b65
    add_content_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'test.git'))
skrzepto 779aa7
    tests.add_readme_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'test.git'))
skrzepto 779aa7
skrzepto 779aa7
    # Add some content to the git repo
Alex Gleason 633b65
    add_content_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'))
skrzepto 779aa7
    tests.add_readme_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'))
skrzepto 779aa7
    tests.add_commit_git_repo(
Pierre-Yves Chibon b130e5
        os.path.join(_config['GIT_FOLDER'], 'forks', 'pingou',
skrzepto 779aa7
                     'test.git'), ncommits=10)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_emails
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='bar@pingou.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='foo@pingou.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=foo.id,
skrzepto 779aa7
        email='foo@bar.com')
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.UserEmail(
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
        email=user_email)
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_emails_pending
skrzepto 779aa7
    email_pend = pagure.lib.model.UserEmailPending(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        email='foo@fp.o',
skrzepto 779aa7
        token='abcdef',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(email_pend)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issues
skrzepto 779aa7
    # Add an issue and tag it so that we can list them
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1001,
skrzepto 779aa7
        uid='foobar',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Problem with jenkins build',
skrzepto 779aa7
        content='For some reason the tests fail at line:24',
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1002,
skrzepto 779aa7
        uid='foobar2',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Unit tests failing',
skrzepto 779aa7
        content='Need to fix code for the unit tests to '
skrzepto 779aa7
                'pass so jenkins build can complete.',
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.Issue(
Alex Gleason 633b65
        id=1003,
skrzepto 779aa7
        uid='foobar3',
Alex Gleason 633b65
        project_id=project1.id,
skrzepto 779aa7
        title='Segfault during execution',
skrzepto 779aa7
        content='Index out of bounds for variable i?',
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pagure_user_group
skrzepto 779aa7
    group = pagure.lib.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="rel-eng", group_type=None)
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    group = pagure.lib.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="admin", group_type=None)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=you.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    group = pagure.lib.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="group", group_type=None)
skrzepto 779aa7
skrzepto 779aa7
    item = pagure.lib.model.PagureUserGroup(
Alex Gleason 633b65
        user_id=foo.id,
skrzepto 779aa7
        group_id=group.id
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # projects_groups
skrzepto 779aa7
    group = pagure.lib.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="rel-eng", group_type=None)
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 6656f7
        group_id=group.id,
Alex Gleason 6656f7
        access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    group = pagure.lib.search_groups(session, pattern=None,
skrzepto 779aa7
                                     group_name="admin", group_type=None)
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test2')
skrzepto 779aa7
    item = pagure.lib.model.ProjectGroup(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 6656f7
        group_id=group.id,
Alex Gleason 6656f7
        access="admin"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_requests
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
Pierre-Yves Chibon b130e5
    forked_repo = pagure.lib.get_authorized_project(session, 'test')
skrzepto 779aa7
    req = pagure.lib.new_pull_request(
skrzepto 779aa7
        session=session,
skrzepto 779aa7
        repo_from=forked_repo,
skrzepto 779aa7
        branch_from='master',
skrzepto 779aa7
        repo_to=repo,
skrzepto 779aa7
        branch_to='master',
skrzepto 779aa7
        title='Fixing code for unittest',
Alex Gleason 6656f7
        user=username
skrzepto 779aa7
    )
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tokens
Alex Gleason 633b65
    tests.create_tokens(session, user_id=pingou.id, project_id=project1.id)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # user_projects
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 633b65
        user_id=foo.id,
Alex Gleason 6656f7
        access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test2')
skrzepto 779aa7
    item = pagure.lib.model.ProjectUser(
skrzepto 779aa7
        project_id=repo.id,
Alex Gleason 633b65
        user_id=you.id,
Alex Gleason 6656f7
        access="commit"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issue_comments
skrzepto 779aa7
    item = pagure.lib.model.IssueComment(
Alex Gleason 633b65
        user_id=pingou.id,
skrzepto 779aa7
        issue_uid='foobar',
skrzepto 779aa7
        comment='We may need to adjust the unittests instead of the code.',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # issue_to_issue
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
skrzepto 779aa7
    all_issues = pagure.lib.search_issues(session, repo)
skrzepto 779aa7
    pagure.lib.add_issue_dependency(session, all_issues[0],
Alex Gleason 6656f7
                                    all_issues[1], 'pingou')
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_request_comments
skrzepto 779aa7
    user = pagure.lib.search_user(session, username='pingou')
skrzepto 779aa7
    # only 1 pull request available atm
skrzepto 779aa7
    pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
skrzepto 779aa7
    item = pagure.lib.model.PullRequestComment(
skrzepto 779aa7
        pull_request_uid=pr.uid,
skrzepto 779aa7
        user_id=user.id,
skrzepto 779aa7
        comment="+1 for me. Btw, could you rebase before you merge?",
skrzepto 779aa7
        notification=0
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # pull_request_flags
skrzepto 779aa7
    # only 1 pull request available atm
skrzepto 779aa7
    pr = pagure.lib.get_pull_request_of_user(session, "pingou")[0]
skrzepto 779aa7
    item = pagure.lib.model.PullRequestFlag(
skrzepto 779aa7
        uid="random_pr_flag_uid",
skrzepto 779aa7
        pull_request_uid=pr.uid,
Alex Gleason 633b65
        user_id=pingou.id,
Alex Gleason 633b65
        username=pingou.user,
skrzepto 779aa7
        percent=80,
skrzepto 779aa7
        comment="Jenkins build passes",
Alex Gleason 6656f7
        url=str(pr.id),
Alex Gleason 6656f7
        status="Open"
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tags_issues
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
skrzepto 779aa7
    issues = pagure.lib.search_issues(session, repo)
skrzepto 779aa7
    item = pagure.lib.model.TagIssue(
skrzepto 779aa7
        issue_uid=issues[0].uid,
Alex Gleason 6656f7
        tag='tag1',
skrzepto 779aa7
    )
skrzepto 779aa7
    session.add(item)
skrzepto 779aa7
    session.commit()
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # tokens_acls
skrzepto 779aa7
    tests.create_tokens_acl(session)
skrzepto 779aa7
skrzepto 779aa7
    ######################################
skrzepto 779aa7
    # Fork a project
skrzepto 779aa7
    # delete fork data
skrzepto 779aa7
    fork_proj_location = "forks/foo/test.git"
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['GIT_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('git folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['DOCS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('docs folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['TICKETS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('tickets folder already deleted')
skrzepto 779aa7
skrzepto 779aa7
    try:
Pierre-Yves Chibon b130e5
        shutil.rmtree(os.path.join(_config['REQUESTS_FOLDER'],
skrzepto 779aa7
                                   fork_proj_location))
skrzepto 779aa7
    except:
skrzepto 779aa7
        print('requests folder already deleted')
skrzepto 779aa7
Pierre-Yves Chibon b130e5
    repo = pagure.lib.get_authorized_project(session, 'test')
Alex Gleason 6656f7
    result = pagure.lib.fork_project(session, 'foo', repo)
skrzepto 779aa7
    if result == 'Repo "test" cloned to "foo/test"':
skrzepto 779aa7
        session.commit()
skrzepto 779aa7
skrzepto 779aa7
Alex Gleason 633b65
def add_content_git_repo(folder, branch='master'):
Alex Gleason 633b65
    """ Create some content for the specified git repo. """
Alex Gleason 633b65
    if not os.path.exists(folder):
Alex Gleason 633b65
        os.makedirs(folder)
Alex Gleason 633b65
    brepo = pygit2.init_repository(folder, bare=True)
Alex Gleason 633b65
Alex Gleason 633b65
    newfolder = tempfile.mkdtemp(prefix='pagure-tests')
Alex Gleason 633b65
    repo = pygit2.clone_repository(folder, newfolder)
Alex Gleason 633b65
Alex Gleason 633b65
    # Create a file in that git repo
Alex Gleason 633b65
    with open(os.path.join(newfolder, 'sources'), 'w') as stream:
Alex Gleason 633b65
        stream.write('foo\n bar')
Alex Gleason 633b65
    repo.index.add('sources')
Alex Gleason 633b65
    repo.index.write()
Alex Gleason 633b65
Alex Gleason 633b65
    parents = []
Alex Gleason 633b65
    commit = None
Alex Gleason 633b65
    try:
Alex Gleason 633b65
        commit = repo.revparse_single(
Alex Gleason 633b65
            'HEAD' if branch == 'master' else branch)
Alex Gleason 633b65
    except KeyError:
Alex Gleason 633b65
        pass
Alex Gleason 633b65
    if commit:
Alex Gleason 633b65
        parents = [commit.oid.hex]
Alex Gleason 633b65
Alex Gleason 633b65
    # Commits the files added
Alex Gleason 633b65
    tree = repo.index.write_tree()
Alex Gleason 633b65
    author = pygit2.Signature(
Alex Gleason 633b65
        'Alice Author', 'alice@authors.tld')
Alex Gleason 633b65
    committer = pygit2.Signature(
Alex Gleason 633b65
        'Cecil Committer', 'cecil@committers.tld')
Alex Gleason 633b65
    repo.create_commit(
Alex Gleason 633b65
        'refs/heads/%s' % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Alex Gleason 633b65
        'Add sources file for testing',
Alex Gleason 633b65
        # binary string representing the tree object ID
Alex Gleason 633b65
        tree,
Alex Gleason 633b65
        # list of binary strings representing parents of the new commit
Alex Gleason 633b65
        parents,
Alex Gleason 633b65
    )
Alex Gleason 633b65
Alex Gleason 633b65
    parents = []
Alex Gleason 633b65
    commit = None
Alex Gleason 633b65
    try:
Alex Gleason 633b65
        commit = repo.revparse_single(
Alex Gleason 633b65
            'HEAD' if branch == 'master' else branch)
Alex Gleason 633b65
    except KeyError:
Alex Gleason 633b65
        pass
Alex Gleason 633b65
    if commit:
Alex Gleason 633b65
        parents = [commit.oid.hex]
Alex Gleason 633b65
Alex Gleason 633b65
    subfolder = os.path.join('folder1', 'folder2')
Alex Gleason 633b65
    if not os.path.exists(os.path.join(newfolder, subfolder)):
Alex Gleason 633b65
        os.makedirs(os.path.join(newfolder, subfolder))
Alex Gleason 633b65
    # Create a file in that git repo
Alex Gleason 633b65
    with open(os.path.join(newfolder, subfolder, 'file'), 'w') as stream:
Alex Gleason 633b65
        stream.write('foo\n bar\nbaz')
Alex Gleason 633b65
    repo.index.add(os.path.join(subfolder, 'file'))
Alex Gleason 633b65
    repo.index.write()
Alex Gleason 633b65
Alex Gleason 633b65
    # Commits the files added
Alex Gleason 633b65
    tree = repo.index.write_tree()
Alex Gleason 633b65
    author = pygit2.Signature(
Alex Gleason 633b65
        'Alice Author', 'alice@authors.tld')
Alex Gleason 633b65
    committer = pygit2.Signature(
Alex Gleason 633b65
        'Cecil Committer', 'cecil@committers.tld')
Alex Gleason 633b65
    repo.create_commit(
Alex Gleason 633b65
        'refs/heads/%s' % branch,  # the name of the reference to update
Alex Gleason 633b65
        author,
Alex Gleason 633b65
        committer,
Alex Gleason 633b65
        'Add some directory and a file for more testing',
Alex Gleason 633b65
        # binary string representing the tree object ID
Alex Gleason 633b65
        tree,
Alex Gleason 633b65
        # list of binary strings representing parents of the new commit
Alex Gleason 633b65
        parents
Alex Gleason 633b65
    )
Alex Gleason 633b65
Alex Gleason 633b65
    # Push to origin
Alex Gleason 633b65
    ori_remote = repo.remotes[0]
Alex Gleason 633b65
    master_ref = repo.lookup_reference(
Alex Gleason 633b65
        'HEAD' if branch == 'master' else 'refs/heads/%s' % branch).resolve()
Alex Gleason 633b65
    refname = '%s:%s' % (master_ref.name, master_ref.name)
Alex Gleason 633b65
Alex Gleason 633b65
    PagureRepo.push(ori_remote, refname)
Alex Gleason 633b65
Alex Gleason 633b65
    shutil.rmtree(newfolder)
Alex Gleason 633b65
Alex Gleason 633b65
skrzepto 779aa7
if __name__ == "__main__":
skrzepto 779aa7
    desc = "Run the dev database initialization/insertion/deletion " \
Pierre-Yves Chibon b130e5
           "script for db located  " + str(_config['DB_URL'])
skrzepto 779aa7
    parser = argparse.ArgumentParser(prog="dev-data", description=desc)
skrzepto 779aa7
    parser.add_argument('-i', '--init', action="store_true",
skrzepto 779aa7
                        help="Create the dev db")
skrzepto 779aa7
    parser.add_argument('-p', '--populate', action="store_true",
skrzepto 779aa7
                        help="Add test data to the db")
skrzepto 779aa7
    parser.add_argument('-d', '--delete', action="store_true",
skrzepto 779aa7
                        help="Wipe the dev db")
skrzepto 779aa7
    parser.add_argument('-a', '--all', action="store_true",
skrzepto 779aa7
                        help="Create, Wipe, Populate the dev db")
skrzepto 779aa7
skrzepto 779aa7
    args = parser.parse_args()
skrzepto 779aa7
skrzepto 779aa7
    # forcing the user to choose
skrzepto 779aa7
    if not any(vars(args).values()):
skrzepto 779aa7
        parser.error('No arguments provided.')
skrzepto 779aa7
skrzepto 779aa7
    if args.init or args.delete or args.all:
skrzepto 779aa7
        eng, meta = init_database()
skrzepto 779aa7
skrzepto 779aa7
    if args.delete or args.all:
skrzepto 779aa7
        empty_dev_db(meta, eng)
skrzepto 779aa7
skrzepto 779aa7
    if args.populate or args.all:
Aurélien Bompard 831553
        session = create_session(_config['DB_URL'])
skrzepto 779aa7
        invalid_option = ['pingou', 'bar@pingou.com', 'foo', 'foo@bar.com']
skrzepto 779aa7
        print("")
skrzepto 779aa7
        user_name = raw_input(
skrzepto 779aa7
            "Enter your username so we can add you into the test data:  ")
skrzepto 779aa7
        while user_name in invalid_option:
skrzepto 779aa7
            print("Reserved names: " + str(invalid_option))
skrzepto 779aa7
            user_name = raw_input(
skrzepto 779aa7
                "Enter your username so we can add you into the test data:  ")
skrzepto 779aa7
skrzepto 779aa7
        if not user_name.replace(" ", ""):
skrzepto 779aa7
            user_name = 'pythagoras'
skrzepto 779aa7
skrzepto 779aa7
        print("")
skrzepto 779aa7
        user_email = raw_input("Enter your user email:  ")
skrzepto 779aa7
skrzepto 779aa7
        while user_email in invalid_option:
skrzepto 779aa7
            print("Reserved names: " + str(invalid_option))
skrzepto 779aa7
            user_email = raw_input("Enter your user email:  ")
skrzepto 779aa7
skrzepto 779aa7
        if not user_email.replace(" ", ""):
skrzepto 779aa7
            user_email = 'pythagoras@math.com'
skrzepto 779aa7
skrzepto 779aa7
        insert_data(session, user_name, user_email)