5
# This file is part of the email2trac utils
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
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.
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
24
Author: Bas van der Vlies
25
Date : 29 September 2205
26
Desc. : Delete Spam tickets from database. Else we get an lot of
30
delete_spam [ -f/--file <configfile> -p/--project <name>]
33
configfile = /etc/email2trac.conf
36
$Id: delete_spam.py.in 180 2007-07-11 09:46:58Z bas $
45
trac_default_version = 0.10
47
def ReadConfig(file, name):
52
if not os.path.isfile(file):
53
print 'File %s does not exists' %file
56
config = ConfigParser.ConfigParser()
59
except ConfigParser.MissingSectionHeaderError,detail:
63
# Use given project name else use defaults
66
if not config.has_section(name):
67
print "Not an valid project name: %s" %name
68
print "Valid names: %s" %config.sections()
72
for option in config.options(name):
73
project[option] = config.get(name, option)
76
project = config.defaults()
82
def new_delete_spam(project, debug):
84
This only works for trac versions higher or equal then 0.10
86
env = Environment(project, create=0)
90
cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';")
92
row = cursor.fetchone()
99
print "Deleting ticket %s" %spam_id
102
tkt = Ticket(env, spam_id, db)
103
except util.TracError, detail:
109
def old_delete_spam(project, debug):
111
This only works for trac versions before 0.10
113
env = Environment(project, create=0)
114
db = env.get_db_cnx()
116
attachment_dir = os.path.join(env.path, 'attachments', 'ticket')
118
tkt_cursor = db.cursor()
120
# Delete the attachments associated with Spam tickets
122
cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';")
124
row = cursor.fetchone()
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()
137
sql_cmd = "SELECT * FROM ticket_change WHERE ticket='%s';" %spam_id
138
tkt_cursor.execute(sql_cmd)
139
row = tkt_cursor.fetchone()
142
sql_cmd = "SELECT * FROM ticket_custom WHERE ticket='%s';" %spam_id
143
tkt_cursor.execute(sql_cmd)
144
row = tkt_cursor.fetchone()
147
sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id
148
tkt_cursor.execute(sql_cmd)
150
sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id
151
tkt_cursor.execute(sql_cmd)
153
sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id
154
tkt_cursor.execute(sql_cmd)
160
dir = os.path.join(attachment_dir, str(spam_id))
161
if os.path.exists(dir):
163
print 'delete %s : %s' %(spam_id, dir)
166
except OSError, detail:
167
print 'Contact system-administrator: %s' %detail
170
cursor.execute("DELETE FROM ticket WHERE component = 'Spam';")
173
if __name__ == '__main__':
174
# Default config file
176
configfile = '@email2trac_conf@'
180
opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project='])
181
except getopt.error,detail:
187
for opt,value in opts:
188
if opt in [ '-h', '--help']:
191
elif opt in ['-f', '--file']:
193
elif opt in ['-p', '--project']:
196
settings = ReadConfig(configfile, project_name)
197
if not settings.has_key('project'):
199
print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
202
if settings.has_key('trac_version'):
203
version = float(settings['trac_version'])
205
version = trac_default_version
207
from trac.env import Environment
208
from trac.ticket import Ticket
209
from trac import util
212
new_delete_spam(settings['project'], int(settings['debug']))
213
#new_delete_spam(settings['project'], 1)
214
#old_delete_spam(settings['project'], 0)
216
old_delete_spam(settings['project'], int(settings['debug']))
218
print 'Spam is deleted succesfully..'