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

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

EmailtoTracScript?:

debian/changelog:

  • New version 0.6

ChangeLog?:

  • Read it

delete_spam.py.in:

  • Ported to 0.9 and 0.10
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 3.5 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 69 2006-05-16 09:45:16Z 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                        cursor = db.cursor()
83
84                        # Delete the attachments associated with Spam tickets
85                        #
86                        cursor.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
87                        while 1:
88                                row = cursor.fetchone()
89                                if not row:
90                                        break
91                                spam_id =  row[0]
92                                attachment_dir = os.path.join(env.get_attachments_dir(), 'ticket', str(spam_id))
93                                if os.path.exists(attachment_dir):
94                                        if debug:
95                                                print 'delete %s : %s' %(spam_id, attachment_dir)
96                                        try:
97                                                shutil.rmtree(attachment_dir)
98                                        except OSError, detail:
99                                                print 'Contact system-administrator: %s' %detail
100                                                continue
101
102                        cursor.execute("DELETE FROM ticket WHERE  component = 'Spam';")
103                        db.commit()
104
105if __name__ == '__main__':
106        # Default config file
107        #
108        configfile = '@email2trac_conf@'
109
110        try:
111                 opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project='])
112        except getopt.error,detail:
113                print __doc__
114                print detail
115                sys.exit(1)
116
117        project_name = None
118        for opt,value in opts:
119                if opt in [ '-h', '--help']:
120                        print __doc__
121                        sys.exit(0)
122                elif opt in ['-f', '--file']:
123                        configfile = value
124                elif opt in ['-p', '--project']:
125                        project_name = value
126       
127        settings = ReadConfig(configfile, project_name)
128        if not settings.has_key('project'):
129                print __doc__
130                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
131                sys.exit(1)
132
133        if settings.has_key('trac_version'):
134                version = float(settings['trac_version'])
135        else:
136                version = trac_default_version
137
138
139        if version == 0.8:
140                from trac.Environment import Environment
141        else:
142                from trac.env import Environment
143
144        delete_spam(settings['project'], int(settings['debug']))
145        print 'Spam is deleted succesfully..'
146
147# EOB
Note: See TracBrowser for help on using the repository browser.