source: trunk/delete_spam.py.in @ 593

Last change on this file since 593 was 593, checked in by bas, 12 years ago

Changed license to apache version 2.0

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 4.1 KB
Line 
1#!@PYTHON@
2#
3# Copyright (C) 2002
4#
5# This file is part of the email2trac utils
6#
7#       Licensed to the Apache Software Foundation (ASF) under one
8#       or more contributor license agreements.  See the NOTICE file
9#       distributed with this work for additional information
10#       regarding copyright ownership.  The ASF licenses this file
11#       to you under the Apache License, Version 2.0 (the
12#       "License"); you may not use this file except in compliance
13#       with the License.  You may obtain a copy of the License at
14#
15#         http://www.apache.org/licenses/LICENSE-2.0
16#
17#       Unless required by applicable law or agreed to in writing,
18#       software distributed under the License is distributed on an
19#       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20#       KIND, either express or implied.  See the License for the
21#       specific language governing permissions and limitations
22#       under the License.
23#
24# vi:
25#       set ts=4
26"""
27Author: Bas van der Vlies
28Date  : 29 September 2205
29Desc. : Delete Spam tickets from database. Else we get an lot of
30        tickets
31
32Usage :
33        delete_spam [ -f/--file <configfile>  -n/--dry-run -p/--project <name> -v/--verbose]
34
35defaults:
36        configfile = /etc/email2trac.conf
37
38SVN Info:
39        $Id: delete_spam.py.in 593 2012-05-01 14:03:23Z bas $
40"""
41
42import os
43import sys
44import getopt
45import shutil
46import ConfigParser
47
48from trac import __version__ as trac_version
49
50def ReadConfig(file, name):
51        """
52        Parse the config file
53        """
54
55        if not os.path.isfile(file):
56                print 'File %s does not exists' %file
57                sys.exit(1)
58
59        config = ConfigParser.ConfigParser()
60        try:
61                config.read(file)
62        except ConfigParser.MissingSectionHeaderError,detail:
63                print detail
64                sys.exit(1)
65
66        # Use given project name else use defaults
67        #
68        if name:
69                if not config.has_section(name):
70                        print "Not an valid project name: %s" %name
71                        print "Valid names: %s" %config.sections()
72                        sys.exit(1)
73
74                project =  dict()
75                for option in  config.options(name):
76                        project[option] = config.get(name, option)
77
78        else:
79                project = config.defaults()
80
81        return project
82
83
84
85def new_delete_spam(parameters):
86        """
87        This only works for trac versions higher or equal then 0.10
88        """
89
90        debug = int(parameters['debug'])
91        DRY_RUN = parameters['DRY_RUN']
92        VERBOSE = parameters['VERBOSE']
93
94        project = parameters['project']
95
96
97        env = Environment(project, create=0)
98        db = env.get_db_cnx()
99
100        cursor = db.cursor()
101        cursor.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
102        while 1:
103                row = cursor.fetchone()
104                if not row:
105                        break
106
107                spam_id =  row[0]
108
109                if debug or DRY_RUN or VERBOSE:
110                        print "Deleting ticket %s" %spam_id
111
112                try:
113                        tkt = Ticket(env, spam_id, db)
114                except util.TracError, detail:
115                        print detail
116                        continue
117
118                if DRY_RUN:
119                        print 'DRY_RUN: tkt.delete()'
120                else:
121                        tkt.delete()
122
123if __name__ == '__main__':
124        # Default config file
125        #
126        configfile = '@email2trac_conf@'
127
128
129        try:
130                 opts, args = getopt.getopt(sys.argv[1:], 'hf:np:v', ['help', 'file=', 'dry-run', 'project=', 'verbose'])
131        except getopt.error,detail:
132                print __doc__
133                print detail
134                sys.exit(1)
135
136        DRY_RUN = False
137        VERBOSE = False
138        project_name = None
139
140        for opt,value in opts:
141                if opt in [ '-h', '--help']:
142                        print __doc__
143                        sys.exit(0)
144                elif opt in ['-f', '--file']:
145                        configfile = value
146                elif opt in ['-n', '--dry-run']:
147                        DRY_RUN = True
148                elif opt in ['-p', '--project']:
149                        project_name = value
150                elif opt in ['-v', '--verbose']:
151                        VERBOSE = True
152
153        # Determine major trac version used to be in email2trac.conf
154        # Quick hack for 0.12
155        #
156        version = '0.%s' %(trac_version.split('.')[1])
157        if version.startswith('0.12'):
158                version = '0.12'
159
160        if VERBOSE:
161                print "Found trac version: %s" %version
162
163        ## We only support versions 0.11 and 0.12
164        #
165        if not version in ['0.11', '0.12']:
166                print 'Trac version %s is not suported' %(version)
167
168        settings = ReadConfig(configfile, project_name)
169        if not settings.has_key('project'):
170                print __doc__
171                print 'No project defined in config file, eg:\n\t project: /data/trac/bas'
172                sys.exit(1)
173
174        settings['DRY_RUN'] = DRY_RUN
175        settings['VERBOSE'] = VERBOSE
176
177        from trac.env import Environment
178        from trac.ticket import Ticket
179        from trac import util
180
181
182
183        new_delete_spam(settings)
184
185        print 'Spam is deleted succesfully..'
186# EOB
Note: See TracBrowser for help on using the repository browser.