1 | #!@PYTHON@ |
---|
2 | # |
---|
3 | # Copyright (C) 2002 |
---|
4 | # |
---|
5 | # This file is part of the pxeconfig 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 | """ |
---|
22 | Author: Bas van der Vlies |
---|
23 | Date : 29 September 2205 |
---|
24 | Desc. : Delete Spam tickets from database. Else we get an lot of |
---|
25 | tickets |
---|
26 | |
---|
27 | Usage : |
---|
28 | delete_spam [ -f/--file <configfile> -p/--project <name>] |
---|
29 | |
---|
30 | defaults: |
---|
31 | configfile = /etc/email2trac.conf |
---|
32 | |
---|
33 | SVN Info: |
---|
34 | $Id: delete_spam.py.in 116 2006-10-05 12:10:20Z bas $ |
---|
35 | """ |
---|
36 | |
---|
37 | import os |
---|
38 | import sys |
---|
39 | import getopt |
---|
40 | import shutil |
---|
41 | import ConfigParser |
---|
42 | |
---|
43 | trac_default_version = 0.9 |
---|
44 | |
---|
45 | def ReadConfig(file, name): |
---|
46 | """ |
---|
47 | Parse the config file |
---|
48 | """ |
---|
49 | |
---|
50 | if not os.path.isfile(file): |
---|
51 | print 'File %s does not exists' %file |
---|
52 | sys.exit(1) |
---|
53 | |
---|
54 | config = ConfigParser.ConfigParser() |
---|
55 | try: |
---|
56 | config.read(file) |
---|
57 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
58 | print detail |
---|
59 | sys.exit(1) |
---|
60 | |
---|
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) |
---|
68 | |
---|
69 | project = dict() |
---|
70 | for option in config.options(name): |
---|
71 | project[option] = config.get(name, option) |
---|
72 | |
---|
73 | else: |
---|
74 | project = config.defaults() |
---|
75 | |
---|
76 | return project |
---|
77 | |
---|
78 | def delete_spam(project, debug): |
---|
79 | env = Environment(project, create=0) |
---|
80 | db = env.get_db_cnx() |
---|
81 | |
---|
82 | attachment_dir = os.path.join(env.path, 'attachments', 'ticket') |
---|
83 | |
---|
84 | cursor = db.cursor() |
---|
85 | tkt_cursor = db.cursor() |
---|
86 | |
---|
87 | # Delete the attachments associated with Spam tickets |
---|
88 | # |
---|
89 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
90 | while 1: |
---|
91 | row = cursor.fetchone() |
---|
92 | if not row: |
---|
93 | break |
---|
94 | spam_id = row[0] |
---|
95 | |
---|
96 | if debug: |
---|
97 | sql_cmd = "SELECT * FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
98 | tkt_cursor.execute(sql_cmd) |
---|
99 | row = tkt_cursor.fetchone() |
---|
100 | print row |
---|
101 | |
---|
102 | sql_cmd = "SELECT * FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
103 | tkt_cursor.execute(sql_cmd) |
---|
104 | row = tkt_cursor.fetchone() |
---|
105 | print row |
---|
106 | |
---|
107 | sql_cmd = "SELECT * FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
108 | tkt_cursor.execute(sql_cmd) |
---|
109 | row = tkt_cursor.fetchone() |
---|
110 | print row |
---|
111 | |
---|
112 | sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id |
---|
113 | tkt_cursor.execute(sql_cmd) |
---|
114 | |
---|
115 | sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id |
---|
116 | tkt_cursor.execute(sql_cmd) |
---|
117 | |
---|
118 | sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id |
---|
119 | tkt_cursor.execute(sql_cmd) |
---|
120 | |
---|
121 | # Ticket commit |
---|
122 | # |
---|
123 | db.commit() |
---|
124 | |
---|
125 | dir = os.path.join(attachment_dir, str(spam_id)) |
---|
126 | if os.path.exists(dir): |
---|
127 | if debug: |
---|
128 | print 'delete %s : %s' %(spam_id, dir) |
---|
129 | try: |
---|
130 | shutil.rmtree(dir) |
---|
131 | except OSError, detail: |
---|
132 | print 'Contact system-administrator: %s' %detail |
---|
133 | continue |
---|
134 | |
---|
135 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
136 | db.commit() |
---|
137 | |
---|
138 | if __name__ == '__main__': |
---|
139 | # Default config file |
---|
140 | # |
---|
141 | configfile = '@email2trac_conf@' |
---|
142 | |
---|
143 | try: |
---|
144 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
145 | except getopt.error,detail: |
---|
146 | print __doc__ |
---|
147 | print detail |
---|
148 | sys.exit(1) |
---|
149 | |
---|
150 | project_name = None |
---|
151 | for opt,value in opts: |
---|
152 | if opt in [ '-h', '--help']: |
---|
153 | print __doc__ |
---|
154 | sys.exit(0) |
---|
155 | elif opt in ['-f', '--file']: |
---|
156 | configfile = value |
---|
157 | elif opt in ['-p', '--project']: |
---|
158 | project_name = value |
---|
159 | |
---|
160 | settings = ReadConfig(configfile, project_name) |
---|
161 | if not settings.has_key('project'): |
---|
162 | print __doc__ |
---|
163 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
164 | sys.exit(1) |
---|
165 | |
---|
166 | if settings.has_key('trac_version'): |
---|
167 | version = float(settings['trac_version']) |
---|
168 | else: |
---|
169 | version = trac_default_version |
---|
170 | |
---|
171 | |
---|
172 | if version == 0.8: |
---|
173 | from trac.Environment import Environment |
---|
174 | else: |
---|
175 | from trac.env import Environment |
---|
176 | |
---|
177 | delete_spam(settings['project'], int(settings['debug'])) |
---|
178 | print 'Spam is deleted succesfully..' |
---|
179 | |
---|
180 | # EOB |
---|