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/--file <configfile> -p/--project <name>] |
---|
29 | |
---|
30 | defaults: |
---|
31 | configfile = /etc/email2trac.conf |
---|
32 | |
---|
33 | SVN Info: |
---|
34 | $Id: delete_spam.py.in 99 2006-07-13 12:39:25Z bas $ |
---|
35 | """ |
---|
36 | |
---|
37 | import os |
---|
38 | import sys |
---|
39 | import getopt |
---|
40 | import shutil |
---|
41 | import ConfigParser |
---|
42 | |
---|
43 | trac_default_version = 0.9 |
---|
44 | |
---|
45 | def 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 | |
---|
78 | def 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 | dir = os.path.join(attachment_dir, str(spam_id)) |
---|
99 | if os.path.exists(dir): |
---|
100 | if debug: |
---|
101 | print 'delete %s : %s' %(spam_id, dir) |
---|
102 | try: |
---|
103 | shutil.rmtree(dir) |
---|
104 | except OSError, detail: |
---|
105 | print 'Contact system-administrator: %s' %detail |
---|
106 | continue |
---|
107 | |
---|
108 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
109 | db.commit() |
---|
110 | |
---|
111 | if __name__ == '__main__': |
---|
112 | # Default config file |
---|
113 | # |
---|
114 | configfile = '@email2trac_conf@' |
---|
115 | |
---|
116 | try: |
---|
117 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
118 | except getopt.error,detail: |
---|
119 | print __doc__ |
---|
120 | print detail |
---|
121 | sys.exit(1) |
---|
122 | |
---|
123 | project_name = None |
---|
124 | for opt,value in opts: |
---|
125 | if opt in [ '-h', '--help']: |
---|
126 | print __doc__ |
---|
127 | sys.exit(0) |
---|
128 | elif opt in ['-f', '--file']: |
---|
129 | configfile = value |
---|
130 | elif opt in ['-p', '--project']: |
---|
131 | project_name = value |
---|
132 | |
---|
133 | settings = ReadConfig(configfile, project_name) |
---|
134 | if not settings.has_key('project'): |
---|
135 | print __doc__ |
---|
136 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
137 | sys.exit(1) |
---|
138 | |
---|
139 | if settings.has_key('trac_version'): |
---|
140 | version = float(settings['trac_version']) |
---|
141 | else: |
---|
142 | version = trac_default_version |
---|
143 | |
---|
144 | |
---|
145 | if version == 0.8: |
---|
146 | from trac.Environment import Environment |
---|
147 | else: |
---|
148 | from trac.env import Environment |
---|
149 | |
---|
150 | delete_spam(settings['project'], int(settings['debug'])) |
---|
151 | print 'Spam is deleted succesfully..' |
---|
152 | |
---|
153 | # EOB |
---|