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

Last change on this file since 113 was 111, checked in by bas, 18 years ago

EmailtoTracScript?:

delete_spam.py.in:

  • Now deletes all Spam ticket information form the different SQL tables

debian/changelog, email2trac.spec, ChangeLog?:

  • Preparing for new release
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.8 KB
Line 
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"""
22Author: Bas van der Vlies
23Date  : 29 September 2205
24Desc. : Delete Spam tickets from database. Else we get an lot of
25        tickets
26
27Usage :
28        delete_spam [ -f/--file <configfile> -p/--project <name>]
29
30defaults:
31        configfile = /etc/email2trac.conf
32
33SVN Info:
34        $Id: delete_spam.py.in 111 2006-09-29 11:07:07Z bas $
35"""
36
37import os
38import sys
39import getopt
40import shutil
41import ConfigParser
42
43trac_default_version = 0.9
44
45def 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
78def 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
86                        # Delete the attachments associated with Spam tickets
87                        #
88                        cursor.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
89                        while 1:
90                                row = cursor.fetchone()
91                                if not row:
92                                        break
93                                spam_id =  row[0]
94
95                                sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id
96                                cursor.execute(sql_cmd)
97
98                                sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id
99                                cursor.execute(sql_cmd)
100
101                                sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id
102                                cursor.execute(sql_cmd)
103
104                                dir = os.path.join(attachment_dir, str(spam_id))
105                                if os.path.exists(dir):
106                                        if debug:
107                                                print 'delete %s : %s' %(spam_id, dir)
108                                        try:
109                                                shutil.rmtree(dir)
110                                        except OSError, detail:
111                                                print 'Contact system-administrator: %s' %detail
112                                                continue
113
114                        cursor.execute("DELETE FROM ticket WHERE  component = 'Spam';")
115                        db.commit()
116
117if __name__ == '__main__':
118        # Default config file
119        #
120        configfile = '@email2trac_conf@'
121
122        try:
123                 opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project='])
124        except getopt.error,detail:
125                print __doc__
126                print detail
127                sys.exit(1)
128
129        project_name = None
130        for opt,value in opts:
131                if opt in [ '-h', '--help']:
132                        print __doc__
133                        sys.exit(0)
134                elif opt in ['-f', '--file']:
135                        configfile = value
136                elif opt in ['-p', '--project']:
137                        project_name = value
138       
139        settings = ReadConfig(configfile, project_name)
140        if not settings.has_key('project'):
141                print __doc__
142                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
143                sys.exit(1)
144
145        if settings.has_key('trac_version'):
146                version = float(settings['trac_version'])
147        else:
148                version = trac_default_version
149
150
151        if version == 0.8:
152                from trac.Environment import Environment
153        else:
154                from trac.env import Environment
155
156        delete_spam(settings['project'], int(settings['debug']))
157        print 'Spam is deleted succesfully..'
158
159# EOB
Note: See TracBrowser for help on using the repository browser.