Changeset 3424

Show
Ignore:
Timestamp:
03/30/08 20:22:44 (8 months ago)
Author:
coderanger
Message:

Starting work on a trac hookscript step. BROKEN!

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracforgeplugin/0.11/tracforge/admin/prototypes.py

    r3419 r3424  
    77 
    88from trac.core import * 
     9from trac.util.compat import reversed 
     10from pkg_resources import resource_stream 
    911 
    1012from tracforge.admin.api import IProjectSetupParticipant 
    1113from tracforge.admin.util import CommandLine, locate 
     14 
     15# Try and use ctypes to get fchmod 
     16try: 
     17    import ctypes 
     18    import ctypes.util 
     19    have_ctypes = True 
     20     
     21    libc = ctypes.CDLL(ctypes.util.find_library('c')) 
     22except ImportError: 
     23    have_ctypes = False 
    1224 
    1325class ProjectSetupParticipantBase(Component): 
     
    134146 
    135147 
     148class SetupSubversionHooks(ProjectSetupParticipantBase): 
     149    """Activate the pre and/or post-commit hooks for Subversion. 
     150     
     151    Valid arguments are `pre`, `post`, or `both`. 
     152     
     153    '''NOTE''': You cannot automatically activate the pre-commit hook on  
     154    Windows at this time. 
     155    """ 
     156     
     157    depends = ['repo_type', 'repo_dir'] 
     158     
     159    arg_map = { 
     160        (True, True): 'both', 
     161        (True, False): 'pre', 
     162        (False, True): 'post', 
     163    } 
     164     
     165    def get_setup_action_defaults(self, action, env): 
     166        if env.config.get('trac', 'repository_type') == 'svn': 
     167            repo_dir = env.config.get('trac', 'repository_dir') 
     168            if repo_dir: 
     169                pre = self._check_hook(repo_dir, 'pre-commit') 
     170                post = self._check_hook(repo_dir, 'post-commit') 
     171                return self.arg_map.get((pre, post)) 
     172     
     173    def execute_setup_action(self, action, args, data, log_cb): 
     174        if data['repo_type'] == 'svn': 
     175            pre, post = dict(reversed(self.arg_map.itertitems())).get(args, (False, False)) 
     176            if pre 
     177     
     178    # Internal methods 
     179    def _check_hook(self, path, hook): 
     180        hook_file = os.path.join(path, 'hooks', hook) 
     181        if os.name == 'nt': 
     182            hook_file += '.bat' 
     183        if not os.path.exists(hook_file): 
     184            return False 
     185        for line in open(hook_file): 
     186            line = line.strip() 
     187            if os.name == 'nt': 
     188                if line.startswith('REM') or line.startswith('::'): 
     189                    continue 
     190            else: 
     191                line = line.split('#', 1)[0] 
     192            if 'trac-'+hook+'-hook' in line: 
     193                return True 
     194        return False 
     195     
     196    def _install_hook(self, path, hook): 
     197        # Open source and sink for the trac hook 
     198        script = resource_stream(__name__, 'scripts/trac-'+hook+'-hook') 
     199        trachook_file = os.path.join(path, 'hooks', 'trac-'+hook+'-hook')  
     200        out = open(trachook_file, 'w') 
     201        # Copy over the given trac hook 
     202        data = script.read(1024) 
     203        while data: 
     204            out.write(data) 
     205            data = script.read(1024) 
     206        script.close() 
     207        out.close() 
     208         
     209        # Add the trac hook to the main hook 
     210        # creating it if needed 
     211        hook_file = os.path.join(path, 'hooks', hook) 
     212        if os.name == 'nt': 
     213            hook_file += '.bat' 
     214        hookf = open(hook_file, 'a') 
     215        hookf.write('%s %s -')