[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 128 2006-10-19 08:58:38Z bas $ |
---|
| 37 | """ |
---|
| 38 | |
---|
| 39 | import os |
---|
| 40 | import sys |
---|
| 41 | import getopt |
---|
| 42 | import shutil |
---|
| 43 | import ConfigParser |
---|
| 44 | |
---|
[69] | 45 | trac_default_version = 0.9 |
---|
| 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 | |
---|
[128] | 89 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 90 | while 1: |
---|
| 91 | row = cursor.fetchone() |
---|
| 92 | if not row: |
---|
| 93 | break |
---|
[99] | 94 | |
---|
[128] | 95 | spam_id = row[0] |
---|
[116] | 96 | |
---|
[128] | 97 | try: |
---|
| 98 | tkt = Ticket(env, spam_id, db) |
---|
| 99 | except TracError, detail: |
---|
| 100 | continue |
---|
[116] | 101 | |
---|
[128] | 102 | if debug: |
---|
| 103 | print "Deleting ticket %s" %spam_id |
---|
| 104 | |
---|
| 105 | tkt.delete() |
---|
[99] | 106 | |
---|
[128] | 107 | def old_delete_spam(project, debug): |
---|
| 108 | """ |
---|
| 109 | This only works for trac versions before 0.10 |
---|
| 110 | """ |
---|
| 111 | env = Environment(project, create=0) |
---|
| 112 | db = env.get_db_cnx() |
---|
| 113 | |
---|
| 114 | attachment_dir = os.path.join(env.path, 'attachments', 'ticket') |
---|
| 115 | cursor = db.cursor() |
---|
| 116 | tkt_cursor = db.cursor() |
---|
| 117 | |
---|
| 118 | # Delete the attachments associated with Spam tickets |
---|
| 119 | # |
---|
| 120 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 121 | while 1: |
---|
| 122 | row = cursor.fetchone() |
---|
| 123 | if not row: |
---|
| 124 | break |
---|
| 125 | spam_id = row[0] |
---|
| 126 | |
---|
| 127 | if debug: |
---|
| 128 | sql_cmd = "SELECT * FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
| 129 | tkt_cursor.execute(sql_cmd) |
---|
| 130 | row = tkt_cursor.fetchone() |
---|
| 131 | print row |
---|
| 132 | |
---|
| 133 | sql_cmd = "SELECT * FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
| 134 | tkt_cursor.execute(sql_cmd) |
---|
| 135 | row = tkt_cursor.fetchone() |
---|
| 136 | print row |
---|
| 137 | |
---|
| 138 | sql_cmd = "SELECT * FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
| 139 | tkt_cursor.execute(sql_cmd) |
---|
| 140 | row = tkt_cursor.fetchone() |
---|
| 141 | print row |
---|
| 142 | |
---|
| 143 | sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
| 144 | tkt_cursor.execute(sql_cmd) |
---|
[111] | 145 | |
---|
[128] | 146 | sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
| 147 | tkt_cursor.execute(sql_cmd) |
---|
| 148 | |
---|
| 149 | sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
| 150 | tkt_cursor.execute(sql_cmd) |
---|
| 151 | |
---|
| 152 | # Ticket commit |
---|
| 153 | # |
---|
| 154 | db.commit() |
---|
| 155 | dir = os.path.join(attachment_dir, str(spam_id)) |
---|
| 156 | if os.path.exists(dir): |
---|
| 157 | if debug: |
---|
| 158 | print 'delete %s : %s' %(spam_id, dir) |
---|
| 159 | try: |
---|
| 160 | shutil.rmtree(dir) |
---|
| 161 | except OSError, detail: |
---|
| 162 | print 'Contact system-administrator: %s' %detail |
---|
| 163 | continue |
---|
[111] | 164 | |
---|
[128] | 165 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
| 166 | db.commit() |
---|
[116] | 167 | |
---|
[22] | 168 | if __name__ == '__main__': |
---|
| 169 | # Default config file |
---|
| 170 | # |
---|
[69] | 171 | configfile = '@email2trac_conf@' |
---|
[22] | 172 | |
---|
[128] | 173 | |
---|
[22] | 174 | try: |
---|
[69] | 175 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
[22] | 176 | except getopt.error,detail: |
---|
| 177 | print __doc__ |
---|
| 178 | print detail |
---|
| 179 | sys.exit(1) |
---|
| 180 | |
---|
[69] | 181 | project_name = None |
---|
[22] | 182 | for opt,value in opts: |
---|
| 183 | if opt in [ '-h', '--help']: |
---|
| 184 | print __doc__ |
---|
| 185 | sys.exit(0) |
---|
| 186 | elif opt in ['-f', '--file']: |
---|
| 187 | configfile = value |
---|
[69] | 188 | elif opt in ['-p', '--project']: |
---|
| 189 | project_name = value |
---|
[22] | 190 | |
---|
[69] | 191 | settings = ReadConfig(configfile, project_name) |
---|
| 192 | if not settings.has_key('project'): |
---|
| 193 | print __doc__ |
---|
| 194 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
| 195 | sys.exit(1) |
---|
| 196 | |
---|
| 197 | if settings.has_key('trac_version'): |
---|
| 198 | version = float(settings['trac_version']) |
---|
| 199 | else: |
---|
| 200 | version = trac_default_version |
---|
| 201 | |
---|
| 202 | if version == 0.8: |
---|
| 203 | from trac.Environment import Environment |
---|
| 204 | else: |
---|
| 205 | from trac.env import Environment |
---|
[128] | 206 | from trac.ticket import Ticket |
---|
[69] | 207 | |
---|
[128] | 208 | if version == 0.10: |
---|
| 209 | new_delete_spam(settings['project'], int(settings['debug'])) |
---|
| 210 | elif version == 0.9: |
---|
| 211 | old_delete_spam(settings['project'], int(settings['debug'])) |
---|
| 212 | elif version == 0.8: |
---|
| 213 | old_delete_spam(settings['project'], int(settings['debug'])) |
---|
| 214 | |
---|
[22] | 215 | print 'Spam is deleted succesfully..' |
---|
| 216 | # EOB |
---|