1 | #!@PYTHON@ |
---|
2 | # |
---|
3 | # Copyright (C) 2002 |
---|
4 | # |
---|
5 | # This file is part of the email2trac utils |
---|
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 | # |
---|
21 | # vi: |
---|
22 | # set ts=4 |
---|
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 : |
---|
30 | delete_spam [ -f/--file <configfile> -p/--project <name>] |
---|
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 | |
---|
45 | trac_default_version = 0.10 |
---|
46 | |
---|
47 | def ReadConfig(file, name): |
---|
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: |
---|
58 | config.read(file) |
---|
59 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
60 | print detail |
---|
61 | sys.exit(1) |
---|
62 | |
---|
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) |
---|
70 | |
---|
71 | project = dict() |
---|
72 | for option in config.options(name): |
---|
73 | project[option] = config.get(name, option) |
---|
74 | |
---|
75 | else: |
---|
76 | project = config.defaults() |
---|
77 | |
---|
78 | return project |
---|
79 | |
---|
80 | |
---|
81 | |
---|
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() |
---|
88 | |
---|
89 | cursor = db.cursor() |
---|
90 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
91 | while 1: |
---|
92 | row = cursor.fetchone() |
---|
93 | if not row: |
---|
94 | break |
---|
95 | |
---|
96 | spam_id = row[0] |
---|
97 | |
---|
98 | if debug: |
---|
99 | print "Deleting ticket %s" %spam_id |
---|
100 | |
---|
101 | try: |
---|
102 | tkt = Ticket(env, spam_id, db) |
---|
103 | except util.TracError, detail: |
---|
104 | print detail |
---|
105 | continue |
---|
106 | |
---|
107 | tkt.delete() |
---|
108 | |
---|
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] |
---|
128 | |
---|
129 | if debug: |
---|
130 | print spam_id |
---|
131 | |
---|
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) |
---|
149 | |
---|
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() |
---|
159 | |
---|
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 |
---|
169 | |
---|
170 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
171 | db.commit() |
---|
172 | |
---|
173 | if __name__ == '__main__': |
---|
174 | # Default config file |
---|
175 | # |
---|
176 | configfile = '@email2trac_conf@' |
---|
177 | |
---|
178 | |
---|
179 | try: |
---|
180 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
181 | except getopt.error,detail: |
---|
182 | print __doc__ |
---|
183 | print detail |
---|
184 | sys.exit(1) |
---|
185 | |
---|
186 | project_name = None |
---|
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 |
---|
193 | elif opt in ['-p', '--project']: |
---|
194 | project_name = value |
---|
195 | |
---|
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 | |
---|
207 | from trac.env import Environment |
---|
208 | from trac.ticket import Ticket |
---|
209 | from trac import util |
---|
210 | |
---|
211 | if version == 0.10: |
---|
212 | new_delete_spam(settings['project'], int(settings['debug'])) |
---|
213 | #new_delete_spam(settings['project'], 1) |
---|
214 | #old_delete_spam(settings['project'], 0) |
---|
215 | elif version == 0.9: |
---|
216 | old_delete_spam(settings['project'], int(settings['debug'])) |
---|
217 | |
---|
218 | print 'Spam is deleted succesfully..' |
---|
219 | # EOB |
---|