Blame progit/hooks/progit_hook.py

Pierre-Yves Chibon 3935a2
#-*- coding: utf-8 -*-
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
"""
Pierre-Yves Chibon 3935a2
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
 Authors:
Pierre-Yves Chibon 3935a2
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
"""
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
import os
Pierre-Yves Chibon 3935a2
import shutil
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
import sqlalchemy as sa
Pierre-Yves Chibon 3935a2
import pygit2
Pierre-Yves Chibon 3935a2
import wtforms
Pierre-Yves Chibon 3935a2
from flask.ext import wtf
Pierre-Yves Chibon 3935a2
from sqlalchemy.orm import relation
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
from progit.hooks import BaseHook
Pierre-Yves Chibon 3935a2
from progit.model import BASE, Project
Pierre-Yves Chibon 3935a2
from progit import SESSION, APP
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
class ProgitTable(BASE):
Pierre-Yves Chibon 3935a2
    """ Stores information about the progit hook deployed on a project.
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    Table -- hook_progit
Pierre-Yves Chibon 3935a2
    """
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    __tablename__ = 'hook_progit'
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 3935a2
    project_id = sa.Column(
Pierre-Yves Chibon 3935a2
        sa.Integer,
Pierre-Yves Chibon 3935a2
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon 3935a2
        nullable=False,
Pierre-Yves Chibon 3935a2
        unique=True,
Pierre-Yves Chibon 3935a2
        index=True)
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    active = sa.Column(sa.Boolean, nullable=False, default=False)
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    project = relation(
Pierre-Yves Chibon 3935a2
        'Project', remote_side=[Project.id], backref='progit_hook')
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
class ProgitForm(wtf.Form):
Pierre-Yves Chibon 3935a2
    ''' Form to configure the progit hook. '''
Pierre-Yves Chibon 3935a2
    active = wtforms.BooleanField(
Pierre-Yves Chibon 3935a2
        'Acive',
Pierre-Yves Chibon 3935a2
        [wtforms.validators.Optional()]
Pierre-Yves Chibon 3935a2
    )
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon a7d0b9
class ProgitHook(BaseHook):
Pierre-Yves Chibon 3935a2
    ''' Progit hook. '''
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon ab5837
    name = 'progit'
Pierre-Yves Chibon 3935a2
    form = ProgitForm
Pierre-Yves Chibon 3935a2
    db_object = ProgitTable
Pierre-Yves Chibon 3935a2
    backref = 'progit_hook'
Pierre-Yves Chibon 3935a2
    form_fields = ['active']
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    @classmethod
Pierre-Yves Chibon 3935a2
    def install(cls, project, dbobj):
Pierre-Yves Chibon 3935a2
        ''' Method called to install the hook for a project.
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon 3935a2
            should be installed
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
        '''
Pierre-Yves Chibon 3935a2
        repopath = os.path.join(APP.config['GIT_FOLDER'], project.path)
Pierre-Yves Chibon 3935a2
        if project.is_fork:
Pierre-Yves Chibon 3935a2
            repopath = os.path.join(APP.config['FORK_FOLDER'], project.path)
Pierre-Yves Chibon 3935a2
        hook_files = os.path.join(
Pierre-Yves Chibon 3935a2
            os.path.dirname(os.path.realpath(__file__)), 'files')
Pierre-Yves Chibon 3935a2
        repo_obj = pygit2.Repository(repopath)
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
        # Install the hook itself
Pierre-Yves Chibon 3935a2
        shutil.copyfile(
Pierre-Yves Chibon 219b1e
            os.path.join(hook_files, 'progit_hook.py'),
Pierre-Yves Chibon 3935a2
            os.path.join(repopath, 'hooks', 'post-receive.progit')
Pierre-Yves Chibon 3935a2
        )
Pierre-Yves Chibon 3935a2
        os.chmod(os.path.join(repopath, 'hooks', 'post-receive.progit'), 0755)
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
    @classmethod
Pierre-Yves Chibon 3935a2
    def remove(cls, project):
Pierre-Yves Chibon 3935a2
        ''' Method called to remove the hook of a project.
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon 3935a2
            should be installed
Pierre-Yves Chibon 3935a2
Pierre-Yves Chibon 3935a2
        '''
Pierre-Yves Chibon 3935a2
        repopath = os.path.join(APP.config['GIT_FOLDER'], project.path)
Pierre-Yves Chibon 3935a2
        if project.is_fork:
Pierre-Yves Chibon 3935a2
            repopath = os.path.join(APP.config['FORK_FOLDER'], project.path)
Pierre-Yves Chibon 1cca69
        hook_path = os.path.join(repopath, 'hooks', 'post-receive.progit')
Pierre-Yves Chibon 1cca69
        if os.path.exists(hook_path):
Pierre-Yves Chibon 1cca69
            os.unlink(hook_path)