Changeset 350


Ignore:
Timestamp:
04/15/10 09:18:47 (14 years ago)
Author:
bas
Message:

Rewrite of delete_spam

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/ChangeLog

    r349 r350  
    77  * Problems with attachments were not reported for ticket updates
    88    Fixed by: Bas van der Vlies
     9
     10  * delete_spam changes:
     11     - only support trac version greater then 0.10
     12     - added -n/--dry-run option
     13     - added -v/--verbose option
     14
     15    Author: Bas van der Vlies
    916
    10172010-03-20
  • trunk/debian/changelog

    r345 r350  
     1email2trac (1.3.5-1) stable; urgency=low
     2
     3  *  See Changes
     4
     5 -- Bas van der Vlies <basv@sara.nl>  Thu, 15 Apr 2010 09:14:14 +0200
     6
    17email2trac (1.3.2-1) stable; urgency=low
    28
     
    713    Fixed by: Bas van der Vlies
    814
    9  -- root <root@subtrac2.rc.sara.nl>  Tue, 30 Mar 2010 13:22:00 +0200
     15 -- Bas van der Vlies <basv@sara.nl>  Tue, 30 Mar 2010 13:22:00 +0200
    1016
    1117email2trac (1.3.1-1) stable; urgency=low
  • trunk/debian/control

    r213 r350  
    99Section: misc
    1010Architecture: any
    11 Maintainer: Bas van der Vlies <bas@sara.nl>
    1211Depends: ${slibs:Depends}, python, cdbs
    1312Description: Converts email to a trac ticket
  • trunk/delete_spam.py.in

    r180 r350  
    2828
    2929Usage :
    30         delete_spam [ -f/--file <configfile> -p/--project <name>]
     30        delete_spam [ -f/--file <configfile>  -n/--dry-run -p/--project <name> -v/--verbose]
    3131
    3232defaults:
     
    8080
    8181
    82 def new_delete_spam(project, debug):
     82def new_delete_spam(parameters):
    8383        """
    8484        This only works for trac versions higher or equal then 0.10
    8585        """
     86
     87        debug = int(parameters['debug'])
     88        DRY_RUN = parameters['DRY_RUN']
     89        VERBOSE = parameters['VERBOSE']
     90
     91        project = parameters['project']
     92
     93
    8694        env = Environment(project, create=0)
    8795        db = env.get_db_cnx()
     
    96104                spam_id =  row[0]
    97105
    98                 if debug:
     106                if debug or DRY_RUN or VERBOSE:
    99107                        print "Deleting ticket %s" %spam_id
    100108
     
    105113                        continue
    106114
    107                 tkt.delete()
    108 
    109 def old_delete_spam(project, debug):
    110         """
    111         This only works for trac versions before 0.10
    112         """
    113         env = Environment(project, create=0)
    114         db = env.get_db_cnx()
    115        
    116         attachment_dir = os.path.join(env.path, 'attachments', 'ticket')
    117         cursor = db.cursor()
    118         tkt_cursor = db.cursor()
    119        
    120         # Delete the attachments associated with Spam tickets
    121         #
    122         cursor.execute("SELECT id FROM ticket WHERE  component = 'Spam';")
    123         while 1:
    124                 row = cursor.fetchone()
    125                 if not row:
    126                         break
    127                 spam_id =  row[0]
    128        
    129                 if debug:
    130                         print spam_id
    131 
    132                         sql_cmd = "SELECT *  FROM attachment WHERE type='ticket' and id='%s';" %spam_id
    133                         tkt_cursor.execute(sql_cmd)
    134                         row = tkt_cursor.fetchone()
    135                         print row
    136                        
    137                         sql_cmd = "SELECT * FROM ticket_change WHERE ticket='%s';" %spam_id
    138                         tkt_cursor.execute(sql_cmd)
    139                         row = tkt_cursor.fetchone()
    140                         print row
    141                        
    142                         sql_cmd = "SELECT * FROM ticket_custom WHERE ticket='%s';" %spam_id
    143                         tkt_cursor.execute(sql_cmd)
    144                         row = tkt_cursor.fetchone()
    145                         print row
    146                        
    147                 sql_cmd = "DELETE FROM attachment WHERE type='ticket' and id='%s';" %spam_id
    148                 tkt_cursor.execute(sql_cmd)
    149 
    150                 sql_cmd = "DELETE FROM ticket_change WHERE ticket='%s';" %spam_id
    151                 tkt_cursor.execute(sql_cmd)
    152                        
    153                 sql_cmd = "DELETE FROM ticket_custom WHERE ticket='%s';" %spam_id
    154                 tkt_cursor.execute(sql_cmd)
    155                        
    156                 # Ticket commit
    157                 #
    158                 db.commit()
    159 
    160                 dir = os.path.join(attachment_dir, str(spam_id))
    161                 if os.path.exists(dir):
    162                         if debug:
    163                                 print 'delete %s : %s' %(spam_id, dir)
    164                         try:
    165                                 shutil.rmtree(dir)
    166                         except OSError, detail:
    167                                 print 'Contact system-administrator: %s' %detail
    168                                 continue
    169 
    170         cursor.execute("DELETE FROM ticket WHERE  component = 'Spam';")
    171         db.commit()
     115                if DRY_RUN:
     116                        print 'DRY_RUN: tkt.delete()'
     117                else:
     118                        print 'For echt tkt.delete()'
    172119
    173120if __name__ == '__main__':
     
    178125
    179126        try:
    180                  opts, args = getopt.getopt(sys.argv[1:], 'hf:p:', ['help', 'file=', 'project='])
     127                 opts, args = getopt.getopt(sys.argv[1:], 'hf:np:v', ['help', 'file=', 'dry-run', 'project=', 'verbose'])
    181128        except getopt.error,detail:
    182129                print __doc__
     
    184131                sys.exit(1)
    185132
     133        DRY_RUN = False
     134        VERBOSE = False
    186135        project_name = None
     136
    187137        for opt,value in opts:
    188138                if opt in [ '-h', '--help']:
     
    191141                elif opt in ['-f', '--file']:
    192142                        configfile = value
     143                elif opt in ['-n', '--dry-run']:
     144                        DRY_RUN = True
    193145                elif opt in ['-p', '--project']:
    194146                        project_name = value
     147                elif opt in ['-v', '--verbose']:
     148                        VERBOSE = True
    195149       
    196150        settings = ReadConfig(configfile, project_name)
     
    205159                version = trac_default_version
    206160
     161        settings['DRY_RUN'] = DRY_RUN
     162        settings['VERBOSE'] = VERBOSE
     163
    207164        from trac.env import Environment
    208165        from trac.ticket import Ticket
    209166        from trac import util
    210167
    211         if version == 0.10:
    212                 new_delete_spam(settings['project'], int(settings['debug']))
    213                 #new_delete_spam(settings['project'], 1)
    214                 #old_delete_spam(settings['project'], 0)
    215         elif version == 0.9:
    216                 old_delete_spam(settings['project'], int(settings['debug']))
     168
     169        if version < 0.10:
     170                print 'Trac version %s is not suported' %(version)
     171
     172        new_delete_spam(settings)
    217173
    218174        print 'Spam is deleted succesfully..'
Note: See TracChangeset for help on using the changeset viewer.