source: TI01-discovery/trunk/ingestAutomation/OAIBatch/oai_ingest.py @ 2252

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI01-discovery/trunk/ingestAutomation/OAIBatch/oai_ingest.py@2252
Revision 2252, 14.1 KB checked in by selatham, 13 years ago (diff)

Copes with MDIP format. Some new config file

  • Property svn:executable set to *
Line 
1#!/usr/bin/env python
2""" Script oai_ingest.py takes parameter <datacentre>.
3The /usr/local/WSClients/OAIBatch directory contains:-
4 - this python script,
5 - a DataProvider specific config file,
6 - the d2b.jar moles creator class which creates moles discovery records,
7 - the python module for extracting spatiotemporal information and adding to postgres db.
8Under this directory the following structure should be maintained:
9 ./data
10 - /DATACENTRE/
11                - discovery/:         Re-named documents ready to ingest in the discovery service.
12                - oai/difYYYYMMDD/    Documents as harvested from OAI
13 Where  /DATACENTRE  varies for the different data providers
14"""
15#History:
16# 12/05/06 SEL spelling correction
17# 30/05/06 SEL cope with many files for processing."Argument list too long" problem.
18# 31/05/06 SEL need to use passwords now. (replace xxxxxx in real version).
19# 16/10/06 SEL Changed to using python oaiClean.py module instead of java code.
20# 16/10/06 SEL exist db upgraded and deployed at different location, java upgrade.
21# 17/10/06 SEL cope with different discovery formats - not just DIF.
22# 23/10/06 SEL keywords not mandatory in config file.
23# 24/10/06 SEL fix bug where 'in' directory not being cleared initially. display more information when running.
24
25import os
26import sys
27import commands
28import string
29import SpaceTimeIngestFromMOLES
30import keywordAdder
31from DIF import DIF
32from MDIP import MDIP
33
34def getID(filename):
35        ''' Gets the identifier out of an input metadata xml record. Copes with DIF and MDIP currently.'''
36        xml=file(filename).read()
37        if datacentre_format == "DIF":
38            d=DIF(xml)
39            ID=d.entryID
40        elif datacentre_format == "MDIP":
41            d=MDIP(xml)
42            ID=d.id
43        else:
44            sys.exit("Only handles DIF or MDIP here.")
45        return ID
46
47status = 0
48numfilesproc = 0
49harvest_home = ""
50datacentre_groups = ""
51datacentre_format = ""
52datacentre_namespace = ""
53NDG_dataProvider = False
54
55if (len(sys.argv) < 2):
56    print "<datacentre>  parameter not supplied."
57    sys.exit()
58else:
59    datacentre = sys.argv[1]
60
61# Other settings and constants
62date_string = commands.getoutput ("date +'%y%m%d_%H%M'")
63os.putenv ('EXIST_HOME', '/usr/local/exist-client')
64os.putenv ('JAVA_HOME', '/usr/java/jdk1.5.0_03')
65os.putenv ('PATH', ':/usr/java/jdk1.5.0_03/bin:/usr/java/jdk1.5.0_03:/usr/java/jdk1.5.0_03/lib/tools.jar:/usr/local/WSClients/OAIBatch:/usr/local/exist-client/bin:/bin:/usr/bin:.')
66os.putenv ('CLASSPATH','.:/usr/java/jdk1.5.0_03/lib/tools.jar')
67
68# Get the harvested records directory and groups for this datacentre from the datacentre specific config file
69# The harvested records directory depends on the datacentres OAI base url, the set and  format. These have to be know up-front.
70# The groups denote which 'portal groups' they belong to - for limiting searches to say NERC-only datacentres records.
71# Groups are added to the intermediate MOLES when it is created.
72datacentre_config_filename = "/usr/local/WSClients/OAIBatch/" + datacentre + "_config.properties"
73print "Datacentre config file = %s" %datacentre_config_filename
74datacentre_config_file = open(datacentre_config_filename, "r")
75
76for line in datacentre_config_file.readlines():
77    words  = string.split(line)
78    if len(words) == 0:
79        continue
80    if words[0] == 'host_path':
81        harvest_home = string.rstrip(words[1])
82    if words[0] == 'groups':
83        datacentre_groups = words[1:]
84    if words[0] == 'format':
85        datacentre_format = words[1]
86    if words[0] == 'namespace':
87        datacentre_namespace = words[1]
88    if words[0] == 'NDG_dataProvider':
89        NDG_dataProvider = True
90
91datacentre_config_file.close()
92
93if harvest_home == "":
94    sys.exit("Failed at getting harvested records directory stage. datacentre config file tried = %s" %datacentre_config_filename)
95else:
96    print "INFO: harvested records are in %s" %harvest_home
97
98if datacentre_groups == "":
99    print "INFO: No groups/keywords set for datacentre %s" %datacentre
100else:
101    print "INFO: datacentre groups/keywords = %s" %datacentre_groups
102
103if datacentre_format == "":
104    sys.exit("Failed at stage: getting datacentre format. datacentre config file tried = %s" %datacentre_config_filename)
105else:
106    print "INFO: format being harvested = %s" %datacentre_format
107
108if datacentre_namespace == "":
109    sys.exit("Failed at stage: getting datacentre namespace. datacentre config file tried = %s" %datacentre_config_filename)
110else:
111    print "INFO: datacentre namespace = %s" %datacentre_namespace
112
113#any records to harvest?
114if len( os.listdir(harvest_home)) == 0:
115    print "Nothing to harvest this time from %s" %datacentre
116    sys.exit()
117
118# The directory to put things for a tape backup (should already exist)
119backupdir = '/disks/glue1/oaiBackup/'
120
121# Create/clear the 'in' directory pristine copy of the discovery records
122if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"):
123    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\}"
124    print "Executing : " + commandline
125    status = os.system(commandline)
126else:
127    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
128    print "Executing : " + commandline
129    status= os.system(commandline)
130
131if status != 0:
132    sys.exit("Failed at creating copy dir stage")
133
134# make the 'in' pristine copy. Cope with there being lots of files in the directory.
135
136commandline = "ls -1 " + harvest_home + "/ | xargs -i cp " + harvest_home + "/{\} /usr/local/WSClients/OAIBatch/data/" + datacentre + "/oai/originals"
137print "Executing : " + commandline
138status = os.system(commandline)
139if status !=0:
140    sys.exit("Failed at making pristine copy stage")
141
142# Create/clear the directory for the 'out' processed copy of the discovery records.
143if os.path.isdir("/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"):
144    commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i rm /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\}"
145    print "Executing : " + commandline
146    status = os.system(commandline)
147else:
148    commandline = "mkdir /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
149    print "Executing : " + commandline
150    status= os.system(commandline)
151
152# The file config.properties contains the location of the particular datacentres harvested records.
153# Copy the datacentre specific version of config to config.properties file.
154commandline = "cp /usr/local/WSClients/OAIBatch/" + datacentre +"_config.properties /usr/local/WSClients/OAIBatch/config.properties"
155print "Executing : " + commandline
156status = os.system(commandline)
157if status !=0:
158    sys.exit("Failed at copying config file stage")
159
160#Change os directory to that with the code in it.
161os.chdir('/usr/local/WSClients/OAIBatch')
162
163
164#Execute the script which processes/renames the files (changed 08/01/07 to get id from inside file)
165indir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals"
166outdir="/usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery"
167#wrapFlag=False
168filenames = os.listdir(indir)
169for filename in filenames:
170        if filename.find('.xml') != -1:
171                original_filename = indir + "/" + filename
172                ident=getID(original_filename)
173                print "ID extracted from the DIF = %s" %ident
174                if NDG_dataProvider:
175                    new_filename = outdir + "/"+ ident.replace(":","__")+".xml"
176                else:
177                    new_filename = outdir + "/" +datacentre_namespace+ "__"+datacentre_format+ "__"+ ident +".xml"
178                print "original file = %s, newfile = %s" %(original_filename, new_filename)
179                commandline = "cp "+original_filename+ " " +new_filename
180                #print "Executing : " + commandline
181                status = os.system(commandline)
182                if status !=0:
183                    sys.exit("Failed at re-naming file stage")
184                numfilesproc += 1
185        else:
186                print 'File %s is not xml format. Not processed'  %(full_filename)
187
188# ingest the datacentres records into eXist db (backups of exist happen nightly).
189commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/original/"+datacentre_format+ "/" +datacentre_namespace+ " -u admin -P xxxxxx -p "+outdir
190print "Executing : actual command to ingest into exist db"
191status = os.system(commandline)
192if status !=0:
193    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
194
195#are there any old records hanging around.If so, remove.
196try:
197    os.stat("./DIF2MOLES")
198except:
199    print "No old moles records hanging around"
200else:
201    commandline = "ls -1 ./DIF2MOLES | xargs -i rm ./DIF2MOLES/{\}"
202    print "Executing : " + commandline
203    status = os.system(commandline)
204    if status !=0:
205        sys.exit("Failed at clearing out DIF2MOLES area.")
206
207# Then run the minimum moles creator for each discovery record
208# Put records in ./DIF2MOLES with original filename
209filenames = os.listdir(outdir)
210for filename in filenames:
211        if filename.find('.xml') != -1:
212                    original_filename = outdir + "/" + filename
213                    ident=getID(original_filename)
214                    if NDG_dataProvider:
215                        molesLocalID = ident.split(":",2)[2]
216                    else:
217                        molesLocalID = ident
218                    print "molesLocalID is %s" %molesLocalID
219                    commandline = "java -jar D2B/d2boneoff.jar repositoryID " +datacentre_namespace+" repositoryLocalID "+datacentre+" format "+ \
220                    datacentre_format+" repository xmldb:exist://glue.badc.rl.ac.uk:8080/exist/xmlrpc userpw xxxxxx targetCollection /db/discovery/original/"+ \
221                    datacentre_format+"/"+datacentre_namespace +" inputRecordID "+ ident+ " outputLocalID "+molesLocalID+ " > ./DIF2MOLES/"+filename
222                    print "Executing command to run d2boneoff.jar"
223                    status= os.system(commandline)
224                    if status==10:
225                        print "WARNING: couldn't find the record"
226                    elif status!=0:
227                        print "ERROR: couldn't create the minimum moles records"
228                        sys.exit
229#There should be some records now
230try:
231    os.stat("./DIF2MOLES")
232except:
233    print "ERROR: couldn't create any minimum moles records for %s" %datacentre
234    sys.exit()
235
236#Add keywords if necessary
237if datacentre_groups == "":
238    commandline = "ls -1 ./DIF2MOLES/ | xargs -i mv ./DIF2MOLES/{\} ./FINALMOLES/"
239    print "Executing : " + commandline
240    status = os.system(commandline)
241    if status !=0:
242        sys.exit("Failed at moving MOLES to FINAL directory")
243else:
244    keywordAdder.main('./DIF2MOLES', './FINALMOLES', ['MDIP', 'http://vocab.ndg.nerc.ac.uk/term/N010/0', 'NDGO0001'])
245
246# ingest the created discovery minimum molesrecords into eXist db.
247commandline = "$EXIST_HOME/bin/client.sh -c /db/discovery/moles -u admin -P xxxxxx -p ./FINALMOLES"
248print "Executing : actual command to ingest into exist db"
249status = os.system(commandline)
250if status !=0:
251    sys.exit("Failed at ingesting into exist db. Datacentre =  %s. Status = %s" %(datacentre,status))
252
253#Extract the spatiotemporal info from created moles and put in Postgres db
254SpaceTimeIngestFromMOLES.main("./FINALMOLES")
255
256#Make copies of discovery and oai/originals and DIF2MOLES areas to backup area for tape backups
257this_backupdir = backupdir + datacentre + "_" + date_string + "_originals"
258commandline = "mkdir " + this_backupdir
259print "Executing : " + commandline
260status = os.system(commandline)
261if status !=0:
262    sys.exit("Failed at creating backup directory %s" %this_backupdir)
263
264commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/oai/originals/{\} " + this_backupdir
265print "Executing : " + commandline
266status = os.system(commandline)
267if status !=0:
268    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
269
270this_backupdir = backupdir + datacentre + "_" + date_string + "_discovery"
271commandline = "mkdir " + this_backupdir
272print "Executing : " + commandline
273status = os.system(commandline)
274if status !=0:
275    sys.exit("Failed at creating backup directory %s" %this_backupdir)
276
277commandline = "ls -1 /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/ | xargs -i cp /usr/local/WSClients/OAIBatch/data/" + datacentre +"/discovery/{\} " + this_backupdir
278print "Executing : " + commandline
279status = os.system(commandline)
280if status !=0:
281    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
282
283this_backupdir = backupdir + datacentre + "_" + date_string + "_FINALMOLES"
284commandline = "mkdir " + this_backupdir
285print "Executing : " + commandline
286status = os.system(commandline)
287if status !=0:
288    sys.exit("Failed at creating backup directory %s" %this_backupdir)
289
290commandline = "ls -1 ./FINALMOLES | xargs -i cp ./FINALMOLES/{\} " + this_backupdir
291print "Executing : " + commandline
292status = os.system(commandline)
293if status !=0:
294    sys.exit("Failed at copying to backup directory %s" %this_backupdir)
295
296#Clear out the original harvest records area and DIF2MOLES
297commandline = "ls -1 " + harvest_home + " | xargs -i rm " + harvest_home + "/{\}"
298print "Executing : " + commandline
299status = os.system(commandline)
300if status !=0:
301    sys.exit("Failed at clearing out original harvest records area %s" %harvest_home)
302
303commandline = "ls -1 ./FINALMOLES | xargs -i rm ./FINALMOLES/{\}"
304print "Executing : " + commandline
305status = os.system(commandline)
306if status !=0:
307    sys.exit("Failed at clearing out FINALMOLES area %s" %harvest_home)
308
309#remove the DIF2MOLES directory
310##commandline = "rmdir ./DIF2MOLES"
311#print "Executing : " + commandline
312#status = os.system(commandline)
313#if status !=0:
314#    sys.exit("Failed at removing DIF2MOLES directory %s" %harvest_home)
315
316print "======================================================"
317print "No. of files pre-processed = %s" %numfilesproc
318if status == 0:
319    print " Procedure oai_ingest.py ran to end"
320else:
321    print "Procedure oai_ingest.py FAILED with status %s" %status
322
323print "======================================================"
Note: See TracBrowser for help on using the repository browser.