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

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

EmailtoTracScript?:

delete_spam.py.in:

  • Fixed an exception error when shutil has not enough priveledges to remove the attachments.
  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 2.9 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 <configfile> ]
29
30defaults:
31        configfile = /etc/email2trac.conf
32
33SVN Info:
34        $Id: delete_spam.py.in 55 2006-02-17 15:29:38Z bas $
35"""
36from trac import Environment, Ticket
37
38import os
39import sys
40import getopt
41import shutil
42import ConfigParser
43
44def ReadConfig(file):
45        """
46        Parse the config file
47        """
48
49        if not os.path.isfile(file):
50                print 'File %s does not exists' %file
51                sys.exit(1)
52
53        config = ConfigParser.ConfigParser()
54
55        try:
56          config.read(file)
57        except ConfigParser.MissingSectionHeaderError,detail:
58          print detail
59          sys.exit(1)
60
61        defaults = config.defaults()
62        if not defaults.has_key('project'):
63                print 'You have define the location of your trac project, eg:'
64                print '\t project: /var/trac/<projectname>'
65                sys.exit(1)
66         
67        return defaults
68
69
70def delete_spam(project, debug):
71                        env = Environment.Environment(project, create=0)
72                        db = env.get_db_cnx()
73                       
74                        cursor = db.cursor()
75
76                        # Delete the attachments associated with Spam tickets
77                        #
78                        cursor.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
79                        while 1:
80                                row = cursor.fetchone()
81                                if not row:
82                                        break
83                                spam_id =  row[0]
84                                attachment_dir = os.path.join(env.get_attachments_dir(), 'ticket', str(spam_id))
85                                if os.path.exists(attachment_dir):
86                                        if debug:
87                                                print 'delete %s : %s' %(spam_id, attachment_dir)
88                                        try:
89                                                shutil.rmtree(attachment_dir)
90                                        except OSError, detail:
91                                                print 'Contact system-administrator: %s' %detail
92                                                continue
93
94                        cursor.execute("DELETE FROM ticket WHERE  component = 'Spam';")
95                        db.commit()
96
97if __name__ == '__main__':
98        # Default config file
99        #
100        configfile = '/etc/email2trac.conf'
101
102        try:
103                 opts, args = getopt.getopt(sys.argv[1:], 'hf:', ['help', 'file='])
104        except getopt.error,detail:
105                print __doc__
106                print detail
107                sys.exit(1)
108
109        for opt,value in opts:
110                if opt in [ '-h', '--help']:
111                        print __doc__
112                        sys.exit(0)
113                elif opt in ['-f', '--file']:
114                        configfile = value
115       
116        settings = ReadConfig(configfile)
117        delete_spam(settings['project'], int(settings['debug']))
118        print 'Spam is deleted succesfully..'
119
120# EOB
Note: See TracBrowser for help on using the repository browser.