[22] | 1 | #!@PYTHON@ |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2002 |
---|
| 4 | # |
---|
[128] | 5 | # This file is part of the email2trac utils |
---|
[22] | 6 | # |
---|
| 7 | # This program is free software; you can redistribute it and/or modify it |
---|
| 8 | # under the terms of the GNU General Public License as published by the |
---|
| 9 | # Free Software Foundation; either version 2, or (at your option) any |
---|
| 10 | # later version. |
---|
| 11 | # |
---|
| 12 | # This program is distributed in the hope that it will be useful, |
---|
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | # GNU General Public License for more details. |
---|
| 16 | # |
---|
| 17 | # You should have received a copy of the GNU General Public License |
---|
| 18 | # along with this program; if not, write to the Free Software |
---|
| 19 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 20 | # |
---|
[128] | 21 | # vi: |
---|
| 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 : |
---|
[69] | 30 | delete_spam [ -f/--file <configfile> -p/--project <name>] |
---|
[22] | 31 | |
---|
| 32 | defaults: |
---|
| 33 | configfile = /etc/email2trac.conf |
---|
| 34 | |
---|
| 35 | SVN Info: |
---|
| 36 | $Id: delete_spam.py.in 180 2007-07-11 09:46:58Z bas $ |
---|
| 37 | """ |
---|
| 38 | |
---|
| 39 | import os |
---|
| 40 | import sys |
---|
| 41 | import getopt |
---|
| 42 | import shutil |
---|
| 43 | import ConfigParser |
---|
| 44 | |
---|
[180] | 45 | trac_default_version = 0.10 |
---|
[69] | 46 | |
---|
| 47 | def ReadConfig(file, name): |
---|
[22] | 48 | """ |
---|
| 49 | Parse the config file |
---|
| 50 | """ |
---|
| 51 | |
---|
| 52 | if not os.path.isfile(file): |
---|
| 53 | print 'File %s does not exists' %file |
---|
| 54 | sys.exit(1) |
---|
| 55 | |
---|
| 56 | config = ConfigParser.ConfigParser() |
---|
| 57 | try: |
---|
[69] | 58 | config.read(file) |
---|
[22] | 59 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
[69] | 60 | print detail |
---|
| 61 | sys.exit(1) |
---|
[22] | 62 | |
---|
[69] | 63 | # Use given project name else use defaults |
---|
| 64 | # |
---|
| 65 | if name: |
---|
| 66 | if not config.has_section(name): |
---|
| 67 | print "Not an valid project name: %s" %name |
---|
| 68 | print "Valid names: %s" %config.sections() |
---|
| 69 | sys.exit(1) |
---|
[22] | 70 | |
---|
[69] | 71 | project = dict() |
---|
| 72 | for option in config.options(name): |
---|
| 73 | project[option] = config.get(name, option) |
---|
[22] | 74 | |
---|
[69] | 75 | else: |
---|
| 76 | project = config.defaults() |
---|
| 77 | |
---|
| 78 | return project |
---|
| 79 | |
---|
[99] | 80 | |
---|
| 81 | |
---|
[128] | 82 | def new_delete_spam(project, debug): |
---|
| 83 | """ |
---|
| 84 | This only works for trac versions higher or equal then 0.10 |
---|
| 85 | """ |
---|
| 86 | env = Environment(project, create=0) |
---|
| 87 | db = env.get_db_cnx() |
---|
[22] | 88 | |
---|
[137] | 89 | cursor = db.cursor() |
---|
[128] | 90 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 91 | while 1: |
---|
| 92 | row = cursor.fetchone() |
---|
| 93 | if not row: |
---|
| 94 | break |
---|
[99] | 95 | |
---|
[128] | 96 | spam_id = row[0] |
---|
[116] | 97 | |
---|
[137] | 98 | if debug: |
---|
| 99 | print "Deleting ticket %s" %spam_id |
---|
| 100 | |
---|
[128] | 101 | try: |
---|
| 102 | tkt = Ticket(env, spam_id, db) |
---|
[137] | 103 | except util.TracError, detail: |
---|
| 104 | print detail |
---|
[128] | 105 | continue |
---|
[116] | 106 | |
---|
[128] | 107 | tkt.delete() |
---|
[99] | 108 | |
---|
[128] | 109 | def old_delete_spam(project, debug): |
---|
| 110 | """ |
---|
| 111 | This only works for trac versions before 0.10 |
---|
| 112 | """ |
---|
| 113 | env = Environment(project, create=0) |
---|
| 114 | db = env.get_db_cnx() |
---|
| 115 | |
---|
| 116 | attachment_dir = os.path.join(env.path, 'attachments', 'ticket') |
---|
| 117 | cursor = db.cursor() |
---|
| 118 | tkt_cursor = db.cursor() |
---|
| 119 | |
---|
| 120 | # Delete the attachments associated with Spam tickets |
---|
| 121 | # |
---|
| 122 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 123 | while 1: |
---|
| 124 | row = cursor.fetchone() |
---|
| 125 | if not row: |
---|
| 126 | break |
---|
| 127 | spam_id = row[0] |
---|
[137] | 128 | |
---|
[128] | 129 | if debug: |
---|
[137] | 130 | print spam_id |
---|
| 131 | |
---|
[128] | 132 | sql_cmd = "SELECT * FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
| 133 | tkt_cursor.execute(sql_cmd) |
---|
| 134 | row = tkt_cursor.fetchone() |
---|
| 135 | print row |
---|
| 136 | |
---|
| 137 | sql_cmd = "SELECT * FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
| 138 | tkt_cursor.execute(sql_cmd) |
---|
| 139 | row = tkt_cursor.fetchone() |
---|
| 140 | print row |
---|
| 141 | |
---|
| 142 | sql_cmd = "SELECT * FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
| 143 | tkt_cursor.execute(sql_cmd) |
---|
| 144 | row = tkt_cursor.fetchone() |
---|
| 145 | print row |
---|
| 146 | |
---|
| 147 | sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
| 148 | tkt_cursor.execute(sql_cmd) |
---|
[111] | 149 | |
---|
[128] | 150 | sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
| 151 | tkt_cursor.execute(sql_cmd) |
---|
| 152 | |
---|
| 153 | sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
| 154 | tkt_cursor.execute(sql_cmd) |
---|
| 155 | |
---|
| 156 | # Ticket commit |
---|
| 157 | # |
---|
| 158 | db.commit() |
---|
[137] | 159 | |
---|
[128] | 160 | dir = os.path.join(attachment_dir, str(spam_id)) |
---|
| 161 | if os.path.exists(dir): |
---|
| 162 | if debug: |
---|
| 163 | print 'delete %s : %s' %(spam_id, dir) |
---|
| 164 | try: |
---|
| 165 | shutil.rmtree(dir) |
---|
| 166 | except OSError, detail: |
---|
| 167 | print 'Contact system-administrator: %s' %detail |
---|
| 168 | continue |
---|
[111] | 169 | |
---|
[128] | 170 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
| 171 | db.commit() |
---|
[116] | 172 | |
---|
[22] | 173 | if __name__ == '__main__': |
---|
| 174 | # Default config file |
---|
| 175 | # |
---|
[69] | 176 | configfile = '@email2trac_conf@' |
---|
[22] | 177 | |
---|
[128] | 178 | |
---|
[22] | 179 | try: |
---|
[69] | 180 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
[22] | 181 | except getopt.error,detail: |
---|
| 182 | print __doc__ |
---|
| 183 | print detail |
---|
| 184 | sys.exit(1) |
---|
| 185 | |
---|
[69] | 186 | project_name = None |
---|
[22] | 187 | for opt,value in opts: |
---|
| 188 | if opt in [ '-h', '--help']: |
---|
| 189 | print __doc__ |
---|
| 190 | sys.exit(0) |
---|
| 191 | elif opt in ['-f', '--file']: |
---|
| 192 | configfile = value |
---|
[69] | 193 | elif opt in ['-p', '--project']: |
---|
| 194 | project_name = value |
---|
[22] | 195 | |
---|
[69] | 196 | settings = ReadConfig(configfile, project_name) |
---|
| 197 | if not settings.has_key('project'): |
---|
| 198 | print __doc__ |
---|
| 199 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
| 200 | sys.exit(1) |
---|
| 201 | |
---|
| 202 | if settings.has_key('trac_version'): |
---|
| 203 | version = float(settings['trac_version']) |
---|
| 204 | else: |
---|
| 205 | version = trac_default_version |
---|
| 206 | |
---|
[180] | 207 | from trac.env import Environment |
---|
| 208 | from trac.ticket import Ticket |
---|
| 209 | from trac import util |
---|
[69] | 210 | |
---|
[128] | 211 | if version == 0.10: |
---|
| 212 | new_delete_spam(settings['project'], int(settings['debug'])) |
---|
[137] | 213 | #new_delete_spam(settings['project'], 1) |
---|
| 214 | #old_delete_spam(settings['project'], 0) |
---|
[128] | 215 | elif version == 0.9: |
---|
| 216 | old_delete_spam(settings['project'], int(settings['debug'])) |
---|
| 217 | |
---|
[22] | 218 | print 'Spam is deleted succesfully..' |
---|
| 219 | # EOB |
---|