source: emailtotracscript/trunk/delete_spam.py.in @ 128

Last change on this file since 128 was 128, checked in by bas, 17 years ago

EmailtoTracScript?:

delete_spam.py.in:

  • Adjusted for trac version 0.10
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
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"""
24Author: Bas van der Vlies
25Date  : 29 September 2205
26Desc. : Delete Spam tickets from database. Else we get an lot of
27        tickets
28
29Usage :
30        delete_spam [ -f/--file <configfile> -p/--project <name>]
31
32defaults:
33        configfile = /etc/email2trac.conf
34
35SVN Info:
36        $Id: delete_spam.py.in 128 2006-10-19 08:58:38Z bas $
37"""
38
39import os
40import sys
41import getopt
42import shutil
43import ConfigParser
44
45trac_default_version = 0.9
46
47def 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
82def 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.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
90        while 1:
91                row = cursor.fetchone()
92                if not row:
93                        break
94
95                spam_id =  row[0]
96
97                try:
98                        tkt = Ticket(env, spam_id, db)
99                except TracError, detail:
100                        continue
101
102                if debug:
103                        print "Deleting ticket %s" %spam_id
104               
105                tkt.delete()
106
107def 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)
145
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
164
165        cursor.execute("DELETE FROM ticket WHERE  component = 'Spam';")
166        db.commit()
167
168if __name__ == '__main__':
169        # Default config file
170        #
171        configfile = '@email2trac_conf@'
172
173
174        try:
175                 opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project='])
176        except getopt.error,detail:
177                print __doc__
178                print detail
179                sys.exit(1)
180
181        project_name = None
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
188                elif opt in ['-p', '--project']:
189                        project_name = value
190       
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
206                from trac.ticket import Ticket
207
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
215        print 'Spam is deleted succesfully..'
216# EOB
Note: See TracBrowser for help on using the repository browser.