plugins/vcs.py
author Nicolas Chauvat <nicolas.chauvat@logilab.fr>
Mon, 23 Jun 2008 13:42:22 +0200
changeset 6 6aad8b499e86
parent 0 7710b138d4eb
permissions -rw-r--r--
fix reST markup

# Copyright (c) 2004 LOGILAB S.A. (Paris, FRANCE).
# http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""Basic plugin for VCS-related actions
This plugin is based on the oobrother's filelib subpackage, and use its
builtin support of Version Control System
"""

__revision__ = '$Id: vcs.py,v 1.6 2004-12-16 09:36:26 syt Exp $'
__metaclass__ = type

import os.path as osp

from logilab.common.interface import implements
from logilab.devtools.vcslib import VCS_UPTODATE, VCS_MODIFIED, \
     VCS_MISSING, VCS_NEW, VCS_CONFLICT, VCS_NOVERSION, VCS_IGNORED, \
     VCS_REMOVED, VCS_NEEDSPATCH, IVCSFile

from oobrother.plugins import AbstractPlugIn
from oobrother.uiutils import ask_for_message


class VCSPlugin(AbstractPlugIn):
    """plugin for vcs-related actions (update, commit, tag, etc.)"""
    name = 'vcs'
    mimetypes = '*',

    vcs_actions = ('update', 'commit', 'tag', 'add', 'remove')
    
    def __init__(self):
        AbstractPlugIn.__init__(self)

    def _execute_vcs_command(self, cmd):
        """executes 'cmd' in 'thefile's parent directory"""
        self.execute_in_console(cmd)
        thefile.update_parent()        
        
    def get_actions(self, thefile):
        """returns consistent list of actions depending of thefile's status"""
        actions = []
        for action_label in VCSPlugin.vcs_actions:
            actions_method = getattr(self, '_actions_for_%s' % action_label,
                                     lambda f: [])
            actions += actions_method(thefile)
        return [('version control', actions)]

    def support_directory(self, thedir):
        """VCSPlugin supports directory"""
        return True
    
    def is_vc_dir(self, thefile):
        """return true if thefile is a directory under version control """
        return thefile.is_directory() and thefile.status != VCS_NOVERSION
    
    def _actions_for_add(self, thefile):
        """tests if thefile supports 'add' and returns related actions"""
        parent = thefile.get_parent()
        if thefile.status == VCS_NOVERSION and \
               parent and parent.status != VCS_NOVERSION:
            return [(_('add'), self.cb_add)]
        return []
    
    def _actions_for_remove(self, thefile):
        """tests if thefile supports 'add' and returns related actions"""
        if thefile.status != VCS_NOVERSION:
            return [(_('remove'), self.cb_remove)]
        return []
    
    def _actions_for_update(self, thefile):
        """tests if thefile supports 'update' and returns related actions"""
        if self.is_vc_dir(thefile) or thefile.status in (VCS_MODIFIED, VCS_MISSING, VCS_NEEDSPATCH):
            return [(_('update'), self.cb_update)]
        return []
    
    def _actions_for_commit(self, thefile):
        """tests if thefile supports 'commit' and returns related actions"""
        if self.is_vc_dir(thefile) or thefile.status in (VCS_MODIFIED, VCS_REMOVED, VCS_NEW):
            return [(_('commit'), self.cb_commit)]
        return []
    
    def _actions_for_tag(self, thefile):
        """tests if thefile supports 'tag' and returns related actions"""
        if thefile.status not in (VCS_NOVERSION, VCS_IGNORED, VCS_REMOVED):
            return [(_('tag'), self.cb_tag)]
        return []

    def cb_update(self, menuitem, thefile):
        """callback for 'update' item'"""
        self._execute_vcs_command(thefile.update())

    def cb_add(self, menuitem, thefile):
        """callback for 'add to vcs' item'"""
        self._execute_vcs_command(thefile.add())

    def cb_remove(self, menuitem, thefile):
        """callback for 'remove from vcs' item'"""
        self._execute_vcs_command(thefile.remove())

    def cb_commit(self, menuitem, thefile):
        """callback for 'commit' item'"""
        commit_msg = ask_for_message('Commit message for %s' % thefile)
        if commit_msg is not None:
            self._execute_vcs_command(thefile.commit(commit_msg))
            
    def cb_tag(self, menuitem, thefile):
        """callback for 'tag' item'"""
        tagname = ask_for_message('Tag name' % thefile)
        if tagname is not None:
            self._execute_vcs_command(thefile.tag(tagname))
        
    
def register(registry):
    """register plugins from this module to the plugins registry"""
    registry.register(VCSPlugin())