Blame progit/hooks/__init__.py

Pierre-Yves Chibon 33b534
# -*- coding: utf-8 -*-
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3add0
"""
Pierre-Yves Chibon a3add0
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3add0
 Authors:
Pierre-Yves Chibon a3add0
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3add0
"""
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon 3976d7
import os
Pierre-Yves Chibon c1b435
import shutil
Pierre-Yves Chibon 9ce34e
import wtforms
Pierre-Yves Chibon 3976d7
Pierre-Yves Chibon 92224b
from progit import APP, get_repo_path
Pierre-Yves Chibon 3976d7
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon 9ce34e
class RequiredIf(wtforms.validators.Required):
Pierre-Yves Chibon 9ce34e
        """ Wtforms validator setting a field as required if another field
Pierre-Yves Chibon 9ce34e
        has a value.
Pierre-Yves Chibon 9ce34e
        """
Pierre-Yves Chibon 9ce34e
Pierre-Yves Chibon aa9a3e
        def __init__(self, fields, *args, **kwargs):
Pierre-Yves Chibon aa9a3e
            if isinstance(fields, basestring):
Pierre-Yves Chibon aa9a3e
                fields = [fields]
Pierre-Yves Chibon aa9a3e
            self.fields = fields
Pierre-Yves Chibon 9ce34e
            super(RequiredIf, self).__init__(*args, **kwargs)
Pierre-Yves Chibon 9ce34e
Pierre-Yves Chibon 9ce34e
        def __call__(self, form, field):
Pierre-Yves Chibon aa9a3e
            for fieldname in self.fields:
Pierre-Yves Chibon aa9a3e
                nfield = form._fields.get(fieldname)
Pierre-Yves Chibon aa9a3e
                if nfield is None:
Pierre-Yves Chibon aa9a3e
                    raise Exception(
Pierre-Yves Chibon aa9a3e
                        'no field named "%s" in form' % fieldname)
Pierre-Yves Chibon aa9a3e
                if bool(nfield.data):
Pierre-Yves Chibon aa9a3e
                    super(RequiredIf, self).__call__(form, field)
Pierre-Yves Chibon 9ce34e
Pierre-Yves Chibon 9ce34e
Pierre-Yves Chibon a3add0
class BaseHook(object):
Pierre-Yves Chibon a3add0
    ''' Base class for progit's hooks. '''
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3add0
    name = None
Pierre-Yves Chibon a3add0
    form = None
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3f816
    @classmethod
Pierre-Yves Chibon 3976d7
    def set_up(cls, project):
Pierre-Yves Chibon 3976d7
        ''' Install the generic post-receive hook that allow us to call
Pierre-Yves Chibon 3976d7
        multiple post-receive hooks as set per plugin.
Pierre-Yves Chibon 3976d7
        '''
Pierre-Yves Chibon 92224b
        repopath = get_repo_path(project)
Pierre-Yves Chibon 3976d7
Pierre-Yves Chibon 3976d7
        hook_files = os.path.join(
Pierre-Yves Chibon 3976d7
            os.path.dirname(os.path.realpath(__file__)), 'files')
Pierre-Yves Chibon 3976d7
Pierre-Yves Chibon 007ef8
        # Make sure the hooks folder exists
Pierre-Yves Chibon 007ef8
        hookfolder = os.path.join(repopath, 'hooks')
Pierre-Yves Chibon 007ef8
        if not os.path.exists(hookfolder):
Pierre-Yves Chibon 007ef8
            os.makedirs(hookfolder)
Pierre-Yves Chibon 007ef8
Pierre-Yves Chibon 3976d7
        # Install the main post-receive file
Pierre-Yves Chibon 007ef8
        postreceive = os.path.join(hookfolder, 'post-receive')
Pierre-Yves Chibon 3976d7
        if not os.path.exists(postreceive):
Pierre-Yves Chibon 3976d7
            shutil.copyfile(
Pierre-Yves Chibon 3976d7
                os.path.join(hook_files, 'post-receive'),
Pierre-Yves Chibon 3976d7
                postreceive)
Pierre-Yves Chibon 3976d7
            os.chmod(postreceive, 0755)
Pierre-Yves Chibon 3976d7
Pierre-Yves Chibon 3976d7
    @classmethod
Pierre-Yves Chibon 4ae85f
    def install(cls, project, dbobj):  # pragma: no cover
Pierre-Yves Chibon daa5de
        ''' Method called to install the hook for a project.
Pierre-Yves Chibon daa5de
Pierre-Yves Chibon daa5de
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon daa5de
            should be installed
Pierre-Yves Chibon a0391b
        :arg dbobj: the DB object the hook uses to store the settings
Pierre-Yves Chibon a0391b
            information.
Pierre-Yves Chibon daa5de
Pierre-Yves Chibon daa5de
        '''
Pierre-Yves Chibon a3add0
        pass
Pierre-Yves Chibon a3add0
Pierre-Yves Chibon a3f816
    @classmethod
Pierre-Yves Chibon 4ae85f
    def remove(cls, project):  # pragma: no cover
Pierre-Yves Chibon daa5de
        ''' Method called to remove the hook of a project.
Pierre-Yves Chibon daa5de
Pierre-Yves Chibon daa5de
        :arg project: a ``progit.model.Project`` object to which the hook
Pierre-Yves Chibon daa5de
            should be installed
Pierre-Yves Chibon daa5de
Pierre-Yves Chibon daa5de
        '''
Pierre-Yves Chibon a3add0
        pass