Changeset 2646

Show
Ignore:
Timestamp:
09/19/07 10:39:58 (1 year ago)
Author:
Bombenbodo
Message:

New option repository_hooks_dir

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tracsvnhooksplugin/0.10/setup.py

    r2438 r2646  
    22 
    33PACKAGE = 'TracSVNHooks' 
    4 VERSION = '0.2
     4VERSION = '0.4
    55 
    66setup( 
    7   name = PACKAGE, 
    8   version = VERSION, 
    9   author = "Robert Barsch", 
    10   author_email = "barsch@lmu.de", 
    11   description = "A interface to edit SVN repository hooks via admin panel", 
    12   license = "BSD", 
    13   keywords = "trac plugin SVN hooks", 
    14   url = "http://trac-hacks.org/wiki/TracSvnHooksPlugin", 
    15   classifiers = [ 'Framework :: Trac', ], 
    16   install_requires = ['TracWebAdmin'], 
    17   packages=['svnhooks'], 
    18   entry_points={'trac.plugins': '%s = svnhooks' % PACKAGE}, 
    19   package_data={'svnhooks': ['templates/*.cs']}, 
     7    name = PACKAGE, 
     8    version = VERSION, 
     9    author = "Robert Barsch", 
     10    author_email = "barsch@lmu.de", 
     11    description = "A interface to edit SVN repository hooks via admin panel", 
     12    license = "BSD", 
     13    keywords = "trac plugin SVN hooks", 
     14    url = "http://svn.geophysik.uni-muenchen.de/trac/tracmods/", 
     15    classifiers = ['Framework :: Trac',], 
     16    install_requires = ['TracWebAdmin'], 
     17    packages = ['svnhooks'], 
     18    entry_points = {'trac.plugins': '%s = svnhooks' % PACKAGE}, 
     19    package_data = {'svnhooks': ['templates/*.cs']}, 
    2020) 
    21                   
     21           
  • tracsvnhooksplugin/0.10/svnhooks/svnhooks.py

    r2455 r2646  
    22from trac.web.chrome import ITemplateProvider 
    33from webadmin.web_ui import IAdminPageProvider 
     4 
    45import string 
    56import os 
    67 
     8__all__ = ['SVNHooksPlugin'] 
     9 
     10BUGMSG = """There is a bug in some versions of SVN connected to the  
     11this hook! By filling the form you *have* to enable the hook.""" 
     12 
    713SECTIONS = { 
    8   'post-commit'        : ''
    9   'pre-commit'         : 'There is a bug in some versions of SVN connected to the pre-commit hook! By filling the form you *have* to enable the hook.'
    10   'post-revprop-change': ''
    11   'pre-revprop-change' : ''
    12   'start-commit'       : 'There is a bug in some versions of SVN connected to the start-commit hook! By filling the form you *have* to enable the hook.'
    13   'pre-lock'          : ''
    14   'post-lock'          : ''
    15   'pre-unlock'         : ''
    16   'post-unlock'        : '' 
     14    'post-commit'        : False
     15    'pre-commit'         : BUGMSG
     16    'post-revprop-change': False
     17    'pre-revprop-change' : False
     18    'start-commit'       : BUGMSG
     19    'pre-lock'           : False
     20    'post-lock'          : False
     21    'pre-unlock'         : False
     22    'post-unlock'        : False 
    1723} 
    1824 
     
    2329    def get_admin_pages(self, req): 
    2430        if req.perm.has_permission('TRAC_ADMIN'): 
    25            yield ('svn', 'Subversion', 'hooks', 'Hooks') 
     31            yield ('svn', 'Subversion', 'hooks', 'Hooks') 
    2632 
    2733    def process_admin_request(self, req, cat, page, path_info): 
    2834        assert req.perm.has_permission('TRAC_ADMIN') 
    29  
     35         
    3036        sections = SECTIONS.keys() 
    31        sections.sort() 
    32  
     37        sections.sort() 
     38         
    3339        # catch selected section 
    34        if req.method == 'POST' and req.args.get('sections'): 
    35          section = req.args.get('sections') 
     40        if req.method == 'POST' and req.args.get('sections'): 
     41            section = req.args.get('sections') 
    3642        else: 
    37           section = sections[0] 
    38  
     43            section = sections[0] 
     44         
    3945        # validate section 
    4046        if section not in sections: 
    4147            raise TracError("Invalid section %s" % section) 
    42  
     48         
    4349        # get config vars 
    4450        repository_type = self.config.get('trac', 'repository_type').upper() 
    45         repository_dir = self.config.get('trac', 'repository_dir') + os.sep + 'hooks' 
    46         base_url = self.config.get('trac', 'base_url') 
    47         project_name = self.config.get('project', 'name') 
    48         smtp_default_domain = self.config.get('notification', 'smtp_default_domain') 
    49        smtp_from = self.config.get('notification', 'smtp_from') 
    50         smtp_replyto = self.config.get('notification', 'smtp_replyto') 
    51          
    52         # repository type must be SVN 
    53         if repository_type!='SVN': 
    54            raise TracError("Wrong repository_type %s in trac.ini" % repository_type) 
    55  
     51        repository_dir = self.config.get('trac', 'repository_dir') 
     52        hooks_dir = self.config.get('trac', 'repository_hooks_dir')  
     53        if not hooks_dir: 
     54            hooks_dir = repository_dir + os.sep + 'hooks' 
     55        project_name = self.config.get('project', 'name') 
     56         
     57        # repository type must be SVN 
     58        if repository_type!='SVN': 
     59            raise TracError("Wrong repository_type %s in trac.ini" \ 
     60                            % repository_type) 
     61         
    5662        # test if repository_dir exists 
    57         if not os.access(repository_dir,os.X_OK|os.W_OK|os.R_OK): 
    58             raise TracError("Can't access repository hook directory %s" % repository_dir) 
    59  
    60         tmplfile = repository_dir + os.sep + section + '.tmpl' 
    61         hookfile = repository_dir + os.sep + section 
    62  
    63         # evaluate forms 
    64         if req.method == 'POST': 
    65           # Change status 
    66           if req.args.get('tooglestatus'): 
    67             if req.args.get('tooglestatus')==' Enable ': 
    68               os.chmod(hookfile,0770) 
    69             else: 
    70               os.chmod(hookfile,0660) 
    71           if req.args.get('savehookfile'): 
    72             current=req.args.get('current').strip().replace('\r', '') 
    73             if current: 
    74               try: 
    75                 fp = open(hookfile,'w') 
    76                 fp.write(current) 
    77                 fp.close() 
    78               except: 
    79                 raise TracError("Can't write repository hook %s" % hookfile) 
    80             else: 
    81               try: 
    82                 os.remove(hookfile) 
    83               except: 
    84                 pass 
    85           elif req.args.get('addemail'): 
    86             if req.args.get('emails'): 
    87               emails = req.args.get('emails') 
    88               subject = req.args.get('subject') 
    89               mfilter = req.args.get('filter') 
    90               try: 
    91                 current="" 
    92                 try: 
    93                   fp = open(hookfile,'r') 
    94                   current = fp.read() 
    95                   fp.close() 
    96                 except: 
    97                   pass 
    98                 fp = open(hookfile,'a') 
    99                 fp.writelines(os.linesep) 
    100                 if 'REPOS="$1"' not in current: 
    101                   fp.writelines('REPOS="$1"'+os.linesep) 
    102                 if 'REV="$2"' not in current: 
    103                   fp.writelines('REV="$2"'+os.linesep) 
    104                 fp.writelines('/usr/lib/subversion/hook-scripts/commit-email.pl "$REPOS" "$REV" \\'+os.linesep) 
    105                 if smtp_from: 
    106                   fp.writelines('--from "'+smtp_from+'" \\'+os.linesep) 
    107                 if smtp_replyto: 
    108                   fp.writelines('-r "'+smtp_replyto+'" \\'+os.linesep) 
    109                 if smtp_default_domain: 
    110                   fp.writelines('-h "'+smtp_default_domain+'" \\'+os.linesep) 
    111                 if mfilter: 
    112                   fp.writelines('-m "'+mfilter+'" \\'+os.linesep) 
    113                 if subject: 
    114                   fp.writelines('-s "'+subject+'" \\'+os.linesep) 
    115                 for email in emails.strip().split(): 
    116                   fp.writelines(email+' \\'+os.linesep) 
    117                 fp.writelines(os.linesep) 
    118                 fp.close() 
    119               except: 
    120                 raise TracError("Can't write repository hook %s" % hookfile) 
    121           elif req.args.get('addtrac'): 
    122               try: 
    123                 current="" 
    124                 try: 
    125                   fp = open(hookfile,'r') 
    126                   current = fp.read() 
    127                   fp.close() 
    128                 except: 
    129                   pass 
    130                 fp = open(hookfile,'a') 
    131                 fp.writelines(os.linesep) 
    132                 if 'REPOS="$1"' not in current: 
    133                   fp.writelines('REPOS="$1"'+os.linesep) 
    134                 if 'REV="$2"' not in current: 
    135                   fp.writelines('REV="$2"'+os.linesep) 
    136                 if 'LOG=' not in current: 
    137                   fp.writelines('LOG=`/usr/bin/svnlook log -r $REV $REPOS`'+os.linesep) 
    138                 if 'AUTHOR=' not in current: 
    139                   fp.writelines('AUTHOR=`/usr/bin/svnlook author -r $REV $REPOS`'+os.linesep) 
    140                 if 'TRAC_ENV=' not in current: 
    141                   fp.writelines('TRAC_ENV="'+self.env.path+'"'+os.linesep) 
    142                 if 'TRAC_URL=' not in current: 
    143                   fp.writelines('TRAC_URL="'+base_url+'"'+os.linesep) 
    144                 fp.writelines('/usr/bin/python /usr/share/trac/contrib/trac-post-commit-hook \\'+os.linesep) 
    145                 fp.writelines('-p "$TRAC_ENV" \\'+os.linesep) 
    146                 fp.writelines('-r "$REV" \\'+os.linesep) 
    147                 fp.writelines('-u "$AUTHOR" \\'+os.linesep) 
    148                 fp.writelines('-m "$LOG" \\'+os.linesep) 
    149                 fp.writelines('-s "$TRAC_URL"'+os.linesep) 
    150                 fp.writelines(os.linesep) 
    151               except: 
    152                 raise TracError("Can't write repository hook %s" % hookfile) 
    153                
    154  
    155         # read current hook file + get status 
    156         current = "" 
    157         try: 
    158           fp = open(hookfile,'r') 
    159           current = fp.read() 
    160           fp.close() 
    161         except: 
    162           pass 
    163            
    164         # read description from hook tmpl file 
    165         orig = "" 
    166         description = "" 
    167         try: 
    168           fp = open(tmplfile,'r') 
    169           orig = fp.readlines() 
    170           fp.close() 
    171           description = " " + string.join(orig[4:]) 
    172         except: 
    173           pass 
    174  
     63        if not os.access(hooks_dir, os.X_OK|os.R_OK): 
     64            raise TracError("Can't access hook directory %s. " % hooks_dir + \ 
     65                            "You may set the option repository_hooks_dir " + \ 
     66                            "in the [trac] section in the trac.ini " + \ 
     67                            "configuration file to point to the right " + \ 
     68                            "location.") 
     69         
     70        tmplfile = hooks_dir + os.sep + section + '.tmpl' 
     71        hookfile = hooks_dir + os.sep + section 
     72         
     73        # read out current hookfile 
     74        current="" 
     75        try: 
     76            fp = open(hookfile,'r') 
     77            current = fp.read() 
     78            fp.close() 
     79        except Exception: 
     80            pass 
     81         
     82        # evaluate forms 
     83        if req.method == 'POST': 
     84            # toogle file status 
     85            if req.args.get('tooglestatus'): 
     86                if req.args.get('tooglestatus')==' Enable ': 
     87                    try: 
     88                        os.chmod(hookfile,0770) 
     89                    except Exception: 
     90                        pass 
     91                else: 
     92                    try: 
     93                        os.chmod(hookfile,0660) 
     94                    except Exception: 
     95                        pass 
     96            # save file 
     97            elif req.args.get('savehookfile'): 
     98                current=req.args.get('current').strip().replace('\r', '') 
     99                if current: 
     100                    try: 
     101                        fp = open(hookfile, 'w') 
     102                        fp.write(current) 
     103                        fp.close() 
     104                    except Exception: 
     105                        raise TracError("Can't write hook file %s" % hookfile) 
     106                else: 
     107                    try: 
     108                        os.remove(hookfile) 
     109                    except Exception: 
     110                        pass 
     111            # add e-mail hook 
     112            elif req.args.get('addemail') and req.args.get('emails'): 
     113                self._add_emailhook(req, hookfile, current) 
     114            # add Trac hook 
     115            elif req.args.get('addtrac'): 
     116                self._add_trachook(req, hookfile, current) 
     117         
     118        # read description from template file 
     119        orig = "" 
     120        try: 
     121            fp = open(tmplfile,'r') 
     122            orig = fp.readlines() 
     123            fp.close() 
     124        except Exception: 
     125            pass 
     126        description = " " + string.join(orig[4:]) 
     127         
     128        # read out current hookfile 
     129        current="" 
     130        try: 
     131            fp = open(hookfile,'r') 
     132            current = fp.read() 
     133            fp.close() 
     134        except Exception: 
     135            pass 
     136         
    175137        req.hdf['svnhooks.section'] = section 
    176138        req.hdf['svnhooks.sections'] = sections 
     
    178140        req.hdf['svnhooks.current'] = current 
    179141        req.hdf['svnhooks.note'] = SECTIONS[section] 
    180        req.hdf['svnhooks.project_name'] = project_name 
     142        req.hdf['svnhooks.project_name'] = project_name 
    181143        req.hdf['svnhooks.description'] = description 
    182          
     144 
    183145        return 'svnhooks.cs', None 
     146 
     147    def _add_emailhook(self, req, hookfile, current): 
     148        """ 
     149        Appends the subversion commit-email.pl script to the end of the  
     150        selected subversion hook file. 
     151        """  
     152        smtp_default_domain = self.config.get('notification', \ 
     153                                              'smtp_default_domain') 
     154        smtp_from = self.config.get('notification', 'smtp_from') 
     155        smtp_replyto = self.config.get('notification', 'smtp_replyto') 
     156        emails = req.args.get('emails') 
     157        subject = req.args.get('subject') 
     158        mfilter = req.args.get('filter') 
     159        # try to append hook 
     160        try: 
     161            fp = open(hookfile,'a') 
     162            fp.writelines(os.linesep) 
     163            if 'REPOS="$1"' not in current: 
     164                fp.writelines('REPOS="$1"' + os.linesep) 
     165            if 'REV="$2"' not in current: 
     166                fp.writelines('REV="$2"' + os.linesep) 
     167            fp.writelines('/usr/lib/subversion/hook-scripts/' + \ 
     168                          'commit-email.pl "$REPOS" "$REV" \\'+os.linesep) 
     169            if smtp_from: 
     170                fp.writelines('--from "' + smtp_from + '" \\' + os.linesep) 
     171            if smtp_replyto: 
     172                fp.writelines('-r "' + smtp_replyto + '" \\' + os.linesep) 
     173            if smtp_default_domain: 
     174                fp.writelines('-h "' + smtp_default_domain + '" \\' \ 
     175                              + os.linesep) 
     176            if mfilter: 
     177                fp.writelines('-m "' + mfilter + '" \\' + os.linesep) 
     178            if subject: 
     179                fp.writelines('-s "' + subject + '" \\' + os.linesep) 
     180            for email in emails.strip().split(): 
     181                fp.writelines(email.strip() + ' \\' + os.linesep) 
     182            fp.writelines(os.linesep) 
     183            fp.close() 
     184        except Exception: 
     185            raise TracError("Can't write repository hook %s" % hookfile) 
     186 
     187    def _add_trachook(self, req, hookfile, current): 
     188        """ 
     189        Appends the trac-post-commit-hook script to the end of the selected 
     190        subversion hook file. 
     191        """ 
     192        base_url = self.config.get('trac', 'base_url') 
     193        # try to append hook 
     194        try: 
     195            fp = open(hookfile, 'a') 
     196            fp.writelines(os.linesep) 
     197            if 'REPOS="$1"' not in current: 
     198                fp.writelines('REPOS="$1"' + os.linesep) 
     199            if 'REV="$2"' not in current: 
     200                fp.writelines('REV="$2"' + os.linesep) 
     201            if 'LOG=' not in current: 
     202                fp.writelines('LOG=`/usr/bin/svnlook log -r $REV $REPOS`' \ 
     203                              + os.linesep) 
     204            if 'AUTHOR=' not in current: 
     205                fp.writelines('AUTHOR=`/usr/bin/svnlook author -r $REV ' + \ 
     206                              '$REPOS`' + os.linesep) 
     207            if 'TRAC_ENV=' not in current: 
     208                fp.writelines('TRAC_ENV="'+self.env.path+'"' + os.linesep) 
     209            if 'TRAC_URL=' not in current: 
     210                fp.writelines('TRAC_URL="'+base_url+'"' + os.linesep) 
     211            fp.writelines('/usr/bin/python /usr/share/trac/contrib/' + \ 
     212                          'trac-post-commit-hook \\' + os.linesep) 
     213            fp.writelines('-p "$TRAC_ENV" \\' + os.linesep) 
     214            fp.writelines('-r "$REV" \\' + os.linesep) 
     215            fp.writelines('-u "$AUTHOR" \\' + os.linesep) 
     216            fp.writelines('-m "$LOG" \\' + os.linesep) 
     217            fp.writelines('-s "$TRAC_URL"' + os.linesep) 
     218            fp.writelines(os.linesep) 
     219            fp.close() 
     220        except Exception: 
     221            raise TracError("Can't write repository hook %s" % hookfile) 
    184222 
    185223 
    186224    # ITemplateProvider methods 
    187225    def get_templates_dirs(self): 
     226        """ 
     227        Return the absolute path of the directory containing the provided 
     228        ClearSilver templates. 
     229        """ 
    188230        from pkg_resources import resource_filename 
    189231        return [resource_filename(__name__, 'templates')] 
    190232 
    191233    def get_htdocs_dirs(self): 
     234        """ 
     235        Return a list of directories with static resources (such as style 
     236        sheets, images, etc.) 
     237         
     238        Each item in the list must be a `(prefix, abspath)` tuple. The 
     239        `prefix` part defines the path in the URL that requests to these 
     240        resources are prefixed with. 
     241         
     242        The `abspath` is the absolute path to the directory containing the 
     243        resources on the local file system. 
     244        """ 
    192245        return []