[22] | 1 | #!@PYTHON@ |
---|
| 2 | # |
---|
| 3 | # Copyright (C) 2002 |
---|
| 4 | # |
---|
[128] | 5 | # This file is part of the email2trac utils |
---|
[22] | 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 | # |
---|
[128] | 21 | # vi: |
---|
| 22 | # set ts=4 |
---|
[22] | 23 | """ |
---|
| 24 | Author: Bas van der Vlies |
---|
| 25 | Date : 29 September 2205 |
---|
| 26 | Desc. : Delete Spam tickets from database. Else we get an lot of |
---|
| 27 | tickets |
---|
| 28 | |
---|
| 29 | Usage : |
---|
[350] | 30 | delete_spam [ -f/--file <configfile> -n/--dry-run -p/--project <name> -v/--verbose] |
---|
[22] | 31 | |
---|
| 32 | defaults: |
---|
| 33 | configfile = /etc/email2trac.conf |
---|
| 34 | |
---|
| 35 | SVN Info: |
---|
| 36 | $Id: delete_spam.py.in 351 2010-04-15 07:25:51Z bas $ |
---|
| 37 | """ |
---|
| 38 | |
---|
| 39 | import os |
---|
| 40 | import sys |
---|
| 41 | import getopt |
---|
| 42 | import shutil |
---|
| 43 | import ConfigParser |
---|
| 44 | |
---|
[180] | 45 | trac_default_version = 0.10 |
---|
[69] | 46 | |
---|
| 47 | def ReadConfig(file, name): |
---|
[22] | 48 | """ |
---|
| 49 | Parse the config file |
---|
| 50 | """ |
---|
| 51 | |
---|
| 52 | if not os.path.isfile(file): |
---|
| 53 | print 'File %s does not exists' %file |
---|
| 54 | sys.exit(1) |
---|
| 55 | |
---|
| 56 | config = ConfigParser.ConfigParser() |
---|
| 57 | try: |
---|
[69] | 58 | config.read(file) |
---|
[22] | 59 | except ConfigParser.MissingSectionHeaderError,detail: |
---|
[69] | 60 | print detail |
---|
| 61 | sys.exit(1) |
---|
[22] | 62 | |
---|
[69] | 63 | # Use given project name else use defaults |
---|
| 64 | # |
---|
| 65 | if name: |
---|
| 66 | if not config.has_section(name): |
---|
| 67 | print "Not an valid project name: %s" %name |
---|
| 68 | print "Valid names: %s" %config.sections() |
---|
| 69 | sys.exit(1) |
---|
[22] | 70 | |
---|
[69] | 71 | project = dict() |
---|
| 72 | for option in config.options(name): |
---|
| 73 | project[option] = config.get(name, option) |
---|
[22] | 74 | |
---|
[69] | 75 | else: |
---|
| 76 | project = config.defaults() |
---|
| 77 | |
---|
| 78 | return project |
---|
| 79 | |
---|
[99] | 80 | |
---|
| 81 | |
---|
[350] | 82 | def new_delete_spam(parameters): |
---|
[128] | 83 | """ |
---|
| 84 | This only works for trac versions higher or equal then 0.10 |
---|
| 85 | """ |
---|
[350] | 86 | |
---|
| 87 | debug = int(parameters['debug']) |
---|
| 88 | DRY_RUN = parameters['DRY_RUN'] |
---|
| 89 | VERBOSE = parameters['VERBOSE'] |
---|
| 90 | |
---|
| 91 | project = parameters['project'] |
---|
| 92 | |
---|
| 93 | |
---|
[128] | 94 | env = Environment(project, create=0) |
---|
| 95 | db = env.get_db_cnx() |
---|
[22] | 96 | |
---|
[137] | 97 | cursor = db.cursor() |
---|
[128] | 98 | cursor.execute("SELECT id FROM ticket WHERE component = 'Spam';") |
---|
| 99 | while 1: |
---|
| 100 | row = cursor.fetchone() |
---|
| 101 | if not row: |
---|
| 102 | break |
---|
[99] | 103 | |
---|
[128] | 104 | spam_id = row[0] |
---|
[116] | 105 | |
---|
[350] | 106 | if debug or DRY_RUN or VERBOSE: |
---|
[137] | 107 | print "Deleting ticket %s" %spam_id |
---|
| 108 | |
---|
[128] | 109 | try: |
---|
| 110 | tkt = Ticket(env, spam_id, db) |
---|
[137] | 111 | except util.TracError, detail: |
---|
| 112 | print detail |
---|
[128] | 113 | continue |
---|
[116] | 114 | |
---|
[350] | 115 | if DRY_RUN: |
---|
| 116 | print 'DRY_RUN: tkt.delete()' |
---|
| 117 | else: |
---|
[351] | 118 | tkt.delete() |
---|
[99] | 119 | |
---|
[22] | 120 | if __name__ == '__main__': |
---|
| 121 | # Default config file |
---|
| 122 | # |
---|
[69] | 123 | configfile = '@email2trac_conf@' |
---|
[22] | 124 | |
---|
[128] | 125 | |
---|
[22] | 126 | try: |
---|
[350] | 127 | opts, args = getopt.getopt(sys.argv[1:], 'hf:np:v', ['help', 'file=', 'dry-run', 'project=', 'verbose']) |
---|
[22] | 128 | except getopt.error,detail: |
---|
| 129 | print __doc__ |
---|
| 130 | print detail |
---|
| 131 | sys.exit(1) |
---|
| 132 | |
---|
[350] | 133 | DRY_RUN = False |
---|
| 134 | VERBOSE = False |
---|
[69] | 135 | project_name = None |
---|
[350] | 136 | |
---|
[22] | 137 | for opt,value in opts: |
---|
| 138 | if opt in [ '-h', '--help']: |
---|
| 139 | print __doc__ |
---|
| 140 | sys.exit(0) |
---|
| 141 | elif opt in ['-f', '--file']: |
---|
| 142 | configfile = value |
---|
[350] | 143 | elif opt in ['-n', '--dry-run']: |
---|
| 144 | DRY_RUN = True |
---|
[69] | 145 | elif opt in ['-p', '--project']: |
---|
| 146 | project_name = value |
---|
[350] | 147 | elif opt in ['-v', '--verbose']: |
---|
| 148 | VERBOSE = True |
---|
[22] | 149 | |
---|
[69] | 150 | settings = ReadConfig(configfile, project_name) |
---|
| 151 | if not settings.has_key('project'): |
---|
| 152 | print __doc__ |
---|
| 153 | print 'No project defined in config file, eg:\n\t project: /data/trac/bas' |
---|
| 154 | sys.exit(1) |
---|
| 155 | |
---|
| 156 | if settings.has_key('trac_version'): |
---|
| 157 | version = float(settings['trac_version']) |
---|
| 158 | else: |
---|
| 159 | version = trac_default_version |
---|
| 160 | |
---|
[350] | 161 | settings['DRY_RUN'] = DRY_RUN |
---|
| 162 | settings['VERBOSE'] = VERBOSE |
---|
| 163 | |
---|
[180] | 164 | from trac.env import Environment |
---|
| 165 | from trac.ticket import Ticket |
---|
| 166 | from trac import util |
---|
[69] | 167 | |
---|
[128] | 168 | |
---|
[350] | 169 | if version < 0.10: |
---|
| 170 | print 'Trac version %s is not suported' %(version) |
---|
| 171 | |
---|
| 172 | new_delete_spam(settings) |
---|
| 173 | |
---|
[22] | 174 | print 'Spam is deleted succesfully..' |
---|
| 175 | # EOB |
---|