[22] | 1 | #!@PYTHON@ |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2002 |
---|
| 4 | # |
---|
[128] | 5 | # This file is part of the email2trac utils |
---|
[22] | 6 | # |
---|
[628] | 7 | # Copyright 2002 SURFsara |
---|
[22] | 8 | # |
---|
[595] | 9 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 10 | # you may not use this file except in compliance with the License. |
---|
| 11 | # You may obtain a copy of the License at |
---|
[22] | 12 | # |
---|
[595] | 13 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
[22] | 14 | # |
---|
[595] | 15 | # Unless required by applicable law or agreed to in writing, software |
---|
| 16 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 17 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 18 | # See the License for the specific language governing permissions and |
---|
| 19 | # limitations under the License. |
---|
| 20 | # |
---|
[128] | 21 | # vi: |
---|
[596] | 22 | # set ts=4 |
---|
[22] | 23 | """ |
---|
| 24 | Author: Bas van der Vlies |
---|
| 25 | Date : 29 September 2205 |
---|
| 26 | Desc. : Delete Spam tickets from database. Else we get an lot of |
---|
| 27 | tickets |
---|
| 28 | |
---|
| 29 | Usage : |
---|
[596] | 30 | delete_spam [ -f/--file <configfile> -n/--dry-run -p/--project <name> -v/--verbose] |
---|
[22] | 31 | |
---|
| 32 | defaults: |
---|
[596] | 33 | configfile = /etc/email2trac.conf |
---|
[22] | 34 | |
---|
| 35 | SVN Info: |
---|
| 36 | $Id: delete_spam.py.in 632 2013-07-12 11:41:24Z bas $ |
---|
| 37 | """ |
---|
| 38 | |
---|
| 39 | import os |
---|
| 40 | import sys |
---|
| 41 | import getopt |
---|
| 42 | import shutil |
---|
| 43 | import ConfigParser |
---|
| 44 | |
---|
[69] | 45 | def ReadConfig(file, name): |
---|
[596] | 46 | """ |
---|
| 47 | Parse the config file |
---|
| 48 | """ |
---|
[22] | 49 | |
---|
[596] | 50 | if not os.path.isfile(file): |
---|
| 51 | print 'File %s does not exists' %file |
---|
| 52 | sys.exit(1) |
---|
[22] | 53 | |
---|
[596] | 54 | config = ConfigParser.ConfigParser() |
---|
| 55 | try: |
---|
| 56 | config.read(file) |
---|
| 57 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
| 58 | print detail |
---|
| 59 | sys.exit(1) |
---|
[22] | 60 | |
---|
[596] | 61 | # Use given project name else use defaults |
---|
| 62 | # |
---|
| 63 | if name: |
---|
| 64 | if not config.has_section(name): |
---|
| 65 | print "Not an valid project name: %s" %name |
---|
| 66 | print "Valid names: %s" %config.sections() |
---|
| 67 | sys.exit(1) |
---|
[22] | 68 | |
---|
[596] | 69 | project = dict() |
---|
| 70 | for option in config.options(name): |
---|
| 71 | project[option] = config.get(name, option) |
---|
[22] | 72 | |
---|
[596] | 73 | else: |
---|
| 74 | project = config.defaults() |
---|
[69] | 75 | |
---|
[596] | 76 | return project |
---|
[69] | 77 | |
---|
[99] | 78 | |
---|
| 79 | |
---|
[350] | 80 | def new_delete_spam(parameters): |
---|
[596] | 81 | """ |
---|
| 82 | This only works for trac versions higher or equal then 0.10 |
---|
| 83 | """ |
---|
[350] | 84 | |
---|
[596] | 85 | debug = int(parameters['debug']) |
---|
| 86 | DRY_RUN = parameters['DRY_RUN'] |
---|
| 87 | VERBOSE = parameters['VERBOSE'] |
---|
[350] | 88 | |
---|
[596] | 89 | project = parameters['project'] |
---|
[350] | 90 | |
---|
| 91 | |
---|
[596] | 92 | env = Environment(project, create=0) |
---|
| 93 | db = env.get_db_cnx() |
---|
[22] | 94 | |
---|
[596] | 95 | cursor = db.cursor() |
---|
| 96 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 97 | while 1: |
---|
| 98 | row = cursor.fetchone() |
---|
| 99 | if not row: |
---|
| 100 | break |
---|
[99] | 101 | |
---|
[596] | 102 | spam_id = row[0] |
---|
[116] | 103 | |
---|
[596] | 104 | if debug or DRY_RUN or VERBOSE: |
---|
| 105 | print "Deleting ticket %s" %spam_id |
---|
[137] | 106 | |
---|
[596] | 107 | try: |
---|
| 108 | tkt = Ticket(env, spam_id, db) |
---|
| 109 | except util.TracError, detail: |
---|
| 110 | print detail |
---|
[602] | 111 | continue |
---|
[116] | 112 | |
---|
[596] | 113 | if DRY_RUN: |
---|
| 114 | print 'DRY_RUN: tkt.delete()' |
---|
| 115 | else: |
---|
| 116 | tkt.delete() |
---|
[99] | 117 | |
---|
[22] | 118 | if __name__ == '__main__': |
---|
[596] | 119 | # Default config file |
---|
| 120 | # |
---|
| 121 | configfile = '@email2trac_conf@' |
---|
[632] | 122 | virtualenv = '@virtualenv@' |
---|
[22] | 123 | |
---|
[128] | 124 | |
---|
[632] | 125 | |
---|
[596] | 126 | try: |
---|
| 127 | opts, args = getopt.getopt(sys.argv[1:], 'hf:np:v', ['help', 'file=', 'dry-run', 'project=', 'verbose']) |
---|
| 128 | except getopt.error,detail: |
---|
| 129 | print __doc__ |
---|
| 130 | print detail |
---|
| 131 | sys.exit(1) |
---|
[22] | 132 | |
---|
[596] | 133 | DRY_RUN = False |
---|
| 134 | VERBOSE = False |
---|
| 135 | project_name = None |
---|
[350] | 136 | |
---|
[596] | 137 | for opt,value in opts: |
---|
| 138 | if opt in [ '-h', '--help']: |
---|
| 139 | print __doc__ |
---|
| 140 | sys.exit(0) |
---|
| 141 | elif opt in ['-f', '--file']: |
---|
| 142 | configfile = value |
---|
| 143 | elif opt in ['-n', '--dry-run']: |
---|
| 144 | DRY_RUN = True |
---|
| 145 | elif opt in ['-p', '--project']: |
---|
| 146 | project_name = value |
---|
| 147 | elif opt in ['-v', '--verbose']: |
---|
| 148 | VERBOSE = True |
---|
[365] | 149 | |
---|
[632] | 150 | |
---|
| 151 | if virtualenv and os.path.exists(virtualenv): |
---|
| 152 | activate_this = os.path.join(virtualenv, 'bin/activate_this.py') |
---|
| 153 | if os.path.exists(activate_this): |
---|
| 154 | execfile(activate_this, dict(__file__=activate_this)) |
---|
| 155 | |
---|
| 156 | |
---|
| 157 | try: |
---|
| 158 | from trac import __version__ as trac_version |
---|
| 159 | from trac import config as trac_config |
---|
| 160 | from trac.env import Environment |
---|
| 161 | from trac.ticket import Ticket |
---|
| 162 | from trac import util |
---|
| 163 | |
---|
| 164 | except ImportError, detail: |
---|
| 165 | print "Can not find a a valid trac installation, solutions could be:" |
---|
| 166 | print "\tset PYTHONPATH" |
---|
| 167 | print "\tuse the --virtualenv <dir> option" |
---|
| 168 | sys.exit(1) |
---|
| 169 | |
---|
[596] | 170 | # Determine major trac version used to be in email2trac.conf |
---|
| 171 | # Quick hack for 0.12 |
---|
| 172 | # |
---|
[632] | 173 | l = trac_version.split('.') |
---|
| 174 | version = '.'.join(l[0:2]) |
---|
[373] | 175 | |
---|
[596] | 176 | if VERBOSE: |
---|
| 177 | print "Found trac version: %s" %version |
---|
[365] | 178 | |
---|
[596] | 179 | ## We only support versions 0.11 and 0.12 |
---|
| 180 | # |
---|
[632] | 181 | if not version in ['0.11', '0.12', '1.0', '1,1']: |
---|
[596] | 182 | print 'Trac version %s is not suported' %(version) |
---|
[373] | 183 | |
---|
[596] | 184 | settings = ReadConfig(configfile, project_name) |
---|
| 185 | if not settings.has_key('project'): |
---|
| 186 | print __doc__ |
---|
| 187 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
| 188 | sys.exit(1) |
---|
[69] | 189 | |
---|
[596] | 190 | settings['DRY_RUN'] = DRY_RUN |
---|
| 191 | settings['VERBOSE'] = VERBOSE |
---|
[350] | 192 | |
---|
[596] | 193 | new_delete_spam(settings) |
---|
[350] | 194 | |
---|
[596] | 195 | print 'Spam is deleted succesfully..' |
---|
[22] | 196 | # EOB |
---|