source: TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/DeleteDatacentreRecords.py @ 5736

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/branches/ingestAutomation-upgrade/OAIBatch/DeleteDatacentreRecords.py@6130
Revision 5736, 5.4 KB checked in by sdonegan, 10 years ago (diff)

Sorted bug in handling errors where no records available to delete

Line 
1#!/usr/bin/env python
2'''
3Test class to test deletions from discovery postgres database
4'''
5import sys, os, getopt, logging
6from ndg.common.src.clients.reldb.postgres.postgresclient import PostgresClient as pgc
7from PostgresDAO import PostgresDAO
8from PostgresRecord import PostgresRecord
9from RecordToDelete import RecordToDelete
10from Utilities import RecordToDelete
11
12class DeleteDatacentreRecords:
13       
14       
15    def _getPostgresDBConnection(self):
16        '''
17        Get the default postgres DB connection - by reading in data from the db config file
18        '''       
19        logging.debug("Setting up connection to postgres DB")
20        self.pgc = pgc(configFile = 'ingest.config')
21        logging.info("Postgres DB connection now set up")
22       
23       
24    def createRecordObject(self,record,dao,recordFilename):
25       
26        '''
27        Simple method to simplify addition of filename and localid
28        '''
29       
30        #work out whether searching using supplied filename or discoveryID
31        if recordFilename is None:
32            dao.getRecordID()
33        else: 
34            dao.getRecordID_using_OriginalDocumentFilename()
35            dao.getDiscoveryID_using_OriginalDocumentFilename()
36       
37       
38    def usage(self):
39        '''
40        Display input params for the script
41        '''
42        print "Usage: python DeleteDatacentreRecords.py [OPTION] <datacentre namespace>"
43        print " - where:\n   <datacentre namespace> is the namespace of the datacentre whose records are to be deleted from the postgres database in ingest.config; and options are:"
44        print " -v - verbose mode for output logging"
45        print " -d - debug mode for output logging"
46        sys.exit(2)
47       
48   
49    def __init__(self,fileToDelete=None):
50        '''
51        Main entry point for script
52        '''
53        self.lineSeparator = "-----------------------------"
54       
55        print self.lineSeparator
56        print "RUNNING: DeleteDataCentreRecords.py"
57       
58        # check for verbose option
59        try:
60            opts, args = getopt.getopt(sys.argv[1:], "vd")
61        except getopt.GetoptError, err:
62            # print help information and exit:
63            print str(err) # will print something like "option -a not recognized"
64           
65        loggingLevel = logging.WARNING
66        for o, a in opts:
67            if o == "-v":
68                print " - Verbose mode ON"
69                loggingLevel = logging.INFO
70            elif o == "-d":
71                print " - Debug mode ON"
72                loggingLevel = logging.DEBUG
73       
74        print self.lineSeparator
75       
76        logging.basicConfig(level=loggingLevel,format='%(asctime)s %(filename)s:%(lineno)d %(levelname)s %(message)s')
77       
78        if fileToDelete is None:
79            self.usage()
80           
81         #self._dbConnection = None
82        self.pgc = pgc(configFile = 'ingest.config')
83       
84        findFiles = "select original_document_filename from original_document where original_document_filename like '%" + fileToDelete + "%';"
85        logging.info("SQL command to find matching entries to delete: " + findFiles)
86        self.listFilesToDelete = self.pgc.runSQLCommand(findFiles)
87       
88        deleteCounter = 0
89        fileCounter = 0
90       
91        if self.listFilesToDelete == None:
92            logging.error("No matching records found to delete!")
93            sys.exit()
94       
95        for i in self.listFilesToDelete:
96               
97            recordFilename = i[0]
98           
99            fileCounter = fileCounter + 1
100           
101            logging.info("Attempting to delete: " + recordFilename)
102           
103            record = RecordToDelete(recordFilename,None)
104            logging.info("Getting discoveryId from database for  %s" %recordFilename)     
105            dao = PostgresDAO(record, self.pgc)             
106            self.createRecordObject(record,dao,recordFilename)
107           
108            #check to see whether a record with this discoveryID actually exists in there
109            if record.db_id is not None:
110       
111                #delete original record
112                try:           
113                    dao.deleteOriginalRecord()
114                    dao._deleteSpatioTemporalData()
115               
116                    #check item deleted (note need to reinitialise the record object!)
117                    dao = PostgresDAO(record, self.pgc)
118                    record = RecordToDelete(recordFilename,None)
119                    self.createRecordObject(record,dao,recordFilename)
120               
121                    if record.db_id is None:
122                        deleteCounter = deleteCounter + 1
123                        print "HAVE SUCCESSFULLY DELETED " + recordFilename + " FROM DATABASE!"
124               
125                except Exception, detail:
126                    logging.error("Could not delete record! %s" %detail)
127           
128            else:
129                logging.warn("Could not detect record in database for: " + fileToDelete)
130       
131        print "\nTotal files found to delete: " + str(fileCounter)
132        print "Files successfully deleted: " + str(deleteCounter) + "\n"
133       
134       
135       
136       
137
138if __name__=="__main__":
139   
140    opts, args = getopt.getopt(sys.argv[1:], '-vd')
141   
142    if len(args) < 1:
143        DeleteDatacentreRecords()
144   
145    DeleteDatacentreRecords(args[0])
146   
147   
Note: See TracBrowser for help on using the repository browser.