source: trunk/delete_spam.py.in @ 602

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

fixed an IndentationError? in delete_spam.py

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