[22] | 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 : |
---|
[69] | 28 | delete_spam [ -f/--file <configfile> -p/--project <name>] |
---|
[22] | 29 | |
---|
| 30 | defaults: |
---|
| 31 | configfile = /etc/email2trac.conf |
---|
| 32 | |
---|
| 33 | SVN Info: |
---|
| 34 | $Id: delete_spam.py.in 69 2006-05-16 09:45:16Z bas $ |
---|
| 35 | """ |
---|
| 36 | |
---|
| 37 | import os |
---|
| 38 | import sys |
---|
| 39 | import getopt |
---|
| 40 | import shutil |
---|
| 41 | import ConfigParser |
---|
| 42 | |
---|
[69] | 43 | trac_default_version = 0.9 |
---|
| 44 | |
---|
| 45 | def ReadConfig(file, name): |
---|
[22] | 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: |
---|
[69] | 56 | config.read(file) |
---|
[22] | 57 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
[69] | 58 | print detail |
---|
| 59 | sys.exit(1) |
---|
[22] | 60 | |
---|
[69] | 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) |
---|
[22] | 68 | |
---|
[69] | 69 | project = dict() |
---|
| 70 | for option in config.options(name): |
---|
| 71 | project[option] = config.get(name, option) |
---|
[22] | 72 | |
---|
[69] | 73 | else: |
---|
| 74 | project = config.defaults() |
---|
| 75 | |
---|
| 76 | return project |
---|
| 77 | |
---|
[22] | 78 | def delete_spam(project, debug): |
---|
[69] | 79 | env = Environment(project, create=0) |
---|
[22] | 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) |
---|
[55] | 96 | try: |
---|
| 97 | shutil.rmtree(attachment_dir) |
---|
| 98 | except OSError, detail: |
---|
| 99 | print 'Contact system-administrator: %s' %detail |
---|
| 100 | continue |
---|
[22] | 101 | |
---|
| 102 | cursor.execute("DELETE FROM ticket WHERE component = 'Spam';") |
---|
| 103 | db.commit() |
---|
| 104 | |
---|
| 105 | if __name__ == '__main__': |
---|
| 106 | # Default config file |
---|
| 107 | # |
---|
[69] | 108 | configfile = '@email2trac_conf@' |
---|
[22] | 109 | |
---|
| 110 | try: |
---|
[69] | 111 | opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project=']) |
---|
[22] | 112 | except getopt.error,detail: |
---|
| 113 | print __doc__ |
---|
| 114 | print detail |
---|
| 115 | sys.exit(1) |
---|
| 116 | |
---|
[69] | 117 | project_name = None |
---|
[22] | 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 |
---|
[69] | 124 | elif opt in ['-p', '--project']: |
---|
| 125 | project_name = value |
---|
[22] | 126 | |
---|
[69] | 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 | |
---|
[22] | 144 | delete_spam(settings['project'], int(settings['debug'])) |
---|
| 145 | print 'Spam is deleted succesfully..' |
---|
| 146 | |
---|
| 147 | # EOB |
---|