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 <configfile> ] |
---|
29 | |
---|
30 | defaults: |
---|
31 | configfile = /etc/email2trac.conf |
---|
32 | |
---|
33 | SVN Info: |
---|
34 | $Id: delete_spam.py.in 22 2006-01-15 10:53:48Z bas $ |
---|
35 | """ |
---|
36 | from trac import Environment, Ticket |
---|
37 | |
---|
38 | import os |
---|
39 | import sys |
---|
40 | import getopt |
---|
41 | import shutil |
---|
42 | import ConfigParser |
---|
43 | |
---|
44 | def 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 | |
---|
70 | def 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 | shutil.rmtree(attachment_dir) |
---|
89 | |
---|
90 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
91 | db.commit() |
---|
92 | |
---|
93 | if __name__ == '__main__': |
---|
94 | # Default config file |
---|
95 | # |
---|
96 | configfile = '/etc/email2trac.conf' |
---|
97 | |
---|
98 | try: |
---|
99 | opts, args = getopt.getopt(sys.argv[1:], 'hf:', ['help', 'file=']) |
---|
100 | except getopt.error,detail: |
---|
101 | print __doc__ |
---|
102 | print detail |
---|
103 | sys.exit(1) |
---|
104 | |
---|
105 | for opt,value in opts: |
---|
106 | if opt in [ '-h', '--help']: |
---|
107 | print __doc__ |
---|
108 | sys.exit(0) |
---|
109 | elif opt in ['-f', '--file']: |
---|
110 | configfile = value |
---|
111 | |
---|
112 | settings = ReadConfig(configfile) |
---|
113 | delete_spam(settings['project'], int(settings['debug'])) |
---|
114 | print 'Spam is deleted succesfully..' |
---|
115 | |
---|
116 | # EOB |
---|