| 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 | |
|---|
| 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 | |
|---|
| | 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) |
|---|