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

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

New method to allow deletion of all records in Discovery database according to datacentre namespace

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        for i in self.listFilesToDelete:
92               
93            recordFilename = i[0]
94           
95            fileCounter = fileCounter + 1
96           
97            logging.info("Attempting to delete: " + recordFilename)
98           
99            record = RecordToDelete(recordFilename,None)
100            logging.info("Getting discoveryId from database for  %s" %recordFilename)     
101            dao = PostgresDAO(record, self.pgc)             
102            self.createRecordObject(record,dao,recordFilename)
103           
104            #check to see whether a record with this discoveryID actually exists in there
105            if record.db_id is not None:
106       
107                #delete original record
108                try:           
109                    dao.deleteOriginalRecord()
110                    dao._deleteSpatioTemporalData()
111               
112                    #check item deleted (note need to reinitialise the record object!)
113                    dao = PostgresDAO(record, self.pgc)
114                    record = RecordToDelete(recordFilename,None)
115                    self.createRecordObject(record,dao,recordFilename)
116               
117                    if record.db_id is None:
118                        deleteCounter = deleteCounter + 1
119                        print "HAVE SUCCESSFULLY DELETED " + recordFilename + " FROM DATABASE!"
120               
121                except Exception, detail:
122                    logging.error("Could not delete record! %s" %detail)
123           
124            else:
125                logging.warn("Could not detect record in database for: " + fileToDelete)
126       
127        print "\nTotal files found to delete: " + str(fileCounter)
128        print "Files successfully deleted: " + str(deleteCounter) + "\n"
129       
130       
131       
132       
133
134if __name__=="__main__":
135   
136    opts, args = getopt.getopt(sys.argv[1:], '-vd')
137   
138    if len(args) < 1:
139        DeleteDatacentreRecords()
140   
141    DeleteDatacentreRecords(args[0])
142   
143   
Note: See TracBrowser for help on using the repository browser.