Changeset 596


Ignore:
Timestamp:
05/02/12 09:32:27 (12 years ago)
Author:
bas
Message:

converted tabs to spaces for delete_spam.py.in

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/delete_spam.py.in

    r595 r596  
    2020#
    2121# vi:
    22 #       set ts=4
     22#   set ts=4
    2323"""
    2424Author: Bas van der Vlies
     
    2828
    2929Usage :
    30         delete_spam [ -f/--file <configfile>  -n/--dry-run -p/--project <name> -v/--verbose]
     30    delete_spam [ -f/--file <configfile>  -n/--dry-run -p/--project <name> -v/--verbose]
    3131
    3232defaults:
    33         configfile = /etc/email2trac.conf
     33    configfile = /etc/email2trac.conf
    3434
    3535SVN Info:
     
    4646
    4747def ReadConfig(file, name):
    48         """
    49         Parse the config file
    50         """
     48    """
     49    Parse the config file
     50    """
    5151
    52         if not os.path.isfile(file):
    53                 print 'File %s does not exists' %file
    54                 sys.exit(1)
     52    if not os.path.isfile(file):
     53        print 'File %s does not exists' %file
     54        sys.exit(1)
    5555
    56         config = ConfigParser.ConfigParser()
    57         try:
    58                 config.read(file)
    59         except ConfigParser.MissingSectionHeaderError,detail:
    60                 print detail
    61                 sys.exit(1)
     56    config = ConfigParser.ConfigParser()
     57    try:
     58        config.read(file)
     59    except ConfigParser.MissingSectionHeaderError,detail:
     60        print detail
     61        sys.exit(1)
    6262
    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)
     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)
    7070
    71                 project =  dict()
    72                 for option in  config.options(name):
    73                         project[option] = config.get(name, option)
     71        project =  dict()
     72        for option in  config.options(name):
     73            project[option] = config.get(name, option)
    7474
    75         else:
    76                 project = config.defaults()
     75    else:
     76        project = config.defaults()
    7777
    78         return project
     78    return project
    7979
    8080
    8181
    8282def new_delete_spam(parameters):
    83         """
    84         This only works for trac versions higher or equal then 0.10
    85         """
     83    """
     84    This only works for trac versions higher or equal then 0.10
     85    """
    8686
    87         debug = int(parameters['debug'])
    88         DRY_RUN = parameters['DRY_RUN']
    89         VERBOSE = parameters['VERBOSE']
     87    debug = int(parameters['debug'])
     88    DRY_RUN = parameters['DRY_RUN']
     89    VERBOSE = parameters['VERBOSE']
    9090
    91         project = parameters['project']
     91    project = parameters['project']
    9292
    9393
    94         env = Environment(project, create=0)
    95         db = env.get_db_cnx()
     94    env = Environment(project, create=0)
     95    db = env.get_db_cnx()
    9696
    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
     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
    103103
    104                 spam_id =  row[0]
     104        spam_id =  row[0]
    105105
    106                 if debug or DRY_RUN or VERBOSE:
    107                         print "Deleting ticket %s" %spam_id
     106        if debug or DRY_RUN or VERBOSE:
     107            print "Deleting ticket %s" %spam_id
    108108
    109                 try:
    110                         tkt = Ticket(env, spam_id, db)
    111                 except util.TracError, detail:
    112                         print detail
    113                         continue
     109        try:
     110            tkt = Ticket(env, spam_id, db)
     111        except util.TracError, detail:
     112                print detail
     113            continue
    114114
    115                 if DRY_RUN:
    116                         print 'DRY_RUN: tkt.delete()'
    117                 else:
    118                         tkt.delete()
     115        if DRY_RUN:
     116            print 'DRY_RUN: tkt.delete()'
     117        else:
     118            tkt.delete()
    119119
    120120if __name__ == '__main__':
    121         # Default config file
    122         #
    123         configfile = '@email2trac_conf@'
     121    # Default config file
     122    #
     123    configfile = '@email2trac_conf@'
    124124
    125125
    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)
     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)
    132132
    133         DRY_RUN = False
    134         VERBOSE = False
    135         project_name = None
     133    DRY_RUN = False
     134    VERBOSE = False
     135    project_name = None
    136136
    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
     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
    149149
    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'
     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'
    156156
    157         if VERBOSE:
    158                 print "Found trac version: %s" %version
     157    if VERBOSE:
     158        print "Found trac version: %s" %version
    159159
    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)
     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)
    164164
    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)
     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)
    170170
    171         settings['DRY_RUN'] = DRY_RUN
    172         settings['VERBOSE'] = VERBOSE
     171    settings['DRY_RUN'] = DRY_RUN
     172    settings['VERBOSE'] = VERBOSE
    173173
    174         from trac.env import Environment
    175         from trac.ticket import Ticket
    176         from trac import util
     174    from trac.env import Environment
     175    from trac.ticket import Ticket
     176    from trac import util
    177177
    178178
    179179
    180         new_delete_spam(settings)
     180    new_delete_spam(settings)
    181181
    182         print 'Spam is deleted succesfully..'
     182    print 'Spam is deleted succesfully..'
    183183# EOB
Note: See TracChangeset for help on using the changeset viewer.